-
Notifications
You must be signed in to change notification settings - Fork 1k
/
bootbox.js
1208 lines (974 loc) · 40.1 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! @preserve
* bootbox.js
* version: 6.0.0
* author: Nick Payne <nick@kurai.co.uk>
* license: MIT
* http://bootboxjs.com/
*/
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('jquery'));
} else {
// Browser globals (root is window)
root.bootbox = factory(root.jQuery);
}
}(this, function init($, undefined) {
'use strict';
let exports = {};
let VERSION = '6.0.0';
exports.VERSION = VERSION;
let locales = {
'en' : {
OK : 'OK',
CANCEL : 'Cancel',
CONFIRM : 'OK'
}
};
let templates = {
dialog: '<div class="bootbox modal" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-body"><div class="bootbox-body"></div></div></div></div></div>',
header: '<div class="modal-header"><h5 class="modal-title"></h5></div>',
footer: '<div class="modal-footer"></div>',
closeButton: '<button type="button" class="bootbox-close-button close btn-close" aria-hidden="true" aria-label="Close"></button>',
form: '<form class="bootbox-form"></form>',
button: '<button type="button" class="btn"></button>',
option: '<option value=""></option>',
promptMessage: '<div class="bootbox-prompt-message"></div>',
inputs: {
text: '<input class="bootbox-input bootbox-input-text form-control" autocomplete="off" type="text" />',
textarea: '<textarea class="bootbox-input bootbox-input-textarea form-control"></textarea>',
email: '<input class="bootbox-input bootbox-input-email form-control" autocomplete="off" type="email" />',
select: '<select class="bootbox-input bootbox-input-select form-select"></select>',
checkbox: '<div class="form-check checkbox"><label class="form-check-label"><input class="form-check-input bootbox-input bootbox-input-checkbox" type="checkbox" /></label></div>',
radio: '<div class="form-check radio"><label class="form-check-label"><input class="form-check-input bootbox-input bootbox-input-radio" type="radio" name="bootbox-radio" /></label></div>',
date: '<input class="bootbox-input bootbox-input-date form-control" autocomplete="off" type="date" />',
time: '<input class="bootbox-input bootbox-input-time form-control" autocomplete="off" type="time" />',
number: '<input class="bootbox-input bootbox-input-number form-control" autocomplete="off" type="number" />',
password: '<input class="bootbox-input bootbox-input-password form-control" autocomplete="off" type="password" />',
range: '<input class="bootbox-input bootbox-input-range form-control-range" autocomplete="off" type="range" />'
}
};
let defaults = {
// Default language used when generating buttons for alert, confirm, and prompt dialogs
locale: 'en',
// Show backdrop or not. Default to static so user has to interact with dialog
backdrop: 'static',
// Animate the modal in/out
animate: true,
// Additional class string applied to the top level dialog
className: null,
// Whether or not to include a close button
closeButton: true,
// Show the dialog immediately by default
show: true,
// Dialog container
container: 'body',
// Default value (used by the prompt helper)
value: '',
// Default input type (used by the prompt helper)
inputType: 'text',
// Custom error message to report if prompt fails validation
errorMessage: null,
// Switch button order from cancel/confirm (default) to confirm/cancel
swapButtonOrder: false,
// Center modal vertically in page
centerVertical: false,
// Append "multiple" property to the select when using the "prompt" helper
multiple: false,
// Automatically scroll modal content when height exceeds viewport height
scrollable: false,
// Whether or not to destroy the modal on hide
reusable: false,
// The element which triggered the dialog
relatedTarget: null,
// The size of the modal to generate
size: null,
// A unique indentifier for this modal
id: null
};
// PUBLIC FUNCTIONS
// *************************************************************************************************************
/**
* Return all currently registered locales, or a specific locale if "name" is defined
* @param {string} [name]
* @returns {(Object|Object[])} An array of the available locale objects, or a single locale object if {name} is not null
*/
exports.locales = function (name) {
return name ? locales[name] : locales;
};
/**
* Register localized strings for the OK, CONFIRM, and CANCEL buttons
* @param {string} name - The key used to identify the new locale in the locales array
* @param {Object} values - An object containing the localized string for each of the OK, CANCEL, and CONFIRM properties of a locale
* @returns The updated bootbox object
*/
exports.addLocale = function (name, values) {
$.each(['OK', 'CANCEL', 'CONFIRM'], function (_, v) {
if (!values[v]) {
throw new Error('Please supply a translation for "' + v + '"');
}
});
locales[name] = {
OK: values.OK,
CANCEL: values.CANCEL,
CONFIRM: values.CONFIRM
};
return exports;
};
/**
* Remove a previously-registered locale
* @param {string} name - The key identifying the locale to remove
* @returns The updated bootbox object
*/
exports.removeLocale = function (name) {
if (name !== 'en') {
delete locales[name];
}
else {
throw new Error('"en" is used as the default and fallback locale and cannot be removed.');
}
return exports;
};
/**
* Set the default locale
* @param {string} name - The key identifying the locale to set as the default locale for all future bootbox calls
* @returns The updated bootbox object
*/
exports.setLocale = function (name) {
return exports.setDefaults('locale', name);
};
/**
* Override default value(s) of Bootbox.
* @returns The updated bootbox object
*/
exports.setDefaults = function () {
let values = {};
if (arguments.length === 2) {
// Allow passing of single key/value...
values[arguments[0]] = arguments[1];
} else {
// ... and as an object too
values = arguments[0];
}
$.extend(defaults, values);
return exports;
};
/**
* Hides all currently active Bootbox modals
* @returns The current bootbox object
*/
exports.hideAll = function () {
$('.bootbox').modal('hide');
return exports;
};
/**
* Allows the base init() function to be overridden
* @param {function} _$ - A function to be called when the bootbox instance is created
* @returns The current bootbox object
*/
exports.init = function (_$) {
return init(_$ || $);
};
// CORE HELPER FUNCTIONS
// *************************************************************************************************************
/**
* The core dialog helper function, which can be used to create any custom Bootstrap modal.
* @param {Object} options - An object used to configure the various properties which define a Bootbox dialog
* @returns A jQuery object upon which Bootstrap's modal function has been called
*/
exports.dialog = function (options) {
if ($.fn.modal === undefined) {
throw new Error(
'"$.fn.modal" is not defined; please double check you have included the Bootstrap JavaScript library. See https://getbootstrap.com/docs/5.1/getting-started/introduction/ for more details.'
);
}
options = sanitize(options);
if ($.fn.modal.Constructor.VERSION) {
options.fullBootstrapVersion = $.fn.modal.Constructor.VERSION;
let i = options.fullBootstrapVersion.indexOf('.');
options.bootstrap = options.fullBootstrapVersion.substring(0, i);
}
else {
// Assuming version 2.3.2, as that was the last "supported" 2.x version
options.bootstrap = '2';
options.fullBootstrapVersion = '2.3.2';
console.warn('Bootbox will *mostly* work with Bootstrap 2, but we do not officially support it. Please upgrade, if possible.');
}
let dialog = $(templates.dialog);
let innerDialog = dialog.find('.modal-dialog');
let body = dialog.find('.modal-body');
let header = $(templates.header);
let footer = $(templates.footer);
let buttons = options.buttons;
let callbacks = {
onEscape: options.onEscape
};
body.find('.bootbox-body').html(options.message);
// Only attempt to create buttons if at least one has been defined in the options object
if (getKeyLength(options.buttons) > 0) {
each(buttons, function (key, b) {
let button = $(templates.button);
button.data('bb-handler', key);
button.addClass(b.className);
switch (key) {
case 'ok':
case 'confirm':
button.addClass('bootbox-accept');
break;
case 'cancel':
button.addClass('bootbox-cancel');
break;
}
button.html(b.label);
if (b.id) {
button.attr({ 'id': b.id });
}
if (b.disabled === true) {
button.prop({ disabled: true });
}
footer.append(button);
callbacks[key] = b.callback;
});
body.after(footer);
}
if (options.animate === true) {
dialog.addClass('fade');
}
if (options.className) {
dialog.addClass(options.className);
}
if (options.id) {
dialog.attr({ 'id': options.id });
}
if (options.size) {
// Requires Bootstrap 3.1.0 or higher
if (options.fullBootstrapVersion.substring(0, 3) < '3.1') {
console.warn('"size" requires Bootstrap 3.1.0 or higher. You appear to be using ' + options.fullBootstrapVersion + '. Please upgrade to use this option.');
}
switch (options.size) {
case 'small':
case 'sm':
innerDialog.addClass('modal-sm');
break;
case 'large':
case 'lg':
innerDialog.addClass('modal-lg');
break;
case 'extra-large':
case 'xl':
innerDialog.addClass('modal-xl');
// Requires Bootstrap 4.2.0 or higher
if (options.fullBootstrapVersion.substring(0, 3) < '4.2') {
console.warn('Using size "xl"/"extra-large" requires Bootstrap 4.2.0 or higher. You appear to be using ' + options.fullBootstrapVersion + '. Please upgrade to use this option.');
}
break;
}
}
if (options.scrollable) {
innerDialog.addClass('modal-dialog-scrollable');
// Requires Bootstrap 4.3.0 or higher
if (options.fullBootstrapVersion.substring(0, 3) < '4.3') {
console.warn('Using "scrollable" requires Bootstrap 4.3.0 or higher. You appear to be using ' + options.fullBootstrapVersion + '. Please upgrade to use this option.');
}
}
if(options.title || options.closeButton) {
if (options.title) {
header.find('.modal-title').html(options.title);
}
else {
header.addClass('border-0');
}
if (options.closeButton) {
let closeButton = $(templates.closeButton);
if (options.bootstrap < 5) {
closeButton.html('×');
}
/* Note: the close button for Bootstrap 5+ does not contain content */
if(options.bootstrap < 4) {
/* Bootstrap 3 and under */
header.prepend(closeButton);
}
else {
header.append(closeButton);
}
}
body.before(header);
}
if (options.centerVertical) {
innerDialog.addClass('modal-dialog-centered');
// Requires Bootstrap 4.0.0-beta.3 or higher
if (options.fullBootstrapVersion < '4.0.0') {
console.warn('"centerVertical" requires Bootstrap 4.0.0-beta.3 or higher. You appear to be using ' + options.fullBootstrapVersion + '. Please upgrade to use this option.');
}
}
// Bootstrap event listeners; these handle extra setup & teardown required after the underlying modal has performed certain actions.
if(!options.reusable) {
// make sure we unbind any listeners once the dialog has definitively been dismissed
dialog.one('hide.bs.modal', { dialog: dialog }, unbindModal);
dialog.one('hidden.bs.modal', { dialog: dialog }, destroyModal);
}
if (options.onHide) {
if (typeof options.onHide === 'function') {
dialog.on('hide.bs.modal', options.onHide);
}
else {
throw new Error('Argument supplied to "onHide" must be a function');
}
}
if (options.onHidden) {
if (typeof options.onHidden === 'function') {
dialog.on('hidden.bs.modal', options.onHidden);
}
else {
throw new Error('Argument supplied to "onHidden" must be a function');
}
}
if (options.onShow) {
if (typeof options.onShow === 'function') {
dialog.on('show.bs.modal', options.onShow);
}
else {
throw new Error('Argument supplied to "onShow" must be a function');
}
}
dialog.one('shown.bs.modal', { dialog: dialog }, focusPrimaryButton);
if (options.onShown) {
if (typeof options.onShown === 'function') {
dialog.on('shown.bs.modal', options.onShown);
}
else {
throw new Error('Argument supplied to "onShown" must be a function');
}
}
// Bootbox event listeners; used to decouple some behaviours from their respective triggers
if (options.backdrop === true) {
let startedOnBody = false;
// Prevents the event from propagating to the backdrop, when something inside the dialog is clicked
dialog.on('mousedown', '.modal-content', function(e) {
e.stopPropagation();
startedOnBody = true;
});
// A boolean true/false according to the Bootstrap docs should show a dialog the user can dismiss by clicking on the background.
// We always only ever pass static/false to the actual $.modal function because with "true" we can't trap this event (the .modal-backdrop swallows it).
// However, we still want to sort-of respect true and invoke the escape mechanism instead
dialog.on('click.dismiss.bs.modal', function (e) {
if (startedOnBody || e.target !== e.currentTarget) {
return;
}
dialog.trigger('escape.close.bb');
});
}
dialog.on('escape.close.bb', function (e) {
// The if() statement looks redundant but it isn't; without it, if we *didn't* have an onEscape handler then processCallback would automatically dismiss the dialog
if (callbacks.onEscape) {
processCallback(e, dialog, callbacks.onEscape);
}
});
dialog.on('click', '.modal-footer button:not(.disabled)', function (e) {
let callbackKey = $(this).data('bb-handler');
if (callbackKey !== undefined) {
// Only process callbacks for buttons we recognize:
processCallback(e, dialog, callbacks[callbackKey]);
}
});
dialog.on('click', '.bootbox-close-button', function (e) {
// onEscape might be falsy, but that's fine; the fact is if the user has managed to click the close button we have to close the dialog, callback or not
processCallback(e, dialog, callbacks.onEscape);
});
dialog.on('keyup', function (e) {
if (e.which === 27) {
dialog.trigger('escape.close.bb');
}
});
/*
The remainder of this method simply deals with adding our dialog element to the DOM, augmenting it with
Bootstrap's modal functionality and then giving the resulting object back to our caller
*/
$(options.container).append(dialog);
dialog.modal({
backdrop: options.backdrop,
keyboard: false,
show: false
});
if (options.show) {
dialog.modal('show', options.relatedTarget);
}
return dialog;
};
/**
* Helper function to simulate the native alert() behavior. **NOTE**: This is non-blocking, so any code that must happen after the alert is dismissed should be placed within the callback function for this alert.
* @returns A jQuery object upon which Bootstrap's modal function has been called
*/
exports.alert = function () {
let options;
options = mergeDialogOptions('alert', ['ok'], ['message', 'callback'], arguments);
// @TODO: can this move inside exports.dialog when we're iterating over each button and checking its button.callback value instead?
if (options.callback && typeof options.callback !== 'function') {
throw new Error('alert requires the "callback" property to be a function when provided');
}
// Override the ok and escape callback to make sure they just invoke the single user-supplied one (if provided)
options.buttons.ok.callback = options.onEscape = function () {
if (typeof options.callback === 'function') {
return options.callback.call(this);
}
return true;
};
return exports.dialog(options);
};
/**
* Helper function to simulate the native confirm() behavior. **NOTE**: This is non-blocking, so any code that must happen after the confirm is dismissed should be placed within the callback function for this confirm.
* @returns A jQuery object upon which Bootstrap's modal function has been called
*/
exports.confirm = function () {
let options;
options = mergeDialogOptions('confirm', ['cancel', 'confirm'], ['message', 'callback'], arguments);
// confirm specific validation; they don't make sense without a callback so make sure it's present
if (typeof options.callback !== 'function') {
throw new Error('confirm requires a callback');
}
// Overrides; undo anything the user tried to set they shouldn't have
options.buttons.cancel.callback = options.onEscape = function () {
return options.callback.call(this, false);
};
options.buttons.confirm.callback = function () {
return options.callback.call(this, true);
};
return exports.dialog(options);
};
/**
* Helper function to simulate the native prompt() behavior. **NOTE**: This is non-blocking, so any code that must happen after the prompt is dismissed should be placed within the callback function for this prompt.
* @returns A jQuery object upon which Bootstrap's modal function has been called
*/
exports.prompt = function () {
let options;
let promptDialog;
let form;
let input;
let shouldShow;
let inputOptions;
// We have to create our form first, otherwise its value is undefined when gearing up our options.
// @TODO this could be solved by allowing message to be a function instead...
form = $(templates.form);
// prompt defaults are more complex than others in that users can override more defaults
options = mergeDialogOptions('prompt', ['cancel', 'confirm'], ['title', 'callback'], arguments);
if (!options.value) {
options.value = defaults.value;
}
if (!options.inputType) {
options.inputType = defaults.inputType;
}
// Capture the user's 'show' value; we always set this to false before spawning the dialog to give us a chance to attach some handlers to it, but we need to make sure we respect a preference not to show it
shouldShow = (options.show === undefined) ? defaults.show : options.show;
// This is required prior to calling the dialog builder below - we need to add an event handler just before the prompt is shown
options.show = false;
// Handles the 'cancel' action
options.buttons.cancel.callback = options.onEscape = function () {
return options.callback.call(this, null);
};
// Prompt submitted - extract the prompt value. This requires a bit of work, given the different input types available.
options.buttons.confirm.callback = function () {
let value;
if (options.inputType === 'checkbox') {
value = input.find('input:checked').map(function () {
return $(this).val();
}).get();
} else if (options.inputType === 'radio') {
value = input.find('input:checked').val();
}
else {
let el = input[0];
// Clear any previous custom error message
if(options.errorMessage) {
el.setCustomValidity('');
}
if (el.checkValidity && !el.checkValidity()) {
// If a custom error message was provided, add it now
if(options.errorMessage){
el.setCustomValidity(options.errorMessage);
}
if(el.reportValidity) {
el.reportValidity();
}
// prevents button callback from being called
return false;
} else {
if (options.inputType === 'select' && options.multiple === true) {
value = input.find('option:selected').map(function () {
return $(this).val();
}).get();
}
else {
value = input.val();
}
}
}
return options.callback.call(this, value);
};
// prompt-specific validation
if (!options.title) {
throw new Error('prompt requires a title');
}
if (typeof options.callback !== 'function') {
throw new Error('prompt requires a callback');
}
if (!templates.inputs[options.inputType]) {
throw new Error('Invalid prompt type');
}
// Create the input based on the supplied type
input = $(templates.inputs[options.inputType]);
switch (options.inputType) {
case 'text':
case 'textarea':
case 'email':
case 'password':
input.val(options.value);
if (options.placeholder) {
input.attr('placeholder', options.placeholder);
}
if (options.pattern) {
input.attr('pattern', options.pattern);
}
if (options.maxlength) {
input.attr('maxlength', options.maxlength);
}
if (options.required) {
input.prop({ 'required': true });
}
if (options.rows && !isNaN(parseInt(options.rows))) {
if (options.inputType === 'textarea') {
input.attr({ 'rows': options.rows });
}
}
break;
case 'date':
case 'time':
case 'number':
case 'range':
input.val(options.value);
if (options.placeholder) {
input.attr('placeholder', options.placeholder);
}
if (options.pattern) {
input.attr('pattern', options.pattern);
}
else {
if(options.inputType === 'date') {
// Add the ISO-8601 short date format as a fallback for browsers without native type="date" support
input.attr('pattern', '\d{4}-\d{2}-\d{2}');
}
else if(options.inputType === 'time') {
// Add an HH:MM pattern as a fallback for browsers without native type="time" support
input.attr('pattern', '\d{2}:\d{2}');
}
}
if (options.required) {
input.prop({ 'required': true });
}
// These input types have extra attributes which affect their input validation.
// Warning: For most browsers, date inputs are buggy in their implementation of 'step', so this attribute will have no effect. Therefore, we don't set the attribute for date inputs.
// @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#Setting_maximum_and_minimum_dates
if (options.inputType !== 'date') {
if (options.step) {
if (options.step === 'any' || (!isNaN(options.step) && parseFloat(options.step) > 0)) {
input.attr('step', options.step);
}
else {
throw new Error('"step" must be a valid positive number or the value "any". See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-step for more information.');
}
}
}
if (minAndMaxAreValid(options.inputType, options.min, options.max)) {
if (options.min !== undefined) {
input.attr('min', options.min);
}
if (options.max !== undefined) {
input.attr('max', options.max);
}
}
break;
case 'select':
let groups = {};
inputOptions = options.inputOptions || [];
if (!Array.isArray(inputOptions)) {
throw new Error('Please pass an array of input options');
}
if (!inputOptions.length) {
throw new Error('prompt with "inputType" set to "select" requires at least one option');
}
if (options.required) {
input.prop({ 'required': true });
}
if (options.multiple) {
input.prop({ 'multiple': true });
}
each(inputOptions, function (_, option) {
// Assume the element to attach to is the input...
let elem = input;
if (option.value === undefined || option.text === undefined) {
throw new Error('each option needs a "value" property and a "text" property');
}
// ... but override that element if this option sits in a group
if (option.group) {
// Initialise group if necessary
if (!groups[option.group]) {
groups[option.group] = $('<optgroup />').attr('label', option.group);
}
elem = groups[option.group];
}
let o = $(templates.option);
o.attr('value', option.value).text(option.text);
elem.append(o);
});
each(groups, function (_, group) {
input.append(group);
});
// Safe to set a select's value as per a normal input
input.val(options.value);
if (options.bootstrap < 5) {
input.removeClass('form-select').addClass('form-control');
}
break;
case 'checkbox':
let checkboxValues = Array.isArray(options.value) ? options.value : [options.value];
inputOptions = options.inputOptions || [];
if (!inputOptions.length) {
throw new Error('prompt with "inputType" set to "checkbox" requires at least one option');
}
// Checkboxes have to nest within a containing element, so they break the rules a bit and we end up re-assigning our 'input' element to this container instead
input = $('<div class="bootbox-checkbox-list"></div>');
each(inputOptions, function (_, option) {
if (option.value === undefined || option.text === undefined) {
throw new Error('each option needs a "value" property and a "text" property');
}
let checkbox = $(templates.inputs[options.inputType]);
checkbox.find('input').attr('value', option.value);
checkbox.find('label').append('\n' + option.text);
// We've ensured values is an array, so we can always iterate over it
each(checkboxValues, function (_, value) {
if (value === option.value) {
checkbox.find('input').prop('checked', true);
}
});
input.append(checkbox);
});
break;
case 'radio':
// Make sure that value is not an array (only a single radio can ever be checked)
if (options.value !== undefined && Array.isArray(options.value)) {
throw new Error('prompt with "inputType" set to "radio" requires a single, non-array value for "value"');
}
inputOptions = options.inputOptions || [];
if (!inputOptions.length) {
throw new Error('prompt with "inputType" set to "radio" requires at least one option');
}
// Radiobuttons have to nest within a containing element, so they break the rules a bit and we end up re-assigning our 'input' element to this container instead
input = $('<div class="bootbox-radiobutton-list"></div>');
// Radiobuttons should always have an initial checked input checked in a "group".
// If value is undefined or doesn't match an input option, select the first radiobutton
let checkFirstRadio = true;
each(inputOptions, function (_, option) {
if (option.value === undefined || option.text === undefined) {
throw new Error('each option needs a "value" property and a "text" property');
}
let radio = $(templates.inputs[options.inputType]);
radio.find('input').attr('value', option.value);
radio.find('label').append('\n' + option.text);
if (options.value !== undefined) {
if (option.value === options.value) {
radio.find('input').prop('checked', true);
checkFirstRadio = false;
}
}
input.append(radio);
});
if (checkFirstRadio) {
input.find('input[type="radio"]').first().prop('checked', true);
}
break;
}
// Now place it in our form
form.append(input);
form.on('submit', function (e) {
e.preventDefault();
// Fix for SammyJS (or similar JS routing library) hijacking the form post.
e.stopPropagation();
// @TODO can we actually click *the* button object instead?
// e.g. buttons.confirm.click() or similar
promptDialog.find('.bootbox-accept').trigger('click');
});
if (options.message && options.message.trim() !== '') {
// Add the form to whatever content the user may have added.
let message = $(templates.promptMessage).html(options.message);
form.prepend(message);
options.message = form;
}
else {
options.message = form;
}
// Generate the dialog
promptDialog = exports.dialog(options);
// Clear the existing handler focusing the submit button...
promptDialog.off('shown.bs.modal', focusPrimaryButton);
// ...and replace it with one focusing our input, if possible
promptDialog.on('shown.bs.modal', function () {
// Need the closure here since input isn'tcan object otherwise
input.focus();
});
if (shouldShow === true) {
promptDialog.modal('show');
}
return promptDialog;
};
// INTERNAL FUNCTIONS
// *************************************************************************************************************
// Map a flexible set of arguments into a single returned object.
// If args.length is already one just return it, otherwise use the properties argument to map the unnamed args to object properties.
// So in the latter case:
//
// mapArguments(["foo", $.noop], ["message", "callback"])
//
// results in
//
// { message: "foo", callback: $.noop }
//
function mapArguments(args, properties) {
let argsLength = args.length;
let options = {};
if (argsLength < 1 || argsLength > 2) {
throw new Error('Invalid argument length');
}
if (argsLength === 2 || typeof args[0] === 'string') {
options[properties[0]] = args[0];
options[properties[1]] = args[1];
} else {
options = args[0];
}
return options;
}
// Merge a set of default dialog options with user supplied arguments
function mergeArguments(defaults, args, properties) {
return $.extend(
// Deep merge
true,
// Ensure the target is an empty, unreferenced object
{},
// The base options object for this type of dialog (often just buttons)
defaults,
// 'args' could be an object or array; if it's an array properties will map it to a proper options object
mapArguments(args, properties)
);
}
// This entry-level method makes heavy use of composition to take a simple range of inputs and return valid options suitable for passing to bootbox.dialog
function mergeDialogOptions(className, labels, properties, args) {
let locale;
if (args && args[0]) {
locale = args[0].locale || defaults.locale;
let swapButtons = args[0].swapButtonOrder || defaults.swapButtonOrder;
if (swapButtons) {
labels = labels.reverse();
}
}
// Build up a base set of dialog properties
let baseOptions = {
className: 'bootbox-' + className,
buttons: createLabels(labels, locale)
};
// Ensure the buttons properties generated, *after* merging with user args are still valid against the supplied labels
return validateButtons(
// Merge the generated base properties with user supplied arguments
mergeArguments(
baseOptions,
args,
// If args.length > 1, properties specify how each arg maps to an object key
properties
),
labels
);
}
// Checks each button object to see if key is valid.
// This function will only be called by the alert, confirm, and prompt helpers.
function validateButtons(options, buttons) {
let allowedButtons = {};
each(buttons, function (key, value) {
allowedButtons[value] = true;
});
each(options.buttons, function (key) {
if (allowedButtons[key] === undefined) {
throw new Error('button key "' + key + '" is not allowed (options are ' + buttons.join(' ') + ')');
}
});
return options;
}
// From a given list of arguments, return a suitable object of button labels.
// All this does is normalise the given labels and translate them where possible.
// e.g. "ok", "confirm" -> { ok: "OK", cancel: "Annuleren" }
function createLabels(labels, locale) {
let buttons = {};