-
Notifications
You must be signed in to change notification settings - Fork 8
/
bootstrap-confirm-button.src.js
73 lines (59 loc) · 2.08 KB
/
bootstrap-confirm-button.src.js
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
/*!
* Bootstrap Confirm Button
* https://github.com/stefanocudini/bootstrap-confirm-button
*
* Copyright 2017, Stefano Cudini - [email protected]
* Licensed under the MIT license.
*/
jQuery.fn.btsConfirmButton = function(opts, callback) {
if(typeof opts === 'string')
opts = {msg: opts};
else if(typeof opts === 'function')
callback = opts;
callback = callback || $.noop;
opts = $.extend({
msg: "I'm sure!",
classname: 'btn-danger',
timeout: 2000
}, opts);
$(this).each(function(idx, btn) {
var timeoToken,
thisBtn$ = $(btn),
datas = thisBtn$.data(),
oriHtml = thisBtn$.html(),
optsEl = $.extend({}, opts);
for(var i in datas) {
var opt, type, val = datas[i];
if( (opt = i.match(/^confirm(.*)$/)) && (type = opt[1].toLowerCase()) ) {
optsEl[type] = val;
}
}
function resetBtn() {
thisBtn$.html(oriHtml).removeClass(optsEl.classname).data('confirmed',false);
}
thisBtn$.data('confirmed', false);
thisBtn$.on('click.confirm', function(e) {
e.preventDefault();
if(thisBtn$.data('confirmed'))
{
$(e.target).trigger('confirm:after');
callback.call(thisBtn$, e);
resetBtn();
}
else
{
thisBtn$.data('confirmed',true);
$(e.target).trigger('confirm:before', e.target);
thisBtn$.html(optsEl.msg).addClass(optsEl.classname).bind('mouseout.confirm', function() {
timeoToken = setTimeout(function() {
resetBtn();
$(e.target).trigger('confirm:expired', e.target);
}, parseInt(optsEl.timeout));
}).bind('mouseover.confirm', function() {
clearTimeout(timeoToken);
});
}
}).removeClass(optsEl.classname);
});
return $(this);
};