| 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 |
3x
3x
21x
21x
21x
21x
17x
17x
15x
15x
17x
21x
20x
1x
20x
2x
1x
1x
1x
18x
18x
4x
3x
1x
21x
19x
15x
15x
4x
2x
21x
15x
15x
15x
15x
21x
17x
17x
17x
17x
17x
17x
17x
1x
17x
2x
17x
17x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
1x
10x
10x
10x
17x
14x
17x
2x
2x
2x
2x
2x
1x
1x
1x
1x
1x
1x
1x
14x
14x
14x
14x
17x
17x
17x
14x
14x
17x
21x
14x
14x
14x
1x
14x
13x
14x
21x
1x
1x
1x
1x
21x
13x
13x
13x
13x
22x
22x
22x
7x
7x
22x
19x
3x
22x
3x
22x
13x
3x
3x
3x
3x
3x
5x
5x
5x
5x
5x
5x
2x
5x
1x
1x
2x
1x
1x
1x
1x
2x
2x
2x
2x
2x
5x
5x
5x
9x
2x
2x
2x
1x
1x
1x
1x
1x
1x
7x
7x
7x
7x
7x
1x
7x
4x
4x
2x
2x
4x
3x
3x
3x
2x
2x
3x
7x
7x
7x
2x
2x
2x
2x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
| import { defaultOptions } from './DialogOptions';
/**
* The Modal component for building alert and confirm dialog.
*/
export class Modal {
private static containerRef: HTMLElement | null = null;
public finalOptions: ModalOptions;
private ref: HTMLElement | undefined;
private closeListeners: Array<(sender: Modal) => void> = [];
constructor(options?: ModalOptions) {
this.finalOptions = {
labelOK: defaultOptions.labelOK,
labelCancel: defaultOptions.labelCancel,
animate: defaultOptions.animate,
...options
};
}
/**
* Renders a [[Modal]] instance with options.
* @param options The modal options, see the [[ModalOptions]] interface
*/
public render = (options?: ModalOptions) => {
this.finalOptions = {...this.finalOptions, ...options};
if (!Modal.containerRef) {
Modal.containerRef = this.renderOverlay();
document.body.appendChild(Modal.containerRef);
}
this.renderElement();
};
/**
* Closes the [[Modal]] instance.
*
* @param event The close event
*/
public close = (event?: any) => {
this.closeListeners.forEach((listener: (sender: Modal) => void) => {
listener(this);
});
if (typeof this.finalOptions.onClosing === "function") {
if (this.finalOptions.onClosing(event)) {
this.removeElement();
Eif (typeof this.finalOptions.onClosed === "function") {
this.finalOptions.onClosed(event);
}
}
} else {
this.removeElement();
if (typeof this.finalOptions.onClosed === "function") {
this.finalOptions.onClosed(event);
}
}
};
public addCloseListener(fn: (sender: Modal) => void) {
this.closeListeners.push(fn);
}
private removeElement = () => {
if (Modal.containerRef && Modal.containerRef.querySelectorAll(".bn-modal").length <= 1) {
Modal.containerRef.remove();
Modal.containerRef = null;
} else {
if (this.ref) {
this.ref.remove();
}
}
}
private renderOverlay = (): HTMLElement => {
const ele = document.createElement("div");
ele.setAttribute("class", "bn-modal-overlay");
document.body.appendChild(ele);
return ele;
};
private renderElement = (): HTMLElement => {
const options = this.finalOptions;
const eleRoot = document.createElement("div");
eleRoot.setAttribute("class", `bn-modal ${options.animate ? "animated" : ""} ${options.css || ""} ${options.theme || ""}`.trim());
this.ref = eleRoot;
Eif (Modal.containerRef) {
Modal.containerRef.appendChild(this.ref);
}
if (options.width) {
eleRoot.style.width = options.width.toString();
}
if (options.height) {
eleRoot.style.height = options.height.toString();
}
let headerHeight = 0;
if (options.title) {
let eleHeader: HTMLElement | null = null;
eleHeader = document.createElement("div");
eleHeader.setAttribute("class", "bn-modal-header");
eleRoot.appendChild(eleHeader);
const eleHeaderTitle = document.createElement("h1");
eleHeaderTitle.innerHTML = options.title;
eleHeader.appendChild(eleHeaderTitle);
const eleHeaderToolbox = document.createElement("div");
eleHeaderToolbox.setAttribute("class", "toolbox");
const eleHeaderToolboxClose = document.createElement("a");
eleHeaderToolboxClose.setAttribute("aria-label", "Close");
eleHeaderToolboxClose.innerHTML = "×";
eleHeaderToolboxClose.addEventListener("click", event => {
this.close();
});
eleHeaderToolbox.appendChild(eleHeaderToolboxClose);
eleHeader.appendChild(eleHeaderToolbox);
headerHeight = eleHeader.clientHeight;
}
// check whether the content is URI
// maybe browsers disable access local resoruces, like Chrome, Firefox
let eleBody;
if (!options.contentType) {
options.contentType = ContentType.TEXT;
}
switch (options.contentType) {
case ContentType.URL:
options.content = options.content;
eleBody = document.createElement("iframe");
eleBody.setAttribute("src", options.content || "");
eleBody.setAttribute("class", "bn-modal-body");
break;
case ContentType.ELEMENT:
eleBody = document.createElement("div");
const elem = document.getElementById(options.content || "");
Eif (elem) {
elem.style.display = "block";
eleBody.appendChild(elem);
}
eleBody.setAttribute("class", "bn-modal-body");
break;
case ContentType.TEXT:
default:
eleBody = document.createElement("div");
eleBody.setAttribute("class", "bn-modal-body");
eleBody.innerHTML = options.content || "";
break;
}
Eif (eleBody) {
eleRoot.appendChild(eleBody);
}
if ((options.buttons && options.buttons.length > 0) || options.tip) {
const eleFooter = this.renderFooter(options);
eleRoot.appendChild(eleFooter);
}
return eleRoot;
};
private renderFooter = (options: ModalOptions): HTMLElement => {
const ele = document.createElement("div");
ele.setAttribute("class", "bn-modal-footer");
if (options.tip) {
ele.appendChild(this.renderTip(options.tip));
}
if (options.buttons) {
ele.appendChild(this.renderButtons(options.buttons));
}
return ele;
};
private renderTip = (tip: string): HTMLElement => {
const ele = document.createElement("div");
ele.setAttribute("class", "tip");
ele.innerHTML = tip;
return ele;
};
private renderButtons = (buttons: ModalButton[]): HTMLElement => {
const eleContainer = document.createElement("div");
eleContainer.setAttribute("class", "actions-container");
Eif (buttons && buttons.length > 0) {
buttons.forEach(button => {
const btn = document.createElement("button");
btn.innerHTML = button.label;
btn.addEventListener("click", event => {
Eif (button.onClick && typeof button.onClick === "function") {
button.onClick(this);
}
});
if (button.css) {
btn.setAttribute("class", button.css);
} else {
btn.setAttribute("class", "btn btn-default btn-light");
}
if (button.style) {
btn.setAttribute("style", button.style);
}
eleContainer.appendChild(btn);
});
}
return eleContainer;
};
}
/** The content type for [[Modal]] */
export enum ContentType {
TEXT,
ELEMENT,
URL,
}
/** The options for [[Modal]] */
export interface ModalOptions {
animate?: boolean;
theme?: string;
content?: string;
contentType?: ContentType;
title?: string;
tip?: string;
css?: string;
width?: number | string;
height?: number | string;
buttons?: ModalButton[];
/**
* The event listener will be called before closing.
* @returns true to continue, otherwise break close event.
*/
onClosing?: (event: any) => boolean;
onClosed?: (event: any) => void;
labelOK?: string;
labelCancel?: string;
}
/** The button definition for [[Modal]] buttons */
export interface ModalButton {
label: string;
style?: string;
css?: string;
onClick?: (Modal: Modal) => void;
}
/**
* Shows an alert dialog.
* @param message The alert message
* @param callback The callback function when you click ok button
*/
export function alert(message: string, callback?: () => void): Modal;
/**
* Shows an alert dialog.
* @param title The alert dialog title
* @param message The alert dialog message
* @param callback The callback function when you click ok button
*/
export function alert(title: string, message: string, callback?: () => void): Modal;
/**
* Shows an alert dialog according to the specified arguments.
*/
export function alert(): Modal {
const args = arguments;
const modal = new Modal();
const modalOptions = modal.finalOptions;
const options: ModalOptions = {};
options.theme = "theme-alert";
options.buttons = [
{
label: modalOptions.labelOK as string,
css: "btn btn-primary",
onClick: () => {
modal.close();
},
},
];
switch (args.length) {
case 1:
options.content = args[0];
break;
case 2:
if (typeof args[1] === "function") {
options.content = args[0];
options.onClosed = args[1];
} else {
options.title = args[0];
options.content = args[1];
}
break;
case 3:
options.title = args[0];
options.content = args[1];
options.onClosed = args[2];
break;
}
Eif (options) {
modal.render(options);
}
return modal;
}
/**
* Shows a confirm dialog.
* @param options The modal options, see [[ModalOptions]] interface
* @returns A [[Modal]] instance
*/
export function confirm(options: ModalOptions): Modal;
/**
* Shows a confirm dialog.
* @param message The confirm message
* @param callback The callback function when click ok button
* @returns A [[Modal]] instance
*/
export function confirm(message: string, callback: () => void): Modal;
/**
* Shows a confirm dialog.
* @param title The confirm title
* @param message The confirm message
* @param callback The callback function when click ok button
* @returns A [[Modal]] instance
*/
export function confirm(title: string, message: string, callback: () => void): Modal;
/**
* Shows a confirm dialog
* @param title The confirm title, if message unspecified, which is as the message
* @param message The confirm message
* @returns A promise
*/
export function confirm(title: string, message?: string) : Promise<void>;
/**
* Shows an confirm dialog according to the specified arguments.
*/
export function confirm(): Modal | Promise<void> {
if (typeof arguments[arguments.length - 1] !== "function") {
const pArgs = arguments;
return new Promise((resolve, reject) => {
switch(pArgs.length) {
case 1:
confirm(pArgs[0], () => {
resolve();
});
break;
case 2:
confirm(pArgs[0], pArgs[1], () => {
resolve();
});
break;
}
});
}
const args = arguments;
const modal = new Modal();
const modalOptions = modal.finalOptions;
const options: ModalOptions = {
theme: "theme-confirm",
};
options.buttons = [
{
label: modalOptions.labelCancel as string,
css: "btn btn-default btn-light",
onClick: () => {
modal.close();
},
},
{
label: modalOptions.labelOK as string,
css: "btn btn-primary",
},
];
switch (args.length) {
case 2:
options.content = arguments[0];
options.buttons[1].onClick = () => {
args[1]();
modal.close();
};
break;
case 3:
options.title = arguments[0];
options.content = arguments[1];
options.buttons[1].onClick = () => {
args[2]();
modal.close();
};
break;
}
Eif (options) {
modal.render(options);
}
return modal;
}
/**
* Opens a window with iframe element for opening the specified uri.
* @param uri The uri to open
* @param title The window title. If empty, show the url
* @param options The [[ModalOptions]] like {width: '80%', height: '80%'}
*/
export function iframe(uri: string, title: string, options?: ModalOptions) : Modal {
options = {
content: uri,
contentType: ContentType.URL,
theme: "theme-iframe",
title: title || uri,
...options,
};
const modal = new Modal(options);
modal.render();
return modal;
}
/**
* Alias for `iframe`
*/
export function url(uri: string, title: string, options?: ModalOptions) : Modal {
return iframe(uri, title, options);
}
/**
* Opens a modal dialog and appends the ID element.
* @param id The element ID
* @param title The dialog title
* @param options The [[ModalOptions]] like {width: 800, height: 600}
*/
export function element(id: string, title: string, options?: ModalOptions) : Modal {
options = {
content: id,
title: title || " ",
theme: "theme-element",
contentType: ContentType.ELEMENT,
...options,
};
const modal = new Modal(options);
modal.addCloseListener(() => {
const ele = document.querySelector("#" + id) as HTMLElement;
Eif (ele) {
ele.style.display = 'none';
document.body.appendChild(ele);
}
return true;
});
modal.render();
return modal;
} |