-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrappedModal.js
134 lines (105 loc) · 4.26 KB
/
WrappedModal.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
//AUTHOR: xXConcasXx
class WrappedModal {
constructor(id, title, body, options, buttons) {
this.id = id;
this.title = title || 'title';
this.body = body;
this.options = options || null;
this.buttons = buttons || null;
}
show() {
if (isNullUndefinedOrEmpty(this.id)) {
throw new Error('ID cannot be empty.');
}
if (isNullUndefinedOrEmpty(this.body)) {
throw new Error('Body cannot be empty.');
}
const modal = document.createElement('div');
const modalDialog = document.createElement('div');
const modalContent = document.createElement('div');
const modalHeader = document.createElement('div');
const modalTitle = document.createElement('h5');
const modalBody = document.createElement('div');
const modalFooter = document.createElement('div');
modal.setAttribute('class', 'modal');
modal.setAttribute('id', this.id);
modal.setAttribute('tabindex', !isNullUndefinedOrEmpty(this.options.tabindex) ? this.options.tabIndex : '-1');
modal.setAttribute('aria-hidden', !isNullUndefinedOrEmpty(this.options.ariaHidden) ? this.options.ariaHidden : 'true');
modalDialog.setAttribute('class', 'modal-dialog');
modalContent.setAttribute('class', 'modal-content');
modalHeader.setAttribute('class', 'modal-header');
modalTitle.setAttribute('class', 'modal-title');
modalTitle.innerText = this.title;
modalBody.setAttribute('class', 'modal-body');
modalBody.insertAdjacentHTML('afterbegin', this.body);
modalFooter.setAttribute('class', 'modal-footer');
if (isNullUndefinedOrEmpty(this.buttons)) {
const defaultButton = document.createElement('button');
defaultButton.setAttribute('type', 'button');
defaultButton.setAttribute('class', 'btn btn-primary');
defaultButton.setAttribute('data-bs-dismiss', 'modal');
defaultButton.innerText = 'OK';
modalFooter.append(defaultButton);
} else {
this.buttons.forEach( (button) => {
const customButton = document.createElement('button');
customButton.setAttribute('type', 'button');
customButton.setAttribute('class', button.class);
customButton.innerText = button.text;
if (!isNullUndefinedOrEmpty(button.dismiss) && button.dismiss) {
customButton.setAttribute('data-bs-dismiss', 'modal');
}
customButton.addEventListener('click', button.function, { once: false });
modalFooter.appendChild(customButton);
});
}
if (!isNullUndefinedOrEmpty(this.options.fade) && this.options.fade) {
modal.classList.add('fade');
}
if (!isNullUndefinedOrEmpty(this.options.shadow) && this.options.shadow) {
modalContent.classList.add('shadow');
}
if (!isNullUndefinedOrEmpty(this.options.static) && this.options.static) {
modal.setAttribute('data-bs-backdrop', 'static');
modal.setAttribute('data-bs-keyboard', 'false');
}
if (!isNullUndefinedOrEmpty(this.options.scrollable) && this.options.scrollable) {
modalDialog.classList.add('modal-dialog-scrollable');
}
if (!isNullUndefinedOrEmpty(this.options.centered) && this.options.centered) {
modalDialog.classList.add('modal-dialog-centered');
}
if (!isNullUndefinedOrEmpty(this.options.size)) {
switch (this.options.size) {
case 'xl':
modalDialog.classList.add('modal-xl');
break;
case 'lg':
modalDialog.classList.add('modal-lg');
break;
case 'sm':
modalDialog.classList.add('modal-sm');
break;
}
}
modal.addEventListener('hidden.bs.modal', () => {
document.body.removeAttribute('class');
document.body.removeAttribute('style');
if (!isNullUndefinedOrEmpty(this.options.reload) && this.options.reload) {
location.reload();
}
modal.remove();
});
modalHeader.append(modalTitle);
modalContent.append(modalHeader);
modalContent.append(modalBody);
modalContent.append(modalFooter);
modalDialog.append(modalContent);
modal.append(modalDialog);
document.body.append(modal);
new bootstrap.Modal(document.getElementById(this.id)).toggle();
}
}
function isNullUndefinedOrEmpty(object) {
return object === null || object === undefined || object === '';
}