/
index.es6.js
2346 lines (2316 loc) · 85.4 KB
/
index.es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Reporter, DI, Registration, PLATFORM } from '@aurelia/kernel';
import { hasBind, hasUnbind, targetObserver, DOM, connectable, ILifecycle, BindingMode, DelegationStrategy, ITargetObserverLocator, SetterObserver, IDOM, ITargetAccessorLocator, BindingBehaviorResource, IObserverLocator, buildTemplateDefinition, HydrateElementInstruction, IRenderable, ITargetedInstruction, IRenderingEngine, CompositionCoordinator, bindable, CustomElementResource, INode, ITemplateFactory, CompiledTemplate, NodeSequence, IExpressionParser, instructionRenderer, ensureExpression, MultiInterpolationBinding, InterpolationBinding, addBinding, Binding, IProjectorLocator, RuntimeBasicConfiguration } from '@aurelia/runtime';
/**
* Listener binding. Handle event binding between view and view model
*/
class Listener {
// tslint:disable-next-line:parameters-max-number
constructor(dom, targetEvent, delegationStrategy, sourceExpression, target, preventDefault, eventManager, locator) {
this.dom = dom;
this.$nextBinding = null;
this.$prevBinding = null;
this.$state = 0 /* none */;
this.delegationStrategy = delegationStrategy;
this.locator = locator;
this.preventDefault = preventDefault;
this.sourceExpression = sourceExpression;
this.target = target;
this.targetEvent = targetEvent;
this.eventManager = eventManager;
}
callSource(event) {
const overrideContext = this.$scope.overrideContext;
overrideContext.$event = event;
const result = this.sourceExpression.evaluate(1048576 /* mustEvaluate */, this.$scope, this.locator);
Reflect.deleteProperty(overrideContext, '$event');
if (result !== true && this.preventDefault) {
event.preventDefault();
}
return result;
}
handleEvent(event) {
this.callSource(event);
}
$bind(flags, scope) {
if (this.$state & 2 /* isBound */) {
if (this.$scope === scope) {
return;
}
this.$unbind(flags | 2048 /* fromBind */);
}
// add isBinding flag
this.$state |= 1 /* isBinding */;
this.$scope = scope;
const sourceExpression = this.sourceExpression;
if (hasBind(sourceExpression)) {
sourceExpression.bind(flags, scope, this);
}
this.handler = this.eventManager.addEventListener(this.dom, this.target, this.targetEvent, this, this.delegationStrategy);
// add isBound flag and remove isBinding flag
this.$state |= 2 /* isBound */;
this.$state &= ~1 /* isBinding */;
}
$unbind(flags) {
if (!(this.$state & 2 /* isBound */)) {
return;
}
// add isUnbinding flag
this.$state |= 64 /* isUnbinding */;
const sourceExpression = this.sourceExpression;
if (hasUnbind(sourceExpression)) {
sourceExpression.unbind(flags, this.$scope, this);
}
this.$scope = null;
this.handler.dispose();
this.handler = null;
// remove isBound and isUnbinding flags
this.$state &= ~(2 /* isBound */ | 64 /* isUnbinding */);
}
observeProperty(flags, obj, propertyName) {
return;
}
handleChange(newValue, previousValue, flags) {
return;
}
}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
/**
* Observer for handling two-way binding with attributes
* Has different strategy for class/style and normal attributes
* TODO: handle SVG/attributes with namespace
*/
let AttributeObserver = class AttributeObserver {
constructor(flags, lifecycle, observerLocator, element, targetAttribute, targetKey) {
this.isDOMObserver = true;
this.persistentFlags = flags & 67108879 /* persistentBindingFlags */;
this.observerLocator = observerLocator;
this.lifecycle = lifecycle;
this.obj = element;
this.targetAttribute = targetAttribute;
if (targetAttribute === 'class') {
this.handleMutationCore = this.handleMutationClassName;
this.setValueCore = this.setValueCoreClassName;
this.getValue = this.getValueClassName;
}
else if (targetAttribute === 'style') {
this.handleMutationCore = this.handleMutationInlineStyle;
this.setValueCore = this.setValueCoreInlineStyle;
this.getValue = this.getValueInlineStyle;
}
this.propertyKey = targetKey;
}
getValue() {
return this.obj.getAttribute(this.propertyKey);
}
getValueInlineStyle() {
return this.obj.style.getPropertyValue(this.propertyKey);
}
getValueClassName() {
return this.obj.classList.contains(this.propertyKey);
}
setValueCore(newValue, flags) {
const obj = this.obj;
const targetAttribute = this.targetAttribute;
if (newValue === null || newValue === undefined) {
obj.removeAttribute(targetAttribute);
}
else {
obj.setAttribute(targetAttribute, newValue);
}
}
setValueCoreInlineStyle(value) {
let priority = '';
if (typeof value === 'string' && value.indexOf('!important') !== -1) {
priority = 'important';
value = value.replace('!important', '');
}
this.obj.style.setProperty(this.propertyKey, value, priority);
}
setValueCoreClassName(newValue) {
const className = this.propertyKey;
const classList = this.obj.classList;
// Why is class attribute observer setValue look different with class attribute accessor?
// ==============
// For class list
// newValue is simply checked if truthy or falsy
// and toggle the class accordingly
// -- the rule of this is quite different to normal attribute
//
// for class attribute, observer is different in a way that it only observe a particular class at a time
// this also comes from syntax, where it would typically be my-class.class="someProperty"
//
// so there is no need for separating class by space and add all of them like class accessor
if (newValue) {
classList.add(className);
}
else {
classList.remove(className);
}
}
handleMutation(mutationRecords) {
let shouldProcess = false;
for (let i = 0, ii = mutationRecords.length; ii > i; ++i) {
const record = mutationRecords[i];
if (record.type === 'attributes' && record.attributeName === this.targetAttribute) {
shouldProcess = true;
break;
}
}
if (shouldProcess) {
this.handleMutationCore();
}
}
handleMutationCore() {
const newValue = this.obj.getAttribute(this.targetAttribute);
if (newValue !== this.currentValue) {
this.currentValue = newValue;
this.setValue(newValue, 0 /* none */);
}
}
handleMutationInlineStyle() {
const css = this.obj.style;
const rule = this.propertyKey;
const newValue = css.getPropertyValue(rule);
if (newValue !== this.currentValue) {
this.currentValue = newValue;
this.setValue(newValue, 0 /* none */);
}
}
handleMutationClassName() {
const className = this.propertyKey;
const newValue = this.obj.classList.contains(className);
if (newValue !== this.currentValue) {
this.currentValue = newValue;
this.setValue(newValue, 0 /* none */);
}
}
subscribe(subscriber) {
if (!this.hasSubscribers()) {
startObservation(this.obj, this);
}
this.addSubscriber(subscriber);
}
unsubscribe(subscriber) {
if (this.removeSubscriber(subscriber) && !this.hasSubscribers()) {
stopObservation(this.obj, this);
}
}
};
AttributeObserver = __decorate([
targetObserver('')
], AttributeObserver);
const startObservation = (element, subscription) => {
if (element.$eMObservers === undefined) {
element.$eMObservers = new Set();
}
if (element.$mObserver === undefined) {
element.$mObserver = DOM.createNodeObserver(element, handleMutation, { attributes: true });
}
element.$eMObservers.add(subscription);
};
const stopObservation = (element, subscription) => {
const $eMObservers = element.$eMObservers;
if ($eMObservers.delete(subscription)) {
if ($eMObservers.size === 0) {
element.$mObserver.disconnect();
element.$mObserver = undefined;
}
return true;
}
return false;
};
const handleMutation = (mutationRecords) => {
mutationRecords[0].target.$eMObservers.forEach(invokeHandleMutation, mutationRecords);
};
function invokeHandleMutation(s) {
s.handleMutation(this);
}
// BindingMode is not a const enum (and therefore not inlined), so assigning them to a variable to save a member accessor is a minor perf tweak
const { oneTime, toView, fromView } = BindingMode;
// pre-combining flags for bitwise checks is a minor perf tweak
const toViewOrOneTime = toView | oneTime;
/**
* Attribute binding. Handle attribute binding betwen view/view model. Understand Html special attributes
*/
let AttributeBinding = class AttributeBinding {
constructor(sourceExpression, target,
// some attributes may have inner structure
// such as class -> collection of class names
// such as style -> collection of style rules
//
// for normal attributes, targetAttribute and targetProperty are the same and can be ignore
targetAttribute, targetKey, mode, observerLocator, locator) {
connectable.assignIdTo(this);
this.$nextBinding = null;
this.$prevBinding = null;
this.$state = 0 /* none */;
this.$lifecycle = locator.get(ILifecycle);
this.$nextConnect = null;
this.$nextPatch = null;
this.$scope = null;
this.locator = locator;
this.mode = mode;
this.observerLocator = observerLocator;
this.sourceExpression = sourceExpression;
this.target = target;
this.targetAttribute = targetAttribute;
this.targetProperty = targetKey;
this.persistentFlags = 0 /* none */;
}
updateTarget(value, flags) {
flags |= this.persistentFlags;
this.targetObserver.setValue(value, flags | 16 /* updateTargetInstance */);
}
updateSource(value, flags) {
flags |= this.persistentFlags;
this.sourceExpression.assign(flags | 32 /* updateSourceExpression */, this.$scope, this.locator, value);
}
handleChange(newValue, _previousValue, flags) {
if (!(this.$state & 2 /* isBound */)) {
return;
}
flags |= this.persistentFlags;
if (this.mode === BindingMode.fromView) {
flags &= ~16 /* updateTargetInstance */;
flags |= 32 /* updateSourceExpression */;
}
if (flags & 16 /* updateTargetInstance */) {
const previousValue = this.targetObserver.getValue();
// if the only observable is an AccessScope then we can assume the passed-in newValue is the correct and latest value
if (this.sourceExpression.$kind !== 10082 /* AccessScope */ || this.observerSlots > 1) {
newValue = this.sourceExpression.evaluate(flags, this.$scope, this.locator);
}
if (newValue !== previousValue) {
this.updateTarget(newValue, flags);
}
if ((this.mode & oneTime) === 0) {
this.version++;
this.sourceExpression.connect(flags, this.$scope, this);
this.unobserve(false);
}
return;
}
if (flags & 32 /* updateSourceExpression */) {
if (newValue !== this.sourceExpression.evaluate(flags, this.$scope, this.locator)) {
this.updateSource(newValue, flags);
}
return;
}
throw Reporter.error(15, flags);
}
$bind(flags, scope) {
if (this.$state & 2 /* isBound */) {
if (this.$scope === scope) {
return;
}
this.$unbind(flags | 2048 /* fromBind */);
}
// add isBinding flag
this.$state |= 1 /* isBinding */;
// Store flags which we can only receive during $bind and need to pass on
// to the AST during evaluate/connect/assign
this.persistentFlags = flags & 67108879 /* persistentBindingFlags */;
this.$scope = scope;
let sourceExpression = this.sourceExpression;
if (hasBind(sourceExpression)) {
sourceExpression.bind(flags, scope, this);
}
let targetObserver = this.targetObserver;
if (!targetObserver) {
targetObserver = this.targetObserver = new AttributeObserver(2048 /* fromBind */, this.$lifecycle, this.observerLocator, this.target, this.targetAttribute, this.targetProperty);
}
if (targetObserver.bind) {
targetObserver.bind(flags);
}
// during bind, binding behavior might have changed sourceExpression
sourceExpression = this.sourceExpression;
if (this.mode & toViewOrOneTime) {
this.updateTarget(sourceExpression.evaluate(flags, scope, this.locator), flags);
}
if (this.mode & toView) {
sourceExpression.connect(flags, scope, this);
}
if (this.mode & fromView) {
targetObserver[this.id] |= 32 /* updateSourceExpression */;
targetObserver.subscribe(this);
}
// add isBound flag and remove isBinding flag
this.$state |= 2 /* isBound */;
this.$state &= ~1 /* isBinding */;
}
$unbind(flags) {
if (!(this.$state & 2 /* isBound */)) {
return;
}
// add isUnbinding flag
this.$state |= 64 /* isUnbinding */;
// clear persistent flags
this.persistentFlags = 0 /* none */;
if (hasUnbind(this.sourceExpression)) {
this.sourceExpression.unbind(flags, this.$scope, this);
}
this.$scope = null;
if (this.targetObserver.unbind) {
this.targetObserver.unbind(flags);
}
if (this.targetObserver.unsubscribe) {
this.targetObserver.unsubscribe(this);
this.targetObserver[this.id] &= ~32 /* updateSourceExpression */;
}
this.unobserve(true);
// remove isBound and isUnbinding flags
this.$state &= ~(2 /* isBound */ | 64 /* isUnbinding */);
}
connect(flags) {
if (this.$state & 2 /* isBound */) {
flags |= this.persistentFlags;
this.sourceExpression.connect(flags | 1048576 /* mustEvaluate */, this.$scope, this);
}
}
patch(flags) {
if (this.$state & 2 /* isBound */) {
flags |= this.persistentFlags;
this.updateTarget(this.sourceExpression.evaluate(flags | 1048576 /* mustEvaluate */, this.$scope, this.locator), flags);
}
}
};
AttributeBinding = __decorate([
connectable()
], AttributeBinding);
let AttributeNSAccessor = class AttributeNSAccessor {
constructor(lifecycle, obj, propertyKey, attributeName, namespace) {
this.isDOMObserver = true;
this.attributeName = attributeName;
this.lifecycle = lifecycle;
this.obj = obj;
this.oldValue = this.currentValue = this.getValue();
this.propertyKey = propertyKey;
this.namespace = namespace;
}
getValue() {
return this.obj.getAttributeNS(this.namespace, this.attributeName);
}
setValueCore(newValue) {
this.obj.setAttributeNS(this.namespace, this.attributeName, newValue);
}
};
AttributeNSAccessor = __decorate([
targetObserver('')
], AttributeNSAccessor);
const defaultMatcher = (a, b) => {
return a === b;
};
let CheckedObserver = class CheckedObserver {
constructor(flags, lifecycle, obj, handler, observerLocator) {
this.persistentFlags = flags & 67108879 /* persistentBindingFlags */;
this.isDOMObserver = true;
this.handler = handler;
this.lifecycle = lifecycle;
this.obj = obj;
this.observerLocator = observerLocator;
}
getValue() {
return this.currentValue;
}
setValueCore(newValue, flags) {
if (!this.valueObserver) {
this.valueObserver = this.obj['$observers'] && (this.obj['$observers'].model || this.obj['$observers'].value);
if (this.valueObserver) {
this.valueObserver.subscribe(this);
}
}
if (this.arrayObserver) {
this.arrayObserver.unsubscribeBatched(this);
this.arrayObserver = null;
}
if (this.obj.type === 'checkbox' && Array.isArray(newValue)) {
this.arrayObserver = this.observerLocator.getArrayObserver(this.persistentFlags | flags, newValue);
this.arrayObserver.subscribeBatched(this);
}
this.synchronizeElement();
}
// handleBatchedCollectionChange (todo: rename to make this explicit?)
handleBatchedChange() {
this.synchronizeElement();
this.notify(448 /* fromFlush */);
}
// handlePropertyChange (todo: rename normal subscribe methods in target observers to batched, since that's what they really are)
handleChange(newValue, previousValue, flags) {
this.synchronizeElement();
this.notify(flags);
}
synchronizeElement() {
const value = this.currentValue;
const element = this.obj;
const elementValue = element.hasOwnProperty('model') ? element['model'] : element.value;
const isRadio = element.type === 'radio';
const matcher = element['matcher'] || defaultMatcher;
if (isRadio) {
element.checked = !!matcher(value, elementValue);
}
else if (value === true) {
element.checked = true;
}
else if (Array.isArray(value)) {
element.checked = value.findIndex(item => !!matcher(item, elementValue)) !== -1;
}
else {
element.checked = false;
}
}
notify(flags) {
if (flags & 2048 /* fromBind */) {
return;
}
const oldValue = this.oldValue;
const newValue = this.currentValue;
if (newValue === oldValue) {
return;
}
this.callSubscribers(this.currentValue, this.oldValue, this.persistentFlags | flags);
}
handleEvent() {
let value = this.currentValue;
const element = this.obj;
const elementValue = element.hasOwnProperty('model') ? element['model'] : element.value;
let index;
const matcher = element['matcher'] || defaultMatcher;
if (element.type === 'checkbox') {
if (Array.isArray(value)) {
index = value.findIndex(item => !!matcher(item, elementValue));
if (element.checked && index === -1) {
value.push(elementValue);
}
else if (!element.checked && index !== -1) {
value.splice(index, 1);
}
// when existing value is array, do not invoke callback as only the array element has changed
return;
}
value = element.checked;
}
else if (element.checked) {
value = elementValue;
}
else {
return;
}
this.oldValue = this.currentValue;
this.currentValue = value;
this.notify(65536 /* fromDOMEvent */ | 262144 /* allowPublishRoundtrip */);
}
subscribe(subscriber) {
if (!this.hasSubscribers()) {
this.handler.subscribe(this.obj, this);
}
this.addSubscriber(subscriber);
}
unsubscribe(subscriber) {
if (this.removeSubscriber(subscriber) && !this.hasSubscribers()) {
this.handler.dispose();
}
}
unbind() {
if (this.arrayObserver) {
this.arrayObserver.unsubscribeBatched(this);
this.arrayObserver = null;
}
if (this.valueObserver) {
this.valueObserver.unsubscribe(this);
}
}
};
CheckedObserver = __decorate([
targetObserver()
], CheckedObserver);
let ClassAttributeAccessor = class ClassAttributeAccessor {
constructor(lifecycle, obj) {
this.isDOMObserver = true;
this.doNotCache = true;
this.lifecycle = lifecycle;
this.nameIndex = null;
this.obj = obj;
this.version = 0;
}
getValue() {
return this.currentValue;
}
setValueCore(newValue) {
const nameIndex = this.nameIndex || {};
let version = this.version;
let names;
let name;
// Add the classes, tracking the version at which they were added.
if (newValue.length) {
const node = this.obj;
names = newValue.split(/\s+/);
for (let i = 0, length = names.length; i < length; i++) {
name = names[i];
if (!name.length) {
continue;
}
nameIndex[name] = version;
node.classList.add(name);
}
}
// Update state variables.
this.nameIndex = nameIndex;
this.version += 1;
// First call to setValue? We're done.
if (version === 0) {
return;
}
// Remove classes from previous version.
version -= 1;
for (name in nameIndex) {
if (!nameIndex.hasOwnProperty(name) || nameIndex[name] !== version) {
continue;
}
// TODO: this has the side-effect that classes already present which are added again,
// will be removed if they're not present in the next update.
// Better would be do have some configurability for this behavior, allowing the user to
// decide whether initial classes always need to be kept, always removed, or something in between
this.obj.classList.remove(name);
}
}
};
ClassAttributeAccessor = __decorate([
targetObserver('')
], ClassAttributeAccessor);
let DataAttributeAccessor = class DataAttributeAccessor {
constructor(lifecycle, obj, propertyKey) {
this.isDOMObserver = true;
this.lifecycle = lifecycle;
this.obj = obj;
this.oldValue = this.currentValue = this.getValue();
this.propertyKey = propertyKey;
}
getValue() {
return this.obj.getAttribute(this.propertyKey);
}
setValueCore(newValue) {
if (newValue === null) {
this.obj.removeAttribute(this.propertyKey);
}
else {
this.obj.setAttribute(this.propertyKey, newValue);
}
}
};
DataAttributeAccessor = __decorate([
targetObserver()
], DataAttributeAccessor);
let ElementPropertyAccessor = class ElementPropertyAccessor {
constructor(lifecycle, obj, propertyKey) {
this.isDOMObserver = true;
this.lifecycle = lifecycle;
this.obj = obj;
this.propertyKey = propertyKey;
}
getValue() {
return this.obj[this.propertyKey];
}
setValueCore(value) {
this.obj[this.propertyKey] = value;
}
};
ElementPropertyAccessor = __decorate([
targetObserver('')
], ElementPropertyAccessor);
//Note: path and deepPath are designed to handle v0 and v1 shadow dom specs respectively
/** @internal */
function findOriginalEventTarget(event) {
return (event.composedPath && event.composedPath()[0]) || (event.deepPath && event.deepPath()[0]) || (event.path && event.path[0]) || event.target;
}
function stopPropagation() {
this.standardStopPropagation();
this.propagationStopped = true;
}
function handleCapturedEvent(event) {
event.propagationStopped = false;
let target = findOriginalEventTarget(event);
const orderedCallbacks = [];
/**
* During capturing phase, event 'bubbles' down from parent. Needs to reorder callback from root down to target
*/
while (target) {
if (target.capturedCallbacks) {
const callback = target.capturedCallbacks[event.type];
if (callback) {
if (event.stopPropagation !== stopPropagation) {
event.standardStopPropagation = event.stopPropagation;
event.stopPropagation = stopPropagation;
}
orderedCallbacks.push(callback);
}
}
target = target.parentNode;
}
for (let i = orderedCallbacks.length - 1; i >= 0 && !event.propagationStopped; i--) {
const orderedCallback = orderedCallbacks[i];
if ('handleEvent' in orderedCallback) {
orderedCallback.handleEvent(event);
}
else {
orderedCallback(event);
}
}
}
function handleDelegatedEvent(event) {
event.propagationStopped = false;
let target = findOriginalEventTarget(event);
while (target && !event.propagationStopped) {
if (target.delegatedCallbacks) {
const callback = target.delegatedCallbacks[event.type];
if (callback) {
if (event.stopPropagation !== stopPropagation) {
event.standardStopPropagation = event.stopPropagation;
event.stopPropagation = stopPropagation;
}
if ('handleEvent' in callback) {
callback.handleEvent(event);
}
else {
callback(event);
}
}
}
target = target.parentNode;
}
}
class ListenerTracker {
constructor(dom, eventName, listener, capture) {
this.dom = dom;
this.capture = capture;
this.count = 0;
this.eventName = eventName;
this.listener = listener;
}
increment() {
this.count++;
if (this.count === 1) {
this.dom.addEventListener(this.eventName, this.listener, null, this.capture);
}
}
decrement() {
this.count--;
if (this.count === 0) {
this.dom.removeEventListener(this.eventName, this.listener, null, this.capture);
}
}
/*@internal*/
dispose() {
if (this.count > 0) {
this.count = 0;
this.dom.removeEventListener(this.eventName, this.listener, null, this.capture);
}
}
}
/**
* Enable dispose() pattern for `delegate` & `capture` commands
*/
class DelegateOrCaptureSubscription {
constructor(entry, lookup, targetEvent, callback) {
this.entry = entry;
this.lookup = lookup;
this.targetEvent = targetEvent;
lookup[targetEvent] = callback;
}
dispose() {
this.entry.decrement();
this.lookup[this.targetEvent] = null;
}
}
/**
* Enable dispose() pattern for addEventListener for `trigger`
*/
class TriggerSubscription {
constructor(dom, target, targetEvent, callback) {
this.dom = dom;
this.target = target;
this.targetEvent = targetEvent;
this.callback = callback;
dom.addEventListener(targetEvent, callback, target);
}
dispose() {
this.dom.removeEventListener(this.targetEvent, this.callback, this.target);
}
}
class EventSubscriber {
constructor(dom, events) {
this.dom = dom;
this.events = events;
this.target = null;
this.handler = null;
}
subscribe(node, callbackOrListener) {
this.target = node;
this.handler = callbackOrListener;
const add = this.dom.addEventListener;
const events = this.events;
for (let i = 0, ii = events.length; ii > i; ++i) {
add(events[i], callbackOrListener, node);
}
}
dispose() {
const node = this.target;
const callbackOrListener = this.handler;
const events = this.events;
const remove = this.dom.removeEventListener;
for (let i = 0, ii = events.length; ii > i; ++i) {
remove(events[i], callbackOrListener, node);
}
this.target = this.handler = null;
}
}
const IEventManager = DI.createInterface('IEventManager').withDefault(x => x.singleton(EventManager));
/** @internal */
class EventManager {
constructor() {
this.delegatedHandlers = {};
this.capturedHandlers = {};
this.delegatedHandlers = {};
this.capturedHandlers = {};
}
addEventListener(dom, target, targetEvent, callbackOrListener, strategy) {
let delegatedHandlers;
let capturedHandlers;
let handlerEntry;
if (strategy === DelegationStrategy.bubbling) {
delegatedHandlers = this.delegatedHandlers;
handlerEntry = delegatedHandlers[targetEvent] || (delegatedHandlers[targetEvent] = new ListenerTracker(dom, targetEvent, handleDelegatedEvent, false));
handlerEntry.increment();
const delegatedCallbacks = target.delegatedCallbacks || (target.delegatedCallbacks = {});
return new DelegateOrCaptureSubscription(handlerEntry, delegatedCallbacks, targetEvent, callbackOrListener);
}
if (strategy === DelegationStrategy.capturing) {
capturedHandlers = this.capturedHandlers;
handlerEntry = capturedHandlers[targetEvent] || (capturedHandlers[targetEvent] = new ListenerTracker(dom, targetEvent, handleCapturedEvent, true));
handlerEntry.increment();
const capturedCallbacks = target.capturedCallbacks || (target.capturedCallbacks = {});
return new DelegateOrCaptureSubscription(handlerEntry, capturedCallbacks, targetEvent, callbackOrListener);
}
return new TriggerSubscription(dom, target, targetEvent, callbackOrListener);
}
dispose() {
let key;
const { delegatedHandlers, capturedHandlers } = this;
for (key in delegatedHandlers) {
delegatedHandlers[key].dispose();
}
for (key in capturedHandlers) {
capturedHandlers[key].dispose();
}
}
}
const childObserverOptions = {
childList: true,
subtree: true,
characterData: true
};
function defaultMatcher$1(a, b) {
return a === b;
}
let SelectValueObserver = class SelectValueObserver {
constructor(flags, lifecycle, obj, handler, observerLocator, dom) {
this.persistentFlags = flags & 67108879 /* persistentBindingFlags */;
this.isDOMObserver = true;
this.lifecycle = lifecycle;
this.obj = obj;
this.handler = handler;
this.observerLocator = observerLocator;
this.dom = dom;
}
getValue() {
return this.currentValue;
}
setValueCore(newValue, flags) {
const isArray = Array.isArray(newValue);
if (!isArray && newValue !== null && newValue !== undefined && this.obj.multiple) {
throw new Error('Only null or Array instances can be bound to a multi-select.');
}
if (this.arrayObserver) {
this.arrayObserver.unsubscribeBatched(this);
this.arrayObserver = null;
}
if (isArray) {
this.arrayObserver = this.observerLocator.getArrayObserver(this.persistentFlags | flags, newValue);
this.arrayObserver.subscribeBatched(this);
}
this.synchronizeOptions();
this.notify(flags);
}
// called when the array mutated (items sorted/added/removed, etc)
handleBatchedChange(indexMap) {
// we don't need to go through the normal setValue logic and can directly call synchronizeOptions here,
// because the change already waited one tick (batched) and there's no point in calling notify when the instance didn't change
this.synchronizeOptions(indexMap);
}
// called when a different value was assigned
handleChange(newValue, previousValue, flags) {
this.setValue(newValue, this.persistentFlags | flags);
}
notify(flags) {
if (flags & 2048 /* fromBind */) {
return;
}
const oldValue = this.oldValue;
const newValue = this.currentValue;
if (newValue === oldValue) {
return;
}
this.callSubscribers(newValue, oldValue, this.persistentFlags | flags);
}
handleEvent() {
// "from-view" changes are always synchronous now, so immediately sync the value and notify subscribers
const shouldNotify = this.synchronizeValue();
if (shouldNotify) {
this.notify(65536 /* fromDOMEvent */ | 262144 /* allowPublishRoundtrip */);
}
}
synchronizeOptions(indexMap) {
const currentValue = this.currentValue;
const isArray = Array.isArray(currentValue);
const obj = this.obj;
const matcher = obj.matcher || defaultMatcher$1;
const options = obj.options;
let i = options.length;
while (i--) {
const option = options[i];
const optionValue = option.hasOwnProperty('model') ? option.model : option.value;
if (isArray) {
option.selected = currentValue.findIndex(item => !!matcher(optionValue, item)) !== -1;
continue;
}
option.selected = !!matcher(optionValue, currentValue);
}
}
synchronizeValue() {
// Spec for synchronizing value from `SelectObserver` to `<select/>`
// When synchronizing value to observed <select/> element, do the following steps:
// A. If `<select/>` is multiple
// 1. Check if current value, called `currentValue` is an array
// a. If not an array, return true to signal value has changed
// b. If is an array:
// i. gather all current selected <option/>, in to array called `values`
// ii. loop through the `currentValue` array and remove items that are nolonger selected based on matcher
// iii. loop through the `values` array and add items that are selected based on matcher
// iv. Return false to signal value hasn't changed
// B. If the select is single
// 1. Let `value` equal the first selected option, if no option selected, then `value` is `null`
// 2. assign `this.currentValue` to `this.oldValue`
// 3. assign `value` to `this.currentValue`
// 4. return `true` to signal value has changed
const obj = this.obj;
const options = obj.options;
const len = options.length;
const currentValue = this.currentValue;
let i = 0;
if (obj.multiple) {
// A.
if (!Array.isArray(currentValue)) {
// A.1.a
return true;
}
// A.1.b
// multi select
let option;
const matcher = obj.matcher || defaultMatcher$1;
// A.1.b.i
const values = [];
while (i < len) {
option = options[i];
if (option.selected) {
values.push(option.hasOwnProperty('model')
? option.model
: option.value);
}
++i;
}
// A.1.b.ii
i = 0;
while (i < currentValue.length) {
const a = currentValue[i];
// Todo: remove arrow fn
if (values.findIndex(b => !!matcher(a, b)) === -1) {
currentValue.splice(i, 1);
}
else {
++i;
}
}
// A.1.b.iii
i = 0;
while (i < values.length) {
const a = values[i];
// Todo: remove arrow fn
if (currentValue.findIndex(b => !!matcher(a, b)) === -1) {
currentValue.push(a);
}
++i;
}
// A.1.b.iv
return false;
}
// B. single select
// B.1
let value = null;
while (i < len) {
const option = options[i];
if (option.selected) {
value = option.hasOwnProperty('model')
? option.model
: option.value;
break;
}
++i;
}
// B.2
this.oldValue = this.currentValue;