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