Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Last active November 24, 2016 00:29
Show Gist options
  • Save dannyvassallo/8768f8ac13e4f422ec7d to your computer and use it in GitHub Desktop.
Save dannyvassallo/8768f8ac13e4f422ec7d to your computer and use it in GitHub Desktop.
Replace Rails 4 confirmation/prompts with a toast message from Dogfalo Materialize CSS. Regular repo - https://github.com/Dogfalo/materialize Sass Gem - https://github.com/mkhairi/materialize-sass Website - http://materializecss.com/
$ ->
$.rails.allowAction = (link) ->
return true unless link.attr("data-confirm")
$.rails.showConfirmDialog link
false
$.rails.confirmed = (link) ->
link.removeAttr "data-confirm"
link.trigger "click.rails"
$.rails.showConfirmDialog = (link) ->
html = undefined
message = undefined
message = link.attr("data-confirm")
html = "<div id=\"modal1\" class=\"modal\" style=\"z-index: 1003; display: block; opacity: 1; transform: scaleX(1); top: 10%;\"> <div class=\"modal-content\"><h4>" + message + "</h4></div><div class=\"modal-footer\"><a class=\"modal-action modal-close waves-effect waves-red btn-flat close\">Cancel</a><a class=\"modal-action modal-close waves-effect waves-green btn-flat confirm\">OK</a></div></div>"
$("body").append html
$("#modal1").openModal complete: ->
$("#modal1").remove()
return $("#modal1 .confirm").on("click", ->
$.rails.confirmed link
)
$("#modal1 .close").on "click", ->
$("#modal1").closeModal()
$(function() {
$.rails.allowAction = function(link) {
if (!link.attr('data-confirm')) {
return true;
}
$.rails.showConfirmDialog(link);
return false;
};
$.rails.confirmed = function(link) {
link.removeAttr('data-confirm');
return link.trigger('click.rails');
};
$.rails.showConfirmDialog = function(link) {
var html, message;
message = link.attr('data-confirm');
html = "<div id=\"modal1\" class=\"modal\" style=\"z-index: 1003; display: block; opacity: 1; transform: scaleX(1); top: 10%;\"> <div class=\"modal-content\"><h4>" + message + "</h4></div><div class=\"modal-footer\"><a class=\"modal-action modal-close waves-effect waves-red btn-flat close\">Cancel</a><a class=\"modal-action modal-close waves-effect waves-green btn-flat confirm\">OK</a></div></div>";
$('body').append(html);
$('#modal1').openModal({
complete: function() {
$('#modal1').remove();
}
});
return $('#modal1 .confirm').on('click', function() {
return $.rails.confirmed(link);
});
return $('#modal1 .close').on('click', function() {
return $('#modal1').closeModal();
});
};
});
@chris-gooley
Copy link

Ive updated this to work with v0.97.8 here: https://gist.github.com/chris-gooley/6d84f13b6bdc178411390ebe1fef9604

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment