-
Notifications
You must be signed in to change notification settings - Fork 11
/
invoker.js
359 lines (327 loc) · 10.8 KB
/
invoker.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(() => {
if (
typeof HTMLButtonElement === "undefined" ||
"source" in ((globalThis.CommandEvent || {}).prototype || {})
) {
return;
}
// XXX: Invoker Buttons used to dispatch 'invoke' events instead of
// 'command' events. We should ensure to prevent 'invoke' events being
// fired in those browsers.
// XXX: https://bugs.chromium.org/p/chromium/issues/detail?id=1523183
// Chrome will dispatch invoke events even with the flag disabled; so
// we need to capture those to prevent duplicate events.
document.addEventListener(
"invoke",
(e) => {
if (e.type == "invoke" && e.isTrusted) {
e.stopImmediatePropagation();
e.preventDefault();
}
},
true,
);
document.addEventListener(
"command",
(e) => {
if (e.type == "command" && e.isTrusted) {
e.stopImmediatePropagation();
e.preventDefault();
}
},
true,
);
function enumerate(obj, key, enumerable = true) {
Object.defineProperty(obj, key, {
...Object.getOwnPropertyDescriptor(obj, key),
enumerable,
});
}
function getRootNode(node) {
if (node && typeof node.getRootNode === "function") {
return node.getRootNode();
}
if (node && node.parentNode) return getRootNode(node.parentNode);
return node;
}
const ShadowRoot = globalThis.ShadowRoot || function () {};
const commandEventSourceElements = new WeakMap();
const commandEventActions = new WeakMap();
class CommandEvent extends Event {
constructor(type, invokeEventInit = {}) {
super(type, invokeEventInit);
const { source, command } = invokeEventInit;
if (source != null && !(source instanceof Element)) {
throw new TypeError(`source must be an element`);
}
commandEventSourceElements.set(this, source || null);
commandEventActions.set(
this,
command !== undefined ? String(command) : "",
);
}
get [Symbol.toStringTag]() {
return "CommandEvent";
}
get source() {
if (!commandEventSourceElements.has(this)) {
throw new TypeError("illegal invocation");
}
const source = commandEventSourceElements.get(this);
if (!(source instanceof Element)) return null;
const invokerRoot = getRootNode(source);
if (invokerRoot !== getRootNode(this.target || document)) {
return invokerRoot.host;
}
return source;
}
get command() {
if (!commandEventActions.has(this)) {
throw new TypeError("illegal invocation");
}
return commandEventActions.get(this);
}
get action() {
throw new Error(
"CommandEvent#action was renamed to CommandEvent#command",
);
}
get invoker() {
throw new Error(
"CommandEvent#invoker was renamed to CommandEvent#source",
);
}
}
enumerate(CommandEvent.prototype, "source");
enumerate(CommandEvent.prototype, "command");
class InvokeEvent extends Event {
constructor() {
throw new Error(
"InvokeEvent has been deprecated, it has been renamed to `CommandEvent`",
);
}
}
const invokerAssociatedElements = new WeakMap();
function applyInvokerMixin(ElementClass) {
Object.defineProperties(ElementClass.prototype, {
commandForElement: {
enumerable: true,
configurable: true,
set(targetElement) {
if (this.hasAttribute("invokeaction")) {
throw new TypeError(
"Element has deprecated `invokeaction` attribute, replace with `command`",
);
} else if (this.hasAttribute("invoketarget")) {
throw new TypeError(
"Element has deprecated `invoketarget` attribute, replace with `commandfor`",
);
} else if (targetElement === null) {
this.removeAttribute("commandfor");
invokerAssociatedElements.delete(this);
} else if (!(targetElement instanceof Element)) {
throw new TypeError(`commandForElement must be an element or null`);
} else {
this.setAttribute("commandfor", "");
const targetRootNode = getRootNode(targetElement);
const thisRootNode = getRootNode(this);
if (
thisRootNode === targetRootNode ||
targetRootNode === this.ownerDocument
) {
invokerAssociatedElements.set(this, targetElement);
} else {
invokerAssociatedElements.delete(this);
}
}
},
get() {
if (this.localName !== "button") {
return null;
}
if (
this.hasAttribute("invokeaction") ||
this.hasAttribute("invoketarget")
) {
console.warn(
"Element has deprecated `invoketarget` or `invokeaction` attribute, use `commandfor` and `command` instead",
);
return null;
}
if (this.disabled) {
return null;
}
if (this.form && this.getAttribute("type") !== "button") {
console.warn(
"Element with `commandFor` is a form participant. " +
"It should explicitly set `type=button` in order for `commandFor` to work",
);
return null;
}
const targetElement = invokerAssociatedElements.get(this);
if (targetElement) {
if (targetElement.isConnected) {
return targetElement;
} else {
invokerAssociatedElements.delete(this);
return null;
}
}
const root = getRootNode(this);
const idref = this.getAttribute("commandfor");
if (
(root instanceof Document || root instanceof ShadowRoot) &&
idref
) {
return root.getElementById(idref) || null;
}
return null;
},
},
command: {
enumerable: true,
configurable: true,
get() {
const value = this.getAttribute("command") || "";
if (value) return value;
return "";
},
set(value) {
this.setAttribute("command", value);
},
},
invokeAction: {
enumerable: false,
configurable: true,
get() {
throw new Error(
`invokeAction is deprecated. It has been renamed to command`,
);
},
set(value) {
throw new Error(
`invokeAction is deprecated. It has been renamed to command`,
);
},
},
invokeTargetElement: {
enumerable: false,
configurable: true,
get() {
throw new Error(
`invokeTargetElement is deprecated. It has been renamed to command`,
);
},
set(value) {
throw new Error(
`invokeTargetElement is deprecated. It has been renamed to command`,
);
},
},
});
}
function handleInvokerActivation(event) {
if (event.defaultPrevented) return;
if (event.type !== "click") return;
const oldInvoker = event.target.closest(
"button[invoketarget], button[invokeaction], input[invoketarget], input[invokeaction]",
);
if (oldInvoker) {
console.warn(
"Elements with `invoketarget` or `invokeaction` are deprecated and should be renamed to use `commandfor` and `command` respectively",
);
}
const source = event.target.closest(
"button[commandfor], button[command], input[commandfor], input[command]",
);
if (!source) return;
if (this.form && this.getAttribute("type") !== "button") {
event.preventDefault();
throw new Error(
"Element with `commandFor` is a form participant. " +
"It should explicitly set `type=button` in order for `commandFor` to work. " +
"In order for it to act as a Submit button, it must not have command or commandfor attributes",
);
}
if (source.hasAttribute("command") !== source.hasAttribute("commandfor")) {
const attr = source.hasAttribute("command") ? "command" : "commandfor";
const missing = source.hasAttribute("command") ? "commandfor" : "command";
throw new Error(
`Element with ${attr} attribute must also have a ${missing} attribute to function.`,
);
}
if (
source.command !== "show-popover" &&
source.command !== "hide-popover" &&
source.command !== "toggle-popover" &&
source.command !== "show-modal" &&
source.command !== "close" &&
!source.command.startsWith("--")
) {
console.warn(
`"${source.command}" is not a valid command value. Custom commands must begin with --`,
);
return;
}
const invokee = source.commandForElement;
const invokeEvent = new CommandEvent("command", {
command: source.command,
source,
});
invokee.dispatchEvent(invokeEvent);
if (invokeEvent.defaultPrevented) return;
const command = invokeEvent.command.toLowerCase();
if (invokee.popover) {
const canShow = !invokee.matches(":popover-open");
const shouldShow =
canShow && (command === "toggle-popover" || command === "show-popover");
const shouldHide = !canShow && command === "hide-popover";
if (shouldShow) {
invokee.showPopover();
} else if (shouldHide) {
invokee.hidePopover();
}
} else if (invokee.localName === "dialog") {
const canShow = !invokee.hasAttribute("open");
const shouldShow = canShow && command === "show-modal";
const shouldHide = !canShow && command === "close";
if (shouldShow) {
invokee.showModal();
} else if (shouldHide) {
invokee.close();
}
}
}
function setupInvokeListeners(target) {
target.addEventListener("click", handleInvokerActivation, true);
}
function observeShadowRoots(ElementClass, callback) {
const attachShadow = ElementClass.prototype.attachShadow;
ElementClass.prototype.attachShadow = function (init) {
const shadow = attachShadow.call(this, init);
callback(shadow);
return shadow;
};
const attachInternals = ElementClass.prototype.attachInternals;
ElementClass.prototype.attachInternals = function () {
const internals = attachInternals.call(this);
if (internals.shadowRoot) callback(internals.shadowRoot);
return internals;
};
}
applyInvokerMixin(globalThis.HTMLButtonElement || function () {});
observeShadowRoots(globalThis.HTMLElement || function () {}, (shadow) => {
setupInvokeListeners(shadow);
});
setupInvokeListeners(document);
Object.defineProperty(window, "CommandEvent", {
value: CommandEvent,
configurable: true,
writable: true,
});
Object.defineProperty(window, "InvokeEvent", {
value: InvokeEvent,
configurable: true,
writable: true,
});
})();