-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsrc
44 lines (36 loc) · 1.68 KB
/
src
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
define(['plugins/dialog-polyfill/dialog-polyfill', 'css!plugins/dialog-polyfill/dialog-polyfill.css'], function (dialogPolyfill) {
//select all modal and convert node list to array
const modalContainers = [].slice.call(document.querySelectorAll('.dcf-js-dialog'));
modalContainers.forEach(modalContainer => {
const trigger = modalContainer.querySelector('.dcf-js-dialog-trigger');
const modalDialog = modalContainer.querySelector('dialog');
const closeButton = modalContainer.querySelector('.dcf-o-dialog__close');
// if global dialog property not present, register all dialog modal with polyfill
if (!window.HTMLDialogElement) {
dialogPolyfill.registerDialog(modalDialog);
}
// show dialog on trigger button click
trigger.addEventListener('click', () => {
modalDialog.showModal();
// translate doesn't seem to work on dialog
modalDialog.style.top = `calc(50% - ${modalDialog.scrollHeight / 2}px)`;
});
// close dialog on close button click
closeButton.addEventListener('click', () => {
modalDialog.close('closed');
});
// close dialog on Esc button press
modalDialog.addEventListener('cancel', () => {
modalDialog.close('cancelled');
});
// close dialog when clicking on dialog backdrop
// for this to work properly, child elements of dialog must span the entire region
// within the dialog box so that when clicking within the dialog, child elements
// are clicked on instead of the dialog box itself
modalContainer.addEventListener('click', e => {
if (e.target == modalDialog) modalDialog.close('cancelled');
console.log(e.target);
});
});
return dialogPolyfill;
});