forked from bootboxjs/bootbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootbox.js
290 lines (254 loc) · 8.48 KB
/
bootbox.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var bootbox = window.bootbox || (function() {
var that = {};
var _locale = _defaultLocale = 'en';
/**
* standard locales. Please add more according to ISO 639-1 standard. Multiple language variants are
* unlikely to be required. If this gets too large it can be split out into separate JS files.
*/
var _locales = {
'en' : {
OK : 'OK',
CANCEL : 'Cancel',
CONFIRM : 'OK'
},
'fr' : {
OK : 'OK',
CANCEL : 'Annuler',
CONFIRM : 'D\'accord'
},
'de' : {
OK : 'OK',
CANCEL : 'Kündigen',
CONFIRM : 'Akzeptieren'
},
'es' : {
OK : 'OK',
CANCEL : 'Cancelar',
CONFIRM : 'Aceptar'
}
};
function _translate(str, locale) {
// we assume if no target locale is probided then we should take it from current setting
if (locale == null) {
locale = _locale;
}
if (typeof _locales[locale][str] == 'string') {
return _locales[locale][str];
}
// if we couldn't find a lookup then try and fallback to a default translation
if (locale != _defaultLocale) {
return _translate(str, _defaultLocale);
}
// if we can't do anything then bail out with whatever string was passed in - last resort
return str;
}
that.setLocale = function(locale) {
for (var i in _locales) {
if (i == locale) {
_locale = locale;
return;
}
}
throw new Error('Invalid locale: '+locale);
}
that.addLocale = function(locale, translations) {
if (typeof _locales[locale] == 'undefined') {
_locales[locale] = {};
}
for (var str in translations) {
_locales[locale][str] = translations[str];
}
}
that.alert = function(/*str, label, cb*/) {
var str = "";
var label = _translate('OK');
var cb = null;
switch (arguments.length) {
case 1:
str = arguments[0];
break;
case 2:
str = arguments[0];
if (typeof arguments[1] == 'function') {
cb = arguments[1];
} else {
label = arguments[1];
}
break;
case 3:
str = arguments[0];
label = arguments[1];
cb = arguments[2];
break;
default:
throw new Error("Incorrect number of arguments: expected 1-3");
break;
}
return that.dialog(str, {
"label": label,
"callback": cb
}, {
"onEscape": cb
});
};
that.confirm = function(/*str, labelCancel, labelOk, cb*/) {
var str = "";
var labelCancel = _translate('CANCEL');
var labelOk = _translate('CONFIRM');
var cb = null;
switch (arguments.length) {
case 1:
str = arguments[0];
break;
case 2:
str = arguments[0];
if (typeof arguments[1] == 'function') {
cb = arguments[1];
} else {
labelCancel = arguments[1];
}
break;
case 3:
str = arguments[0];
labelCancel = arguments[1];
if (typeof arguments[2] == 'function') {
cb = arguments[2];
} else {
labelOk = arguments[2];
}
break;
case 4:
str = arguments[0];
labelCancel = arguments[1];
labelOk = arguments[2];
cb = arguments[3];
break;
default:
throw new Error("Incorrect number of arguments: expected 1-4");
break;
}
return that.dialog(str, [{
"label": labelCancel,
"callback": function() {
if (typeof cb == 'function') {
cb(false);
}
}
}, {
"label": labelOk,
"callback": function() {
if (typeof cb == 'function') {
cb(true);
}
}
}]);
}
that.dialog = function(str, handlers, options) {
var hideSource = null;
var buttons = "";
var callbacks = [];
var options = options || {};
// check for single object and convert to array if necessary
if (handlers == null) {
handlers = [];
} else if (typeof handlers.length == 'undefined') {
handlers = [handlers];
}
var i = handlers.length;
while (i--) {
var label = null;
var _class = null;
var callback = null;
if (typeof handlers[i]['label'] == 'undefined' &&
typeof handlers[i]['class'] == 'undefined' &&
typeof handlers[i]['callback'] == 'undefined') {
// if we've got nothing we expect, check for condensed format
var propCount = 0; // condensed will only match if this == 1
var property = null; // save the last property we found
for (var j in handlers[i]) {
property = j;
propCount ++;
if (propCount > 1) {
// forget it, too many properties
break;
}
}
if (propCount == 1 && typeof handlers[i][j] == 'function') {
// matches condensed format of label -> function
handlers[i]['label'] = property;
handlers[i]['callback'] = handlers[i][j];
}
}
if (typeof handlers[i]['callback']== 'function') {
callback = handlers[i]['callback'];
}
if (handlers[i]['class']) {
_class = handlers[i]['class'];
} else if (i == handlers.length -1 && handlers.length <= 2) {
_class = 'primary';
} else if (i == 0 && handlers.length == 2) {
_class = 'danger';
}
if (handlers[i]['label']) {
label = handlers[i]['label'];
} else {
label = "Option "+(i+1);
}
buttons += "<a data-handler='"+i+"' class='btn "+_class+"' href='#'>"+label+"</a>";
callbacks[i] = callback;
}
var div = $([
"<div class='bootbox modal hide fade'>",
"<div class='modal-body'>",
str,
"</div>",
"<div class='modal-footer'>",
buttons,
"</div>",
"</div>"
].join("\n"));
div.bind('hidden', function() {
div.remove();
});
div.bind('hide', function() {
if (hideSource == 'escape' &&
typeof options.onEscape == 'function') {
options.onEscape();
}
});
// hook into the modal's keyup trigger to check for the escape key
$(document).bind('keyup.modal', function ( e ) {
if (e.which == 27) {
hideSource = 'escape';
}
});
// well, *if* we have a primary - give the last dom element (first displayed) focus
div.bind('shown', function() {
$("a.primary:last", div).focus();
});
$("a", div).click(function(e) {
e.preventDefault();
hideSource = 'button';
div.modal("hide");
var handler = $(this).data("handler");
var cb = callbacks[handler];
if (typeof cb == 'function') {
cb();
}
});
if (options.keyboard == null) {
options.keyboard = (typeof options.onEscape == 'function');
}
div.modal({
"backdrop" : options.backdrop || "static",
"show" : options.show || true,
"keyboard" : options.keyboard
});
$("body").append(div);
return div;
}
that.hideAll = function() {
$(".bootbox").modal("hide");
}
return that;
})();