public

Confirmation dialog using jQuery and Bootstrap

  • Download Gist
jquery.bootstrap.confirm.popover.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*
Copyright (c) 2011 Damien Antipa, http://www.nethead.at/, http://damien.antipa.at
 
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* jQuery Plugin: Confirmation Dialog
*
* Requirements: jQuery 1.6.4, Bootstrap 1.3.0
* http://jquery.com/
* http://twitter.github.com/bootstrap/
*
* This Plugin can be used for anchors, to show a confirmation popup before redirecting to the link.
* Original code by Damian Antipa <http://damien.antipa.at/2011/10/jquery-plugin-confirmation/>
*
*/
(function($){
$.fn.extend({
confirmDialog: function(options) {
var defaults = {
message: '<strong>Are you sure</strong>',
dialog: '<div id="confirm-dialog" class="popover">' +
'<div class="arrow"></div>' +
'<div class="inner">' +
'<div class="content">' +
'<p class="message"></p>' +
'<p class="button-group"><a href="#" class="btn small danger"></a><a href="#" class="btn small"></a></p>' +
'</div>' +
'</div>' +
'</div>',
cancelButton: 'Cancel'
};
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
var $elem = $(this)
//is there an existing click handler registered
if ($elem.data('events') && $elem.data('events').click) {
//save the handler (TODO: assumes only one)
var targetClickFun = $elem.data('events').click[0].handler;
//unbind it to prevent it firing
$elem.unbind('click');
}else{
//assume there is a href attribute to redirect to
var targetClickFun = function() {window.location.href = $elem.attr('href');};
}
$elem.bind('click', function(e) {
e.preventDefault();
if(!$('#confirm-dialog').length) {
var offset = $elem.offset();
var $dialog = $(o.dialog).appendTo('body');
var x;
if(offset.left > $dialog.width()) {
//dialog can be left
x = e.pageX - $dialog.width();
$dialog.addClass('left');
} else {
x = e.pageX;
$dialog.addClass('right');
}
var y = e.pageY - $dialog.height() / 2 - $elem.innerHeight() / 2;
$dialog.css({
display: 'block',
position: 'absolute',
top: y,
left: x
});
$dialog.find('p.button-group').css({
marginTop: '5px',
textAlign: 'right'
});
 
$dialog.find('a.btn').css({
marginLeft: '3px'
});
$dialog.find('p.message').html(o.message);
$dialog.find('a.btn:eq(0)').text($elem.text()).bind('click', function(e) {
$dialog.remove();
targetClickFun();
});
$dialog.find('a.btn:eq(1)').text(o.cancelButton).bind('click', function(e) {
$dialog.remove();
});
$dialog.bind('mouseleave', function() {
$dialog.fadeOut('slow', function() {
$dialog.remove();
});
});
}
});
});
}
});
})(jQuery);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.