/
dialogs.ts
1153 lines (1003 loc) · 37.3 KB
/
dialogs.ts
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
import { getjQuery, isBS3, isBS5Plus } from "./environment";
import { Fluent } from "./fluent";
import { htmlEncode } from "./html";
import { iconClassName, type IconClassName } from "./icons";
import { localText } from "./localtext";
import { isArrayLike, isPromiseLike, omitUndefined } from "./system";
/**
* Options for a message dialog button
*/
export interface DialogButton {
/** Button text */
text?: string;
/** Button hint */
hint?: string;
/** Button icon */
icon?: IconClassName;
/** Click handler */
click?: (e: MouseEvent) => void | false | Promise<void | false>;
/** CSS class for button */
cssClass?: string;
/** The code that is returned from message dialog function when this button is clicked.
* If this is set, and click event will not be defaultPrevented dialog will close.
*/
result?: string;
}
export type DialogType = "bsmodal" | "uidialog" | "panel";
/**
* Options that apply to all dialog types
*/
export interface DialogOptions {
/** Auto dispose dialog on close, default is true */
autoDispose?: boolean;
/** True to auto open dialog */
autoOpen?: boolean;
/** Backdrop type, static to make it modal, e.g. can't be closed by clicking outside */
backdrop?: boolean | "static"
/** List of buttons to show on the dialog */
buttons?: DialogButton[];
/** Vertically center modal */
centered?: boolean;
/** Show close button, default is true */
closeButton?: boolean;
/** Close dialog on escape key. Default is true for message dialogs. */
closeOnEscape?: boolean;
/** CSS class to use for all dialog types. Is added to the top ui-dialog, panel or modal element */
dialogClass?: string;
/** Dialog content/body element, or callback that will populate the content element */
element?: HTMLElement | ArrayLike<HTMLElement> | ((element: HTMLElement) => void);
/** Enable / disable animation. Default is false for message dialogs, true for other dialogs */
fade?: boolean;
/** Sets one of modal-fullscreen{-...-down} classes. Only used for bootstrap modals */
fullScreen?: boolean | "sm-down" | "md-down" | "lg-down" | "xl-down" | "xxl-down",
/** Modal option for jQuery UI dialog compatibility only. Not to be confused with Bootstrap modal. */
modal?: boolean;
/** Event handler that is called when dialog is opened */
onOpen?: (e?: Event) => void;
/** Event handler that is called when dialog is closed */
onClose?: (result: string, e?: Event) => void;
/** Prefer Bootstrap modals to jQuery UI dialogs when both are available */
preferBSModal?: boolean;
/** Prefer Panel even when Modal / jQuery UI is available */
preferPanel?: boolean;
/** Callback to get options specific to the dialog provider type */
providerOptions?: (type: DialogType, opt: DialogOptions) => any;
/** Scrollable, sets content of the modal to scrollable, only for Bootstrap */
scrollable?: boolean;
/** Size. Default is null for (500px) message dialogs, lg for normal dialogs */
size?: "sm" | "md" | "lg" | "xl";
/** Dialog title */
title?: string;
/** Only used for jQuery UI dialogs for backwards compatibility */
width?: number;
}
/**
* Wrapper for different types of dialogs, including jQuery UI, Bootstrap modals, and Panels.
*/
export class Dialog {
private el: HTMLElement;
private dialogResult: string;
/**
* Creates a new dialog. The type of the dialog will be determined based on
* the availability of jQuery UI, Bootstrap, and the options provided.
* @param opt Optional configuration for the dialog
*/
constructor(opt?: DialogOptions);
constructor(opt?: DialogOptions, create = true) {
if (isArrayLike(opt?.element))
this.el = opt.element[0];
else if (typeof opt?.element !== "function")
this.el = opt?.element;
this.dialogResult = this.el?.dataset.dialogResult;
if (!create) {
return;
}
this.el ??= document.createElement("div");
opt = Object.assign({}, Dialog.defaults, omitUndefined(opt));
if (opt.closeOnEscape === void 0 && opt.closeButton)
opt.closeOnEscape = true;
if (typeof opt.element === "function")
opt.element(this.el);
if (this.el.classList.contains("hidden") &&
typeof opt.element !== "function")
this.el.classList.remove("hidden");
if (opt.preferPanel || (!hasBSModal() && !hasUIDialog))
this.createPanel(opt);
else if (hasUIDialog() && (!hasBSModal() || !opt.preferBSModal))
this.createUIDialog(opt);
else {
this.createBSModal(opt);
}
if (opt.onOpen)
this.onOpen(opt.onOpen);
if (opt.onClose)
this.onClose(opt.onClose);
if (opt.autoDispose)
this.onClose(() => setTimeout(this.dispose.bind(this), 0));
if (opt.title !== void 0) {
this.title(opt.title);
}
if (opt.autoOpen)
this.open();
}
/** Default set of dialog options */
static defaults: DialogOptions = {
autoDispose: true,
autoOpen: true,
backdrop: false,
centered: true,
closeButton: true,
fade: false,
fullScreen: "md-down",
modal: true,
preferBSModal: true,
size: "lg"
}
/** Default set of message dialog options */
static messageDefaults: MessageDialogOptions = {
autoDispose: true,
autoOpen: true,
backdrop: false,
centered: true,
closeButton: true,
closeOnEscape: true,
fade: true,
fullScreen: null,
htmlEncode: true,
modal: true,
preferBSModal: true,
preWrap: true,
size: "md"
}
/**
* Gets the dialog instance for the specified element.
* @param el The dialog body element (.s-Panel, .ui-dialog-content, or .modal-body) or the root element (.modal, .ui-dialog, .s-Panel)
* @returns The dialog instance, or null if the element is not a dialog.
*/
static getInstance(el: HTMLElement | ArrayLike<HTMLElement>): Dialog {
el = getDialogContentNode(el);
if (!el)
return null;
return new (Dialog as any)({ element: el }, false);
}
/** The result code of the button that is clicked. Also attached to the dialog element as data-dialog-result */
get result(): string {
return this.el ? this.el.dataset.dialogResult : this.dialogResult;
}
/** Closes dialog setting the result to null */
close(): this;
/** Closes dialog with the result set to value */
close(result: string): this;
close(result?: string): this {
this.el && (this.el.dataset.dialogResult = result ?? null);
this.dialogResult = result ?? null;
var target = getDialogEventsNode(this.el);
if (!target)
return;
if (target.classList.contains("panel-body"))
closePanel(this.el);
else if (target.classList.contains("ui-dialog-content"))
getjQuery()?.(this.el).dialog?.("close");
else if (target.classList.contains("modal")) {
if (isBS5Plus()) {
bootstrap?.Modal?.getInstance?.(target)?.hide?.();
} else {
let $ = getjQuery();
if ($?.fn?.modal)
$(target).modal?.("close");
}
}
return this;
}
/**
* Adds an event handler that is called when the dialog is closed. If the second parameter is true, the handler is called before the dialog is closed and
* the closing can be cancelled by calling preventDefault on the event object.
* @param handler The event handler function
* @param before Indicates whether the handler should be called before the dialog is closed
* @returns The dialog instance
*/
onClose(handler: (result?: string, e?: Event) => void, before = false) {
var target = getDialogEventsNode(this.el);
if (!target)
return;
if (target.classList.contains("panel-body"))
Fluent.on(target, before ? "panelbeforeclose" : "panelclose", e => handler(this.result, e));
else if (target.classList.contains("ui-dialog-content"))
Fluent.on(target, before ? "dialogbeforeclose" : "dialogclose", e => handler(this.result, e));
else if (target.classList.contains("modal"))
Fluent.on(target, before ? "hide.bs.modal" : "hidden.bs.modal", e => handler(this.result, e));
}
/**
* Adds an event handler that is called when the dialog is opened. If the second parameter is true, the handler is called before the dialog is opened and
* the opening can be cancelled by calling preventDefault on the event object.
* @param handler The event handler function
* @param before Indicates whether the handler should be called before the dialog is opened
* @returns The dialog instance
*/
onOpen(handler: (e?: Event) => void, before = false): this {
var target = getDialogEventsNode(this.el);
if (!target)
return;
if (target.classList.contains("panel-body"))
Fluent.on(target, before ? "panelbeforeopen" : "panelopen", handler);
else if (target.classList.contains("ui-dialog-content"))
Fluent.on(target, before ? "dialogbeforeopen" : "dialogopen", handler);
else if (target.classList.contains("modal"))
Fluent.on(target, before ? "show.bs.modal" : "shown.bs.modal", handler);
return this;
}
/** Opens the dialog */
open() {
var target = getDialogEventsNode(this.el);
if (!target)
return;
if (target.classList.contains("panel-body"))
openPanel(this.el);
else if (target.classList.contains("ui-dialog-content"))
getjQuery()?.(target).dialog("open");
else if (target.classList.contains("modal")) {
if (isBS5Plus()) {
bootstrap?.Modal?.getInstance?.(target)?.show?.();
} else {
let $ = getjQuery();
if ($?.fn?.modal)
$(target).modal?.("show");
}
}
return this;
}
/** Gets the title text of the dialog */
title(): string;
/** Sets the title text of the dialog. */
title(value: string): this;
title(value?: string): string | this {
let title = this.getHeaderNode()?.querySelector(".modal-title, .panel-titlebar-text, .ui-dialog-title");
if (value === void 0 && !arguments.length)
return title?.textContent;
title && (title.textContent = value);
return this;
}
/** Returns the type of the dialog, or null if no dialog on the current element or if the element is null, e.g. dialog was disposed */
get type(): DialogType {
var root = getDialogNode(this.el);
if (!root)
return null;
if (root.classList.contains("modal"))
return "bsmodal";
if (root.classList.contains("ui-dialog"))
return "uidialog";
if (root.classList.contains("s-Panel"))
return "panel";
return null;
}
/** Gets the body/content element of the dialog */
getContentNode(): HTMLElement {
return this.el;
}
/** Gets the dialog element of the dialog */
getDialogNode(): HTMLElement {
return getDialogNode(this.el);
}
/** Gets the node that receives events for the dialog. It's .ui-dialog-content, .modal, or .panel-body */
getEventsNode(): HTMLElement {
return getDialogEventsNode(this.el);
}
/** Gets the footer element of the dialog */
getFooterNode(): HTMLElement {
return this.getDialogNode()?.querySelector(".modal-footer, .panel-footer, .ui-dialog-footer");
}
/** Gets the header element of the dialog */
getHeaderNode(): HTMLElement {
return this.getDialogNode()?.querySelector(".modal-header, .panel-titlebar, .ui-dialog-titlebar");
}
private onButtonClick(e: MouseEvent, btn: DialogButton) {
e ??= new Event("click") as MouseEvent;
if (!btn.click) {
if (btn.result)
this.close(btn.result);
return;
}
var value = btn.click(e);
if (!btn.result)
return;
if (isPromiseLike(value))
value.then(value => value !== false && !Fluent.isDefaultPrevented(e) && this.close(btn.result));
else if (value !== false && !Fluent.isDefaultPrevented(e))
this.close(btn.result);
}
private createBSButtons(footer: Fluent, buttons: DialogButton[]) {
for (let btn of buttons) {
Fluent(dialogButtonToBS(btn))
.appendTo(footer)
.on("click", e => this.onButtonClick(e, btn));
}
}
private createBSModal(opt: DialogOptions): void {
var modal = Fluent("div")
.class(["modal", opt.dialogClass, opt.fade && "fade"])
.attr("tabindex", "-1")
.appendTo(document.body);
let header = Fluent("div")
.class("modal-header")
.append(Fluent("h5").class("modal-title"));
let bs5 = isBS5Plus();
if (opt.closeButton) {
let closeButton = Fluent("button")
.class(bs5 ? "btn-close" : "close")
.attr("type", "button")
.data(`${bs5 ? "bs-" : ""}dismiss`, "modal")
.attr("aria-label", DialogTexts.CloseButton);
if (!bs5) {
closeButton.append(Fluent("span").attr("aria-hidden", "true").text("\u2715"));
}
if (isBS3()) {
closeButton.prependTo(header);
} else {
closeButton.appendTo(header);
}
}
this.el.classList.add("modal-body");
let footer = Fluent("div")
.class("modal-footer");
Fluent("div")
.class([
"modal-dialog",
opt.size && "modal-" + opt.size,
opt.fullScreen && "modal-fullscreen" + (typeof opt.fullScreen === "string" ? `-${opt.fullScreen}` : ""),
opt.centered && "modal-dialog-centered",
opt.scrollable && "modal-scrollable"])
.append(Fluent("div")
.class("modal-content")
.append(header)
.append(this.el)
.append(footer))
.appendTo(modal);
if (opt.buttons) {
this.createBSButtons(footer, opt.buttons);
}
let modalOpt = {
backdrop: opt.backdrop,
keyboard: opt.closeOnEscape
};
if (opt.providerOptions)
Object.assign(modalOpt, opt.providerOptions("bsmodal", opt));
if (bs5 && bootstrap.Modal) {
var modalObj = new bootstrap.Modal(modal.getNode(), modalOpt);
if (modalObj && modalObj._focustrap && modalObj._focustrap._handleFocusin) {
var org: Function = modalObj._focustrap._handleFocusin;
modalObj._focustrap._handleFocusin = function (event: Event) {
if (event.target &&
(event.target as any).closest('.ui-datepicker, .select2-drop, .cke, .cke_dialog, .flatpickr-calendar'))
return;
org.apply(this, arguments);
}
}
}
else {
getjQuery()?.(modal.getNode())?.modal?.(modalOpt);
};
}
private createPanel(opt: DialogOptions) {
let titlebar = Fluent("div")
.class("panel-titlebar")
.append(Fluent("div")
.class("panel-titlebar-text"));
let panel = Fluent("div")
.class(["s-Panel", "hidden", opt.dialogClass])
.append(titlebar)
this.el.classList.add("panel-body");
if (this.el.parentElement &&
this.el.parentElement !== document.body) {
this.el.parentElement.insertBefore(panel.getNode(), this.el);
}
panel.append(this.el);
if (opt.closeButton) {
Fluent("button")
.class("panel-titlebar-close")
.attr("type", "button")
.on("click", this.close.bind(this, null))
.appendTo(titlebar);
}
opt.buttons && this.createBSButtons(Fluent("div")
.class("panel-footer")
.appendTo(panel), opt.buttons);
}
private createUIDialog(opt: DialogOptions): void {
let uiOpt: any = {
autoOpen: opt.autoOpen,
dialogClass: opt.dialogClass,
title: opt.title,
modal: opt.modal,
width: opt.width,
resizable: false
} as any;
if (opt.centered)
uiOpt.position = { my: 'center', at: 'center', of: window };
if (opt.buttons) {
uiOpt.buttons = opt.buttons.map(btn => {
let uiButton = dialogButtonToUI(btn);
uiButton.click = (e: MouseEvent) => this.onButtonClick(e, btn);
return uiButton;
});
}
if (opt.providerOptions)
uiOpt = Object.assign(uiOpt, opt.providerOptions("uidialog", omitUndefined(opt)));
getjQuery()?.(this.el).dialog(uiOpt);
}
/**
* Disposes the dialog, removing it from the DOM and unbinding all event handlers.
*/
dispose(): void {
try {
let target = getDialogEventsNode(this.el) ?? this.el;
if (!target)
return;
try {
if (target.classList.contains("ui-dialog-content")) {
getjQuery?.()(target)?.dialog?.('destroy');
target.classList.remove("ui-dialog-content");
target = target.closest(".ui-dialog") ?? target;
}
else if (target.classList.contains("modal")) {
if (!getjQuery() && isBS5Plus()) {
if (typeof bootstrap !== "undefined")
bootstrap.Modal?.getInstance(target)?.dispose?.();
}
else {
getjQuery()?.(target)?.modal?.(isBS3() ? "destroy" : "dispose");
}
this.el?.classList.remove("modal-body");
}
else {
this.el?.classList.remove("panel-body");
target = target.closest(".s-Panel") ?? target;
}
}
finally {
Fluent.remove(target);
}
}
finally {
this.el = null;
}
}
}
/** Returns true if Bootstrap modal is available */
export function hasBSModal() {
return isBS5Plus() || !!(getjQuery()?.fn?.modal);
}
/** Returns true if jQuery UI dialog is available */
export function hasUIDialog() {
return !!(getjQuery()?.ui?.dialog);
}
/** Calls Bootstrap button.noConflict method if both jQuery UI and Bootstrap buttons are available in the page */
export function uiAndBSButtonNoConflict() {
const $ = getjQuery();
// if both jQuery UI and bootstrap button exists, prefer jQuery UI button as UI dialog needs them
if ($ && $.fn?.button?.noConflict && $.ui?.button) {
$.fn.btn = $.fn.button.noConflict();
}
}
uiAndBSButtonNoConflict();
function dialogButtonToBS(x: DialogButton): HTMLButtonElement {
let html = htmlEncode(x.text);
let iconClass = iconClassName(x.icon);
if (iconClass)
html = '<i class="' + htmlEncode(iconClass) + '"><i>' + (html ? (" " + html) : "");
let button = document.createElement("button");
button.type = "button";
button.classList.add("btn");
Fluent.addClass(button, x.cssClass ?? "btn-secondary");
if (x.hint)
button.setAttribute("title", x.hint);
button.innerHTML = html;
return button;
}
function dialogButtonToUI(x: DialogButton): any {
let html = htmlEncode(x.text);
let iconClass = iconClassName(x.icon);
if (iconClass)
html = '<i class="' + htmlEncode(iconClass) + '"></i>' + (html ? (" " + html) : "");
let button = {
text: html,
click: x.click
} as any;
if (x.cssClass)
button.cssClass = x.cssClass;
return button;
}
/**
* Creates a dialog button which, by default, has "Yes" as caption (localized) and "ok" as the result.
* @param opt - Optional configuration for the dialog button.
* @returns The dialog button with the specified configuration.
*/
export function okDialogButton(opt?: DialogButton): DialogButton {
return {
text: opt?.text != void 0 ? opt.text : DialogTexts.OkButton,
cssClass: opt?.cssClass != void 0 ? opt.cssClass : 'btn-info',
result: opt?.result != void 0 ? opt.result : 'ok',
click: opt?.click
}
}
/**
* Creates a dialog button which, by default, has "Yes" as the caption (localized) and "yes" as the result.
* @param opt - Optional configuration for the dialog button.
* @returns The dialog button with the specified configuration.
*/
export function yesDialogButton(opt?: DialogButton): DialogButton {
return {
text: opt?.text != void 0 ? opt.text : DialogTexts.YesButton,
cssClass: opt?.cssClass != void 0 ? opt.cssClass : 'btn-primary',
result: opt?.result != void 0 ? opt.result : 'yes',
click: opt?.click
}
}
/**
* Creates a dialog button which, by default, has "No" as the caption (localized) and "no" as the result.
* @param opt - Optional configuration for the dialog button.
* @returns The dialog button with the specified configuration.
*/
export function noDialogButton(opt?: DialogButton): DialogButton {
return {
text: opt?.text != void 0 ? opt.text : DialogTexts.NoButton,
cssClass: opt?.cssClass != void 0 ? opt.cssClass : isBS5Plus() ? 'btn-danger' : 'btn-default',
result: opt?.result != void 0 ? opt.result : 'no',
click: opt?.click
}
}
/**
* Creates a dialog button which, by default, has "Cancel" as the caption (localized) and "cancel" as the result.
* @param opt - Optional configuration for the dialog button.
* @returns The dialog button with the specified configuration.
*/
export function cancelDialogButton(opt?: DialogButton): DialogButton {
return {
text: opt?.text != void 0 ? opt.text : DialogTexts.CancelButton,
cssClass: opt?.cssClass != void 0 ? opt.cssClass : isBS5Plus() ? 'btn-secondary' : 'btn-default',
result: opt?.result != void 0 ? opt.result : 'cancel',
click: opt?.click
}
}
/**
* Namespace containing localizable text constants for dialogs.
*/
export namespace DialogTexts {
/**
* Title for alert dialogs.
*/
export declare const AlertTitle: string;
/**
* Text for the cancel button in dialogs.
*/
export declare const CancelButton: string;
/**
* Text for the close button in dialogs.
*/
export declare const CloseButton: string;
/**
* Title for confirmation dialogs.
*/
export declare const ConfirmationTitle: string;
/**
* Title for information dialogs.
*/
export declare const InformationTitle: string;
/**
* Hint for maximizing dialogs.
*/
export declare const MaximizeHint: string;
/**
* Text for the "No" button in dialogs.
*/
export declare const NoButton: string;
/**
* Text for the "OK" button in dialogs.
*/
export declare const OkButton: string;
/**
* Hint for restoring dialogs.
*/
export declare const RestoreHint: string;
/**
* Title for success dialogs.
*/
export declare const SuccessTitle: string;
/**
* Title for warning dialogs.
*/
export declare const WarningTitle: string;
/**
* Text for the "Yes" button in dialogs.
*/
export declare const YesButton: string;
const defaultTxt: Record<string, string> = {
AlertTitle: 'Alert',
CancelButton: 'Cancel',
CloseButton: 'Close',
ConfirmationTitle: 'Confirm',
InformationTitle: 'Information',
MaximizeHint: 'Maximize',
NoButton: 'No',
OkButton: 'OK',
RestoreHint: 'Restore',
SuccessTitle: 'Success',
WarningTitle: 'Warning',
YesButton: 'Yes'
};
function get(this: string) {
return htmlEncode(localText("Dialogs." + this, defaultTxt[this]));
}
for (let k of Object.keys(defaultTxt)) {
Object.defineProperty(DialogTexts, k, {
get: get.bind(k)
});
}
}
function closePanel(el: (HTMLElement | ArrayLike<HTMLElement>)) {
let panel = getDialogNode(el);
if (!panel || panel.classList.contains("hidden"))
return;
var eventsNode = getDialogEventsNode(el) ?? panel;
let event = Fluent.trigger(eventsNode, "panelbeforeclose");
if (Fluent.isDefaultPrevented(event))
return;
panel.classList.add("hidden");
let uniqueName = panel.dataset.paneluniquename;
if (uniqueName) {
document.querySelectorAll(`[data-hiddenby="${uniqueName}"]`).forEach(hiddenBy => {
hiddenBy.removeAttribute("data-hiddenby");
});
}
Fluent.trigger(window, "resize");
document.querySelectorAll(".require-layout").forEach((rl: HTMLElement) => Fluent.isVisibleLike(rl) && Fluent.trigger(rl, "layout"));
Fluent.trigger(eventsNode, "panelclose");
}
function openPanel(element: HTMLElement | ArrayLike<HTMLElement>, uniqueName?: string) {
let panel = getDialogNode(element);
if (!panel)
return;
let container = panel.parentElement && panel.parentElement !== document.body ? panel.parentElement :
(document.querySelector('.panels-container') ?? document.querySelector('section.content') as HTMLElement ?? panel.parentElement ?? document.body);
if (panel.parentElement !== container) {
container.appendChild(panel);
}
let eventNode = getDialogEventsNode(element) ?? panel;
let event = Fluent.trigger(eventNode, "panelbeforeopen");
if (Fluent.isDefaultPrevented(event))
return;
panel.dataset.paneluniquename = uniqueName || panel.id || new Date().getTime().toString();
function setHideBy(e: HTMLElement) {
if (e === panel ||
e.tagName === "LINK" ||
e.tagName === "SCRIPT" ||
e.classList.contains("hidden") ||
e.dataset.hiddenby ||
(container && e.parentElement !== container) && !Fluent.isVisibleLike(e))
return;
e.dataset.hiddenby = panel.dataset.paneluniquename;
}
if (container) {
let c = container.children;
const cl = c.length;
for (let i = 0; i < cl; i++) {
setHideBy(c[i] as HTMLElement);
}
}
document.querySelectorAll('.ui-dialog, .ui-widget-overlay, .modal.show, .modal.in').forEach(setHideBy);
panel.classList.remove("hidden");
delete panel.dataset.hiddenby;
Fluent.trigger(eventNode, "panelopen");
}
/** Returns .s-Panel, .modal, .ui-dialog */
function getDialogNode(element: HTMLElement | ArrayLike<HTMLElement>): HTMLElement {
if (isArrayLike(element))
element = element[0];
if (!element)
return null;
return element.closest(".modal, .s-Panel, .ui-dialog");
}
/** Returns .panel-body, .modal, .ui-dialog-content */
function getDialogEventsNode(element: HTMLElement | ArrayLike<HTMLElement>): HTMLElement {
if (isArrayLike(element))
element = element[0];
if (!element)
return null;
return element.closest(".modal, .panel-body, .ui-dialog-content") as HTMLElement ??
getDialogNode(element)?.querySelector(".panel-body, .ui-dialog-content");
}
/** Returns .panel-body, .modal, .ui-dialog-content */
function getDialogContentNode(element: HTMLElement | ArrayLike<HTMLElement>): HTMLElement {
if (isArrayLike(element))
element = element[0];
if (!element)
return null;
return element.closest<HTMLElement>(".modal-body, .panel-body, .ui-dialog-content") ??
getDialogNode(element)?.querySelector(".modal-body, .panel-body, .ui-dialog-content");
}
/**
* Options that apply to all message dialog types
*/
export interface MessageDialogOptions extends DialogOptions {
/** HTML encode the message, default is true */
htmlEncode?: boolean;
/** Wrap the message in a `<pre>` element, so that line endings are preserved, default is true */
preWrap?: boolean;
}
function getMessageBodyHtml(message: string, options?: MessageDialogOptions): string {
let encode = options == null || options.htmlEncode == null || options.htmlEncode;
if (encode)
message = htmlEncode(message);
let preWrap = options == null || (options.preWrap == null && encode) || options.preWrap;
return '<div class="message"' + (preWrap ? ' style="white-space: pre-wrap">' : '>') + message + '</div>';
}
function createMessageDialog(opt: {
cssClass: string,
title: string,
getButtons: () => DialogButton[],
native: (msg: string) => string,
message: string,
options: MessageDialogOptions
}): Partial<Dialog> {
if (!hasBSModal() && !hasUIDialog()) {
var result = opt.native(opt.message);
opt.options?.onClose(result);
return {
result
}
}
let options: MessageDialogOptions = Object.assign({}, Dialog.messageDefaults, {
dialogClass: "s-MessageDialog" + (opt.cssClass ? " " + opt.cssClass : ""),
title: opt.title
} satisfies MessageDialogOptions, opt.options);
if (options.buttons == void 0) {
options.buttons = opt.getButtons();
}
if (options.providerOptions === void 0) {
options.providerOptions = (type) => {
if (type === "uidialog") {
return {
width: '40%',
maxWidth: 450,
minWidth: 180,
resizable: false
}
}
}
}
if (options.element === void 0) {
options.element = el => el.innerHTML = getMessageBodyHtml(opt.message, options);
}
return new Dialog(options);
}
/**
* Displays an alert dialog
* @param message The message to display
* @param options Additional options.
* @see AlertOptions
* @example
* alertDialog("An error occured!"); }
*/
export function alertDialog(message: string, options?: MessageDialogOptions): Partial<Dialog> {
return createMessageDialog({
message,
options,
cssClass: "s-AlertDialog",
title: DialogTexts.AlertTitle,
getButtons: () => [okDialogButton({ cssClass: 'btn-danger' })],
native: (msg) => {
alert(msg);
return "ok";
}
});
}
/** Additional options for confirm dialog */
export interface ConfirmDialogOptions extends MessageDialogOptions {
/** True to also add a cancel button */
cancelButton?: boolean;
/** Event handler for cancel button click */
onCancel?: () => void;
/** Event handler for no button click */
onNo?: () => void;
}
/**
* Display a confirmation dialog
* @param message The message to display
* @param onYes Callback for Yes button click
* @param options Additional options.
* @see ConfirmOptions
* @example
* confirmDialog("Are you sure you want to delete?", () => {
* // do something when yes is clicked
* }
*/
export function confirmDialog(message: string, onYes: () => void, options?: ConfirmDialogOptions): Partial<Dialog> {
return createMessageDialog({
message,
options,
cssClass: "s-ConfirmDialog",
title: DialogTexts.ConfirmationTitle,
getButtons: () => {
let buttons = [yesDialogButton({ click: onYes }), noDialogButton({ click: options?.onNo })];
if (options?.cancelButton)
buttons.push(cancelDialogButton({ click: options?.onCancel }));
return buttons;
},
native: (msg) => {
var result = window.confirm(msg);
if (result) {
onYes?.();
return 'yes';
}
else {
options?.onNo();
return result === false ? "no" : "";
}
}
});
}
/**
* Display an information dialog
* @param message The message to display
* @param onOk Callback for OK button click
* @param options Additional options.
* @see ConfirmOptions
* @example
* informationDialog("Operation complete", () => {
* // do something when OK is clicked
* }
*/
export function informationDialog(message: string, onOk?: () => void, options?: MessageDialogOptions): Partial<Dialog> {
return createMessageDialog({
message,
options,
cssClass: "s-InformationDialog",
title: DialogTexts.InformationTitle,
getButtons: () => [okDialogButton({ click: onOk, cssClass: 'btn-info' })],
native: (msg) => {
alert(msg);
onOk?.();
return "ok";
}
});
}
/**