-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-handlers.js
161 lines (133 loc) · 4.83 KB
/
data-handlers.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
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
/**
* @copyright 2023 Chris Zuber <[email protected]>
*/
import { on, query, each, addClass, removeClass } from './dom.js';
export function remove() {
each(this.dataset.remove, el => el.remove());
}
export function hide() {
each(this.dataset.hide, el => el.hidden = true);
}
export function unhide() {
each(this.dataset.unhide, el => el.hidden = false);
}
export function disable() {
each(this.dataset.disable, el => el.disabled = true);
}
export function enable() {
each(this.dataset.enable, el => el.disabled = false);
}
export function fullScreen() {
const target = document.querySelector(this.dataset.fullScreen);
if (target instanceof Element) {
if (document.fullscreenElement instanceof Element && document.fullscreenElement.isSameNode(target)) {
document.exitFullscreen();
} else {
target.requestFullscreen();
}
} else if (document.fullscreenElement instanceof Element) {
document.exitFullscreen();
}
}
export async function cookie() {
const { cookie: name, value = null, domain = null, maxAge, expires, sameSite = 'Strict', path = '/' } = this.dataset;
if (typeof value === 'string') {
await cookieStore.set({ name, value, domain, path,
maxAge: typeof maxAge === 'string' ? parseInt(maxAge) : null, sameSite,
expires: typeof expires === 'string' ? new Date(expires) : null,
secure: this.dataset.hasOwnProperty('secure'),
});
} else {
await cookieStore.delete({ name, domain, path, secure: this.dataset.hasOwnProperty('secure') });
}
}
export function scrollTo() {
const { scrollTo, behavior = 'smooth', block = 'start', inline = 'nearest' } = this.dataset;
const target = document.querySelector(scrollTo);
if (target instanceof Element) {
target.scrollIntoView({ behavior, block, inline });
}
}
export function show() {
const target = document.querySelector(this.dataset.show);
if (target.show instanceof Function) {
target.show();
} else {
target.open = true;
}
}
export function open() {
each(this.dataset.open, el => el.open = true);
}
export function showModal() {
const target = document.querySelector(this.dataset.showModal);
if (target.showModal instanceof Function) {
target.showModal();
}
}
export function close() {
const { close, returnValue } = this.dataset;
each(close, el => el.tagName === 'DIALOG' ? el.close(returnValue) : el.open = false);
}
export function toggleAttribute() {
const { selector = ':root', toggleAttribute } = this.dataset;
if ('checked' in this) {
const checked = this.checked;
each(selector, el => el.toggleAttribute(toggleAttribute, checked));
} else {
each(selector, el => el.toggleAttribute(toggleAttribute));
}
}
export function copy() {
if (navigator.clipboard && navigator.clipboard.writeText instanceof Function) {
navigator.clipboard.writeText(this.dataset.copy);
}
}
export function navigate() {
switch(this.dataset.navigate) {
case 'back':
history.back();
break;
case 'forward':
history.forward();
break;
case 'reload':
location.reload();
break;
default:
throw new DOMException(`Invalid value for data-navigate: ${this.dataset.navigate}`);
}
}
export function toggleClass() {
const { selector = ':root', toggleClass = '' } = this.dataset;
if ('checked' in this) {
if (this.checked) {
addClass(selector, ...toggleClass.split(' '));
} else {
removeClass(selector, ...toggleClass.split(' '));
}
} else {
each(selector, el => toggleClass.split(' ').forEach(cn => el.classList.toggle(cn)));
}
}
export function init({ base = document, capture, once, passive, signal } = {}) {
// NOTE: Need to set keyboard handlers for non-button elements
const events = ['click'];
const opts = { capture, passive, once, signal };
on(query('[data-remove]', base), events, remove, opts);
on(query('[data-hide]', base), events, hide, opts);
on(query('[data-unhide]', base), events, unhide, opts);
on(query('[data-disable]', base), events, disable, opts);
on(query('[data-enable]', base), events, enable, opts);
on(query('[data-scroll-to]', base), events, scrollTo, opts);
on(query('[data-show]', base), events, show, opts);
on(query('[data-open]', base), events, open, opts);
on(query('[data-show-modal]', base), events, showModal, opts);
on(query('[data-close]', base), events, close, opts);
on(query('[data-toggle-attribute]', base), events, toggleAttribute, opts);
on(query('[data-toggle-class]', base), events, toggleClass, opts);
on(query('[data-cookie]', base), events, cookie, opts);
on(query('[data-navigate]', base), events, navigate, opts);
on(query('[data-full-screen]', base), events, fullScreen, opts);
on(query('[data-copy]', base), events, copy, opts);
}