forked from ethaizone/Bootstrap-Confirmation
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrap3-confirmation.js
228 lines (185 loc) · 6.74 KB
/
bootstrap3-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
/* ========================================================================
* bootstrap3-confirmation.js v1.0.1
* Adaptation of bootstrap-confirmation.js
* from Nimit Suwannagate <[email protected]>
* http://ethaizone.github.io/Bootstrap-Confirmation/
* ========================================================================
* Copyright 2013 Thomas Jacquart <[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";
// COMFIRMATION PUBLIC CLASS DEFINITION
// ====================================
var Confirmation = function (element, options) {
// First init from popover
this.init('confirmation', element, options)
var options = this.options
if (options.singleton) {
// remove others when I show me
}
var that = this;
if (options.popout) {
// close when I click outside the box
var $tip = this.tip()
// $('html').on('click', function(){
// that.leave(that)
// })
}
}
if (!$.fn.popover || !$.fn.tooltip) throw new Error('Confirmation requires popover.js and tooltip.js')
Confirmation.DEFAULTS = $.extend({} , $.fn.popover.Constructor.DEFAULTS, {
placement : 'top'
, title : 'Are you sure ?'
, btnOkClass : 'btn btn-primary'
, btnOkLabel : 'Yes'
, btnOkIcon : 'glyphicon glyphicon-ok'
, btnCancelClass: 'btn btn-default'
, btnCancelLabel: 'No'
, btnCancelIcon : 'glyphicon glyphicon-remove'
, singleton : false
, popout : false
, href : '#'
, target : '_self'
, onConfirm : function(){}
, onCancel : function(){}
, template : '<div class="popover">'
+ '<div class="arrow"></div>'
+ '<h3 class="popover-title"></h3>'
+ '<div class="popover-content">'
+ '<a data-apply="confirmation">Yes</a>'
+ ' <a data-dismiss="confirmation">No</a>'
+ '</div>'
+ '</div>'
})
// NOTE: CONFIRMATION EXTENDS popover.js
// ================================
Confirmation.prototype = $.extend({}, $.fn.popover.Constructor.prototype)
Confirmation.prototype.constructor = Confirmation
Confirmation.prototype.getDefaults = function () {
return Confirmation.DEFAULTS
}
Confirmation.prototype.setContent = function () {
var that = this;
var $tip = this.tip()
var title = this.getTitle()
var $btnOk = $tip.find('[data-apply="confirmation"]');
var $btnCancel = $tip.find('[data-dismiss="confirmation"]');
var options = that.options
$btnOk.addClass(this.getBtnOkClass())
.html(this.getBtnOkLabel())
.prepend($('<i></i>').addClass(this.getBtnOkIcon()), " ")
.attr('href', this.getHref())
.attr('target', this.getTarget())
.one('click', function() {
options.onConfirm()
that.leave(that)
})
$btnCancel.addClass(this.getBtnCancelClass())
.html(this.getBtnCancelLabel())
.prepend($('<i></i>').addClass(this.getBtnCancelIcon()), " ")
.one('click', function(){
options.onCancel()
that.leave(that)
})
$tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
$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.getBtnOkClass = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-btnOkClass')
|| (typeof o.btnOkClass == 'function' ?
o.btnOkClass.call($e[0]) :
o.btnOkClass)
}
Confirmation.prototype.getBtnOkLabel = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-btnOkLabel')
|| (typeof o.btnOkLabel == 'function' ?
o.btnOkLabel.call($e[0]) :
o.btnOkLabel)
}
Confirmation.prototype.getBtnOkIcon = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-btnOkIcon')
|| (typeof o.btnOkIcon == 'function' ?
o.btnOkIcon.call($e[0]) :
o.btnOkIcon)
}
Confirmation.prototype.getBtnCancelClass = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-btnCancelClass')
|| (typeof o.btnCancelClass == 'function' ?
o.btnCancelClass.call($e[0]) :
o.btnCancelClass)
}
Confirmation.prototype.getBtnCancelLabel = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-btnCancelLabel')
|| (typeof o.btnCancelLabel == 'function' ?
o.btnCancelLabel.call($e[0]) :
o.btnCancelLabel)
}
Confirmation.prototype.getBtnCancelIcon = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-btnCancelIcon')
|| (typeof o.btnCancelIcon == 'function' ?
o.btnCancelIcon.call($e[0]) :
o.btnCancelIcon)
}
Confirmation.prototype.getHref = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-href')
|| (typeof o.href == 'function' ?
o.href.call($e[0]) :
o.href)
}
Confirmation.prototype.getTarget = function () {
var $e = this.$element
var o = this.options
return $e.attr('data-target')
|| (typeof o.target == 'function' ?
o.target.call($e[0]) :
o.target)
}
// CONFIRMATION PLUGIN DEFINITION
// ==============================
var old = $.fn.confirmation
$.fn.confirmation = function (option) {
var that = this
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.confirmation')
var options = typeof option == 'object' && option
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);