forked from mistic100/Bootstrap-Confirmation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-confirmation.js
258 lines (217 loc) · 7.42 KB
/
bootstrap-confirmation.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*!
* Bootstrap Confirmation
* Copyright 2013 Nimit Suwannagate <[email protected]>
* Copyright 2014 Damien "Mistic" Sorel <http://www.strangeplanet.fr>
* Licensed under the Apache License, Version 2.0 (the "License")
*/
(function ($) {
'use strict';
// Confirmation extends popover.js
if (!$.fn.popover) throw new Error('Confirmation requires popover.js');
// CONFIRMATION PUBLIC CLASS DEFINITION
// ===============================
var Confirmation = function (element, options) {
this.init('confirmation', element, options);
var that = this;
if (!this.options.selector) {
// get existing href and target
if (this.$element.attr('href')) {
this.options.href = this.$element.attr('href');
this.$element.removeAttr('href');
if (this.$element.attr('target')) {
this.options.target = this.$element.attr('target');
}
}
// cancel original event
this.$element.on(that.options.trigger, function(e, ack) {
if (!ack) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
}
});
// trigger original event on confirm
this.$element.on('confirmed.bs.confirmation', function(e) {
$(this).trigger(that.options.trigger, [true]);
});
// manage singleton
this.$element.on('show.bs.confirmation', function(e) {
if (that.options.singleton) {
// close all other popover already initialized
$(that.options._selector).not($(this)).filter(function() {
return $(this).data('bs.confirmation') !== undefined;
}).confirmation('hide');
}
});
}
if (!this.options._isDelegate) {
// manage popout
this.eventBody = false;
this.uid = this.$element[0].id || this.getUID('group_');
this.$element.on('shown.bs.confirmation', function(e) {
if (that.options.popout && !that.eventBody) {
var $this = $(this);
that.eventBody = $('body').on('click.bs.confirmation.'+that.uid, function(e) {
if ($(that.options._selector).is(e.target)) {
return;
}
// close all popover already initialized
$(that.options._selector).filter(function() {
return $(this).data('bs.confirmation') !== undefined;
}).confirmation('hide');
$('body').off('click.bs.'+that.uid);
that.eventBody = false;
});
}
});
}
};
Confirmation.DEFAULTS = $.extend({}, $.fn.popover.Constructor.DEFAULTS, {
placement: 'top',
title: 'Are you sure?',
html: true,
href: false,
popout: false,
singleton: false,
target: '_self',
onConfirm: $.noop,
onCancel: $.noop,
btnOkClass: 'btn-xs btn-primary',
btnOkIcon: 'glyphicon glyphicon-ok',
btnOkLabel: 'Yes',
btnCancelClass: 'btn-xs btn-default',
btnCancelIcon: 'glyphicon glyphicon-remove',
btnCancelLabel: 'No',
template:
'<div class="popover confirmation">' +
'<div class="arrow"></div>' +
'<h3 class="popover-title"></h3>' +
'<div class="popover-content text-center">'+
'<div class="btn-group">'+
'<a class="btn" data-apply="confirmation"></a>'+
'<a class="btn" data-dismiss="confirmation"></a>'+
'</div>'+
'</div>'+
'</div>'
});
Confirmation.prototype = $.extend({}, $.fn.popover.Constructor.prototype);
Confirmation.prototype.constructor = Confirmation;
Confirmation.prototype.getDefaults = function () {
return Confirmation.DEFAULTS;
};
// custom init keeping trace of selectors
Confirmation.prototype.init = function(type, element, options) {
$.fn.popover.Constructor.prototype.init.call(this, type, element, options);
this.options._isDelegate = false;
if (options.selector) { // container of buttons
this.options._selector = this._options._selector = options._root_selector +' '+ options.selector;
}
else if (options._selector) { // children of container
this.options._selector = options._selector;
this.options._isDelegate = true;
}
else { // standalone
this.options._selector = options._root_selector;
}
};
Confirmation.prototype.setContent = function () {
var that = this,
$tip = this.tip(),
o = this.options;
$tip.find('.popover-title')[o.html ? 'html' : 'text'](this.getTitle());
// configure 'ok' button
$tip.find('[data-apply="confirmation"]')
.addClass(o.btnOkClass)
.html(o.btnOkLabel)
.prepend($('<i></i>').addClass(o.btnOkIcon), ' ')
.off('click')
.one('click', function(e) {
that.getOnConfirm.call(that).call(that.$element);
that.$element.trigger('confirmed.bs.confirmation');
that.leave(that);
});
// add href to confirm button if needed
if (o.href && o.href != "#")
$tip.find('[data-apply="confirmation"]').attr({
href: o.href,
target: o.target
});
}
// configure 'cancel' button
$tip.find('[data-dismiss="confirmation"]')
.addClass(o.btnCancelClass)
.html(o.btnCancelLabel)
.prepend($('<i></i>').addClass(o.btnCancelIcon), ' ')
.off('click')
.one('click', function(e) {
that.getOnCancel.call(that).call(that.$element);
that.$element.trigger('canceled.bs.confirmation');
that.leave(that);
});
$tip.removeClass('fade top bottom left right in');
// IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
// this manually by checking the contents.
if (!$tip.find('.popover-title').html()) {
$tip.find('.popover-title').hide();
}
};
Confirmation.prototype.getOnConfirm = function() {
if (this.$element.attr('data-on-confirm')) {
return getFunctionFromString(this.$element.attr('data-on-confirm'));
}
else {
return this.options.onConfirm;
}
};
Confirmation.prototype.getOnCancel = function() {
if (this.$element.attr('data-on-cancel')) {
return getFunctionFromString(this.$element.attr('data-on-cancel'));
}
else {
return this.options.onCancel;
}
};
/*
* Generates an anonymous function from a function name
* function name may contain dots (.) to navigate through objects
* root context is window
*/
function getFunctionFromString(functionName) {
var context = window,
namespaces = functionName.split('.'),
func = namespaces.pop();
for (var i=0, l=namespaces.length; i<l; i++) {
context = context[namespaces[i]];
}
return function() {
context[func].call(this);
};
}
// CONFIRMATION PLUGIN DEFINITION
// =========================
var old = $.fn.confirmation;
$.fn.confirmation = function (option) {
var options = (typeof option == 'object' && option) || {};
options._root_selector = this.selector;
return this.each(function () {
var $this = $(this),
data = $this.data('bs.confirmation');
if (!data && option == 'destroy') {
return;
}
if (!data) {
$this.data('bs.confirmation', (data = new Confirmation(this, options)));
}
if (typeof option == 'string') {
data[option]();
}
});
};
$.fn.confirmation.Constructor = Confirmation;
// CONFIRMATION NO CONFLICT
// ===================
$.fn.confirmation.noConflict = function () {
$.fn.confirmation = old;
return this;
};
}(jQuery));