Created
August 14, 2013 16:57
-
-
Save clouddueling/6233055 to your computer and use it in GitHub Desktop.
confirm directive angularjs
This file contains hidden or 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
| directives.directive('confirmButton', function($document) { | |
| return { | |
| restrict: 'A', | |
| link: function(scope, element, attrs) { | |
| var buttonId, html, message, nope, title, yep; | |
| buttonId = Math.floor(Math.random() * 10000000000); | |
| attrs.buttonId = buttonId; | |
| message = attrs.message || ""; | |
| yep = attrs.yes || "Yes"; | |
| nope = attrs.no || "No"; | |
| title = attrs.title || "Confirm"; | |
| placement = attrs.placement || 'right'; | |
| html = "<div id='button-" + buttonId + "'> \ | |
| <span class='confirmbutton-msg'>" + message + "</span> \ | |
| <span class='confirmbutton-yes btn btn-danger'>" + yep + "</span> \ | |
| <span class='confirmbutton-no btn'>" + nope + "</span> \ | |
| </div>"; | |
| element.popover({ | |
| content: html, | |
| html: true, | |
| trigger: "manual", | |
| title: title, | |
| placement: placement, | |
| }); | |
| return element.bind('click', function(e) { | |
| var dontBubble = true, | |
| pop; | |
| e.stopPropagation(); | |
| element.popover('show'); | |
| pop = $("#button-" + buttonId); | |
| pop.closest(".popover").click(function(e) { | |
| e.preventDefault(); | |
| if (dontBubble) | |
| e.stopPropagation(); | |
| }); | |
| pop.find('.confirmbutton-yes').click(function(e) { | |
| // this closes popover and doesn't conflict with popup directive | |
| pop.closest('.popover').remove(); | |
| scope.$eval(attrs.confirmButton); | |
| }); | |
| // just hide the popover from view | |
| pop.find('.confirmbutton-no').click(function(e) { | |
| // kill binds | |
| $document.off('click.confirmbutton.' + buttonId); | |
| pop.closest('.popover').remove(); | |
| }); | |
| $document.on('click.confirmbutton.' + buttonId, ":not(.popover, .popover *)", function() { | |
| $document.off('click.confirmbutton.' + buttonId); | |
| pop.closest('.popover').remove(); | |
| }); | |
| }); | |
| } | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment