forked from ethaizone/Bootstrap-Confirmation
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrap-confirmation.js
244 lines (185 loc) · 6.56 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
/* ===========================================================
* bootstrap-confirmation.js v1.0.1
* http://ethaizone.github.io/Bootstrap-Confirmation/
* ===========================================================
* Copyright 2013 Nimit Suwannagate <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================================================== */
!function ($) {
"use strict"; // jshint ;_;
/* CONFIRMATION PUBLIC CLASS DEFINITION
* =============================== */
//var for check event at body can have only one.
var event_body = false;
var Confirmation = function (element, options) {
var that = this;
// remove href attribute of button
$(element).removeAttr('href')
this.init('confirmation', element, options)
$(element).on('show', function(e) {
var options = that.options;
var all = options.all_selector;
if(options.singleton) {
$(all).not(that.$element).confirmation('hide');
}
});
$(element).on('shown', function(e) {
var options = that.options;
var all = options.all_selector;
$(this).next('.popover').one('click.dismiss.confirmation', '[data-dismiss="confirmation"]', $.proxy(that.hide, that))
if(that.isPopout()) {
if(!event_body) {
event_body = $('body').on('click', function (e) {
if($(all).is(e.target)) return;
if($(all).next('div').has(e.target).length) return;
$(all).confirmation('hide');
$('body').unbind(e);
event_body = false;
return;
});
}
}
});
}
/* NOTE: CONFIRMATION EXTENDS BOOTSTRAP-TOOLTIP.js
========================================== */
Confirmation.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {
constructor: Confirmation
, setContent: function () {
var $tip = this.tip()
, title = this.getTitle()
, href = this.getHref()
, target = this.getTarget()
, $e = this.$element
, btnOkClass = this.getBtnOkClass()
, btnCancelClass = this.getBtnCancelClass()
, btnOkLabel = this.getBtnOkLabel()
, btnCancelLabel = this.getBtnCancelLabel()
, o = this.options
$tip.find('.popover-title').text(title);
var btnOk = $tip.find('.popover-content > div > a:not([data-dismiss="confirmation"])');
var btnCancel = $tip.find('.popover-content > div > a[data-dismiss="confirmation"]');
btnOk.addClass(btnOkClass).html(btnOkLabel).attr('href', href).attr('target', target).on('click', o.onConfirm);
btnCancel.addClass(btnCancelClass).html(btnCancelLabel).on('click', o.onCancel);
$tip.removeClass('fade top bottom left right in')
}
, hasContent: function () {
return this.getTitle()
}
, isPopout: function () {
var popout
, $e = this.$element
, o = this.options
popout = $e.attr('data-popout') || (typeof o.popout == 'function' ? o.popout.call($e[0]) : o.popout)
if(popout == 'false') popout = false;
return popout
}
, getHref: function () {
var href
, $e = this.$element
, o = this.options
href = $e.attr('data-href') || (typeof o.href == 'function' ? o.href.call($e[0]) : o.href)
return href
}
, getTarget: function () {
var target
, $e = this.$element
, o = this.options
target = $e.attr('data-target') || (typeof o.target == 'function' ? o.target.call($e[0]) : o.target)
return target
}
, getBtnOkClass: function () {
var btnOkClass
, $e = this.$element
, o = this.options
btnOkClass = $e.attr('data-btnOkClass') || (typeof o.btnOkClass == 'function' ? o.btnOkClass.call($e[0]) : o.btnOkClass)
return btnOkClass
}
, getBtnCancelClass: function () {
var btnCancelClass
, $e = this.$element
, o = this.options
btnCancelClass = $e.attr('data-btnCancelClass') || (typeof o.btnCancelClass == 'function' ? o.btnCancelClass.call($e[0]) : o.btnCancelClass)
return btnCancelClass
}
, getBtnOkLabel: function () {
var btnOkLabel
, $e = this.$element
, o = this.options
btnOkLabel = $e.attr('data-btnOkLabel') || (typeof o.btnOkLabel == 'function' ? o.btnOkLabel.call($e[0]) : o.btnOkLabel)
return btnOkLabel
}
, getBtnCancelLabel: function () {
var btnCancelLabel
, $e = this.$element
, o = this.options
btnCancelLabel = $e.attr('data-btnCancelLabel') || (typeof o.btnCancelLabel == 'function' ? o.btnCancelLabel.call($e[0]) : o.btnCancelLabel)
return btnCancelLabel
}
, tip: function () {
this.$tip = this.$tip || $(this.options.template)
return this.$tip
}
, destroy: function () {
this.hide().$element.off('.' + this.type).removeData(this.type)
}
})
/* CONFIRMATION PLUGIN DEFINITION
* ======================= */
var old = $.fn.confirmation
$.fn.confirmation = function (option) {
var that = this
return this.each(function () {
var $this = $(this)
, data = $this.data('confirmation')
, options = typeof option == 'object' && option
options = options || {}
options.all_selector = that.selector
if (!data) $this.data('confirmation', (data = new Confirmation(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.confirmation.Constructor = Confirmation
$.fn.confirmation.defaults = $.extend({} , $.fn.tooltip.defaults, {
placement: 'top'
, trigger: 'click'
, target : '_self'
, href : '#'
, title: 'Are you sure?'
, template: '<div class="popover">' +
'<div class="arrow"></div>' +
'<h3 class="popover-title"></h3>' +
'<div class="popover-content text-center">' +
'<div class="btn-group">' +
'<a class="btn btn-small" href="" target=""></a>' +
'<a class="btn btn-small" data-dismiss="confirmation"></a>' +
'</div>' +
'</div>' +
'</div>'
, btnOkClass: 'btn-primary'
, btnCancelClass: ''
, btnOkLabel: '<i class="icon-ok-sign icon-white"></i> Yes'
, btnCancelLabel: '<i class="icon-remove-sign"></i> No'
, singleton: false
, popout: false
, onConfirm: function(){}
, onCancel: function(){}
})
/* POPOVER NO CONFLICT
* =================== */
$.fn.confirmation.noConflict = function () {
$.fn.confirmation = old
return this
}
}(window.jQuery);