-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathModal.js
More file actions
117 lines (96 loc) · 4.34 KB
/
Modal.js
File metadata and controls
117 lines (96 loc) · 4.34 KB
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
import Bs5Utils from "../Bs5Utils";
import {Modal as ModalBs} from "bootstrap";
export default class Modal {
/**
* A counter for the Modals
* @type {number}
*/
#count = 0;
/**
* Display a modal
* @param type - the theme of the snack
* @param title - the title of the modal, if omitted, the modal-header element is removed
* @param content - the content of the modal, if omitted, the modal-body element is removed
* @param buttons - any action buttons, if omitted, the the modal-footer element is removed
* @param centered - set whether the modal is centered
* @param dismissible - set whether the dismiss button should show
* @param backdrop - set the type of backdrop: true, false, static
* @param keyboard - set whether the escape key closes the modal
* @param focus - set whether the modal is autofocussed when initialized
* @param fullscreen - set whether the modal is fullscreen
* @param modalSize - set the size of the modal: sm, lg, xl by default, it's an empty string
*/
show({
type,
title = '',
content = '',
buttons = [],
centered = false,
dismissible = true,
backdrop = dismissible ? true : 'static',
keyboard = dismissible,
focus = true,
fullscreen = false,
size = ''
}) {
this.#count++;
size = ['sm', 'lg', 'xl'].includes(size) ? `modal-${size}` : '';
fullscreen = fullscreen ? 'modal-fullscreen' : '';
centered = centered ? 'modal-dialog-centered modal-dialog-scrollable' : '';
const style = Bs5Utils.defaults.styles[type],
btnCloseStyles = style.btnClose.join(' '),
borderStyles = style.border,
modal = document.createElement('div');
modal.setAttribute('id', `modal-${this.#count}`)
modal.setAttribute('tabindex', '-1');
modal.classList.add('modal');
let footerHtml = '',
buttonIds = [];
if (Array.isArray(buttons) && buttons.length) {
footerHtml += `<div class="modal-footer ${borderStyles.join(' ')}">`;
buttons.forEach((button, key) => {
const type = button.type || 'button';
switch (type) {
case 'dismiss':
footerHtml += `<button type="button" class="${button.class}" data-bs-dismiss="modal">${button.text}</button>`;
break;
default:
let id = `modal-${this.#count}-button-${key}`;
footerHtml += `<button type="button" id="${id}" class="${button.class}">${button.text}</button>`;
if (button.hasOwnProperty('handler') && typeof button.handler === 'function') {
buttonIds.push({
id,
handler: button.handler
});
}
}
});
footerHtml += `</div>`;
}
modal.innerHTML = ` <div class="modal-dialog ${centered} ${fullscreen} ${size}">
<div class="modal-content border-0">
${title.length ? `<div class="modal-header border-0 ${style.main.join(' ')}">
<h5 class="modal-title">${title}</h5>
${dismissible ? `<button type="button" class="btn-close ${btnCloseStyles}" data-bs-dismiss="modal" aria-label="Close"></button>` : ``}
</div>` : ``}
${content.length ? `<div class="modal-body">${content}</div>` : ``}
${footerHtml}
</div>
</div>`;
document.body.appendChild(modal);
modal.addEventListener('hidden.bs.modal', function (e) {
e.target.remove();
});
buttonIds.forEach(value => {
document.getElementById(value.id).addEventListener('click', value.handler)
});
const opts = {
backdrop,
keyboard,
focus
};
const bsModal = new ModalBs(modal, opts);
bsModal.show();
return bsModal;
}
}