-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic.js
98 lines (85 loc) · 2.99 KB
/
semantic.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
/* eslint-disable no-empty */
/* eslint-disable no-unused-vars */
/* jshint esversion: 9 */
// close dropdowns when clicking outside
document.addEventListener('mouseup', (event) => {
let ele = event.target;
while (ele.tagName && ele.tagName !== 'BODY' && ele.tagName !== 'DETAILS') {
ele = ele.parentNode;
}
if (ele.tagName === 'DETAILS') {
return;
}
try {
document.querySelectorAll('details[role=list][open]').forEach((details) => {
details.removeAttribute('open');
});
} catch (er) {}
});
(function (modal, undefined) {
const clickClose = (event) => {
event = event || window.event;
if (event.target.tagName.toLowerCase() === 'dialog' && event.target.getAttribute('open') === 'true') {
document.querySelector('dialog[open]').removeAttribute('open');
document.documentElement.classList.remove('no-scollbars');
document.removeEventListener('click', clickClose);
document.removeEventListener('keydown', escClose);
}
};
const escClose = (event) => {
event = event || window.event;
if (event.key === 'Escape') {
document.querySelector('dialog[open]').removeAttribute('open');
document.documentElement.classList.remove('no-scollbars');
document.removeEventListener('click', clickClose);
document.removeEventListener('keydown', escClose);
}
};
modal.show = (event, allowCloseOutside = true) => {
let dialog;
if (typeof event === 'string') {
dialog = document.getElementById(event);
} else {
event = event || window.event;
event.preventDefault();
dialog = document.getElementById(event.currentTarget.getAttribute('data-target'));
}
dialog.setAttribute('open', true);
document.documentElement.classList.add('no-scollbars');
if (allowCloseOutside) {
document.addEventListener('click', clickClose);
document.addEventListener('keydown', escClose);
}
};
modal.hide = (event) => {
let dialog;
if (typeof event === 'string') {
dialog = document.getElementById(event);
} else {
event = event || window.event;
dialog = document.getElementById(event.currentTarget.getAttribute('data-target'));
if (event.target.href.split('#').pop() === dialog)
event.preventDefault();
}
dialog.removeAttribute('open');
document.documentElement.classList.remove('no-scollbars');
document.removeEventListener('click', clickClose);
document.removeEventListener('keydown', escClose);
};
modal.toggle = (event, allowCloseOutside = true) => {
let dialog;
if (typeof event === 'string') {
console.log(event);
dialog = document.getElementById(event);
} else {
event = event || window.event;
event.preventDefault();
dialog = document.getElementById(event.currentTarget.getAttribute('data-target'));
}
if (dialog.getAttribute('open') !== 'true') {
modal.show(event, allowCloseOutside);
} else {
modal.hide(event);
}
};
}(window.modal = window.modal || {}));