Forked from dannyvassallo/_materializecss-rails-confirm.coffee
Last active
May 1, 2018 12:55
-
-
Save chris-gooley/6d84f13b6bdc178411390ebe1fef9604 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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
$.rails.allowAction = (link) -> | |
return true unless link.attr("data-confirm") | |
$.rails.showConfirmDialog link | |
false | |
$.rails.confirmed = (link) -> | |
link.removeAttr "data-confirm" | |
link[0].click() # force click on element directly | |
$.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").modal complete: -> | |
$("#modal1").remove() | |
$("#modal1 .close").on "click", -> | |
$("#modal1").modal('close') | |
return $("#modal1 .confirm").on("click", -> | |
$.rails.confirmed link | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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"); | |
}; | |
return $.rails.showConfirmDialog = function(link) { | |
var html, message; | |
html = void 0; | |
message = void 0; | |
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").modal({ | |
complete: function() { | |
return $("#modal1").remove(); | |
} | |
}); | |
$("#modal1 .close").on("click", function() { | |
return $("#modal1").modal('close'); | |
}); | |
return $("#modal1 .confirm").on("click", function() { | |
return $.rails.confirmed(link); | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Chris, this looks great !!
But I just have one problem, the modal does not close with the buttons (cancel or ok).
To solve it, I just change the last functions :
I also made some updates to have a background and slideUp/slideDown animations if you need it :
See Updates