-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
210 lines (156 loc) · 4.62 KB
/
index.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
export interface ModalOptions {
title?: string;
message?: string;
centered?: boolean;
size?: 'small' | 'default' | 'large' | 'extra-large';
fade?: boolean;
closeOnBackdropClick?: boolean;
closeOnEsc?: boolean;
focus?: boolean;
}
export interface ConfirmOptions extends ModalOptions {
confirmText?: string;
cancelText?: string;
confirmClass?: string;
cancelClass?: string;
}
export interface AlertOptions extends ModalOptions {
okText?: string;
okClass?: string;
}
interface CustomModal extends bootstrap.Modal {
wrapper: HTMLElement;
}
function mergeDefaults<T extends Object>(defaults: T, options?: T): T {
return Object.assign({}, defaults, options);
}
function createModal(options: ModalOptions, footerElements?: HTMLElement[]): CustomModal {
const wrapper = document.createElement('div');
wrapper.className = 'modal ';
if(options.fade) wrapper.className += ' fade';
wrapper.tabIndex = -1;
document.body.appendChild(wrapper);
const dialog = document.createElement('div');
dialog.className = 'modal-dialog ';
if(options.centered) dialog.className += 'modal-dialog-centered ';
switch(options.size) {
case 'small':
dialog.className += 'modal-sm';
break;
case 'large':
dialog.className += 'modal-lg';
break;
case 'extra-large':
dialog.className += 'modal-xl';
break;
}
wrapper.appendChild(dialog);
const content = document.createElement('div');
content.className = 'modal-content';
dialog.appendChild(content);
const header = document.createElement('div');
header.className = 'modal-header';
content.appendChild(header);
const title = document.createElement('h5');
title.className = 'modal-title';
title.innerText = options.title;
header.appendChild(title);
if(options?.message ?? true) {
const body = document.createElement('div');
body.className = 'modal-body';
content.appendChild(body);
const message = document.createElement('p');
message.innerText = options.message;
body.appendChild(message);
}
const footer = document.createElement('div');
footer.className = 'modal-footer';
content.appendChild(footer);
footerElements?.forEach((el) => {
footer.appendChild(el);
});
return Object.assign(
new window.bootstrap.Modal(wrapper, {
backdrop: options.closeOnBackdropClick,
keyboard: options.closeOnEsc,
focus: options.focus,
}),
{
wrapper
}
);
}
function confirm(arg1?: ConfirmOptions | string): Promise<boolean> {
let options: ConfirmOptions = typeof arg1 === 'string' ? {
title: arg1,
size: 'small',
message: '',
}: arg1;
const defaults: ConfirmOptions = {
title: 'Confirm',
message: 'Are you sure you want to do this?',
centered: true,
fade: true,
size: 'default',
cancelText: 'Cancel',
cancelClass: 'btn-secondary',
confirmText: 'Confirm',
confirmClass: 'btn-primary',
closeOnBackdropClick: true,
closeOnEsc: true,
focus: true,
}
options = mergeDefaults(defaults, options);
return new Promise((resolve) => {
const cancel = document.createElement('button');
cancel.className = 'btn ' + options.cancelClass;
cancel.innerText = options.cancelText;
const confirm = document.createElement('button');
confirm.className = 'btn ' + options.confirmClass;
confirm.innerText = options.confirmText;
const modal = createModal(options, [cancel, confirm]);
modal.wrapper.addEventListener('hidden.bs.modal', modal.wrapper.remove);
cancel.addEventListener('click', () => {
modal.hide();
resolve(false);
});
confirm.addEventListener('click', () => {
modal.hide();
resolve(true);
});
modal.show();
});
}
function alert(arg1?: AlertOptions | string): Promise<void> {
let options: AlertOptions = typeof arg1 === 'string' ? {
title: arg1,
size: 'small',
message: '',
} : arg1;
const defaults: AlertOptions = {
title: 'Alert',
message: '',
centered: true,
fade: true,
size: 'default',
okText: 'OK',
okClass: 'btn-primary',
closeOnBackdropClick: true,
closeOnEsc: true,
focus: true,
}
options = mergeDefaults(defaults, options);
return new Promise((resolve) => {
const ok = document.createElement('button');
ok.className = 'btn ' + options.okClass;
ok.innerText = options.okText;
const modal = createModal(options, [ok]);
modal.wrapper.addEventListener('hidden.bs.modal', modal.wrapper.remove);
ok.addEventListener('click', () => {
modal.hide();
resolve();
});
modal.show();
});
}
export { confirm, alert, createModal };