-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
146 lines (127 loc) · 4.1 KB
/
command.js
File metadata and controls
146 lines (127 loc) · 4.1 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
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
if (! ('command' in HTMLButtonElement.prototype)) {
const COMMAND_FOR = Symbol('command:for');
const CLICK_OPTIONS = { passive: true };
Object.defineProperty(HTMLButtonElement.prototype, 'command', {
get() {
return this.getAttribute('command');
},
set(val) {
if (typeof val === 'string') {
this.setAttribute('command', val);
} else {
this.removeAttribute('command');
}
}
});
Object.defineProperty(HTMLButtonElement.prototype, 'commandForElement', {
get() {
if (this[COMMAND_FOR] instanceof Element) {
return this[COMMAND_FOR];
} else if (this.hasAttribute('commandfor')) {
return document.getElementById(this.getAttribute('commandfor'));
} else {
return null;
}
},
set(val) {
if (! (val instanceof Element)) {
this.removeAttribute('commandfor');
this[COMMAND_FOR] = null;
} else if (val.id.length === 0) {
this.setAttribute('commandfor', '');
this[COMMAND_FOR] = val;
} else {
this.setAttribute('commandfor', val.id);
this[COMMAND_FOR] = null;
}
}
});
class CommandEvent extends Event {
#source = null;
#command = '';
constructor(type, { source, command, bubbles, cancelable, composed } = {}) {
super(type, { bubbles, cancelable, composed });
if (typeof command === 'string') {
this.#command = command;
}
if (source instanceof HTMLButtonElement) {
this.#source = source;
}
}
get command() {
return this.#command;
}
get source() {
return this.#source;
}
}
function dispatchCommand({ currentTarget }) {
if (currentTarget instanceof HTMLButtonElement && currentTarget.hasAttribute('command') && currentTarget.hasAttribute('commandfor')) {
const target = currentTarget.commandForElement;
const command = currentTarget.command;
if (target instanceof Element && typeof command === 'string') {
const event = new CommandEvent('command', { command, source: currentTarget, cancelable: true });
target.dispatchEvent(event);
if (! event.defaultPrevented) {
switch(command) {
case 'show-modal':
if (target instanceof HTMLDialogElement) {
target.showModal();
}
break;
case 'close':
if (target instanceof HTMLDialogElement) {
target.close();
}
break;
case 'request-close':
if (target instanceof HTMLDialogElement) {
target.requestClose();
}
break;
case 'show-popover':
target.showPopover();
break;
case 'hide-popover':
target.hidePopover();
break;
case 'toggle-popover':
target.togglePopover();
break;
}
}
}
}
}
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node instanceof HTMLButtonElement && node.hasAttribute('command') && node.hasAttribute('commandfor')) {
node.addEventListener('click', dispatchCommand, CLICK_OPTIONS);
} else if (node instanceof Element) {
for (const btn of node.querySelectorAll('button[command][commandfor]')) {
btn.addEventListener('click', dispatchCommand, CLICK_OPTIONS);
}
}
}
} else if (
mutation.type === 'attributes'
&& mutation.target instanceof HTMLButtonElement
&& typeof mutation.oldValue !== typeof mutation.target.getAttribute(mutation.attributeName)
) {
if (mutation.target.hasAttribute('command') && mutation.target.hasAttribute('commandfor')) {
mutation.target.addEventListener('click', dispatchCommand, CLICK_OPTIONS);
} else {
mutation.target.removeEventListener('click', dispatchCommand, CLICK_OPTIONS);
}
}
}
});
const config = { childList: true, subtree: true, attributes: true, attributeFilter: ['command', 'commandfor'], attributeOldValue: true };
observer.observe(document, config);
document.querySelectorAll('button').forEach(btn => btn.addEventListener('click', dispatchCommand, CLICK_OPTIONS));
globalThis.CommandEvent = CommandEvent;
// For use in Shadow DOM
globalThis[Symbol.for('polyfill-command')] = target => observer.observe(target, config);
}