stimulus-rails 0.5.2 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/stimulus.js +1924 -2
- data/app/assets/javascripts/stimulus.min.js +8 -0
- data/lib/stimulus/engine.rb +1 -1
- data/lib/stimulus/manifest.rb +6 -5
- data/lib/stimulus/version.rb +1 -1
- metadata +3 -5
- data/app/assets/javascripts/stimulus@2.js +0 -1699
- data/app/assets/javascripts/stimulus@3.0.0-beta.1.js +0 -2058
- data/app/assets/javascripts/stimulus@3.0.0-beta.2.js +0 -2034
@@ -1,4 +1,1926 @@
|
|
1
|
-
//= require ./stimulus@2.js
|
2
|
-
//= link ./stimulus@3.0.0-beta.2.js
|
3
1
|
//= link ./stimulus-autoloader.js
|
4
2
|
//= link ./stimulus-importmap-autoloader.js
|
3
|
+
|
4
|
+
/*
|
5
|
+
Stimulus 3.0.0
|
6
|
+
Copyright © 2021 Basecamp, LLC
|
7
|
+
*/
|
8
|
+
class EventListener {
|
9
|
+
constructor(eventTarget, eventName, eventOptions) {
|
10
|
+
this.eventTarget = eventTarget;
|
11
|
+
this.eventName = eventName;
|
12
|
+
this.eventOptions = eventOptions;
|
13
|
+
this.unorderedBindings = new Set();
|
14
|
+
}
|
15
|
+
connect() {
|
16
|
+
this.eventTarget.addEventListener(this.eventName, this, this.eventOptions);
|
17
|
+
}
|
18
|
+
disconnect() {
|
19
|
+
this.eventTarget.removeEventListener(this.eventName, this, this.eventOptions);
|
20
|
+
}
|
21
|
+
bindingConnected(binding) {
|
22
|
+
this.unorderedBindings.add(binding);
|
23
|
+
}
|
24
|
+
bindingDisconnected(binding) {
|
25
|
+
this.unorderedBindings.delete(binding);
|
26
|
+
}
|
27
|
+
handleEvent(event) {
|
28
|
+
const extendedEvent = extendEvent(event);
|
29
|
+
for (const binding of this.bindings) {
|
30
|
+
if (extendedEvent.immediatePropagationStopped) {
|
31
|
+
break;
|
32
|
+
}
|
33
|
+
else {
|
34
|
+
binding.handleEvent(extendedEvent);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
get bindings() {
|
39
|
+
return Array.from(this.unorderedBindings).sort((left, right) => {
|
40
|
+
const leftIndex = left.index, rightIndex = right.index;
|
41
|
+
return leftIndex < rightIndex ? -1 : leftIndex > rightIndex ? 1 : 0;
|
42
|
+
});
|
43
|
+
}
|
44
|
+
}
|
45
|
+
function extendEvent(event) {
|
46
|
+
if ("immediatePropagationStopped" in event) {
|
47
|
+
return event;
|
48
|
+
}
|
49
|
+
else {
|
50
|
+
const { stopImmediatePropagation } = event;
|
51
|
+
return Object.assign(event, {
|
52
|
+
immediatePropagationStopped: false,
|
53
|
+
stopImmediatePropagation() {
|
54
|
+
this.immediatePropagationStopped = true;
|
55
|
+
stopImmediatePropagation.call(this);
|
56
|
+
}
|
57
|
+
});
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
class Dispatcher {
|
62
|
+
constructor(application) {
|
63
|
+
this.application = application;
|
64
|
+
this.eventListenerMaps = new Map;
|
65
|
+
this.started = false;
|
66
|
+
}
|
67
|
+
start() {
|
68
|
+
if (!this.started) {
|
69
|
+
this.started = true;
|
70
|
+
this.eventListeners.forEach(eventListener => eventListener.connect());
|
71
|
+
}
|
72
|
+
}
|
73
|
+
stop() {
|
74
|
+
if (this.started) {
|
75
|
+
this.started = false;
|
76
|
+
this.eventListeners.forEach(eventListener => eventListener.disconnect());
|
77
|
+
}
|
78
|
+
}
|
79
|
+
get eventListeners() {
|
80
|
+
return Array.from(this.eventListenerMaps.values())
|
81
|
+
.reduce((listeners, map) => listeners.concat(Array.from(map.values())), []);
|
82
|
+
}
|
83
|
+
bindingConnected(binding) {
|
84
|
+
this.fetchEventListenerForBinding(binding).bindingConnected(binding);
|
85
|
+
}
|
86
|
+
bindingDisconnected(binding) {
|
87
|
+
this.fetchEventListenerForBinding(binding).bindingDisconnected(binding);
|
88
|
+
}
|
89
|
+
handleError(error, message, detail = {}) {
|
90
|
+
this.application.handleError(error, `Error ${message}`, detail);
|
91
|
+
}
|
92
|
+
fetchEventListenerForBinding(binding) {
|
93
|
+
const { eventTarget, eventName, eventOptions } = binding;
|
94
|
+
return this.fetchEventListener(eventTarget, eventName, eventOptions);
|
95
|
+
}
|
96
|
+
fetchEventListener(eventTarget, eventName, eventOptions) {
|
97
|
+
const eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget);
|
98
|
+
const cacheKey = this.cacheKey(eventName, eventOptions);
|
99
|
+
let eventListener = eventListenerMap.get(cacheKey);
|
100
|
+
if (!eventListener) {
|
101
|
+
eventListener = this.createEventListener(eventTarget, eventName, eventOptions);
|
102
|
+
eventListenerMap.set(cacheKey, eventListener);
|
103
|
+
}
|
104
|
+
return eventListener;
|
105
|
+
}
|
106
|
+
createEventListener(eventTarget, eventName, eventOptions) {
|
107
|
+
const eventListener = new EventListener(eventTarget, eventName, eventOptions);
|
108
|
+
if (this.started) {
|
109
|
+
eventListener.connect();
|
110
|
+
}
|
111
|
+
return eventListener;
|
112
|
+
}
|
113
|
+
fetchEventListenerMapForEventTarget(eventTarget) {
|
114
|
+
let eventListenerMap = this.eventListenerMaps.get(eventTarget);
|
115
|
+
if (!eventListenerMap) {
|
116
|
+
eventListenerMap = new Map;
|
117
|
+
this.eventListenerMaps.set(eventTarget, eventListenerMap);
|
118
|
+
}
|
119
|
+
return eventListenerMap;
|
120
|
+
}
|
121
|
+
cacheKey(eventName, eventOptions) {
|
122
|
+
const parts = [eventName];
|
123
|
+
Object.keys(eventOptions).sort().forEach(key => {
|
124
|
+
parts.push(`${eventOptions[key] ? "" : "!"}${key}`);
|
125
|
+
});
|
126
|
+
return parts.join(":");
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
const descriptorPattern = /^((.+?)(@(window|document))?->)?(.+?)(#([^:]+?))(:(.+))?$/;
|
131
|
+
function parseActionDescriptorString(descriptorString) {
|
132
|
+
const source = descriptorString.trim();
|
133
|
+
const matches = source.match(descriptorPattern) || [];
|
134
|
+
return {
|
135
|
+
eventTarget: parseEventTarget(matches[4]),
|
136
|
+
eventName: matches[2],
|
137
|
+
eventOptions: matches[9] ? parseEventOptions(matches[9]) : {},
|
138
|
+
identifier: matches[5],
|
139
|
+
methodName: matches[7]
|
140
|
+
};
|
141
|
+
}
|
142
|
+
function parseEventTarget(eventTargetName) {
|
143
|
+
if (eventTargetName == "window") {
|
144
|
+
return window;
|
145
|
+
}
|
146
|
+
else if (eventTargetName == "document") {
|
147
|
+
return document;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
function parseEventOptions(eventOptions) {
|
151
|
+
return eventOptions.split(":").reduce((options, token) => Object.assign(options, { [token.replace(/^!/, "")]: !/^!/.test(token) }), {});
|
152
|
+
}
|
153
|
+
function stringifyEventTarget(eventTarget) {
|
154
|
+
if (eventTarget == window) {
|
155
|
+
return "window";
|
156
|
+
}
|
157
|
+
else if (eventTarget == document) {
|
158
|
+
return "document";
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
function camelize(value) {
|
163
|
+
return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase());
|
164
|
+
}
|
165
|
+
function capitalize(value) {
|
166
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
167
|
+
}
|
168
|
+
function dasherize(value) {
|
169
|
+
return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`);
|
170
|
+
}
|
171
|
+
function tokenize(value) {
|
172
|
+
return value.match(/[^\s]+/g) || [];
|
173
|
+
}
|
174
|
+
|
175
|
+
class Action {
|
176
|
+
constructor(element, index, descriptor) {
|
177
|
+
this.element = element;
|
178
|
+
this.index = index;
|
179
|
+
this.eventTarget = descriptor.eventTarget || element;
|
180
|
+
this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name");
|
181
|
+
this.eventOptions = descriptor.eventOptions || {};
|
182
|
+
this.identifier = descriptor.identifier || error("missing identifier");
|
183
|
+
this.methodName = descriptor.methodName || error("missing method name");
|
184
|
+
}
|
185
|
+
static forToken(token) {
|
186
|
+
return new this(token.element, token.index, parseActionDescriptorString(token.content));
|
187
|
+
}
|
188
|
+
toString() {
|
189
|
+
const eventNameSuffix = this.eventTargetName ? `@${this.eventTargetName}` : "";
|
190
|
+
return `${this.eventName}${eventNameSuffix}->${this.identifier}#${this.methodName}`;
|
191
|
+
}
|
192
|
+
get params() {
|
193
|
+
if (this.eventTarget instanceof Element) {
|
194
|
+
return this.getParamsFromEventTargetAttributes(this.eventTarget);
|
195
|
+
}
|
196
|
+
else {
|
197
|
+
return {};
|
198
|
+
}
|
199
|
+
}
|
200
|
+
getParamsFromEventTargetAttributes(eventTarget) {
|
201
|
+
const params = {};
|
202
|
+
const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`);
|
203
|
+
const attributes = Array.from(eventTarget.attributes);
|
204
|
+
attributes.forEach(({ name, value }) => {
|
205
|
+
const match = name.match(pattern);
|
206
|
+
const key = match && match[1];
|
207
|
+
if (key) {
|
208
|
+
Object.assign(params, { [camelize(key)]: typecast(value) });
|
209
|
+
}
|
210
|
+
});
|
211
|
+
return params;
|
212
|
+
}
|
213
|
+
get eventTargetName() {
|
214
|
+
return stringifyEventTarget(this.eventTarget);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
const defaultEventNames = {
|
218
|
+
"a": e => "click",
|
219
|
+
"button": e => "click",
|
220
|
+
"form": e => "submit",
|
221
|
+
"input": e => e.getAttribute("type") == "submit" ? "click" : "input",
|
222
|
+
"select": e => "change",
|
223
|
+
"textarea": e => "input"
|
224
|
+
};
|
225
|
+
function getDefaultEventNameForElement(element) {
|
226
|
+
const tagName = element.tagName.toLowerCase();
|
227
|
+
if (tagName in defaultEventNames) {
|
228
|
+
return defaultEventNames[tagName](element);
|
229
|
+
}
|
230
|
+
}
|
231
|
+
function error(message) {
|
232
|
+
throw new Error(message);
|
233
|
+
}
|
234
|
+
function typecast(value) {
|
235
|
+
try {
|
236
|
+
return JSON.parse(value);
|
237
|
+
}
|
238
|
+
catch (o_O) {
|
239
|
+
return value;
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
class Binding {
|
244
|
+
constructor(context, action) {
|
245
|
+
this.context = context;
|
246
|
+
this.action = action;
|
247
|
+
}
|
248
|
+
get index() {
|
249
|
+
return this.action.index;
|
250
|
+
}
|
251
|
+
get eventTarget() {
|
252
|
+
return this.action.eventTarget;
|
253
|
+
}
|
254
|
+
get eventOptions() {
|
255
|
+
return this.action.eventOptions;
|
256
|
+
}
|
257
|
+
get identifier() {
|
258
|
+
return this.context.identifier;
|
259
|
+
}
|
260
|
+
handleEvent(event) {
|
261
|
+
if (this.willBeInvokedByEvent(event)) {
|
262
|
+
this.invokeWithEvent(event);
|
263
|
+
}
|
264
|
+
}
|
265
|
+
get eventName() {
|
266
|
+
return this.action.eventName;
|
267
|
+
}
|
268
|
+
get method() {
|
269
|
+
const method = this.controller[this.methodName];
|
270
|
+
if (typeof method == "function") {
|
271
|
+
return method;
|
272
|
+
}
|
273
|
+
throw new Error(`Action "${this.action}" references undefined method "${this.methodName}"`);
|
274
|
+
}
|
275
|
+
invokeWithEvent(event) {
|
276
|
+
const { target, currentTarget } = event;
|
277
|
+
try {
|
278
|
+
const { params } = this.action;
|
279
|
+
const actionEvent = Object.assign(event, { params });
|
280
|
+
this.method.call(this.controller, actionEvent);
|
281
|
+
this.context.logDebugActivity(this.methodName, { event, target, currentTarget, action: this.methodName });
|
282
|
+
}
|
283
|
+
catch (error) {
|
284
|
+
const { identifier, controller, element, index } = this;
|
285
|
+
const detail = { identifier, controller, element, index, event };
|
286
|
+
this.context.handleError(error, `invoking action "${this.action}"`, detail);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
willBeInvokedByEvent(event) {
|
290
|
+
const eventTarget = event.target;
|
291
|
+
if (this.element === eventTarget) {
|
292
|
+
return true;
|
293
|
+
}
|
294
|
+
else if (eventTarget instanceof Element && this.element.contains(eventTarget)) {
|
295
|
+
return this.scope.containsElement(eventTarget);
|
296
|
+
}
|
297
|
+
else {
|
298
|
+
return this.scope.containsElement(this.action.element);
|
299
|
+
}
|
300
|
+
}
|
301
|
+
get controller() {
|
302
|
+
return this.context.controller;
|
303
|
+
}
|
304
|
+
get methodName() {
|
305
|
+
return this.action.methodName;
|
306
|
+
}
|
307
|
+
get element() {
|
308
|
+
return this.scope.element;
|
309
|
+
}
|
310
|
+
get scope() {
|
311
|
+
return this.context.scope;
|
312
|
+
}
|
313
|
+
}
|
314
|
+
|
315
|
+
class ElementObserver {
|
316
|
+
constructor(element, delegate) {
|
317
|
+
this.element = element;
|
318
|
+
this.started = false;
|
319
|
+
this.delegate = delegate;
|
320
|
+
this.elements = new Set;
|
321
|
+
this.mutationObserver = new MutationObserver((mutations) => this.processMutations(mutations));
|
322
|
+
}
|
323
|
+
start() {
|
324
|
+
if (!this.started) {
|
325
|
+
this.started = true;
|
326
|
+
this.mutationObserver.observe(this.element, { attributes: true, childList: true, subtree: true });
|
327
|
+
this.refresh();
|
328
|
+
}
|
329
|
+
}
|
330
|
+
stop() {
|
331
|
+
if (this.started) {
|
332
|
+
this.mutationObserver.takeRecords();
|
333
|
+
this.mutationObserver.disconnect();
|
334
|
+
this.started = false;
|
335
|
+
}
|
336
|
+
}
|
337
|
+
refresh() {
|
338
|
+
if (this.started) {
|
339
|
+
const matches = new Set(this.matchElementsInTree());
|
340
|
+
for (const element of Array.from(this.elements)) {
|
341
|
+
if (!matches.has(element)) {
|
342
|
+
this.removeElement(element);
|
343
|
+
}
|
344
|
+
}
|
345
|
+
for (const element of Array.from(matches)) {
|
346
|
+
this.addElement(element);
|
347
|
+
}
|
348
|
+
}
|
349
|
+
}
|
350
|
+
processMutations(mutations) {
|
351
|
+
if (this.started) {
|
352
|
+
for (const mutation of mutations) {
|
353
|
+
this.processMutation(mutation);
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}
|
357
|
+
processMutation(mutation) {
|
358
|
+
if (mutation.type == "attributes") {
|
359
|
+
this.processAttributeChange(mutation.target, mutation.attributeName);
|
360
|
+
}
|
361
|
+
else if (mutation.type == "childList") {
|
362
|
+
this.processRemovedNodes(mutation.removedNodes);
|
363
|
+
this.processAddedNodes(mutation.addedNodes);
|
364
|
+
}
|
365
|
+
}
|
366
|
+
processAttributeChange(node, attributeName) {
|
367
|
+
const element = node;
|
368
|
+
if (this.elements.has(element)) {
|
369
|
+
if (this.delegate.elementAttributeChanged && this.matchElement(element)) {
|
370
|
+
this.delegate.elementAttributeChanged(element, attributeName);
|
371
|
+
}
|
372
|
+
else {
|
373
|
+
this.removeElement(element);
|
374
|
+
}
|
375
|
+
}
|
376
|
+
else if (this.matchElement(element)) {
|
377
|
+
this.addElement(element);
|
378
|
+
}
|
379
|
+
}
|
380
|
+
processRemovedNodes(nodes) {
|
381
|
+
for (const node of Array.from(nodes)) {
|
382
|
+
const element = this.elementFromNode(node);
|
383
|
+
if (element) {
|
384
|
+
this.processTree(element, this.removeElement);
|
385
|
+
}
|
386
|
+
}
|
387
|
+
}
|
388
|
+
processAddedNodes(nodes) {
|
389
|
+
for (const node of Array.from(nodes)) {
|
390
|
+
const element = this.elementFromNode(node);
|
391
|
+
if (element && this.elementIsActive(element)) {
|
392
|
+
this.processTree(element, this.addElement);
|
393
|
+
}
|
394
|
+
}
|
395
|
+
}
|
396
|
+
matchElement(element) {
|
397
|
+
return this.delegate.matchElement(element);
|
398
|
+
}
|
399
|
+
matchElementsInTree(tree = this.element) {
|
400
|
+
return this.delegate.matchElementsInTree(tree);
|
401
|
+
}
|
402
|
+
processTree(tree, processor) {
|
403
|
+
for (const element of this.matchElementsInTree(tree)) {
|
404
|
+
processor.call(this, element);
|
405
|
+
}
|
406
|
+
}
|
407
|
+
elementFromNode(node) {
|
408
|
+
if (node.nodeType == Node.ELEMENT_NODE) {
|
409
|
+
return node;
|
410
|
+
}
|
411
|
+
}
|
412
|
+
elementIsActive(element) {
|
413
|
+
if (element.isConnected != this.element.isConnected) {
|
414
|
+
return false;
|
415
|
+
}
|
416
|
+
else {
|
417
|
+
return this.element.contains(element);
|
418
|
+
}
|
419
|
+
}
|
420
|
+
addElement(element) {
|
421
|
+
if (!this.elements.has(element)) {
|
422
|
+
if (this.elementIsActive(element)) {
|
423
|
+
this.elements.add(element);
|
424
|
+
if (this.delegate.elementMatched) {
|
425
|
+
this.delegate.elementMatched(element);
|
426
|
+
}
|
427
|
+
}
|
428
|
+
}
|
429
|
+
}
|
430
|
+
removeElement(element) {
|
431
|
+
if (this.elements.has(element)) {
|
432
|
+
this.elements.delete(element);
|
433
|
+
if (this.delegate.elementUnmatched) {
|
434
|
+
this.delegate.elementUnmatched(element);
|
435
|
+
}
|
436
|
+
}
|
437
|
+
}
|
438
|
+
}
|
439
|
+
|
440
|
+
class AttributeObserver {
|
441
|
+
constructor(element, attributeName, delegate) {
|
442
|
+
this.attributeName = attributeName;
|
443
|
+
this.delegate = delegate;
|
444
|
+
this.elementObserver = new ElementObserver(element, this);
|
445
|
+
}
|
446
|
+
get element() {
|
447
|
+
return this.elementObserver.element;
|
448
|
+
}
|
449
|
+
get selector() {
|
450
|
+
return `[${this.attributeName}]`;
|
451
|
+
}
|
452
|
+
start() {
|
453
|
+
this.elementObserver.start();
|
454
|
+
}
|
455
|
+
stop() {
|
456
|
+
this.elementObserver.stop();
|
457
|
+
}
|
458
|
+
refresh() {
|
459
|
+
this.elementObserver.refresh();
|
460
|
+
}
|
461
|
+
get started() {
|
462
|
+
return this.elementObserver.started;
|
463
|
+
}
|
464
|
+
matchElement(element) {
|
465
|
+
return element.hasAttribute(this.attributeName);
|
466
|
+
}
|
467
|
+
matchElementsInTree(tree) {
|
468
|
+
const match = this.matchElement(tree) ? [tree] : [];
|
469
|
+
const matches = Array.from(tree.querySelectorAll(this.selector));
|
470
|
+
return match.concat(matches);
|
471
|
+
}
|
472
|
+
elementMatched(element) {
|
473
|
+
if (this.delegate.elementMatchedAttribute) {
|
474
|
+
this.delegate.elementMatchedAttribute(element, this.attributeName);
|
475
|
+
}
|
476
|
+
}
|
477
|
+
elementUnmatched(element) {
|
478
|
+
if (this.delegate.elementUnmatchedAttribute) {
|
479
|
+
this.delegate.elementUnmatchedAttribute(element, this.attributeName);
|
480
|
+
}
|
481
|
+
}
|
482
|
+
elementAttributeChanged(element, attributeName) {
|
483
|
+
if (this.delegate.elementAttributeValueChanged && this.attributeName == attributeName) {
|
484
|
+
this.delegate.elementAttributeValueChanged(element, attributeName);
|
485
|
+
}
|
486
|
+
}
|
487
|
+
}
|
488
|
+
|
489
|
+
class StringMapObserver {
|
490
|
+
constructor(element, delegate) {
|
491
|
+
this.element = element;
|
492
|
+
this.delegate = delegate;
|
493
|
+
this.started = false;
|
494
|
+
this.stringMap = new Map;
|
495
|
+
this.mutationObserver = new MutationObserver(mutations => this.processMutations(mutations));
|
496
|
+
}
|
497
|
+
start() {
|
498
|
+
if (!this.started) {
|
499
|
+
this.started = true;
|
500
|
+
this.mutationObserver.observe(this.element, { attributes: true, attributeOldValue: true });
|
501
|
+
this.refresh();
|
502
|
+
}
|
503
|
+
}
|
504
|
+
stop() {
|
505
|
+
if (this.started) {
|
506
|
+
this.mutationObserver.takeRecords();
|
507
|
+
this.mutationObserver.disconnect();
|
508
|
+
this.started = false;
|
509
|
+
}
|
510
|
+
}
|
511
|
+
refresh() {
|
512
|
+
if (this.started) {
|
513
|
+
for (const attributeName of this.knownAttributeNames) {
|
514
|
+
this.refreshAttribute(attributeName, null);
|
515
|
+
}
|
516
|
+
}
|
517
|
+
}
|
518
|
+
processMutations(mutations) {
|
519
|
+
if (this.started) {
|
520
|
+
for (const mutation of mutations) {
|
521
|
+
this.processMutation(mutation);
|
522
|
+
}
|
523
|
+
}
|
524
|
+
}
|
525
|
+
processMutation(mutation) {
|
526
|
+
const attributeName = mutation.attributeName;
|
527
|
+
if (attributeName) {
|
528
|
+
this.refreshAttribute(attributeName, mutation.oldValue);
|
529
|
+
}
|
530
|
+
}
|
531
|
+
refreshAttribute(attributeName, oldValue) {
|
532
|
+
const key = this.delegate.getStringMapKeyForAttribute(attributeName);
|
533
|
+
if (key != null) {
|
534
|
+
if (!this.stringMap.has(attributeName)) {
|
535
|
+
this.stringMapKeyAdded(key, attributeName);
|
536
|
+
}
|
537
|
+
const value = this.element.getAttribute(attributeName);
|
538
|
+
if (this.stringMap.get(attributeName) != value) {
|
539
|
+
this.stringMapValueChanged(value, key, oldValue);
|
540
|
+
}
|
541
|
+
if (value == null) {
|
542
|
+
const oldValue = this.stringMap.get(attributeName);
|
543
|
+
this.stringMap.delete(attributeName);
|
544
|
+
if (oldValue)
|
545
|
+
this.stringMapKeyRemoved(key, attributeName, oldValue);
|
546
|
+
}
|
547
|
+
else {
|
548
|
+
this.stringMap.set(attributeName, value);
|
549
|
+
}
|
550
|
+
}
|
551
|
+
}
|
552
|
+
stringMapKeyAdded(key, attributeName) {
|
553
|
+
if (this.delegate.stringMapKeyAdded) {
|
554
|
+
this.delegate.stringMapKeyAdded(key, attributeName);
|
555
|
+
}
|
556
|
+
}
|
557
|
+
stringMapValueChanged(value, key, oldValue) {
|
558
|
+
if (this.delegate.stringMapValueChanged) {
|
559
|
+
this.delegate.stringMapValueChanged(value, key, oldValue);
|
560
|
+
}
|
561
|
+
}
|
562
|
+
stringMapKeyRemoved(key, attributeName, oldValue) {
|
563
|
+
if (this.delegate.stringMapKeyRemoved) {
|
564
|
+
this.delegate.stringMapKeyRemoved(key, attributeName, oldValue);
|
565
|
+
}
|
566
|
+
}
|
567
|
+
get knownAttributeNames() {
|
568
|
+
return Array.from(new Set(this.currentAttributeNames.concat(this.recordedAttributeNames)));
|
569
|
+
}
|
570
|
+
get currentAttributeNames() {
|
571
|
+
return Array.from(this.element.attributes).map(attribute => attribute.name);
|
572
|
+
}
|
573
|
+
get recordedAttributeNames() {
|
574
|
+
return Array.from(this.stringMap.keys());
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
function add(map, key, value) {
|
579
|
+
fetch(map, key).add(value);
|
580
|
+
}
|
581
|
+
function del(map, key, value) {
|
582
|
+
fetch(map, key).delete(value);
|
583
|
+
prune(map, key);
|
584
|
+
}
|
585
|
+
function fetch(map, key) {
|
586
|
+
let values = map.get(key);
|
587
|
+
if (!values) {
|
588
|
+
values = new Set();
|
589
|
+
map.set(key, values);
|
590
|
+
}
|
591
|
+
return values;
|
592
|
+
}
|
593
|
+
function prune(map, key) {
|
594
|
+
const values = map.get(key);
|
595
|
+
if (values != null && values.size == 0) {
|
596
|
+
map.delete(key);
|
597
|
+
}
|
598
|
+
}
|
599
|
+
|
600
|
+
class Multimap {
|
601
|
+
constructor() {
|
602
|
+
this.valuesByKey = new Map();
|
603
|
+
}
|
604
|
+
get keys() {
|
605
|
+
return Array.from(this.valuesByKey.keys());
|
606
|
+
}
|
607
|
+
get values() {
|
608
|
+
const sets = Array.from(this.valuesByKey.values());
|
609
|
+
return sets.reduce((values, set) => values.concat(Array.from(set)), []);
|
610
|
+
}
|
611
|
+
get size() {
|
612
|
+
const sets = Array.from(this.valuesByKey.values());
|
613
|
+
return sets.reduce((size, set) => size + set.size, 0);
|
614
|
+
}
|
615
|
+
add(key, value) {
|
616
|
+
add(this.valuesByKey, key, value);
|
617
|
+
}
|
618
|
+
delete(key, value) {
|
619
|
+
del(this.valuesByKey, key, value);
|
620
|
+
}
|
621
|
+
has(key, value) {
|
622
|
+
const values = this.valuesByKey.get(key);
|
623
|
+
return values != null && values.has(value);
|
624
|
+
}
|
625
|
+
hasKey(key) {
|
626
|
+
return this.valuesByKey.has(key);
|
627
|
+
}
|
628
|
+
hasValue(value) {
|
629
|
+
const sets = Array.from(this.valuesByKey.values());
|
630
|
+
return sets.some(set => set.has(value));
|
631
|
+
}
|
632
|
+
getValuesForKey(key) {
|
633
|
+
const values = this.valuesByKey.get(key);
|
634
|
+
return values ? Array.from(values) : [];
|
635
|
+
}
|
636
|
+
getKeysForValue(value) {
|
637
|
+
return Array.from(this.valuesByKey)
|
638
|
+
.filter(([key, values]) => values.has(value))
|
639
|
+
.map(([key, values]) => key);
|
640
|
+
}
|
641
|
+
}
|
642
|
+
|
643
|
+
class IndexedMultimap extends Multimap {
|
644
|
+
constructor() {
|
645
|
+
super();
|
646
|
+
this.keysByValue = new Map;
|
647
|
+
}
|
648
|
+
get values() {
|
649
|
+
return Array.from(this.keysByValue.keys());
|
650
|
+
}
|
651
|
+
add(key, value) {
|
652
|
+
super.add(key, value);
|
653
|
+
add(this.keysByValue, value, key);
|
654
|
+
}
|
655
|
+
delete(key, value) {
|
656
|
+
super.delete(key, value);
|
657
|
+
del(this.keysByValue, value, key);
|
658
|
+
}
|
659
|
+
hasValue(value) {
|
660
|
+
return this.keysByValue.has(value);
|
661
|
+
}
|
662
|
+
getKeysForValue(value) {
|
663
|
+
const set = this.keysByValue.get(value);
|
664
|
+
return set ? Array.from(set) : [];
|
665
|
+
}
|
666
|
+
}
|
667
|
+
|
668
|
+
class TokenListObserver {
|
669
|
+
constructor(element, attributeName, delegate) {
|
670
|
+
this.attributeObserver = new AttributeObserver(element, attributeName, this);
|
671
|
+
this.delegate = delegate;
|
672
|
+
this.tokensByElement = new Multimap;
|
673
|
+
}
|
674
|
+
get started() {
|
675
|
+
return this.attributeObserver.started;
|
676
|
+
}
|
677
|
+
start() {
|
678
|
+
this.attributeObserver.start();
|
679
|
+
}
|
680
|
+
stop() {
|
681
|
+
this.attributeObserver.stop();
|
682
|
+
}
|
683
|
+
refresh() {
|
684
|
+
this.attributeObserver.refresh();
|
685
|
+
}
|
686
|
+
get element() {
|
687
|
+
return this.attributeObserver.element;
|
688
|
+
}
|
689
|
+
get attributeName() {
|
690
|
+
return this.attributeObserver.attributeName;
|
691
|
+
}
|
692
|
+
elementMatchedAttribute(element) {
|
693
|
+
this.tokensMatched(this.readTokensForElement(element));
|
694
|
+
}
|
695
|
+
elementAttributeValueChanged(element) {
|
696
|
+
const [unmatchedTokens, matchedTokens] = this.refreshTokensForElement(element);
|
697
|
+
this.tokensUnmatched(unmatchedTokens);
|
698
|
+
this.tokensMatched(matchedTokens);
|
699
|
+
}
|
700
|
+
elementUnmatchedAttribute(element) {
|
701
|
+
this.tokensUnmatched(this.tokensByElement.getValuesForKey(element));
|
702
|
+
}
|
703
|
+
tokensMatched(tokens) {
|
704
|
+
tokens.forEach(token => this.tokenMatched(token));
|
705
|
+
}
|
706
|
+
tokensUnmatched(tokens) {
|
707
|
+
tokens.forEach(token => this.tokenUnmatched(token));
|
708
|
+
}
|
709
|
+
tokenMatched(token) {
|
710
|
+
this.delegate.tokenMatched(token);
|
711
|
+
this.tokensByElement.add(token.element, token);
|
712
|
+
}
|
713
|
+
tokenUnmatched(token) {
|
714
|
+
this.delegate.tokenUnmatched(token);
|
715
|
+
this.tokensByElement.delete(token.element, token);
|
716
|
+
}
|
717
|
+
refreshTokensForElement(element) {
|
718
|
+
const previousTokens = this.tokensByElement.getValuesForKey(element);
|
719
|
+
const currentTokens = this.readTokensForElement(element);
|
720
|
+
const firstDifferingIndex = zip(previousTokens, currentTokens)
|
721
|
+
.findIndex(([previousToken, currentToken]) => !tokensAreEqual(previousToken, currentToken));
|
722
|
+
if (firstDifferingIndex == -1) {
|
723
|
+
return [[], []];
|
724
|
+
}
|
725
|
+
else {
|
726
|
+
return [previousTokens.slice(firstDifferingIndex), currentTokens.slice(firstDifferingIndex)];
|
727
|
+
}
|
728
|
+
}
|
729
|
+
readTokensForElement(element) {
|
730
|
+
const attributeName = this.attributeName;
|
731
|
+
const tokenString = element.getAttribute(attributeName) || "";
|
732
|
+
return parseTokenString(tokenString, element, attributeName);
|
733
|
+
}
|
734
|
+
}
|
735
|
+
function parseTokenString(tokenString, element, attributeName) {
|
736
|
+
return tokenString.trim().split(/\s+/).filter(content => content.length)
|
737
|
+
.map((content, index) => ({ element, attributeName, content, index }));
|
738
|
+
}
|
739
|
+
function zip(left, right) {
|
740
|
+
const length = Math.max(left.length, right.length);
|
741
|
+
return Array.from({ length }, (_, index) => [left[index], right[index]]);
|
742
|
+
}
|
743
|
+
function tokensAreEqual(left, right) {
|
744
|
+
return left && right && left.index == right.index && left.content == right.content;
|
745
|
+
}
|
746
|
+
|
747
|
+
class ValueListObserver {
|
748
|
+
constructor(element, attributeName, delegate) {
|
749
|
+
this.tokenListObserver = new TokenListObserver(element, attributeName, this);
|
750
|
+
this.delegate = delegate;
|
751
|
+
this.parseResultsByToken = new WeakMap;
|
752
|
+
this.valuesByTokenByElement = new WeakMap;
|
753
|
+
}
|
754
|
+
get started() {
|
755
|
+
return this.tokenListObserver.started;
|
756
|
+
}
|
757
|
+
start() {
|
758
|
+
this.tokenListObserver.start();
|
759
|
+
}
|
760
|
+
stop() {
|
761
|
+
this.tokenListObserver.stop();
|
762
|
+
}
|
763
|
+
refresh() {
|
764
|
+
this.tokenListObserver.refresh();
|
765
|
+
}
|
766
|
+
get element() {
|
767
|
+
return this.tokenListObserver.element;
|
768
|
+
}
|
769
|
+
get attributeName() {
|
770
|
+
return this.tokenListObserver.attributeName;
|
771
|
+
}
|
772
|
+
tokenMatched(token) {
|
773
|
+
const { element } = token;
|
774
|
+
const { value } = this.fetchParseResultForToken(token);
|
775
|
+
if (value) {
|
776
|
+
this.fetchValuesByTokenForElement(element).set(token, value);
|
777
|
+
this.delegate.elementMatchedValue(element, value);
|
778
|
+
}
|
779
|
+
}
|
780
|
+
tokenUnmatched(token) {
|
781
|
+
const { element } = token;
|
782
|
+
const { value } = this.fetchParseResultForToken(token);
|
783
|
+
if (value) {
|
784
|
+
this.fetchValuesByTokenForElement(element).delete(token);
|
785
|
+
this.delegate.elementUnmatchedValue(element, value);
|
786
|
+
}
|
787
|
+
}
|
788
|
+
fetchParseResultForToken(token) {
|
789
|
+
let parseResult = this.parseResultsByToken.get(token);
|
790
|
+
if (!parseResult) {
|
791
|
+
parseResult = this.parseToken(token);
|
792
|
+
this.parseResultsByToken.set(token, parseResult);
|
793
|
+
}
|
794
|
+
return parseResult;
|
795
|
+
}
|
796
|
+
fetchValuesByTokenForElement(element) {
|
797
|
+
let valuesByToken = this.valuesByTokenByElement.get(element);
|
798
|
+
if (!valuesByToken) {
|
799
|
+
valuesByToken = new Map;
|
800
|
+
this.valuesByTokenByElement.set(element, valuesByToken);
|
801
|
+
}
|
802
|
+
return valuesByToken;
|
803
|
+
}
|
804
|
+
parseToken(token) {
|
805
|
+
try {
|
806
|
+
const value = this.delegate.parseValueForToken(token);
|
807
|
+
return { value };
|
808
|
+
}
|
809
|
+
catch (error) {
|
810
|
+
return { error };
|
811
|
+
}
|
812
|
+
}
|
813
|
+
}
|
814
|
+
|
815
|
+
class BindingObserver {
|
816
|
+
constructor(context, delegate) {
|
817
|
+
this.context = context;
|
818
|
+
this.delegate = delegate;
|
819
|
+
this.bindingsByAction = new Map;
|
820
|
+
}
|
821
|
+
start() {
|
822
|
+
if (!this.valueListObserver) {
|
823
|
+
this.valueListObserver = new ValueListObserver(this.element, this.actionAttribute, this);
|
824
|
+
this.valueListObserver.start();
|
825
|
+
}
|
826
|
+
}
|
827
|
+
stop() {
|
828
|
+
if (this.valueListObserver) {
|
829
|
+
this.valueListObserver.stop();
|
830
|
+
delete this.valueListObserver;
|
831
|
+
this.disconnectAllActions();
|
832
|
+
}
|
833
|
+
}
|
834
|
+
get element() {
|
835
|
+
return this.context.element;
|
836
|
+
}
|
837
|
+
get identifier() {
|
838
|
+
return this.context.identifier;
|
839
|
+
}
|
840
|
+
get actionAttribute() {
|
841
|
+
return this.schema.actionAttribute;
|
842
|
+
}
|
843
|
+
get schema() {
|
844
|
+
return this.context.schema;
|
845
|
+
}
|
846
|
+
get bindings() {
|
847
|
+
return Array.from(this.bindingsByAction.values());
|
848
|
+
}
|
849
|
+
connectAction(action) {
|
850
|
+
const binding = new Binding(this.context, action);
|
851
|
+
this.bindingsByAction.set(action, binding);
|
852
|
+
this.delegate.bindingConnected(binding);
|
853
|
+
}
|
854
|
+
disconnectAction(action) {
|
855
|
+
const binding = this.bindingsByAction.get(action);
|
856
|
+
if (binding) {
|
857
|
+
this.bindingsByAction.delete(action);
|
858
|
+
this.delegate.bindingDisconnected(binding);
|
859
|
+
}
|
860
|
+
}
|
861
|
+
disconnectAllActions() {
|
862
|
+
this.bindings.forEach(binding => this.delegate.bindingDisconnected(binding));
|
863
|
+
this.bindingsByAction.clear();
|
864
|
+
}
|
865
|
+
parseValueForToken(token) {
|
866
|
+
const action = Action.forToken(token);
|
867
|
+
if (action.identifier == this.identifier) {
|
868
|
+
return action;
|
869
|
+
}
|
870
|
+
}
|
871
|
+
elementMatchedValue(element, action) {
|
872
|
+
this.connectAction(action);
|
873
|
+
}
|
874
|
+
elementUnmatchedValue(element, action) {
|
875
|
+
this.disconnectAction(action);
|
876
|
+
}
|
877
|
+
}
|
878
|
+
|
879
|
+
class ValueObserver {
|
880
|
+
constructor(context, receiver) {
|
881
|
+
this.context = context;
|
882
|
+
this.receiver = receiver;
|
883
|
+
this.stringMapObserver = new StringMapObserver(this.element, this);
|
884
|
+
this.valueDescriptorMap = this.controller.valueDescriptorMap;
|
885
|
+
this.invokeChangedCallbacksForDefaultValues();
|
886
|
+
}
|
887
|
+
start() {
|
888
|
+
this.stringMapObserver.start();
|
889
|
+
}
|
890
|
+
stop() {
|
891
|
+
this.stringMapObserver.stop();
|
892
|
+
}
|
893
|
+
get element() {
|
894
|
+
return this.context.element;
|
895
|
+
}
|
896
|
+
get controller() {
|
897
|
+
return this.context.controller;
|
898
|
+
}
|
899
|
+
getStringMapKeyForAttribute(attributeName) {
|
900
|
+
if (attributeName in this.valueDescriptorMap) {
|
901
|
+
return this.valueDescriptorMap[attributeName].name;
|
902
|
+
}
|
903
|
+
}
|
904
|
+
stringMapKeyAdded(key, attributeName) {
|
905
|
+
const descriptor = this.valueDescriptorMap[attributeName];
|
906
|
+
if (!this.hasValue(key)) {
|
907
|
+
this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), descriptor.writer(descriptor.defaultValue));
|
908
|
+
}
|
909
|
+
}
|
910
|
+
stringMapValueChanged(value, name, oldValue) {
|
911
|
+
const descriptor = this.valueDescriptorNameMap[name];
|
912
|
+
if (value === null)
|
913
|
+
return;
|
914
|
+
if (oldValue === null) {
|
915
|
+
oldValue = descriptor.writer(descriptor.defaultValue);
|
916
|
+
}
|
917
|
+
this.invokeChangedCallback(name, value, oldValue);
|
918
|
+
}
|
919
|
+
stringMapKeyRemoved(key, attributeName, oldValue) {
|
920
|
+
const descriptor = this.valueDescriptorNameMap[key];
|
921
|
+
if (this.hasValue(key)) {
|
922
|
+
this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), oldValue);
|
923
|
+
}
|
924
|
+
else {
|
925
|
+
this.invokeChangedCallback(key, descriptor.writer(descriptor.defaultValue), oldValue);
|
926
|
+
}
|
927
|
+
}
|
928
|
+
invokeChangedCallbacksForDefaultValues() {
|
929
|
+
for (const { key, name, defaultValue, writer } of this.valueDescriptors) {
|
930
|
+
if (defaultValue != undefined && !this.controller.data.has(key)) {
|
931
|
+
this.invokeChangedCallback(name, writer(defaultValue), undefined);
|
932
|
+
}
|
933
|
+
}
|
934
|
+
}
|
935
|
+
invokeChangedCallback(name, rawValue, rawOldValue) {
|
936
|
+
const changedMethodName = `${name}Changed`;
|
937
|
+
const changedMethod = this.receiver[changedMethodName];
|
938
|
+
if (typeof changedMethod == "function") {
|
939
|
+
const descriptor = this.valueDescriptorNameMap[name];
|
940
|
+
const value = descriptor.reader(rawValue);
|
941
|
+
let oldValue = rawOldValue;
|
942
|
+
if (rawOldValue) {
|
943
|
+
oldValue = descriptor.reader(rawOldValue);
|
944
|
+
}
|
945
|
+
changedMethod.call(this.receiver, value, oldValue);
|
946
|
+
}
|
947
|
+
}
|
948
|
+
get valueDescriptors() {
|
949
|
+
const { valueDescriptorMap } = this;
|
950
|
+
return Object.keys(valueDescriptorMap).map(key => valueDescriptorMap[key]);
|
951
|
+
}
|
952
|
+
get valueDescriptorNameMap() {
|
953
|
+
const descriptors = {};
|
954
|
+
Object.keys(this.valueDescriptorMap).forEach(key => {
|
955
|
+
const descriptor = this.valueDescriptorMap[key];
|
956
|
+
descriptors[descriptor.name] = descriptor;
|
957
|
+
});
|
958
|
+
return descriptors;
|
959
|
+
}
|
960
|
+
hasValue(attributeName) {
|
961
|
+
const descriptor = this.valueDescriptorNameMap[attributeName];
|
962
|
+
const hasMethodName = `has${capitalize(descriptor.name)}`;
|
963
|
+
return this.receiver[hasMethodName];
|
964
|
+
}
|
965
|
+
}
|
966
|
+
|
967
|
+
class TargetObserver {
|
968
|
+
constructor(context, delegate) {
|
969
|
+
this.context = context;
|
970
|
+
this.delegate = delegate;
|
971
|
+
this.targetsByName = new Multimap;
|
972
|
+
}
|
973
|
+
start() {
|
974
|
+
if (!this.tokenListObserver) {
|
975
|
+
this.tokenListObserver = new TokenListObserver(this.element, this.attributeName, this);
|
976
|
+
this.tokenListObserver.start();
|
977
|
+
}
|
978
|
+
}
|
979
|
+
stop() {
|
980
|
+
if (this.tokenListObserver) {
|
981
|
+
this.disconnectAllTargets();
|
982
|
+
this.tokenListObserver.stop();
|
983
|
+
delete this.tokenListObserver;
|
984
|
+
}
|
985
|
+
}
|
986
|
+
tokenMatched({ element, content: name }) {
|
987
|
+
if (this.scope.containsElement(element)) {
|
988
|
+
this.connectTarget(element, name);
|
989
|
+
}
|
990
|
+
}
|
991
|
+
tokenUnmatched({ element, content: name }) {
|
992
|
+
this.disconnectTarget(element, name);
|
993
|
+
}
|
994
|
+
connectTarget(element, name) {
|
995
|
+
if (!this.targetsByName.has(name, element)) {
|
996
|
+
this.targetsByName.add(name, element);
|
997
|
+
this.delegate.targetConnected(element, name);
|
998
|
+
}
|
999
|
+
}
|
1000
|
+
disconnectTarget(element, name) {
|
1001
|
+
if (this.targetsByName.has(name, element)) {
|
1002
|
+
this.targetsByName.delete(name, element);
|
1003
|
+
this.delegate.targetDisconnected(element, name);
|
1004
|
+
}
|
1005
|
+
}
|
1006
|
+
disconnectAllTargets() {
|
1007
|
+
for (const name of this.targetsByName.keys) {
|
1008
|
+
for (const element of this.targetsByName.getValuesForKey(name)) {
|
1009
|
+
this.disconnectTarget(element, name);
|
1010
|
+
}
|
1011
|
+
}
|
1012
|
+
}
|
1013
|
+
get attributeName() {
|
1014
|
+
return `data-${this.context.identifier}-target`;
|
1015
|
+
}
|
1016
|
+
get element() {
|
1017
|
+
return this.context.element;
|
1018
|
+
}
|
1019
|
+
get scope() {
|
1020
|
+
return this.context.scope;
|
1021
|
+
}
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
class Context {
|
1025
|
+
constructor(module, scope) {
|
1026
|
+
this.logDebugActivity = (functionName, detail = {}) => {
|
1027
|
+
const { identifier, controller, element } = this;
|
1028
|
+
detail = Object.assign({ identifier, controller, element }, detail);
|
1029
|
+
this.application.logDebugActivity(this.identifier, functionName, detail);
|
1030
|
+
};
|
1031
|
+
this.module = module;
|
1032
|
+
this.scope = scope;
|
1033
|
+
this.controller = new module.controllerConstructor(this);
|
1034
|
+
this.bindingObserver = new BindingObserver(this, this.dispatcher);
|
1035
|
+
this.valueObserver = new ValueObserver(this, this.controller);
|
1036
|
+
this.targetObserver = new TargetObserver(this, this);
|
1037
|
+
try {
|
1038
|
+
this.controller.initialize();
|
1039
|
+
this.logDebugActivity("initialize");
|
1040
|
+
}
|
1041
|
+
catch (error) {
|
1042
|
+
this.handleError(error, "initializing controller");
|
1043
|
+
}
|
1044
|
+
}
|
1045
|
+
connect() {
|
1046
|
+
this.bindingObserver.start();
|
1047
|
+
this.valueObserver.start();
|
1048
|
+
this.targetObserver.start();
|
1049
|
+
try {
|
1050
|
+
this.controller.connect();
|
1051
|
+
this.logDebugActivity("connect");
|
1052
|
+
}
|
1053
|
+
catch (error) {
|
1054
|
+
this.handleError(error, "connecting controller");
|
1055
|
+
}
|
1056
|
+
}
|
1057
|
+
disconnect() {
|
1058
|
+
try {
|
1059
|
+
this.controller.disconnect();
|
1060
|
+
this.logDebugActivity("disconnect");
|
1061
|
+
}
|
1062
|
+
catch (error) {
|
1063
|
+
this.handleError(error, "disconnecting controller");
|
1064
|
+
}
|
1065
|
+
this.targetObserver.stop();
|
1066
|
+
this.valueObserver.stop();
|
1067
|
+
this.bindingObserver.stop();
|
1068
|
+
}
|
1069
|
+
get application() {
|
1070
|
+
return this.module.application;
|
1071
|
+
}
|
1072
|
+
get identifier() {
|
1073
|
+
return this.module.identifier;
|
1074
|
+
}
|
1075
|
+
get schema() {
|
1076
|
+
return this.application.schema;
|
1077
|
+
}
|
1078
|
+
get dispatcher() {
|
1079
|
+
return this.application.dispatcher;
|
1080
|
+
}
|
1081
|
+
get element() {
|
1082
|
+
return this.scope.element;
|
1083
|
+
}
|
1084
|
+
get parentElement() {
|
1085
|
+
return this.element.parentElement;
|
1086
|
+
}
|
1087
|
+
handleError(error, message, detail = {}) {
|
1088
|
+
const { identifier, controller, element } = this;
|
1089
|
+
detail = Object.assign({ identifier, controller, element }, detail);
|
1090
|
+
this.application.handleError(error, `Error ${message}`, detail);
|
1091
|
+
}
|
1092
|
+
targetConnected(element, name) {
|
1093
|
+
this.invokeControllerMethod(`${name}TargetConnected`, element);
|
1094
|
+
}
|
1095
|
+
targetDisconnected(element, name) {
|
1096
|
+
this.invokeControllerMethod(`${name}TargetDisconnected`, element);
|
1097
|
+
}
|
1098
|
+
invokeControllerMethod(methodName, ...args) {
|
1099
|
+
const controller = this.controller;
|
1100
|
+
if (typeof controller[methodName] == "function") {
|
1101
|
+
controller[methodName](...args);
|
1102
|
+
}
|
1103
|
+
}
|
1104
|
+
}
|
1105
|
+
|
1106
|
+
function readInheritableStaticArrayValues(constructor, propertyName) {
|
1107
|
+
const ancestors = getAncestorsForConstructor(constructor);
|
1108
|
+
return Array.from(ancestors.reduce((values, constructor) => {
|
1109
|
+
getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name));
|
1110
|
+
return values;
|
1111
|
+
}, new Set));
|
1112
|
+
}
|
1113
|
+
function readInheritableStaticObjectPairs(constructor, propertyName) {
|
1114
|
+
const ancestors = getAncestorsForConstructor(constructor);
|
1115
|
+
return ancestors.reduce((pairs, constructor) => {
|
1116
|
+
pairs.push(...getOwnStaticObjectPairs(constructor, propertyName));
|
1117
|
+
return pairs;
|
1118
|
+
}, []);
|
1119
|
+
}
|
1120
|
+
function getAncestorsForConstructor(constructor) {
|
1121
|
+
const ancestors = [];
|
1122
|
+
while (constructor) {
|
1123
|
+
ancestors.push(constructor);
|
1124
|
+
constructor = Object.getPrototypeOf(constructor);
|
1125
|
+
}
|
1126
|
+
return ancestors.reverse();
|
1127
|
+
}
|
1128
|
+
function getOwnStaticArrayValues(constructor, propertyName) {
|
1129
|
+
const definition = constructor[propertyName];
|
1130
|
+
return Array.isArray(definition) ? definition : [];
|
1131
|
+
}
|
1132
|
+
function getOwnStaticObjectPairs(constructor, propertyName) {
|
1133
|
+
const definition = constructor[propertyName];
|
1134
|
+
return definition ? Object.keys(definition).map(key => [key, definition[key]]) : [];
|
1135
|
+
}
|
1136
|
+
|
1137
|
+
function bless(constructor) {
|
1138
|
+
return shadow(constructor, getBlessedProperties(constructor));
|
1139
|
+
}
|
1140
|
+
function shadow(constructor, properties) {
|
1141
|
+
const shadowConstructor = extend(constructor);
|
1142
|
+
const shadowProperties = getShadowProperties(constructor.prototype, properties);
|
1143
|
+
Object.defineProperties(shadowConstructor.prototype, shadowProperties);
|
1144
|
+
return shadowConstructor;
|
1145
|
+
}
|
1146
|
+
function getBlessedProperties(constructor) {
|
1147
|
+
const blessings = readInheritableStaticArrayValues(constructor, "blessings");
|
1148
|
+
return blessings.reduce((blessedProperties, blessing) => {
|
1149
|
+
const properties = blessing(constructor);
|
1150
|
+
for (const key in properties) {
|
1151
|
+
const descriptor = blessedProperties[key] || {};
|
1152
|
+
blessedProperties[key] = Object.assign(descriptor, properties[key]);
|
1153
|
+
}
|
1154
|
+
return blessedProperties;
|
1155
|
+
}, {});
|
1156
|
+
}
|
1157
|
+
function getShadowProperties(prototype, properties) {
|
1158
|
+
return getOwnKeys(properties).reduce((shadowProperties, key) => {
|
1159
|
+
const descriptor = getShadowedDescriptor(prototype, properties, key);
|
1160
|
+
if (descriptor) {
|
1161
|
+
Object.assign(shadowProperties, { [key]: descriptor });
|
1162
|
+
}
|
1163
|
+
return shadowProperties;
|
1164
|
+
}, {});
|
1165
|
+
}
|
1166
|
+
function getShadowedDescriptor(prototype, properties, key) {
|
1167
|
+
const shadowingDescriptor = Object.getOwnPropertyDescriptor(prototype, key);
|
1168
|
+
const shadowedByValue = shadowingDescriptor && "value" in shadowingDescriptor;
|
1169
|
+
if (!shadowedByValue) {
|
1170
|
+
const descriptor = Object.getOwnPropertyDescriptor(properties, key).value;
|
1171
|
+
if (shadowingDescriptor) {
|
1172
|
+
descriptor.get = shadowingDescriptor.get || descriptor.get;
|
1173
|
+
descriptor.set = shadowingDescriptor.set || descriptor.set;
|
1174
|
+
}
|
1175
|
+
return descriptor;
|
1176
|
+
}
|
1177
|
+
}
|
1178
|
+
const getOwnKeys = (() => {
|
1179
|
+
if (typeof Object.getOwnPropertySymbols == "function") {
|
1180
|
+
return (object) => [
|
1181
|
+
...Object.getOwnPropertyNames(object),
|
1182
|
+
...Object.getOwnPropertySymbols(object)
|
1183
|
+
];
|
1184
|
+
}
|
1185
|
+
else {
|
1186
|
+
return Object.getOwnPropertyNames;
|
1187
|
+
}
|
1188
|
+
})();
|
1189
|
+
const extend = (() => {
|
1190
|
+
function extendWithReflect(constructor) {
|
1191
|
+
function extended() {
|
1192
|
+
return Reflect.construct(constructor, arguments, new.target);
|
1193
|
+
}
|
1194
|
+
extended.prototype = Object.create(constructor.prototype, {
|
1195
|
+
constructor: { value: extended }
|
1196
|
+
});
|
1197
|
+
Reflect.setPrototypeOf(extended, constructor);
|
1198
|
+
return extended;
|
1199
|
+
}
|
1200
|
+
function testReflectExtension() {
|
1201
|
+
const a = function () { this.a.call(this); };
|
1202
|
+
const b = extendWithReflect(a);
|
1203
|
+
b.prototype.a = function () { };
|
1204
|
+
return new b;
|
1205
|
+
}
|
1206
|
+
try {
|
1207
|
+
testReflectExtension();
|
1208
|
+
return extendWithReflect;
|
1209
|
+
}
|
1210
|
+
catch (error) {
|
1211
|
+
return (constructor) => class extended extends constructor {
|
1212
|
+
};
|
1213
|
+
}
|
1214
|
+
})();
|
1215
|
+
|
1216
|
+
function blessDefinition(definition) {
|
1217
|
+
return {
|
1218
|
+
identifier: definition.identifier,
|
1219
|
+
controllerConstructor: bless(definition.controllerConstructor)
|
1220
|
+
};
|
1221
|
+
}
|
1222
|
+
|
1223
|
+
class Module {
|
1224
|
+
constructor(application, definition) {
|
1225
|
+
this.application = application;
|
1226
|
+
this.definition = blessDefinition(definition);
|
1227
|
+
this.contextsByScope = new WeakMap;
|
1228
|
+
this.connectedContexts = new Set;
|
1229
|
+
}
|
1230
|
+
get identifier() {
|
1231
|
+
return this.definition.identifier;
|
1232
|
+
}
|
1233
|
+
get controllerConstructor() {
|
1234
|
+
return this.definition.controllerConstructor;
|
1235
|
+
}
|
1236
|
+
get contexts() {
|
1237
|
+
return Array.from(this.connectedContexts);
|
1238
|
+
}
|
1239
|
+
connectContextForScope(scope) {
|
1240
|
+
const context = this.fetchContextForScope(scope);
|
1241
|
+
this.connectedContexts.add(context);
|
1242
|
+
context.connect();
|
1243
|
+
}
|
1244
|
+
disconnectContextForScope(scope) {
|
1245
|
+
const context = this.contextsByScope.get(scope);
|
1246
|
+
if (context) {
|
1247
|
+
this.connectedContexts.delete(context);
|
1248
|
+
context.disconnect();
|
1249
|
+
}
|
1250
|
+
}
|
1251
|
+
fetchContextForScope(scope) {
|
1252
|
+
let context = this.contextsByScope.get(scope);
|
1253
|
+
if (!context) {
|
1254
|
+
context = new Context(this, scope);
|
1255
|
+
this.contextsByScope.set(scope, context);
|
1256
|
+
}
|
1257
|
+
return context;
|
1258
|
+
}
|
1259
|
+
}
|
1260
|
+
|
1261
|
+
class ClassMap {
|
1262
|
+
constructor(scope) {
|
1263
|
+
this.scope = scope;
|
1264
|
+
}
|
1265
|
+
has(name) {
|
1266
|
+
return this.data.has(this.getDataKey(name));
|
1267
|
+
}
|
1268
|
+
get(name) {
|
1269
|
+
return this.getAll(name)[0];
|
1270
|
+
}
|
1271
|
+
getAll(name) {
|
1272
|
+
const tokenString = this.data.get(this.getDataKey(name)) || "";
|
1273
|
+
return tokenize(tokenString);
|
1274
|
+
}
|
1275
|
+
getAttributeName(name) {
|
1276
|
+
return this.data.getAttributeNameForKey(this.getDataKey(name));
|
1277
|
+
}
|
1278
|
+
getDataKey(name) {
|
1279
|
+
return `${name}-class`;
|
1280
|
+
}
|
1281
|
+
get data() {
|
1282
|
+
return this.scope.data;
|
1283
|
+
}
|
1284
|
+
}
|
1285
|
+
|
1286
|
+
class DataMap {
|
1287
|
+
constructor(scope) {
|
1288
|
+
this.scope = scope;
|
1289
|
+
}
|
1290
|
+
get element() {
|
1291
|
+
return this.scope.element;
|
1292
|
+
}
|
1293
|
+
get identifier() {
|
1294
|
+
return this.scope.identifier;
|
1295
|
+
}
|
1296
|
+
get(key) {
|
1297
|
+
const name = this.getAttributeNameForKey(key);
|
1298
|
+
return this.element.getAttribute(name);
|
1299
|
+
}
|
1300
|
+
set(key, value) {
|
1301
|
+
const name = this.getAttributeNameForKey(key);
|
1302
|
+
this.element.setAttribute(name, value);
|
1303
|
+
return this.get(key);
|
1304
|
+
}
|
1305
|
+
has(key) {
|
1306
|
+
const name = this.getAttributeNameForKey(key);
|
1307
|
+
return this.element.hasAttribute(name);
|
1308
|
+
}
|
1309
|
+
delete(key) {
|
1310
|
+
if (this.has(key)) {
|
1311
|
+
const name = this.getAttributeNameForKey(key);
|
1312
|
+
this.element.removeAttribute(name);
|
1313
|
+
return true;
|
1314
|
+
}
|
1315
|
+
else {
|
1316
|
+
return false;
|
1317
|
+
}
|
1318
|
+
}
|
1319
|
+
getAttributeNameForKey(key) {
|
1320
|
+
return `data-${this.identifier}-${dasherize(key)}`;
|
1321
|
+
}
|
1322
|
+
}
|
1323
|
+
|
1324
|
+
class Guide {
|
1325
|
+
constructor(logger) {
|
1326
|
+
this.warnedKeysByObject = new WeakMap;
|
1327
|
+
this.logger = logger;
|
1328
|
+
}
|
1329
|
+
warn(object, key, message) {
|
1330
|
+
let warnedKeys = this.warnedKeysByObject.get(object);
|
1331
|
+
if (!warnedKeys) {
|
1332
|
+
warnedKeys = new Set;
|
1333
|
+
this.warnedKeysByObject.set(object, warnedKeys);
|
1334
|
+
}
|
1335
|
+
if (!warnedKeys.has(key)) {
|
1336
|
+
warnedKeys.add(key);
|
1337
|
+
this.logger.warn(message, object);
|
1338
|
+
}
|
1339
|
+
}
|
1340
|
+
}
|
1341
|
+
|
1342
|
+
function attributeValueContainsToken(attributeName, token) {
|
1343
|
+
return `[${attributeName}~="${token}"]`;
|
1344
|
+
}
|
1345
|
+
|
1346
|
+
class TargetSet {
|
1347
|
+
constructor(scope) {
|
1348
|
+
this.scope = scope;
|
1349
|
+
}
|
1350
|
+
get element() {
|
1351
|
+
return this.scope.element;
|
1352
|
+
}
|
1353
|
+
get identifier() {
|
1354
|
+
return this.scope.identifier;
|
1355
|
+
}
|
1356
|
+
get schema() {
|
1357
|
+
return this.scope.schema;
|
1358
|
+
}
|
1359
|
+
has(targetName) {
|
1360
|
+
return this.find(targetName) != null;
|
1361
|
+
}
|
1362
|
+
find(...targetNames) {
|
1363
|
+
return targetNames.reduce((target, targetName) => target
|
1364
|
+
|| this.findTarget(targetName)
|
1365
|
+
|| this.findLegacyTarget(targetName), undefined);
|
1366
|
+
}
|
1367
|
+
findAll(...targetNames) {
|
1368
|
+
return targetNames.reduce((targets, targetName) => [
|
1369
|
+
...targets,
|
1370
|
+
...this.findAllTargets(targetName),
|
1371
|
+
...this.findAllLegacyTargets(targetName)
|
1372
|
+
], []);
|
1373
|
+
}
|
1374
|
+
findTarget(targetName) {
|
1375
|
+
const selector = this.getSelectorForTargetName(targetName);
|
1376
|
+
return this.scope.findElement(selector);
|
1377
|
+
}
|
1378
|
+
findAllTargets(targetName) {
|
1379
|
+
const selector = this.getSelectorForTargetName(targetName);
|
1380
|
+
return this.scope.findAllElements(selector);
|
1381
|
+
}
|
1382
|
+
getSelectorForTargetName(targetName) {
|
1383
|
+
const attributeName = this.schema.targetAttributeForScope(this.identifier);
|
1384
|
+
return attributeValueContainsToken(attributeName, targetName);
|
1385
|
+
}
|
1386
|
+
findLegacyTarget(targetName) {
|
1387
|
+
const selector = this.getLegacySelectorForTargetName(targetName);
|
1388
|
+
return this.deprecate(this.scope.findElement(selector), targetName);
|
1389
|
+
}
|
1390
|
+
findAllLegacyTargets(targetName) {
|
1391
|
+
const selector = this.getLegacySelectorForTargetName(targetName);
|
1392
|
+
return this.scope.findAllElements(selector).map(element => this.deprecate(element, targetName));
|
1393
|
+
}
|
1394
|
+
getLegacySelectorForTargetName(targetName) {
|
1395
|
+
const targetDescriptor = `${this.identifier}.${targetName}`;
|
1396
|
+
return attributeValueContainsToken(this.schema.targetAttribute, targetDescriptor);
|
1397
|
+
}
|
1398
|
+
deprecate(element, targetName) {
|
1399
|
+
if (element) {
|
1400
|
+
const { identifier } = this;
|
1401
|
+
const attributeName = this.schema.targetAttribute;
|
1402
|
+
const revisedAttributeName = this.schema.targetAttributeForScope(identifier);
|
1403
|
+
this.guide.warn(element, `target:${targetName}`, `Please replace ${attributeName}="${identifier}.${targetName}" with ${revisedAttributeName}="${targetName}". ` +
|
1404
|
+
`The ${attributeName} attribute is deprecated and will be removed in a future version of Stimulus.`);
|
1405
|
+
}
|
1406
|
+
return element;
|
1407
|
+
}
|
1408
|
+
get guide() {
|
1409
|
+
return this.scope.guide;
|
1410
|
+
}
|
1411
|
+
}
|
1412
|
+
|
1413
|
+
class Scope {
|
1414
|
+
constructor(schema, element, identifier, logger) {
|
1415
|
+
this.targets = new TargetSet(this);
|
1416
|
+
this.classes = new ClassMap(this);
|
1417
|
+
this.data = new DataMap(this);
|
1418
|
+
this.containsElement = (element) => {
|
1419
|
+
return element.closest(this.controllerSelector) === this.element;
|
1420
|
+
};
|
1421
|
+
this.schema = schema;
|
1422
|
+
this.element = element;
|
1423
|
+
this.identifier = identifier;
|
1424
|
+
this.guide = new Guide(logger);
|
1425
|
+
}
|
1426
|
+
findElement(selector) {
|
1427
|
+
return this.element.matches(selector)
|
1428
|
+
? this.element
|
1429
|
+
: this.queryElements(selector).find(this.containsElement);
|
1430
|
+
}
|
1431
|
+
findAllElements(selector) {
|
1432
|
+
return [
|
1433
|
+
...this.element.matches(selector) ? [this.element] : [],
|
1434
|
+
...this.queryElements(selector).filter(this.containsElement)
|
1435
|
+
];
|
1436
|
+
}
|
1437
|
+
queryElements(selector) {
|
1438
|
+
return Array.from(this.element.querySelectorAll(selector));
|
1439
|
+
}
|
1440
|
+
get controllerSelector() {
|
1441
|
+
return attributeValueContainsToken(this.schema.controllerAttribute, this.identifier);
|
1442
|
+
}
|
1443
|
+
}
|
1444
|
+
|
1445
|
+
class ScopeObserver {
|
1446
|
+
constructor(element, schema, delegate) {
|
1447
|
+
this.element = element;
|
1448
|
+
this.schema = schema;
|
1449
|
+
this.delegate = delegate;
|
1450
|
+
this.valueListObserver = new ValueListObserver(this.element, this.controllerAttribute, this);
|
1451
|
+
this.scopesByIdentifierByElement = new WeakMap;
|
1452
|
+
this.scopeReferenceCounts = new WeakMap;
|
1453
|
+
}
|
1454
|
+
start() {
|
1455
|
+
this.valueListObserver.start();
|
1456
|
+
}
|
1457
|
+
stop() {
|
1458
|
+
this.valueListObserver.stop();
|
1459
|
+
}
|
1460
|
+
get controllerAttribute() {
|
1461
|
+
return this.schema.controllerAttribute;
|
1462
|
+
}
|
1463
|
+
parseValueForToken(token) {
|
1464
|
+
const { element, content: identifier } = token;
|
1465
|
+
const scopesByIdentifier = this.fetchScopesByIdentifierForElement(element);
|
1466
|
+
let scope = scopesByIdentifier.get(identifier);
|
1467
|
+
if (!scope) {
|
1468
|
+
scope = this.delegate.createScopeForElementAndIdentifier(element, identifier);
|
1469
|
+
scopesByIdentifier.set(identifier, scope);
|
1470
|
+
}
|
1471
|
+
return scope;
|
1472
|
+
}
|
1473
|
+
elementMatchedValue(element, value) {
|
1474
|
+
const referenceCount = (this.scopeReferenceCounts.get(value) || 0) + 1;
|
1475
|
+
this.scopeReferenceCounts.set(value, referenceCount);
|
1476
|
+
if (referenceCount == 1) {
|
1477
|
+
this.delegate.scopeConnected(value);
|
1478
|
+
}
|
1479
|
+
}
|
1480
|
+
elementUnmatchedValue(element, value) {
|
1481
|
+
const referenceCount = this.scopeReferenceCounts.get(value);
|
1482
|
+
if (referenceCount) {
|
1483
|
+
this.scopeReferenceCounts.set(value, referenceCount - 1);
|
1484
|
+
if (referenceCount == 1) {
|
1485
|
+
this.delegate.scopeDisconnected(value);
|
1486
|
+
}
|
1487
|
+
}
|
1488
|
+
}
|
1489
|
+
fetchScopesByIdentifierForElement(element) {
|
1490
|
+
let scopesByIdentifier = this.scopesByIdentifierByElement.get(element);
|
1491
|
+
if (!scopesByIdentifier) {
|
1492
|
+
scopesByIdentifier = new Map;
|
1493
|
+
this.scopesByIdentifierByElement.set(element, scopesByIdentifier);
|
1494
|
+
}
|
1495
|
+
return scopesByIdentifier;
|
1496
|
+
}
|
1497
|
+
}
|
1498
|
+
|
1499
|
+
class Router {
|
1500
|
+
constructor(application) {
|
1501
|
+
this.application = application;
|
1502
|
+
this.scopeObserver = new ScopeObserver(this.element, this.schema, this);
|
1503
|
+
this.scopesByIdentifier = new Multimap;
|
1504
|
+
this.modulesByIdentifier = new Map;
|
1505
|
+
}
|
1506
|
+
get element() {
|
1507
|
+
return this.application.element;
|
1508
|
+
}
|
1509
|
+
get schema() {
|
1510
|
+
return this.application.schema;
|
1511
|
+
}
|
1512
|
+
get logger() {
|
1513
|
+
return this.application.logger;
|
1514
|
+
}
|
1515
|
+
get controllerAttribute() {
|
1516
|
+
return this.schema.controllerAttribute;
|
1517
|
+
}
|
1518
|
+
get modules() {
|
1519
|
+
return Array.from(this.modulesByIdentifier.values());
|
1520
|
+
}
|
1521
|
+
get contexts() {
|
1522
|
+
return this.modules.reduce((contexts, module) => contexts.concat(module.contexts), []);
|
1523
|
+
}
|
1524
|
+
start() {
|
1525
|
+
this.scopeObserver.start();
|
1526
|
+
}
|
1527
|
+
stop() {
|
1528
|
+
this.scopeObserver.stop();
|
1529
|
+
}
|
1530
|
+
loadDefinition(definition) {
|
1531
|
+
this.unloadIdentifier(definition.identifier);
|
1532
|
+
const module = new Module(this.application, definition);
|
1533
|
+
this.connectModule(module);
|
1534
|
+
}
|
1535
|
+
unloadIdentifier(identifier) {
|
1536
|
+
const module = this.modulesByIdentifier.get(identifier);
|
1537
|
+
if (module) {
|
1538
|
+
this.disconnectModule(module);
|
1539
|
+
}
|
1540
|
+
}
|
1541
|
+
getContextForElementAndIdentifier(element, identifier) {
|
1542
|
+
const module = this.modulesByIdentifier.get(identifier);
|
1543
|
+
if (module) {
|
1544
|
+
return module.contexts.find(context => context.element == element);
|
1545
|
+
}
|
1546
|
+
}
|
1547
|
+
handleError(error, message, detail) {
|
1548
|
+
this.application.handleError(error, message, detail);
|
1549
|
+
}
|
1550
|
+
createScopeForElementAndIdentifier(element, identifier) {
|
1551
|
+
return new Scope(this.schema, element, identifier, this.logger);
|
1552
|
+
}
|
1553
|
+
scopeConnected(scope) {
|
1554
|
+
this.scopesByIdentifier.add(scope.identifier, scope);
|
1555
|
+
const module = this.modulesByIdentifier.get(scope.identifier);
|
1556
|
+
if (module) {
|
1557
|
+
module.connectContextForScope(scope);
|
1558
|
+
}
|
1559
|
+
}
|
1560
|
+
scopeDisconnected(scope) {
|
1561
|
+
this.scopesByIdentifier.delete(scope.identifier, scope);
|
1562
|
+
const module = this.modulesByIdentifier.get(scope.identifier);
|
1563
|
+
if (module) {
|
1564
|
+
module.disconnectContextForScope(scope);
|
1565
|
+
}
|
1566
|
+
}
|
1567
|
+
connectModule(module) {
|
1568
|
+
this.modulesByIdentifier.set(module.identifier, module);
|
1569
|
+
const scopes = this.scopesByIdentifier.getValuesForKey(module.identifier);
|
1570
|
+
scopes.forEach(scope => module.connectContextForScope(scope));
|
1571
|
+
}
|
1572
|
+
disconnectModule(module) {
|
1573
|
+
this.modulesByIdentifier.delete(module.identifier);
|
1574
|
+
const scopes = this.scopesByIdentifier.getValuesForKey(module.identifier);
|
1575
|
+
scopes.forEach(scope => module.disconnectContextForScope(scope));
|
1576
|
+
}
|
1577
|
+
}
|
1578
|
+
|
1579
|
+
const defaultSchema = {
|
1580
|
+
controllerAttribute: "data-controller",
|
1581
|
+
actionAttribute: "data-action",
|
1582
|
+
targetAttribute: "data-target",
|
1583
|
+
targetAttributeForScope: identifier => `data-${identifier}-target`
|
1584
|
+
};
|
1585
|
+
|
1586
|
+
class Application {
|
1587
|
+
constructor(element = document.documentElement, schema = defaultSchema) {
|
1588
|
+
this.logger = console;
|
1589
|
+
this.debug = false;
|
1590
|
+
this.logDebugActivity = (identifier, functionName, detail = {}) => {
|
1591
|
+
if (this.debug) {
|
1592
|
+
this.logFormattedMessage(identifier, functionName, detail);
|
1593
|
+
}
|
1594
|
+
};
|
1595
|
+
this.element = element;
|
1596
|
+
this.schema = schema;
|
1597
|
+
this.dispatcher = new Dispatcher(this);
|
1598
|
+
this.router = new Router(this);
|
1599
|
+
}
|
1600
|
+
static start(element, schema) {
|
1601
|
+
const application = new Application(element, schema);
|
1602
|
+
application.start();
|
1603
|
+
return application;
|
1604
|
+
}
|
1605
|
+
async start() {
|
1606
|
+
await domReady();
|
1607
|
+
this.logDebugActivity("application", "starting");
|
1608
|
+
this.dispatcher.start();
|
1609
|
+
this.router.start();
|
1610
|
+
this.logDebugActivity("application", "start");
|
1611
|
+
}
|
1612
|
+
stop() {
|
1613
|
+
this.logDebugActivity("application", "stopping");
|
1614
|
+
this.dispatcher.stop();
|
1615
|
+
this.router.stop();
|
1616
|
+
this.logDebugActivity("application", "stop");
|
1617
|
+
}
|
1618
|
+
register(identifier, controllerConstructor) {
|
1619
|
+
if (controllerConstructor.shouldLoad) {
|
1620
|
+
this.load({ identifier, controllerConstructor });
|
1621
|
+
}
|
1622
|
+
}
|
1623
|
+
load(head, ...rest) {
|
1624
|
+
const definitions = Array.isArray(head) ? head : [head, ...rest];
|
1625
|
+
definitions.forEach(definition => this.router.loadDefinition(definition));
|
1626
|
+
}
|
1627
|
+
unload(head, ...rest) {
|
1628
|
+
const identifiers = Array.isArray(head) ? head : [head, ...rest];
|
1629
|
+
identifiers.forEach(identifier => this.router.unloadIdentifier(identifier));
|
1630
|
+
}
|
1631
|
+
get controllers() {
|
1632
|
+
return this.router.contexts.map(context => context.controller);
|
1633
|
+
}
|
1634
|
+
getControllerForElementAndIdentifier(element, identifier) {
|
1635
|
+
const context = this.router.getContextForElementAndIdentifier(element, identifier);
|
1636
|
+
return context ? context.controller : null;
|
1637
|
+
}
|
1638
|
+
handleError(error, message, detail) {
|
1639
|
+
var _a;
|
1640
|
+
this.logger.error(`%s\n\n%o\n\n%o`, message, error, detail);
|
1641
|
+
(_a = window.onerror) === null || _a === void 0 ? void 0 : _a.call(window, message, "", 0, 0, error);
|
1642
|
+
}
|
1643
|
+
logFormattedMessage(identifier, functionName, detail = {}) {
|
1644
|
+
detail = Object.assign({ application: this }, detail);
|
1645
|
+
this.logger.groupCollapsed(`${identifier} #${functionName}`);
|
1646
|
+
this.logger.log("details:", Object.assign({}, detail));
|
1647
|
+
this.logger.groupEnd();
|
1648
|
+
}
|
1649
|
+
}
|
1650
|
+
function domReady() {
|
1651
|
+
return new Promise(resolve => {
|
1652
|
+
if (document.readyState == "loading") {
|
1653
|
+
document.addEventListener("DOMContentLoaded", () => resolve());
|
1654
|
+
}
|
1655
|
+
else {
|
1656
|
+
resolve();
|
1657
|
+
}
|
1658
|
+
});
|
1659
|
+
}
|
1660
|
+
|
1661
|
+
function ClassPropertiesBlessing(constructor) {
|
1662
|
+
const classes = readInheritableStaticArrayValues(constructor, "classes");
|
1663
|
+
return classes.reduce((properties, classDefinition) => {
|
1664
|
+
return Object.assign(properties, propertiesForClassDefinition(classDefinition));
|
1665
|
+
}, {});
|
1666
|
+
}
|
1667
|
+
function propertiesForClassDefinition(key) {
|
1668
|
+
return {
|
1669
|
+
[`${key}Class`]: {
|
1670
|
+
get() {
|
1671
|
+
const { classes } = this;
|
1672
|
+
if (classes.has(key)) {
|
1673
|
+
return classes.get(key);
|
1674
|
+
}
|
1675
|
+
else {
|
1676
|
+
const attribute = classes.getAttributeName(key);
|
1677
|
+
throw new Error(`Missing attribute "${attribute}"`);
|
1678
|
+
}
|
1679
|
+
}
|
1680
|
+
},
|
1681
|
+
[`${key}Classes`]: {
|
1682
|
+
get() {
|
1683
|
+
return this.classes.getAll(key);
|
1684
|
+
}
|
1685
|
+
},
|
1686
|
+
[`has${capitalize(key)}Class`]: {
|
1687
|
+
get() {
|
1688
|
+
return this.classes.has(key);
|
1689
|
+
}
|
1690
|
+
}
|
1691
|
+
};
|
1692
|
+
}
|
1693
|
+
|
1694
|
+
function TargetPropertiesBlessing(constructor) {
|
1695
|
+
const targets = readInheritableStaticArrayValues(constructor, "targets");
|
1696
|
+
return targets.reduce((properties, targetDefinition) => {
|
1697
|
+
return Object.assign(properties, propertiesForTargetDefinition(targetDefinition));
|
1698
|
+
}, {});
|
1699
|
+
}
|
1700
|
+
function propertiesForTargetDefinition(name) {
|
1701
|
+
return {
|
1702
|
+
[`${name}Target`]: {
|
1703
|
+
get() {
|
1704
|
+
const target = this.targets.find(name);
|
1705
|
+
if (target) {
|
1706
|
+
return target;
|
1707
|
+
}
|
1708
|
+
else {
|
1709
|
+
throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`);
|
1710
|
+
}
|
1711
|
+
}
|
1712
|
+
},
|
1713
|
+
[`${name}Targets`]: {
|
1714
|
+
get() {
|
1715
|
+
return this.targets.findAll(name);
|
1716
|
+
}
|
1717
|
+
},
|
1718
|
+
[`has${capitalize(name)}Target`]: {
|
1719
|
+
get() {
|
1720
|
+
return this.targets.has(name);
|
1721
|
+
}
|
1722
|
+
}
|
1723
|
+
};
|
1724
|
+
}
|
1725
|
+
|
1726
|
+
function ValuePropertiesBlessing(constructor) {
|
1727
|
+
const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values");
|
1728
|
+
const propertyDescriptorMap = {
|
1729
|
+
valueDescriptorMap: {
|
1730
|
+
get() {
|
1731
|
+
return valueDefinitionPairs.reduce((result, valueDefinitionPair) => {
|
1732
|
+
const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair);
|
1733
|
+
const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key);
|
1734
|
+
return Object.assign(result, { [attributeName]: valueDescriptor });
|
1735
|
+
}, {});
|
1736
|
+
}
|
1737
|
+
}
|
1738
|
+
};
|
1739
|
+
return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => {
|
1740
|
+
return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair));
|
1741
|
+
}, propertyDescriptorMap);
|
1742
|
+
}
|
1743
|
+
function propertiesForValueDefinitionPair(valueDefinitionPair) {
|
1744
|
+
const definition = parseValueDefinitionPair(valueDefinitionPair);
|
1745
|
+
const { key, name, reader: read, writer: write } = definition;
|
1746
|
+
return {
|
1747
|
+
[name]: {
|
1748
|
+
get() {
|
1749
|
+
const value = this.data.get(key);
|
1750
|
+
if (value !== null) {
|
1751
|
+
return read(value);
|
1752
|
+
}
|
1753
|
+
else {
|
1754
|
+
return definition.defaultValue;
|
1755
|
+
}
|
1756
|
+
},
|
1757
|
+
set(value) {
|
1758
|
+
if (value === undefined) {
|
1759
|
+
this.data.delete(key);
|
1760
|
+
}
|
1761
|
+
else {
|
1762
|
+
this.data.set(key, write(value));
|
1763
|
+
}
|
1764
|
+
}
|
1765
|
+
},
|
1766
|
+
[`has${capitalize(name)}`]: {
|
1767
|
+
get() {
|
1768
|
+
return this.data.has(key) || definition.hasCustomDefaultValue;
|
1769
|
+
}
|
1770
|
+
}
|
1771
|
+
};
|
1772
|
+
}
|
1773
|
+
function parseValueDefinitionPair([token, typeDefinition]) {
|
1774
|
+
return valueDescriptorForTokenAndTypeDefinition(token, typeDefinition);
|
1775
|
+
}
|
1776
|
+
function parseValueTypeConstant(constant) {
|
1777
|
+
switch (constant) {
|
1778
|
+
case Array: return "array";
|
1779
|
+
case Boolean: return "boolean";
|
1780
|
+
case Number: return "number";
|
1781
|
+
case Object: return "object";
|
1782
|
+
case String: return "string";
|
1783
|
+
}
|
1784
|
+
}
|
1785
|
+
function parseValueTypeDefault(defaultValue) {
|
1786
|
+
switch (typeof defaultValue) {
|
1787
|
+
case "boolean": return "boolean";
|
1788
|
+
case "number": return "number";
|
1789
|
+
case "string": return "string";
|
1790
|
+
}
|
1791
|
+
if (Array.isArray(defaultValue))
|
1792
|
+
return "array";
|
1793
|
+
if (Object.prototype.toString.call(defaultValue) === "[object Object]")
|
1794
|
+
return "object";
|
1795
|
+
}
|
1796
|
+
function parseValueTypeObject(typeObject) {
|
1797
|
+
const typeFromObject = parseValueTypeConstant(typeObject.type);
|
1798
|
+
if (typeFromObject) {
|
1799
|
+
const defaultValueType = parseValueTypeDefault(typeObject.default);
|
1800
|
+
if (typeFromObject !== defaultValueType) {
|
1801
|
+
throw new Error(`Type "${typeFromObject}" must match the type of the default value. Given default value: "${typeObject.default}" as "${defaultValueType}"`);
|
1802
|
+
}
|
1803
|
+
return typeFromObject;
|
1804
|
+
}
|
1805
|
+
}
|
1806
|
+
function parseValueTypeDefinition(typeDefinition) {
|
1807
|
+
const typeFromObject = parseValueTypeObject(typeDefinition);
|
1808
|
+
const typeFromDefaultValue = parseValueTypeDefault(typeDefinition);
|
1809
|
+
const typeFromConstant = parseValueTypeConstant(typeDefinition);
|
1810
|
+
const type = typeFromObject || typeFromDefaultValue || typeFromConstant;
|
1811
|
+
if (type)
|
1812
|
+
return type;
|
1813
|
+
throw new Error(`Unknown value type "${typeDefinition}"`);
|
1814
|
+
}
|
1815
|
+
function defaultValueForDefinition(typeDefinition) {
|
1816
|
+
const constant = parseValueTypeConstant(typeDefinition);
|
1817
|
+
if (constant)
|
1818
|
+
return defaultValuesByType[constant];
|
1819
|
+
const defaultValue = typeDefinition.default;
|
1820
|
+
if (defaultValue !== undefined)
|
1821
|
+
return defaultValue;
|
1822
|
+
return typeDefinition;
|
1823
|
+
}
|
1824
|
+
function valueDescriptorForTokenAndTypeDefinition(token, typeDefinition) {
|
1825
|
+
const key = `${dasherize(token)}-value`;
|
1826
|
+
const type = parseValueTypeDefinition(typeDefinition);
|
1827
|
+
return {
|
1828
|
+
type,
|
1829
|
+
key,
|
1830
|
+
name: camelize(key),
|
1831
|
+
get defaultValue() { return defaultValueForDefinition(typeDefinition); },
|
1832
|
+
get hasCustomDefaultValue() { return parseValueTypeDefault(typeDefinition) !== undefined; },
|
1833
|
+
reader: readers[type],
|
1834
|
+
writer: writers[type] || writers.default
|
1835
|
+
};
|
1836
|
+
}
|
1837
|
+
const defaultValuesByType = {
|
1838
|
+
get array() { return []; },
|
1839
|
+
boolean: false,
|
1840
|
+
number: 0,
|
1841
|
+
get object() { return {}; },
|
1842
|
+
string: ""
|
1843
|
+
};
|
1844
|
+
const readers = {
|
1845
|
+
array(value) {
|
1846
|
+
const array = JSON.parse(value);
|
1847
|
+
if (!Array.isArray(array)) {
|
1848
|
+
throw new TypeError("Expected array");
|
1849
|
+
}
|
1850
|
+
return array;
|
1851
|
+
},
|
1852
|
+
boolean(value) {
|
1853
|
+
return !(value == "0" || value == "false");
|
1854
|
+
},
|
1855
|
+
number(value) {
|
1856
|
+
return Number(value);
|
1857
|
+
},
|
1858
|
+
object(value) {
|
1859
|
+
const object = JSON.parse(value);
|
1860
|
+
if (object === null || typeof object != "object" || Array.isArray(object)) {
|
1861
|
+
throw new TypeError("Expected object");
|
1862
|
+
}
|
1863
|
+
return object;
|
1864
|
+
},
|
1865
|
+
string(value) {
|
1866
|
+
return value;
|
1867
|
+
}
|
1868
|
+
};
|
1869
|
+
const writers = {
|
1870
|
+
default: writeString,
|
1871
|
+
array: writeJSON,
|
1872
|
+
object: writeJSON
|
1873
|
+
};
|
1874
|
+
function writeJSON(value) {
|
1875
|
+
return JSON.stringify(value);
|
1876
|
+
}
|
1877
|
+
function writeString(value) {
|
1878
|
+
return `${value}`;
|
1879
|
+
}
|
1880
|
+
|
1881
|
+
class Controller {
|
1882
|
+
constructor(context) {
|
1883
|
+
this.context = context;
|
1884
|
+
}
|
1885
|
+
static get shouldLoad() {
|
1886
|
+
return true;
|
1887
|
+
}
|
1888
|
+
get application() {
|
1889
|
+
return this.context.application;
|
1890
|
+
}
|
1891
|
+
get scope() {
|
1892
|
+
return this.context.scope;
|
1893
|
+
}
|
1894
|
+
get element() {
|
1895
|
+
return this.scope.element;
|
1896
|
+
}
|
1897
|
+
get identifier() {
|
1898
|
+
return this.scope.identifier;
|
1899
|
+
}
|
1900
|
+
get targets() {
|
1901
|
+
return this.scope.targets;
|
1902
|
+
}
|
1903
|
+
get classes() {
|
1904
|
+
return this.scope.classes;
|
1905
|
+
}
|
1906
|
+
get data() {
|
1907
|
+
return this.scope.data;
|
1908
|
+
}
|
1909
|
+
initialize() {
|
1910
|
+
}
|
1911
|
+
connect() {
|
1912
|
+
}
|
1913
|
+
disconnect() {
|
1914
|
+
}
|
1915
|
+
dispatch(eventName, { target = this.element, detail = {}, prefix = this.identifier, bubbles = true, cancelable = true } = {}) {
|
1916
|
+
const type = prefix ? `${prefix}:${eventName}` : eventName;
|
1917
|
+
const event = new CustomEvent(type, { detail, bubbles, cancelable });
|
1918
|
+
target.dispatchEvent(event);
|
1919
|
+
return event;
|
1920
|
+
}
|
1921
|
+
}
|
1922
|
+
Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing];
|
1923
|
+
Controller.targets = [];
|
1924
|
+
Controller.values = {};
|
1925
|
+
|
1926
|
+
export { Application, AttributeObserver, Context, Controller, ElementObserver, IndexedMultimap, Multimap, StringMapObserver, TokenListObserver, ValueListObserver, add, defaultSchema, del, fetch, prune };
|