1 | import * as LogManager from 'aurelia-logging';
|
2 | import {metadata,Origin,protocol} from 'aurelia-metadata';
|
3 | import {DOM,PLATFORM,FEATURE} from 'aurelia-pal';
|
4 | import {TemplateRegistryEntry,Loader} from 'aurelia-loader';
|
5 | import {relativeToFile} from 'aurelia-path';
|
6 | import {Container,resolver,inject} from 'aurelia-dependency-injection';
|
7 | import {ValueConverterResource,BindingBehaviorResource,camelCase,Binding,createOverrideContext,subscriberCollection,bindingMode,ObserverLocator,EventManager} from 'aurelia-binding';
|
8 | import {TaskQueue} from 'aurelia-task-queue';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export const animationEvent = {
|
14 | enterBegin: 'animation:enter:begin',
|
15 | enterActive: 'animation:enter:active',
|
16 | enterDone: 'animation:enter:done',
|
17 | enterTimeout: 'animation:enter:timeout',
|
18 |
|
19 | leaveBegin: 'animation:leave:begin',
|
20 | leaveActive: 'animation:leave:active',
|
21 | leaveDone: 'animation:leave:done',
|
22 | leaveTimeout: 'animation:leave:timeout',
|
23 |
|
24 | staggerNext: 'animation:stagger:next',
|
25 |
|
26 | removeClassBegin: 'animation:remove-class:begin',
|
27 | removeClassActive: 'animation:remove-class:active',
|
28 | removeClassDone: 'animation:remove-class:done',
|
29 | removeClassTimeout: 'animation:remove-class:timeout',
|
30 |
|
31 | addClassBegin: 'animation:add-class:begin',
|
32 | addClassActive: 'animation:add-class:active',
|
33 | addClassDone: 'animation:add-class:done',
|
34 | addClassTimeout: 'animation:add-class:timeout',
|
35 |
|
36 | animateBegin: 'animation:animate:begin',
|
37 | animateActive: 'animation:animate:active',
|
38 | animateDone: 'animation:animate:done',
|
39 | animateTimeout: 'animation:animate:timeout',
|
40 |
|
41 | sequenceBegin: 'animation:sequence:begin',
|
42 | sequenceDone: 'animation:sequence:done'
|
43 | };
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | export class Animator {
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 | enter(element: HTMLElement): Promise<boolean> {
|
55 | return Promise.resolve(false);
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | leave(element: HTMLElement): Promise<boolean> {
|
64 | return Promise.resolve(false);
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | removeClass(element: HTMLElement, className: string): Promise<boolean> {
|
74 | element.classList.remove(className);
|
75 | return Promise.resolve(false);
|
76 | }
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | addClass(element: HTMLElement, className: string): Promise<boolean> {
|
85 | element.classList.add(className);
|
86 | return Promise.resolve(false);
|
87 | }
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | animate(element: HTMLElement | Array<HTMLElement>, className: string): Promise<boolean> {
|
97 | return Promise.resolve(false);
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | runSequence(animations:Array<any>): Promise<boolean> {}
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | registerEffect(effectName: string, properties: Object): void {}
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 | unregisterEffect(effectName: string): void {}
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | export class CompositionTransactionNotifier {
|
126 | constructor(owner) {
|
127 | this.owner = owner;
|
128 | this.owner._compositionCount++;
|
129 | }
|
130 |
|
131 | |
132 |
|
133 |
|
134 | done(): void {
|
135 | this.owner._compositionCount--;
|
136 | this.owner._tryCompleteTransaction();
|
137 | }
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | export class CompositionTransactionOwnershipToken {
|
144 | constructor(owner) {
|
145 | this.owner = owner;
|
146 | this.owner._ownershipToken = this;
|
147 | this.thenable = this._createThenable();
|
148 | }
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 | waitForCompositionComplete(): Promise<void> {
|
155 | this.owner._tryCompleteTransaction();
|
156 | return this.thenable;
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 | resolve(): void {
|
163 | this._resolveCallback();
|
164 | }
|
165 |
|
166 | _createThenable() {
|
167 | return new Promise((resolve, reject) => {
|
168 | this._resolveCallback = resolve;
|
169 | });
|
170 | }
|
171 | }
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | export class CompositionTransaction {
|
177 | |
178 |
|
179 |
|
180 | constructor() {
|
181 | this._ownershipToken = null;
|
182 | this._compositionCount = 0;
|
183 | }
|
184 |
|
185 | |
186 |
|
187 |
|
188 |
|
189 | tryCapture(): CompositionTransactionOwnershipToken {
|
190 | return this._ownershipToken === null
|
191 | ? new CompositionTransactionOwnershipToken(this)
|
192 | : null;
|
193 | }
|
194 |
|
195 | |
196 |
|
197 |
|
198 |
|
199 | enlist(): CompositionTransactionNotifier {
|
200 | return new CompositionTransactionNotifier(this);
|
201 | }
|
202 |
|
203 | _tryCompleteTransaction() {
|
204 | if (this._compositionCount <= 0) {
|
205 | this._compositionCount = 0;
|
206 |
|
207 | if (this._ownershipToken !== null) {
|
208 | let token = this._ownershipToken;
|
209 | this._ownershipToken = null;
|
210 | token.resolve();
|
211 | }
|
212 | }
|
213 | }
|
214 | }
|
215 |
|
216 | const capitalMatcher = /([A-Z])/g;
|
217 |
|
218 | function addHyphenAndLower(char) {
|
219 | return '-' + char.toLowerCase();
|
220 | }
|
221 |
|
222 | export function _hyphenate(name) {
|
223 | return (name.charAt(0).toLowerCase() + name.slice(1)).replace(capitalMatcher, addHyphenAndLower);
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | export function _isAllWhitespace(node) {
|
230 |
|
231 | return !(node.auInterpolationTarget || (/[^\t\n\r ]/.test(node.textContent)));
|
232 | }
|
233 |
|
234 | export class ViewEngineHooksResource {
|
235 | constructor() {}
|
236 |
|
237 | initialize(container, target) {
|
238 | this.instance = container.get(target);
|
239 | }
|
240 |
|
241 | register(registry, name) {
|
242 | registry.registerViewEngineHooks(this.instance);
|
243 | }
|
244 |
|
245 | load(container, target) {}
|
246 |
|
247 | static convention(name) {
|
248 | if (name.endsWith('ViewEngineHooks')) {
|
249 | return new ViewEngineHooksResource();
|
250 | }
|
251 | }
|
252 | }
|
253 |
|
254 | export function viewEngineHooks(target) {
|
255 | let deco = function(t) {
|
256 | metadata.define(metadata.resource, new ViewEngineHooksResource(), t);
|
257 | };
|
258 |
|
259 | return target ? deco(target) : deco;
|
260 | }
|
261 |
|
262 | interface EventHandler {
|
263 | eventName: string;
|
264 | bubbles: boolean;
|
265 | capture: boolean;
|
266 | dispose: Function;
|
267 | handler: Function;
|
268 | }
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | export class ElementEvents {
|
275 | constructor(element: EventTarget) {
|
276 | this.element = element;
|
277 | this.subscriptions = {};
|
278 | }
|
279 |
|
280 | _enqueueHandler(handler: EventHandler): void {
|
281 | this.subscriptions[handler.eventName] = this.subscriptions[handler.eventName] || [];
|
282 | this.subscriptions[handler.eventName].push(handler);
|
283 | }
|
284 |
|
285 | _dequeueHandler(handler: EventHandler): EventHandler {
|
286 | let index;
|
287 | let subscriptions = this.subscriptions[handler.eventName];
|
288 | if (subscriptions) {
|
289 | index = subscriptions.indexOf(handler);
|
290 | if (index > -1) {
|
291 | subscriptions.splice(index, 1);
|
292 | }
|
293 | }
|
294 | return handler;
|
295 | }
|
296 |
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | publish(eventName: string, detail?: Object = {}, bubbles?: boolean = true, cancelable?: boolean = true) {
|
305 | let event = DOM.createCustomEvent(eventName, {cancelable, bubbles, detail});
|
306 | this.element.dispatchEvent(event);
|
307 | }
|
308 |
|
309 | |
310 |
|
311 |
|
312 |
|
313 | subscribe(eventName: string, handler: Function, captureOrOptions?: boolean = true): EventHandler {
|
314 | if (typeof handler === 'function') {
|
315 | const eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, false);
|
316 | return eventHandler;
|
317 | }
|
318 |
|
319 | return undefined;
|
320 | }
|
321 |
|
322 | |
323 |
|
324 |
|
325 |
|
326 | subscribeOnce(eventName: string, handler: Function, captureOrOptions?: boolean = true): EventHandler {
|
327 | if (typeof handler === 'function') {
|
328 | const eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, true);
|
329 | return eventHandler;
|
330 | }
|
331 |
|
332 | return undefined;
|
333 | }
|
334 |
|
335 | |
336 |
|
337 |
|
338 |
|
339 | dispose(eventName: string): void {
|
340 | if (eventName && typeof eventName === 'string') {
|
341 | let subscriptions = this.subscriptions[eventName];
|
342 | if (subscriptions) {
|
343 | while (subscriptions.length) {
|
344 | let subscription = subscriptions.pop();
|
345 | if (subscription) {
|
346 | subscription.dispose();
|
347 | }
|
348 | }
|
349 | }
|
350 | } else {
|
351 | this.disposeAll();
|
352 | }
|
353 | }
|
354 |
|
355 | |
356 |
|
357 |
|
358 | disposeAll() {
|
359 | for (let key in this.subscriptions) {
|
360 | this.dispose(key);
|
361 | }
|
362 | }
|
363 | }
|
364 |
|
365 | class EventHandlerImpl {
|
366 | constructor(owner: ElementEvents, eventName: string, handler: Function, captureOrOptions: boolean, once: boolean) {
|
367 | this.owner = owner;
|
368 | this.eventName = eventName;
|
369 | this.handler = handler;
|
370 |
|
371 | this.capture = typeof captureOrOptions === 'boolean' ? captureOrOptions : captureOrOptions.capture;
|
372 | this.bubbles = !this.capture;
|
373 | this.captureOrOptions = captureOrOptions;
|
374 | this.once = once;
|
375 | owner.element.addEventListener(eventName, this, captureOrOptions);
|
376 | owner._enqueueHandler(this);
|
377 | }
|
378 |
|
379 | handleEvent(e) {
|
380 |
|
381 | const fn = this.handler;
|
382 | fn(e);
|
383 | if (this.once) {
|
384 | this.dispose();
|
385 | }
|
386 | }
|
387 |
|
388 | dispose() {
|
389 | this.owner.element.removeEventListener(this.eventName, this, this.captureOrOptions);
|
390 | this.owner._dequeueHandler(this);
|
391 | this.owner = this.handler = null;
|
392 | }
|
393 | }
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | export class ResourceLoadContext {
|
399 | dependencies: Object;
|
400 |
|
401 | |
402 |
|
403 |
|
404 | constructor() {
|
405 | this.dependencies = {};
|
406 | }
|
407 |
|
408 | |
409 |
|
410 |
|
411 |
|
412 | addDependency(url: string): void {
|
413 | this.dependencies[url] = true;
|
414 | }
|
415 |
|
416 | |
417 |
|
418 |
|
419 |
|
420 | hasDependency(url: string): boolean {
|
421 | return url in this.dependencies;
|
422 | }
|
423 | }
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | export class ViewCompileInstruction {
|
429 | targetShadowDOM: boolean;
|
430 | compileSurrogate: boolean;
|
431 | associatedModuleId: any;
|
432 |
|
433 | |
434 |
|
435 |
|
436 | static normal: ViewCompileInstruction;
|
437 |
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 | constructor(targetShadowDOM?: boolean = false, compileSurrogate?: boolean = false) {
|
444 | this.targetShadowDOM = targetShadowDOM;
|
445 | this.compileSurrogate = compileSurrogate;
|
446 | this.associatedModuleId = null;
|
447 | }
|
448 | }
|
449 |
|
450 | ViewCompileInstruction.normal = new ViewCompileInstruction();
|
451 |
|
452 |
|
453 |
|
454 |
|
455 | interface ViewCreateInstruction {
|
456 | |
457 |
|
458 |
|
459 | enhance?: boolean;
|
460 | |
461 |
|
462 |
|
463 | partReplacements?: Object;
|
464 | }
|
465 |
|
466 |
|
467 |
|
468 |
|
469 | export class BehaviorInstruction {
|
470 |
|
471 | initiatedByBehavior: boolean;
|
472 | enhance: boolean;
|
473 | partReplacements: any;
|
474 | viewFactory: ViewFactory;
|
475 | originalAttrName: string;
|
476 | skipContentProcessing: boolean;
|
477 | contentFactory: any;
|
478 | viewModel: Object;
|
479 | anchorIsContainer: boolean;
|
480 | host: Element;
|
481 | attributes: Object;
|
482 | type: HtmlBehaviorResource;
|
483 | attrName: string;
|
484 | inheritBindingContext: boolean;
|
485 |
|
486 | |
487 |
|
488 |
|
489 | static normal: BehaviorInstruction;
|
490 |
|
491 | |
492 |
|
493 |
|
494 |
|
495 | static enhance(): BehaviorInstruction {
|
496 | let instruction = new BehaviorInstruction();
|
497 | instruction.enhance = true;
|
498 | return instruction;
|
499 | }
|
500 |
|
501 | |
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | static unitTest(type: HtmlBehaviorResource, attributes: Object): BehaviorInstruction {
|
508 | let instruction = new BehaviorInstruction();
|
509 | instruction.type = type;
|
510 | instruction.attributes = attributes || {};
|
511 | return instruction;
|
512 | }
|
513 |
|
514 | |
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 | static element(node: Node, type: HtmlBehaviorResource): BehaviorInstruction {
|
521 | let instruction = new BehaviorInstruction();
|
522 | instruction.type = type;
|
523 | instruction.attributes = {};
|
524 | instruction.anchorIsContainer = !(node.hasAttribute('containerless') || type.containerless);
|
525 | instruction.initiatedByBehavior = true;
|
526 | return instruction;
|
527 | }
|
528 |
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | static attribute(attrName: string, type?: HtmlBehaviorResource): BehaviorInstruction {
|
536 | let instruction = new BehaviorInstruction();
|
537 | instruction.attrName = attrName;
|
538 | instruction.type = type || null;
|
539 | instruction.attributes = {};
|
540 | return instruction;
|
541 | }
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 | static dynamic(host: Element, viewModel: Object, viewFactory: ViewFactory): BehaviorInstruction {
|
551 | let instruction = new BehaviorInstruction();
|
552 | instruction.host = host;
|
553 | instruction.viewModel = viewModel;
|
554 | instruction.viewFactory = viewFactory;
|
555 | instruction.inheritBindingContext = true;
|
556 | return instruction;
|
557 | }
|
558 |
|
559 | |
560 |
|
561 |
|
562 | constructor() {
|
563 | this.initiatedByBehavior = false;
|
564 | this.enhance = false;
|
565 | this.partReplacements = null;
|
566 | this.viewFactory = null;
|
567 | this.originalAttrName = null;
|
568 | this.skipContentProcessing = false;
|
569 | this.contentFactory = null;
|
570 | this.viewModel = null;
|
571 | this.anchorIsContainer = false;
|
572 | this.host = null;
|
573 | this.attributes = null;
|
574 | this.type = null;
|
575 | this.attrName = null;
|
576 | this.inheritBindingContext = false;
|
577 | }
|
578 | }
|
579 |
|
580 | BehaviorInstruction.normal = new BehaviorInstruction();
|
581 |
|
582 |
|
583 |
|
584 |
|
585 | export class TargetInstruction {
|
586 |
|
587 | injectorId:number;
|
588 | parentInjectorId:number;
|
589 |
|
590 | shadowSlot:boolean;
|
591 | slotName:string;
|
592 | slotFallbackFactory:any;
|
593 |
|
594 | contentExpression:any;
|
595 |
|
596 | expressions:Array<Object>;
|
597 | behaviorInstructions:Array<BehaviorInstruction>;
|
598 | providers:Array<Function>;
|
599 |
|
600 | viewFactory:ViewFactory;
|
601 |
|
602 | anchorIsContainer:boolean;
|
603 | elementInstruction:BehaviorInstruction;
|
604 | lifting:boolean;
|
605 |
|
606 | values:Object;
|
607 |
|
608 | |
609 |
|
610 |
|
611 | static noExpressions = Object.freeze([]);
|
612 |
|
613 | |
614 |
|
615 |
|
616 |
|
617 |
|
618 | static shadowSlot(parentInjectorId: number): TargetInstruction {
|
619 | let instruction = new TargetInstruction();
|
620 | instruction.parentInjectorId = parentInjectorId;
|
621 | instruction.shadowSlot = true;
|
622 | return instruction;
|
623 | }
|
624 |
|
625 | |
626 |
|
627 |
|
628 |
|
629 |
|
630 | static contentExpression(expression): TargetInstruction {
|
631 | let instruction = new TargetInstruction();
|
632 | instruction.contentExpression = expression;
|
633 | return instruction;
|
634 | }
|
635 |
|
636 | |
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | static lifting(parentInjectorId: number, liftingInstruction: BehaviorInstruction): TargetInstruction {
|
643 | let instruction = new TargetInstruction();
|
644 | instruction.parentInjectorId = parentInjectorId;
|
645 | instruction.expressions = TargetInstruction.noExpressions;
|
646 | instruction.behaviorInstructions = [liftingInstruction];
|
647 | instruction.viewFactory = liftingInstruction.viewFactory;
|
648 | instruction.providers = [liftingInstruction.type.target];
|
649 | instruction.lifting = true;
|
650 | return instruction;
|
651 | }
|
652 |
|
653 | |
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 | static normal(injectorId: number, parentInjectorId: number, providers: Array<Function>, behaviorInstructions: Array<BehaviorInstruction>, expressions: Array<Object>, elementInstruction: BehaviorInstruction): TargetInstruction {
|
664 | let instruction = new TargetInstruction();
|
665 | instruction.injectorId = injectorId;
|
666 | instruction.parentInjectorId = parentInjectorId;
|
667 | instruction.providers = providers;
|
668 | instruction.behaviorInstructions = behaviorInstructions;
|
669 | instruction.expressions = expressions;
|
670 | instruction.anchorIsContainer = elementInstruction ? elementInstruction.anchorIsContainer : true;
|
671 | instruction.elementInstruction = elementInstruction;
|
672 | return instruction;
|
673 | }
|
674 |
|
675 | |
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 | static surrogate(providers: Array<Function>, behaviorInstructions: Array<BehaviorInstruction>, expressions: Array<Object>, values: Object): TargetInstruction {
|
684 | let instruction = new TargetInstruction();
|
685 | instruction.expressions = expressions;
|
686 | instruction.behaviorInstructions = behaviorInstructions;
|
687 | instruction.providers = providers;
|
688 | instruction.values = values;
|
689 | return instruction;
|
690 | }
|
691 |
|
692 | |
693 |
|
694 |
|
695 | constructor() {
|
696 | this.injectorId = null;
|
697 | this.parentInjectorId = null;
|
698 |
|
699 | this.shadowSlot = false;
|
700 | this.slotName = null;
|
701 | this.slotFallbackFactory = null;
|
702 |
|
703 | this.contentExpression = null;
|
704 |
|
705 | this.expressions = null;
|
706 | this.behaviorInstructions = null;
|
707 | this.providers = null;
|
708 |
|
709 | this.viewFactory = null;
|
710 |
|
711 | this.anchorIsContainer = false;
|
712 | this.elementInstruction = null;
|
713 | this.lifting = false;
|
714 |
|
715 | this.values = null;
|
716 | }
|
717 | }
|
718 |
|
719 |
|
720 |
|
721 |
|
722 | interface ViewStrategy {
|
723 | |
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory>;
|
732 | }
|
733 |
|
734 |
|
735 |
|
736 |
|
737 | export const viewStrategy: Function = protocol.create('aurelia:view-strategy', {
|
738 | validate(target) {
|
739 | if (!(typeof target.loadViewFactory === 'function')) {
|
740 | return 'View strategies must implement: loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext): Promise<ViewFactory>';
|
741 | }
|
742 |
|
743 | return true;
|
744 | },
|
745 | compose(target) {
|
746 | if (!(typeof target.makeRelativeTo === 'function')) {
|
747 | target.makeRelativeTo = PLATFORM.noop;
|
748 | }
|
749 | }
|
750 | });
|
751 |
|
752 |
|
753 |
|
754 |
|
755 | @viewStrategy()
|
756 | export class RelativeViewStrategy {
|
757 | |
758 |
|
759 |
|
760 |
|
761 | constructor(path: string) {
|
762 | this.path = path;
|
763 | this.absolutePath = null;
|
764 | }
|
765 |
|
766 | |
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory> {
|
775 | if (this.absolutePath === null && this.moduleId) {
|
776 | this.absolutePath = relativeToFile(this.path, this.moduleId);
|
777 | }
|
778 |
|
779 | compileInstruction.associatedModuleId = this.moduleId;
|
780 | return viewEngine.loadViewFactory(this.absolutePath || this.path, compileInstruction, loadContext, target);
|
781 | }
|
782 |
|
783 | |
784 |
|
785 |
|
786 |
|
787 | makeRelativeTo(file: string): void {
|
788 | if (this.absolutePath === null) {
|
789 | this.absolutePath = relativeToFile(this.path, file);
|
790 | }
|
791 | }
|
792 | }
|
793 |
|
794 |
|
795 |
|
796 |
|
797 | @viewStrategy()
|
798 | export class ConventionalViewStrategy {
|
799 | |
800 |
|
801 |
|
802 |
|
803 |
|
804 | constructor(viewLocator: ViewLocator, origin: Origin) {
|
805 | this.moduleId = origin.moduleId;
|
806 | this.viewUrl = viewLocator.convertOriginToViewUrl(origin);
|
807 | }
|
808 |
|
809 | |
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory> {
|
818 | compileInstruction.associatedModuleId = this.moduleId;
|
819 | return viewEngine.loadViewFactory(this.viewUrl, compileInstruction, loadContext, target);
|
820 | }
|
821 | }
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 | @viewStrategy()
|
828 | export class NoViewStrategy {
|
829 | |
830 |
|
831 |
|
832 |
|
833 |
|
834 | constructor(dependencies?: Array<string|Function|Object>, dependencyBaseUrl?: string) {
|
835 | this.dependencies = dependencies || null;
|
836 | this.dependencyBaseUrl = dependencyBaseUrl || '';
|
837 | }
|
838 |
|
839 | |
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory> {
|
848 | let entry = this.entry;
|
849 | let dependencies = this.dependencies;
|
850 |
|
851 | if (entry && entry.factoryIsReady) {
|
852 | return Promise.resolve(null);
|
853 | }
|
854 |
|
855 | this.entry = entry = new TemplateRegistryEntry(this.moduleId || this.dependencyBaseUrl);
|
856 |
|
857 |
|
858 | entry.dependencies = [];
|
859 | entry.templateIsLoaded = true;
|
860 |
|
861 | if (dependencies !== null) {
|
862 | for (let i = 0, ii = dependencies.length; i < ii; ++i) {
|
863 | let current = dependencies[i];
|
864 |
|
865 | if (typeof current === 'string' || typeof current === 'function') {
|
866 | entry.addDependency(current);
|
867 | } else {
|
868 | entry.addDependency(current.from, current.as);
|
869 | }
|
870 | }
|
871 | }
|
872 |
|
873 | compileInstruction.associatedModuleId = this.moduleId;
|
874 |
|
875 |
|
876 | return viewEngine.loadViewFactory(entry, compileInstruction, loadContext, target);
|
877 | }
|
878 | }
|
879 |
|
880 |
|
881 |
|
882 |
|
883 | @viewStrategy()
|
884 | export class TemplateRegistryViewStrategy {
|
885 | |
886 |
|
887 |
|
888 |
|
889 |
|
890 | constructor(moduleId: string, entry: TemplateRegistryEntry) {
|
891 | this.moduleId = moduleId;
|
892 | this.entry = entry;
|
893 | }
|
894 |
|
895 | |
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory> {
|
904 | let entry = this.entry;
|
905 |
|
906 | if (entry.factoryIsReady) {
|
907 | return Promise.resolve(entry.factory);
|
908 | }
|
909 |
|
910 | compileInstruction.associatedModuleId = this.moduleId;
|
911 | return viewEngine.loadViewFactory(entry, compileInstruction, loadContext, target);
|
912 | }
|
913 | }
|
914 |
|
915 |
|
916 |
|
917 |
|
918 | @viewStrategy()
|
919 | export class InlineViewStrategy {
|
920 | |
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 | constructor(markup: string, dependencies?: Array<string|Function|Object>, dependencyBaseUrl?: string) {
|
927 | this.markup = markup;
|
928 | this.dependencies = dependencies || null;
|
929 | this.dependencyBaseUrl = dependencyBaseUrl || '';
|
930 | }
|
931 |
|
932 | |
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory> {
|
941 | let entry = this.entry;
|
942 | let dependencies = this.dependencies;
|
943 |
|
944 | if (entry && entry.factoryIsReady) {
|
945 | return Promise.resolve(entry.factory);
|
946 | }
|
947 |
|
948 | this.entry = entry = new TemplateRegistryEntry(this.moduleId || this.dependencyBaseUrl);
|
949 | entry.template = DOM.createTemplateFromMarkup(this.markup);
|
950 |
|
951 | if (dependencies !== null) {
|
952 | for (let i = 0, ii = dependencies.length; i < ii; ++i) {
|
953 | let current = dependencies[i];
|
954 |
|
955 | if (typeof current === 'string' || typeof current === 'function') {
|
956 | entry.addDependency(current);
|
957 | } else {
|
958 | entry.addDependency(current.from, current.as);
|
959 | }
|
960 | }
|
961 | }
|
962 |
|
963 | compileInstruction.associatedModuleId = this.moduleId;
|
964 | return viewEngine.loadViewFactory(entry, compileInstruction, loadContext, target);
|
965 | }
|
966 | }
|
967 |
|
968 | interface IStaticViewConfig {
|
969 | template: string | HTMLTemplateElement;
|
970 | dependencies?: Function[] | (() => Array<Function | Promise<Function | Record<string, Function>>>);
|
971 | }
|
972 |
|
973 | @viewStrategy()
|
974 | export class StaticViewStrategy {
|
975 |
|
976 |
|
977 | template: string | HTMLTemplateElement;
|
978 |
|
979 | dependencies: Function[] | (() => Array<Function | Promise<Function | Record<string, Function>>>);
|
980 | factoryIsReady: boolean;
|
981 | factory: ViewFactory;
|
982 |
|
983 | constructor(config: string | HTMLTemplateElement | IStaticViewConfig) {
|
984 | if (typeof config === 'string' || config instanceof HTMLTemplateElement) {
|
985 | config = {
|
986 | template: config
|
987 | };
|
988 | }
|
989 | this.template = config.template;
|
990 | this.dependencies = config.dependencies || [];
|
991 | this.factoryIsReady = false;
|
992 | this.onReady = null;
|
993 | }
|
994 |
|
995 | |
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 | loadViewFactory(viewEngine: ViewEngine, compileInstruction: ViewCompileInstruction, loadContext: ResourceLoadContext, target: any): Promise<ViewFactory> {
|
1004 | if (this.factoryIsReady) {
|
1005 | return Promise.resolve(this.factory);
|
1006 | }
|
1007 | let deps = this.dependencies;
|
1008 | deps = typeof deps === 'function' ? deps() : deps;
|
1009 | deps = deps ? deps : [];
|
1010 | deps = Array.isArray(deps) ? deps : [deps];
|
1011 |
|
1012 | return Promise.all(deps).then((dependencies) => {
|
1013 | const container = viewEngine.container;
|
1014 | const appResources = viewEngine.appResources;
|
1015 | const viewCompiler = viewEngine.viewCompiler;
|
1016 | const viewResources = new ViewResources(appResources);
|
1017 |
|
1018 | let resource;
|
1019 | let elDeps = [];
|
1020 |
|
1021 | if (target) {
|
1022 |
|
1023 | viewResources.autoRegister(container, target);
|
1024 | }
|
1025 |
|
1026 | for (let dep of dependencies) {
|
1027 | if (typeof dep === 'function') {
|
1028 |
|
1029 | resource = viewResources.autoRegister(container, dep);
|
1030 | } else if (dep && typeof dep === 'object') {
|
1031 |
|
1032 | for (let key in dep) {
|
1033 | let exported = dep[key];
|
1034 | if (typeof exported === 'function') {
|
1035 | resource = viewResources.autoRegister(container, exported);
|
1036 | }
|
1037 | }
|
1038 | } else {
|
1039 | throw new Error(`dependency neither function nor object. Received: "${typeof dep}"`);
|
1040 | }
|
1041 | if (resource.elementName !== null) {
|
1042 | elDeps.push(resource);
|
1043 | }
|
1044 | }
|
1045 |
|
1046 | return Promise.all(elDeps.map(el => el.load(container, el.target))).then(() => {
|
1047 | const factory = viewCompiler.compile(this.template, viewResources, compileInstruction);
|
1048 | this.factoryIsReady = true;
|
1049 | this.factory = factory;
|
1050 | return factory;
|
1051 | });
|
1052 | });
|
1053 | }
|
1054 | }
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 | export class ViewLocator {
|
1060 | |
1061 |
|
1062 |
|
1063 | static viewStrategyMetadataKey = 'aurelia:view-strategy';
|
1064 |
|
1065 | |
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 | getViewStrategy(value: any): ViewStrategy {
|
1071 | if (!value) {
|
1072 | return null;
|
1073 | }
|
1074 |
|
1075 | if (typeof value === 'object' && 'getViewStrategy' in value) {
|
1076 | let origin = Origin.get(value.constructor);
|
1077 |
|
1078 | value = value.getViewStrategy();
|
1079 |
|
1080 | if (typeof value === 'string') {
|
1081 | value = new RelativeViewStrategy(value);
|
1082 | }
|
1083 |
|
1084 | viewStrategy.assert(value);
|
1085 |
|
1086 | if (origin.moduleId) {
|
1087 | value.makeRelativeTo(origin.moduleId);
|
1088 | }
|
1089 |
|
1090 | return value;
|
1091 | }
|
1092 |
|
1093 | if (typeof value === 'string') {
|
1094 | value = new RelativeViewStrategy(value);
|
1095 | }
|
1096 |
|
1097 | if (viewStrategy.validate(value)) {
|
1098 | return value;
|
1099 | }
|
1100 |
|
1101 | if (typeof value !== 'function') {
|
1102 | value = value.constructor;
|
1103 | }
|
1104 |
|
1105 |
|
1106 | if ('$view' in value) {
|
1107 | let c = value.$view;
|
1108 | let view;
|
1109 | c = typeof c === 'function' ? c.call(value) : c;
|
1110 | if (c === null) {
|
1111 | view = new NoViewStrategy();
|
1112 | } else {
|
1113 | view = c instanceof StaticViewStrategy ? c : new StaticViewStrategy(c);
|
1114 | }
|
1115 | metadata.define(ViewLocator.viewStrategyMetadataKey, view, value);
|
1116 | return view;
|
1117 | }
|
1118 |
|
1119 | let origin = Origin.get(value);
|
1120 | let strategy = metadata.get(ViewLocator.viewStrategyMetadataKey, value);
|
1121 |
|
1122 | if (!strategy) {
|
1123 | if (!origin.moduleId) {
|
1124 | throw new Error('Cannot determine default view strategy for object.', value);
|
1125 | }
|
1126 |
|
1127 | strategy = this.createFallbackViewStrategy(origin);
|
1128 | } else if (origin.moduleId) {
|
1129 | strategy.moduleId = origin.moduleId;
|
1130 | }
|
1131 |
|
1132 | return strategy;
|
1133 | }
|
1134 |
|
1135 | |
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 | createFallbackViewStrategy(origin: Origin): ViewStrategy {
|
1142 | return new ConventionalViewStrategy(this, origin);
|
1143 | }
|
1144 |
|
1145 | |
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 | convertOriginToViewUrl(origin: Origin): string {
|
1152 | let moduleId = origin.moduleId;
|
1153 | let id = (moduleId.endsWith('.js') || moduleId.endsWith('.ts')) ? moduleId.substring(0, moduleId.length - 3) : moduleId;
|
1154 | return id + '.html';
|
1155 | }
|
1156 | }
|
1157 |
|
1158 | function mi(name) {
|
1159 | throw new Error(`BindingLanguage must implement ${name}().`);
|
1160 | }
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 | export class BindingLanguage {
|
1166 | |
1167 |
|
1168 |
|
1169 |
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 | inspectAttribute(resources: ViewResources, elementName: string, attrName: string, attrValue: string): Object {
|
1175 | mi('inspectAttribute');
|
1176 | }
|
1177 |
|
1178 | |
1179 |
|
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 | createAttributeInstruction(resources: ViewResources, element: Element, info: Object, existingInstruction?: Object): BehaviorInstruction {
|
1187 | mi('createAttributeInstruction');
|
1188 | }
|
1189 |
|
1190 | |
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 | inspectTextContent(resources: ViewResources, value: string): Object {
|
1197 | mi('inspectTextContent');
|
1198 | }
|
1199 | }
|
1200 |
|
1201 | let noNodes = Object.freeze([]);
|
1202 |
|
1203 | export class SlotCustomAttribute {
|
1204 | static inject() {
|
1205 | return [DOM.Element];
|
1206 | }
|
1207 |
|
1208 | constructor(element) {
|
1209 | this.element = element;
|
1210 | this.element.auSlotAttribute = this;
|
1211 | }
|
1212 |
|
1213 | valueChanged(newValue, oldValue) {
|
1214 |
|
1215 | }
|
1216 | }
|
1217 |
|
1218 | export class PassThroughSlot {
|
1219 | constructor(anchor, name, destinationName, fallbackFactory) {
|
1220 | this.anchor = anchor;
|
1221 | this.anchor.viewSlot = this;
|
1222 | this.name = name;
|
1223 | this.destinationName = destinationName;
|
1224 | this.fallbackFactory = fallbackFactory;
|
1225 | this.destinationSlot = null;
|
1226 | this.projections = 0;
|
1227 | this.contentView = null;
|
1228 |
|
1229 | let attr = new SlotCustomAttribute(this.anchor);
|
1230 | attr.value = this.destinationName;
|
1231 | }
|
1232 |
|
1233 | get needsFallbackRendering() {
|
1234 | return this.fallbackFactory && this.projections === 0;
|
1235 | }
|
1236 |
|
1237 | renderFallbackContent(view, nodes, projectionSource, index) {
|
1238 | if (this.contentView === null) {
|
1239 | this.contentView = this.fallbackFactory.create(this.ownerView.container);
|
1240 | this.contentView.bind(this.ownerView.bindingContext, this.ownerView.overrideContext);
|
1241 |
|
1242 | let slots = Object.create(null);
|
1243 | slots[this.destinationSlot.name] = this.destinationSlot;
|
1244 |
|
1245 | ShadowDOM.distributeView(this.contentView, slots, projectionSource, index, this.destinationSlot.name);
|
1246 | }
|
1247 | }
|
1248 |
|
1249 | passThroughTo(destinationSlot) {
|
1250 | this.destinationSlot = destinationSlot;
|
1251 | }
|
1252 |
|
1253 | addNode(view, node, projectionSource, index) {
|
1254 | if (this.contentView !== null) {
|
1255 | this.contentView.removeNodes();
|
1256 | this.contentView.detached();
|
1257 | this.contentView.unbind();
|
1258 | this.contentView = null;
|
1259 | }
|
1260 |
|
1261 | if (node.viewSlot instanceof PassThroughSlot) {
|
1262 | node.viewSlot.passThroughTo(this);
|
1263 | return;
|
1264 | }
|
1265 |
|
1266 | this.projections++;
|
1267 | this.destinationSlot.addNode(view, node, projectionSource, index);
|
1268 | }
|
1269 |
|
1270 | removeView(view, projectionSource) {
|
1271 | this.projections--;
|
1272 | this.destinationSlot.removeView(view, projectionSource);
|
1273 |
|
1274 | if (this.needsFallbackRendering) {
|
1275 | this.renderFallbackContent(null, noNodes, projectionSource);
|
1276 | }
|
1277 | }
|
1278 |
|
1279 | removeAll(projectionSource) {
|
1280 | this.projections = 0;
|
1281 | this.destinationSlot.removeAll(projectionSource);
|
1282 |
|
1283 | if (this.needsFallbackRendering) {
|
1284 | this.renderFallbackContent(null, noNodes, projectionSource);
|
1285 | }
|
1286 | }
|
1287 |
|
1288 | projectFrom(view, projectionSource) {
|
1289 | this.destinationSlot.projectFrom(view, projectionSource);
|
1290 | }
|
1291 |
|
1292 | created(ownerView) {
|
1293 | this.ownerView = ownerView;
|
1294 | }
|
1295 |
|
1296 | bind(view) {
|
1297 | if (this.contentView) {
|
1298 | this.contentView.bind(view.bindingContext, view.overrideContext);
|
1299 | }
|
1300 | }
|
1301 |
|
1302 | attached() {
|
1303 | if (this.contentView) {
|
1304 | this.contentView.attached();
|
1305 | }
|
1306 | }
|
1307 |
|
1308 | detached() {
|
1309 | if (this.contentView) {
|
1310 | this.contentView.detached();
|
1311 | }
|
1312 | }
|
1313 |
|
1314 | unbind() {
|
1315 | if (this.contentView) {
|
1316 | this.contentView.unbind();
|
1317 | }
|
1318 | }
|
1319 | }
|
1320 |
|
1321 | export class ShadowSlot {
|
1322 | constructor(anchor, name, fallbackFactory) {
|
1323 | this.anchor = anchor;
|
1324 | this.anchor.isContentProjectionSource = true;
|
1325 | this.anchor.viewSlot = this;
|
1326 | this.name = name;
|
1327 | this.fallbackFactory = fallbackFactory;
|
1328 | this.contentView = null;
|
1329 | this.projections = 0;
|
1330 | this.children = [];
|
1331 | this.projectFromAnchors = null;
|
1332 | this.destinationSlots = null;
|
1333 | }
|
1334 |
|
1335 | get needsFallbackRendering() {
|
1336 | return this.fallbackFactory && this.projections === 0;
|
1337 | }
|
1338 |
|
1339 | addNode(view, node, projectionSource, index, destination) {
|
1340 | if (this.contentView !== null) {
|
1341 | this.contentView.removeNodes();
|
1342 | this.contentView.detached();
|
1343 | this.contentView.unbind();
|
1344 | this.contentView = null;
|
1345 | }
|
1346 |
|
1347 | if (node.viewSlot instanceof PassThroughSlot) {
|
1348 | node.viewSlot.passThroughTo(this);
|
1349 | return;
|
1350 | }
|
1351 |
|
1352 | if (this.destinationSlots !== null) {
|
1353 | ShadowDOM.distributeNodes(view, [node], this.destinationSlots, this, index);
|
1354 | } else {
|
1355 | node.auOwnerView = view;
|
1356 | node.auProjectionSource = projectionSource;
|
1357 | node.auAssignedSlot = this;
|
1358 |
|
1359 | let anchor = this._findAnchor(view, node, projectionSource, index);
|
1360 | let parent = anchor.parentNode;
|
1361 |
|
1362 | parent.insertBefore(node, anchor);
|
1363 | this.children.push(node);
|
1364 | this.projections++;
|
1365 | }
|
1366 | }
|
1367 |
|
1368 | removeView(view, projectionSource) {
|
1369 | if (this.destinationSlots !== null) {
|
1370 | ShadowDOM.undistributeView(view, this.destinationSlots, this);
|
1371 | } else if (this.contentView && this.contentView.hasSlots) {
|
1372 | ShadowDOM.undistributeView(view, this.contentView.slots, projectionSource);
|
1373 | } else {
|
1374 | let found = this.children.find(x => x.auSlotProjectFrom === projectionSource);
|
1375 | if (found) {
|
1376 | let children = found.auProjectionChildren;
|
1377 |
|
1378 | for (let i = 0, ii = children.length; i < ii; ++i) {
|
1379 | let child = children[i];
|
1380 |
|
1381 | if (child.auOwnerView === view) {
|
1382 | children.splice(i, 1);
|
1383 | view.fragment.appendChild(child);
|
1384 | i--; ii--;
|
1385 | this.projections--;
|
1386 | }
|
1387 | }
|
1388 |
|
1389 | if (this.needsFallbackRendering) {
|
1390 | this.renderFallbackContent(view, noNodes, projectionSource);
|
1391 | }
|
1392 | }
|
1393 | }
|
1394 | }
|
1395 |
|
1396 | removeAll(projectionSource) {
|
1397 | if (this.destinationSlots !== null) {
|
1398 | ShadowDOM.undistributeAll(this.destinationSlots, this);
|
1399 | } else if (this.contentView && this.contentView.hasSlots) {
|
1400 | ShadowDOM.undistributeAll(this.contentView.slots, projectionSource);
|
1401 | } else {
|
1402 | let found = this.children.find(x => x.auSlotProjectFrom === projectionSource);
|
1403 |
|
1404 | if (found) {
|
1405 | let children = found.auProjectionChildren;
|
1406 | for (let i = 0, ii = children.length; i < ii; ++i) {
|
1407 | let child = children[i];
|
1408 | child.auOwnerView.fragment.appendChild(child);
|
1409 | this.projections--;
|
1410 | }
|
1411 |
|
1412 | found.auProjectionChildren = [];
|
1413 |
|
1414 | if (this.needsFallbackRendering) {
|
1415 | this.renderFallbackContent(null, noNodes, projectionSource);
|
1416 | }
|
1417 | }
|
1418 | }
|
1419 | }
|
1420 |
|
1421 | _findAnchor(view, node, projectionSource, index) {
|
1422 | if (projectionSource) {
|
1423 |
|
1424 | let found = this.children.find(x => x.auSlotProjectFrom === projectionSource);
|
1425 | if (found) {
|
1426 | if (index !== undefined) {
|
1427 | let children = found.auProjectionChildren;
|
1428 | let viewIndex = -1;
|
1429 | let lastView;
|
1430 |
|
1431 | for (let i = 0, ii = children.length; i < ii; ++i) {
|
1432 | let current = children[i];
|
1433 |
|
1434 | if (current.auOwnerView !== lastView) {
|
1435 | viewIndex++;
|
1436 | lastView = current.auOwnerView;
|
1437 |
|
1438 | if (viewIndex >= index && lastView !== view) {
|
1439 | children.splice(i, 0, node);
|
1440 | return current;
|
1441 | }
|
1442 | }
|
1443 | }
|
1444 | }
|
1445 |
|
1446 | found.auProjectionChildren.push(node);
|
1447 | return found;
|
1448 | }
|
1449 | }
|
1450 |
|
1451 | return this.anchor;
|
1452 | }
|
1453 |
|
1454 | projectTo(slots) {
|
1455 | this.destinationSlots = slots;
|
1456 | }
|
1457 |
|
1458 | projectFrom(view, projectionSource) {
|
1459 | let anchor = DOM.createComment('anchor');
|
1460 | let parent = this.anchor.parentNode;
|
1461 | anchor.auSlotProjectFrom = projectionSource;
|
1462 | anchor.auOwnerView = view;
|
1463 | anchor.auProjectionChildren = [];
|
1464 | parent.insertBefore(anchor, this.anchor);
|
1465 | this.children.push(anchor);
|
1466 |
|
1467 | if (this.projectFromAnchors === null) {
|
1468 | this.projectFromAnchors = [];
|
1469 | }
|
1470 |
|
1471 | this.projectFromAnchors.push(anchor);
|
1472 | }
|
1473 |
|
1474 | renderFallbackContent(view, nodes, projectionSource, index) {
|
1475 | if (this.contentView === null) {
|
1476 | this.contentView = this.fallbackFactory.create(this.ownerView.container);
|
1477 | this.contentView.bind(this.ownerView.bindingContext, this.ownerView.overrideContext);
|
1478 | this.contentView.insertNodesBefore(this.anchor);
|
1479 | }
|
1480 |
|
1481 | if (this.contentView.hasSlots) {
|
1482 | let slots = this.contentView.slots;
|
1483 | let projectFromAnchors = this.projectFromAnchors;
|
1484 |
|
1485 | if (projectFromAnchors !== null) {
|
1486 | for (let slotName in slots) {
|
1487 | let slot = slots[slotName];
|
1488 |
|
1489 | for (let i = 0, ii = projectFromAnchors.length; i < ii; ++i) {
|
1490 | let anchor = projectFromAnchors[i];
|
1491 | slot.projectFrom(anchor.auOwnerView, anchor.auSlotProjectFrom);
|
1492 | }
|
1493 | }
|
1494 | }
|
1495 |
|
1496 | this.fallbackSlots = slots;
|
1497 | ShadowDOM.distributeNodes(view, nodes, slots, projectionSource, index);
|
1498 | }
|
1499 | }
|
1500 |
|
1501 | created(ownerView) {
|
1502 | this.ownerView = ownerView;
|
1503 | }
|
1504 |
|
1505 | bind(view) {
|
1506 | if (this.contentView) {
|
1507 | this.contentView.bind(view.bindingContext, view.overrideContext);
|
1508 | }
|
1509 | }
|
1510 |
|
1511 | attached() {
|
1512 | if (this.contentView) {
|
1513 | this.contentView.attached();
|
1514 | }
|
1515 | }
|
1516 |
|
1517 | detached() {
|
1518 | if (this.contentView) {
|
1519 | this.contentView.detached();
|
1520 | }
|
1521 | }
|
1522 |
|
1523 | unbind() {
|
1524 | if (this.contentView) {
|
1525 | this.contentView.unbind();
|
1526 | }
|
1527 | }
|
1528 | }
|
1529 |
|
1530 | export class ShadowDOM {
|
1531 | static defaultSlotKey = '__au-default-slot-key__';
|
1532 |
|
1533 | static getSlotName(node) {
|
1534 | if (node.auSlotAttribute === undefined) {
|
1535 | return ShadowDOM.defaultSlotKey;
|
1536 | }
|
1537 |
|
1538 | return node.auSlotAttribute.value;
|
1539 | }
|
1540 |
|
1541 | static distributeView(view, slots, projectionSource, index, destinationOverride) {
|
1542 | let nodes;
|
1543 |
|
1544 | if (view === null) {
|
1545 | nodes = noNodes;
|
1546 | } else {
|
1547 | let childNodes = view.fragment.childNodes;
|
1548 | let ii = childNodes.length;
|
1549 | nodes = new Array(ii);
|
1550 |
|
1551 | for (let i = 0; i < ii; ++i) {
|
1552 | nodes[i] = childNodes[i];
|
1553 | }
|
1554 | }
|
1555 |
|
1556 | ShadowDOM.distributeNodes(
|
1557 | view,
|
1558 | nodes,
|
1559 | slots,
|
1560 | projectionSource,
|
1561 | index,
|
1562 | destinationOverride
|
1563 | );
|
1564 | }
|
1565 |
|
1566 | static undistributeView(view, slots, projectionSource) {
|
1567 | for (let slotName in slots) {
|
1568 | slots[slotName].removeView(view, projectionSource);
|
1569 | }
|
1570 | }
|
1571 |
|
1572 | static undistributeAll(slots, projectionSource) {
|
1573 | for (let slotName in slots) {
|
1574 | slots[slotName].removeAll(projectionSource);
|
1575 | }
|
1576 | }
|
1577 |
|
1578 | static distributeNodes(view, nodes, slots, projectionSource, index, destinationOverride) {
|
1579 | for (let i = 0, ii = nodes.length; i < ii; ++i) {
|
1580 | let currentNode = nodes[i];
|
1581 | let nodeType = currentNode.nodeType;
|
1582 |
|
1583 | if (currentNode.isContentProjectionSource) {
|
1584 | currentNode.viewSlot.projectTo(slots);
|
1585 |
|
1586 | for (let slotName in slots) {
|
1587 | slots[slotName].projectFrom(view, currentNode.viewSlot);
|
1588 | }
|
1589 |
|
1590 | nodes.splice(i, 1);
|
1591 | ii--; i--;
|
1592 | } else if (nodeType === 1 || nodeType === 3 || currentNode.viewSlot instanceof PassThroughSlot) {
|
1593 | if (nodeType === 3 && _isAllWhitespace(currentNode)) {
|
1594 | nodes.splice(i, 1);
|
1595 | ii--; i--;
|
1596 | } else {
|
1597 | let found = slots[destinationOverride || ShadowDOM.getSlotName(currentNode)];
|
1598 |
|
1599 | if (found) {
|
1600 | found.addNode(view, currentNode, projectionSource, index);
|
1601 | nodes.splice(i, 1);
|
1602 | ii--; i--;
|
1603 | }
|
1604 | }
|
1605 | } else {
|
1606 | nodes.splice(i, 1);
|
1607 | ii--; i--;
|
1608 | }
|
1609 | }
|
1610 |
|
1611 | for (let slotName in slots) {
|
1612 | let slot = slots[slotName];
|
1613 |
|
1614 | if (slot.needsFallbackRendering) {
|
1615 | slot.renderFallbackContent(view, nodes, projectionSource, index);
|
1616 | }
|
1617 | }
|
1618 | }
|
1619 | }
|
1620 |
|
1621 | function register(lookup, name, resource, type) {
|
1622 | if (!name) {
|
1623 | return;
|
1624 | }
|
1625 |
|
1626 | let existing = lookup[name];
|
1627 | if (existing) {
|
1628 | if (existing !== resource) {
|
1629 | throw new Error(`Attempted to register ${type} when one with the same name already exists. Name: ${name}.`);
|
1630 | }
|
1631 |
|
1632 | return;
|
1633 | }
|
1634 |
|
1635 | lookup[name] = resource;
|
1636 | }
|
1637 |
|
1638 |
|
1639 |
|
1640 |
|
1641 | interface ViewEngineHooks {
|
1642 | |
1643 |
|
1644 |
|
1645 |
|
1646 |
|
1647 |
|
1648 | beforeCompile?: (content: DocumentFragment, resources: ViewResources, instruction: ViewCompileInstruction) => void;
|
1649 | |
1650 |
|
1651 |
|
1652 |
|
1653 | afterCompile?: (viewFactory: ViewFactory) => void;
|
1654 | |
1655 |
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 | beforeCreate?: (viewFactory: ViewFactory, container: Container, content: DocumentFragment, instruction: ViewCreateInstruction) => void;
|
1662 | |
1663 |
|
1664 |
|
1665 |
|
1666 | afterCreate?: (view: View) => void;
|
1667 |
|
1668 | |
1669 |
|
1670 |
|
1671 |
|
1672 | beforeBind?: (view: View) => void;
|
1673 |
|
1674 | |
1675 |
|
1676 |
|
1677 |
|
1678 | beforeUnbind?: (view: View) => void;
|
1679 | }
|
1680 |
|
1681 | interface IBindablePropertyConfig {
|
1682 | |
1683 |
|
1684 |
|
1685 | name?: string;
|
1686 | attribute?: string;
|
1687 | |
1688 |
|
1689 |
|
1690 | defaultBindingMode?: bindingMode | 'oneTime' | 'oneWay' | 'twoWay' | 'fromView' | 'toView';
|
1691 | |
1692 |
|
1693 |
|
1694 | changeHandler?: string;
|
1695 | |
1696 |
|
1697 |
|
1698 | defaultValue?: any;
|
1699 | |
1700 |
|
1701 |
|
1702 | primaryProperty?: boolean;
|
1703 |
|
1704 | [key: string]: any;
|
1705 | }
|
1706 |
|
1707 | interface IStaticResourceConfig {
|
1708 | |
1709 |
|
1710 |
|
1711 | type?: 'element' | 'attribute' | 'valueConverter' | 'bindingBehavior' | 'viewEngineHooks';
|
1712 | |
1713 |
|
1714 |
|
1715 | name?: string;
|
1716 | |
1717 |
|
1718 |
|
1719 | templateController?: boolean;
|
1720 | |
1721 |
|
1722 |
|
1723 | defaultBindingMode?: bindingMode | 'oneTime' | 'oneWay' | 'twoWay' | 'fromView' | 'toView';
|
1724 | |
1725 |
|
1726 |
|
1727 | hasDynamicOptions?: boolean;
|
1728 | |
1729 |
|
1730 |
|
1731 | usesShadowDOM?: boolean;
|
1732 | |
1733 |
|
1734 |
|
1735 | shadowDOMOptions?: ShadowRootInit;
|
1736 | |
1737 |
|
1738 |
|
1739 | containerless?: boolean;
|
1740 | |
1741 |
|
1742 |
|
1743 | processAttributes?: (viewCompiler: ViewCompiler, resources: ViewResources, node: Element, attributes: NamedNodeMap, elementInstruction: BehaviorInstruction) => void;
|
1744 | |
1745 |
|
1746 |
|
1747 |
|
1748 |
|
1749 |
|
1750 |
|
1751 | processContent?: (viewCompiler: ViewCompiler, resources: ViewResources, node: Element, instruction: BehaviorInstruction) => boolean;
|
1752 | |
1753 |
|
1754 |
|
1755 | bindables?: (string | IBindablePropertyConfig)[];
|
1756 | }
|
1757 |
|
1758 | export function validateBehaviorName(name: string, type: string) {
|
1759 | if (/[A-Z]/.test(name)) {
|
1760 | let newName = _hyphenate(name);
|
1761 | LogManager
|
1762 | .getLogger('templating')
|
1763 | .warn(`'${name}' is not a valid ${type} name and has been converted to '${newName}'. Upper-case letters are not allowed because the DOM is not case-sensitive.`);
|
1764 | return newName;
|
1765 | }
|
1766 | return name;
|
1767 | }
|
1768 |
|
1769 | const conventionMark = '__au_resource__';
|
1770 |
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 | export class ViewResources {
|
1776 |
|
1777 | |
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 | static convention(target: Function, existing?: HtmlBehaviorResource): HtmlBehaviorResource | ValueConverterResource | BindingBehaviorResource | ViewEngineHooksResource {
|
1783 | let resource;
|
1784 |
|
1785 |
|
1786 |
|
1787 | if (existing && conventionMark in existing) {
|
1788 | return existing;
|
1789 | }
|
1790 | if ('$resource' in target) {
|
1791 | let config = target.$resource;
|
1792 |
|
1793 | if (typeof config === 'string') {
|
1794 |
|
1795 |
|
1796 | resource = existing || new HtmlBehaviorResource();
|
1797 | resource[conventionMark] = true;
|
1798 | if (!resource.elementName) {
|
1799 |
|
1800 | resource.elementName = validateBehaviorName(config, 'custom element');
|
1801 | }
|
1802 | } else {
|
1803 |
|
1804 | if (typeof config === 'function') {
|
1805 |
|
1806 | config = config.call(target);
|
1807 | }
|
1808 | if (typeof config === 'string') {
|
1809 |
|
1810 |
|
1811 | config = { name: config };
|
1812 | }
|
1813 |
|
1814 |
|
1815 |
|
1816 | config = Object.assign({}, config);
|
1817 |
|
1818 | let resourceType = config.type || 'element';
|
1819 |
|
1820 |
|
1821 | let name = config.name;
|
1822 | switch (resourceType) {
|
1823 | case 'element': case 'attribute':
|
1824 |
|
1825 | resource = existing || new HtmlBehaviorResource();
|
1826 | resource[conventionMark] = true;
|
1827 | if (resourceType === 'element') {
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 | if (!resource.elementName) {
|
1833 | resource.elementName = name
|
1834 | ? validateBehaviorName(name, 'custom element')
|
1835 | : _hyphenate(target.name);
|
1836 | }
|
1837 | } else {
|
1838 |
|
1839 |
|
1840 |
|
1841 |
|
1842 | if (!resource.attributeName) {
|
1843 | resource.attributeName = name
|
1844 | ? validateBehaviorName(name, 'custom attribute')
|
1845 | : _hyphenate(target.name);
|
1846 | }
|
1847 | }
|
1848 | if ('templateController' in config) {
|
1849 |
|
1850 | config.liftsContent = config.templateController;
|
1851 | delete config.templateController;
|
1852 | }
|
1853 | if ('defaultBindingMode' in config && resource.attributeDefaultBindingMode !== undefined) {
|
1854 |
|
1855 |
|
1856 | config.attributeDefaultBindingMode = config.defaultBindingMode;
|
1857 | delete config.defaultBindingMode;
|
1858 | }
|
1859 |
|
1860 | delete config.name;
|
1861 |
|
1862 | Object.assign(resource, config);
|
1863 | break;
|
1864 | case 'valueConverter':
|
1865 | resource = new ValueConverterResource(camelCase(name || target.name));
|
1866 | break;
|
1867 | case 'bindingBehavior':
|
1868 | resource = new BindingBehaviorResource(camelCase(name || target.name));
|
1869 | break;
|
1870 | case 'viewEngineHooks':
|
1871 | resource = new ViewEngineHooksResource();
|
1872 | break;
|
1873 | }
|
1874 | }
|
1875 |
|
1876 | if (resource instanceof HtmlBehaviorResource) {
|
1877 |
|
1878 |
|
1879 |
|
1880 |
|
1881 |
|
1882 | let bindables = typeof config === 'string' ? undefined : config.bindables;
|
1883 | let currentProps = resource.properties;
|
1884 | if (Array.isArray(bindables)) {
|
1885 | for (let i = 0, ii = bindables.length; ii > i; ++i) {
|
1886 | let prop = bindables[i];
|
1887 | if (!prop || (typeof prop !== 'string' && !prop.name)) {
|
1888 | throw new Error(`Invalid bindable property at "${i}" for class "${target.name}". Expected either a string or an object with "name" property.`);
|
1889 | }
|
1890 | let newProp = new BindableProperty(prop);
|
1891 |
|
1892 |
|
1893 |
|
1894 | let existed = false;
|
1895 | for (let j = 0, jj = currentProps.length; jj > j; ++j) {
|
1896 | if (currentProps[j].name === newProp.name) {
|
1897 | existed = true;
|
1898 | break;
|
1899 | }
|
1900 | }
|
1901 | if (existed) {
|
1902 | continue;
|
1903 | }
|
1904 | newProp.registerWith(target, resource);
|
1905 | }
|
1906 | }
|
1907 | }
|
1908 | }
|
1909 | return resource;
|
1910 | }
|
1911 |
|
1912 | |
1913 |
|
1914 |
|
1915 | bindingLanguage = null;
|
1916 |
|
1917 | |
1918 |
|
1919 |
|
1920 |
|
1921 |
|
1922 | constructor(parent?: ViewResources, viewUrl?: string) {
|
1923 | this.parent = parent || null;
|
1924 | this.hasParent = this.parent !== null;
|
1925 | this.viewUrl = viewUrl || '';
|
1926 | this.lookupFunctions = {
|
1927 | valueConverters: this.getValueConverter.bind(this),
|
1928 | bindingBehaviors: this.getBindingBehavior.bind(this)
|
1929 | };
|
1930 | this.attributes = Object.create(null);
|
1931 | this.elements = Object.create(null);
|
1932 | this.valueConverters = Object.create(null);
|
1933 | this.bindingBehaviors = Object.create(null);
|
1934 | this.attributeMap = Object.create(null);
|
1935 | this.values = Object.create(null);
|
1936 | this.beforeCompile = this.afterCompile = this.beforeCreate = this.afterCreate = this.beforeBind = this.beforeUnbind = false;
|
1937 | }
|
1938 |
|
1939 | _tryAddHook(obj, name) {
|
1940 | if (typeof obj[name] === 'function') {
|
1941 | let func = obj[name].bind(obj);
|
1942 | let counter = 1;
|
1943 | let callbackName;
|
1944 |
|
1945 | while (this[callbackName = name + counter.toString()] !== undefined) {
|
1946 | counter++;
|
1947 | }
|
1948 |
|
1949 | this[name] = true;
|
1950 | this[callbackName] = func;
|
1951 | }
|
1952 | }
|
1953 |
|
1954 | _invokeHook(name, one, two, three, four) {
|
1955 | if (this.hasParent) {
|
1956 | this.parent._invokeHook(name, one, two, three, four);
|
1957 | }
|
1958 |
|
1959 | if (this[name]) {
|
1960 | this[name + '1'](one, two, three, four);
|
1961 |
|
1962 | let callbackName = name + '2';
|
1963 | if (this[callbackName]) {
|
1964 | this[callbackName](one, two, three, four);
|
1965 |
|
1966 | callbackName = name + '3';
|
1967 | if (this[callbackName]) {
|
1968 | this[callbackName](one, two, three, four);
|
1969 |
|
1970 | let counter = 4;
|
1971 |
|
1972 | while (this[callbackName = name + counter.toString()] !== undefined) {
|
1973 | this[callbackName](one, two, three, four);
|
1974 | counter++;
|
1975 | }
|
1976 | }
|
1977 | }
|
1978 | }
|
1979 | }
|
1980 |
|
1981 | |
1982 |
|
1983 |
|
1984 |
|
1985 | registerViewEngineHooks(hooks: ViewEngineHooks): void {
|
1986 | this._tryAddHook(hooks, 'beforeCompile');
|
1987 | this._tryAddHook(hooks, 'afterCompile');
|
1988 | this._tryAddHook(hooks, 'beforeCreate');
|
1989 | this._tryAddHook(hooks, 'afterCreate');
|
1990 | this._tryAddHook(hooks, 'beforeBind');
|
1991 | this._tryAddHook(hooks, 'beforeUnbind');
|
1992 | }
|
1993 |
|
1994 | |
1995 |
|
1996 |
|
1997 |
|
1998 |
|
1999 | getBindingLanguage(bindingLanguageFallback: BindingLanguage): BindingLanguage {
|
2000 | return this.bindingLanguage || (this.bindingLanguage = bindingLanguageFallback);
|
2001 | }
|
2002 |
|
2003 | |
2004 |
|
2005 |
|
2006 |
|
2007 | patchInParent(newParent: ViewResources): void {
|
2008 | let originalParent = this.parent;
|
2009 |
|
2010 | this.parent = newParent || null;
|
2011 | this.hasParent = this.parent !== null;
|
2012 |
|
2013 | if (newParent.parent === null) {
|
2014 | newParent.parent = originalParent;
|
2015 | newParent.hasParent = originalParent !== null;
|
2016 | }
|
2017 | }
|
2018 |
|
2019 | |
2020 |
|
2021 |
|
2022 |
|
2023 |
|
2024 | relativeToView(path: string): string {
|
2025 | return relativeToFile(path, this.viewUrl);
|
2026 | }
|
2027 |
|
2028 | |
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 | registerElement(tagName: string, behavior: HtmlBehaviorResource): void {
|
2034 | register(this.elements, tagName, behavior, 'an Element');
|
2035 | }
|
2036 |
|
2037 | |
2038 |
|
2039 |
|
2040 |
|
2041 |
|
2042 | getElement(tagName: string): HtmlBehaviorResource {
|
2043 | return this.elements[tagName] || (this.hasParent ? this.parent.getElement(tagName) : null);
|
2044 | }
|
2045 |
|
2046 | |
2047 |
|
2048 |
|
2049 |
|
2050 |
|
2051 | mapAttribute(attribute: string): string {
|
2052 | return this.attributeMap[attribute] || (this.hasParent ? this.parent.mapAttribute(attribute) : null);
|
2053 | }
|
2054 |
|
2055 | |
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 |
|
2061 | registerAttribute(attribute: string, behavior: HtmlBehaviorResource, knownAttribute: string): void {
|
2062 | this.attributeMap[attribute] = knownAttribute;
|
2063 | register(this.attributes, attribute, behavior, 'an Attribute');
|
2064 | }
|
2065 |
|
2066 | |
2067 |
|
2068 |
|
2069 |
|
2070 |
|
2071 | getAttribute(attribute: string): HtmlBehaviorResource {
|
2072 | return this.attributes[attribute] || (this.hasParent ? this.parent.getAttribute(attribute) : null);
|
2073 | }
|
2074 |
|
2075 | |
2076 |
|
2077 |
|
2078 |
|
2079 |
|
2080 | registerValueConverter(name: string, valueConverter: Object): void {
|
2081 | register(this.valueConverters, name, valueConverter, 'a ValueConverter');
|
2082 | }
|
2083 |
|
2084 | |
2085 |
|
2086 |
|
2087 |
|
2088 |
|
2089 | getValueConverter(name: string): Object {
|
2090 | return this.valueConverters[name] || (this.hasParent ? this.parent.getValueConverter(name) : null);
|
2091 | }
|
2092 |
|
2093 | |
2094 |
|
2095 |
|
2096 |
|
2097 |
|
2098 | registerBindingBehavior(name: string, bindingBehavior: Object): void {
|
2099 | register(this.bindingBehaviors, name, bindingBehavior, 'a BindingBehavior');
|
2100 | }
|
2101 |
|
2102 | |
2103 |
|
2104 |
|
2105 |
|
2106 |
|
2107 | getBindingBehavior(name: string): Object {
|
2108 | return this.bindingBehaviors[name] || (this.hasParent ? this.parent.getBindingBehavior(name) : null);
|
2109 | }
|
2110 |
|
2111 | |
2112 |
|
2113 |
|
2114 |
|
2115 |
|
2116 | registerValue(name: string, value: any): void {
|
2117 | register(this.values, name, value, 'a value');
|
2118 | }
|
2119 |
|
2120 | |
2121 |
|
2122 |
|
2123 |
|
2124 |
|
2125 | getValue(name: string): any {
|
2126 | return this.values[name] || (this.hasParent ? this.parent.getValue(name) : null);
|
2127 | }
|
2128 |
|
2129 | |
2130 |
|
2131 |
|
2132 |
|
2133 |
|
2134 |
|
2135 |
|
2136 |
|
2137 |
|
2138 |
|
2139 | autoRegister(container, impl) {
|
2140 | let resourceTypeMeta = metadata.getOwn(metadata.resource, impl);
|
2141 | if (resourceTypeMeta) {
|
2142 | if (resourceTypeMeta instanceof HtmlBehaviorResource) {
|
2143 |
|
2144 | ViewResources.convention(impl, resourceTypeMeta);
|
2145 |
|
2146 |
|
2147 | if (resourceTypeMeta.attributeName === null && resourceTypeMeta.elementName === null) {
|
2148 |
|
2149 | HtmlBehaviorResource.convention(impl.name, resourceTypeMeta);
|
2150 | }
|
2151 | if (resourceTypeMeta.attributeName === null && resourceTypeMeta.elementName === null) {
|
2152 |
|
2153 | resourceTypeMeta.elementName = _hyphenate(impl.name);
|
2154 | }
|
2155 | }
|
2156 | } else {
|
2157 | resourceTypeMeta = ViewResources.convention(impl)
|
2158 | || HtmlBehaviorResource.convention(impl.name)
|
2159 | || ValueConverterResource.convention(impl.name)
|
2160 | || BindingBehaviorResource.convention(impl.name)
|
2161 | || ViewEngineHooksResource.convention(impl.name);
|
2162 | if (!resourceTypeMeta) {
|
2163 |
|
2164 | resourceTypeMeta = new HtmlBehaviorResource();
|
2165 | resourceTypeMeta.elementName = _hyphenate(impl.name);
|
2166 | }
|
2167 | metadata.define(metadata.resource, resourceTypeMeta, impl);
|
2168 | }
|
2169 | resourceTypeMeta.initialize(container, impl);
|
2170 | resourceTypeMeta.register(this);
|
2171 | return resourceTypeMeta;
|
2172 | }
|
2173 | }
|
2174 |
|
2175 |
|
2176 |
|
2177 |
|
2178 |
|
2179 | interface ViewNode {
|
2180 | |
2181 |
|
2182 |
|
2183 |
|
2184 |
|
2185 | bind(bindingContext: Object, overrideContext?: Object): void;
|
2186 | |
2187 |
|
2188 |
|
2189 | attached(): void;
|
2190 | |
2191 |
|
2192 |
|
2193 | detached(): void;
|
2194 | |
2195 |
|
2196 |
|
2197 | unbind(): void;
|
2198 | }
|
2199 |
|
2200 | export class View {
|
2201 | |
2202 |
|
2203 |
|
2204 | container: Container;
|
2205 |
|
2206 | |
2207 |
|
2208 |
|
2209 | viewFactory: ViewFactory;
|
2210 |
|
2211 | |
2212 |
|
2213 |
|
2214 | fragment: DocumentFragment | Element;
|
2215 |
|
2216 | |
2217 |
|
2218 |
|
2219 | bindingContext: Object;
|
2220 |
|
2221 | |
2222 |
|
2223 |
|
2224 | overrideContext: Object;
|
2225 |
|
2226 | |
2227 |
|
2228 |
|
2229 | controller: Controller;
|
2230 |
|
2231 | |
2232 |
|
2233 |
|
2234 |
|
2235 |
|
2236 |
|
2237 |
|
2238 |
|
2239 |
|
2240 | constructor(container: Container, viewFactory: ViewFactory, fragment: DocumentFragment, controllers: Controller[], bindings: Binding[], children: ViewNode[], slots: Object) {
|
2241 | this.container = container;
|
2242 | this.viewFactory = viewFactory;
|
2243 | this.resources = viewFactory.resources;
|
2244 | this.fragment = fragment;
|
2245 | this.firstChild = fragment.firstChild;
|
2246 | this.lastChild = fragment.lastChild;
|
2247 | this.controllers = controllers;
|
2248 | this.bindings = bindings;
|
2249 | this.children = children;
|
2250 | this.slots = slots;
|
2251 | this.hasSlots = false;
|
2252 | this.fromCache = false;
|
2253 | this.isBound = false;
|
2254 | this.isAttached = false;
|
2255 | this.bindingContext = null;
|
2256 | this.overrideContext = null;
|
2257 | this.controller = null;
|
2258 | this.viewModelScope = null;
|
2259 | this.animatableElement = undefined;
|
2260 | this._isUserControlled = false;
|
2261 | this.contentView = null;
|
2262 |
|
2263 | for (let key in slots) {
|
2264 | this.hasSlots = true;
|
2265 | break;
|
2266 | }
|
2267 | }
|
2268 |
|
2269 | |
2270 |
|
2271 |
|
2272 | returnToCache(): void {
|
2273 | this.viewFactory.returnViewToCache(this);
|
2274 | }
|
2275 |
|
2276 | |
2277 |
|
2278 |
|
2279 | created(): void {
|
2280 | let i;
|
2281 | let ii;
|
2282 | let controllers = this.controllers;
|
2283 |
|
2284 | for (i = 0, ii = controllers.length; i < ii; ++i) {
|
2285 | controllers[i].created(this);
|
2286 | }
|
2287 | }
|
2288 |
|
2289 | |
2290 |
|
2291 |
|
2292 |
|
2293 |
|
2294 | bind(bindingContext: Object, overrideContext?: Object, _systemUpdate?: boolean): void {
|
2295 | let controllers;
|
2296 | let bindings;
|
2297 | let children;
|
2298 | let i;
|
2299 | let ii;
|
2300 |
|
2301 | if (_systemUpdate && this._isUserControlled) {
|
2302 | return;
|
2303 | }
|
2304 |
|
2305 | if (this.isBound) {
|
2306 | if (this.bindingContext === bindingContext) {
|
2307 | return;
|
2308 | }
|
2309 |
|
2310 | this.unbind();
|
2311 | }
|
2312 |
|
2313 | this.isBound = true;
|
2314 | this.bindingContext = bindingContext;
|
2315 | this.overrideContext = overrideContext || createOverrideContext(bindingContext);
|
2316 |
|
2317 | this.resources._invokeHook('beforeBind', this);
|
2318 |
|
2319 | bindings = this.bindings;
|
2320 | for (i = 0, ii = bindings.length; i < ii; ++i) {
|
2321 | bindings[i].bind(this);
|
2322 | }
|
2323 |
|
2324 | if (this.viewModelScope !== null) {
|
2325 | bindingContext.bind(this.viewModelScope.bindingContext, this.viewModelScope.overrideContext);
|
2326 | this.viewModelScope = null;
|
2327 | }
|
2328 |
|
2329 | controllers = this.controllers;
|
2330 | for (i = 0, ii = controllers.length; i < ii; ++i) {
|
2331 | controllers[i].bind(this);
|
2332 | }
|
2333 |
|
2334 | children = this.children;
|
2335 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2336 | children[i].bind(bindingContext, overrideContext, true);
|
2337 | }
|
2338 |
|
2339 | if (this.hasSlots) {
|
2340 | ShadowDOM.distributeView(this.contentView, this.slots);
|
2341 | }
|
2342 | }
|
2343 |
|
2344 | |
2345 |
|
2346 |
|
2347 |
|
2348 | addBinding(binding: Object): void {
|
2349 | this.bindings.push(binding);
|
2350 |
|
2351 | if (this.isBound) {
|
2352 | binding.bind(this);
|
2353 | }
|
2354 | }
|
2355 |
|
2356 | |
2357 |
|
2358 |
|
2359 | unbind(): void {
|
2360 | let controllers;
|
2361 | let bindings;
|
2362 | let children;
|
2363 | let i;
|
2364 | let ii;
|
2365 |
|
2366 | if (this.isBound) {
|
2367 | this.isBound = false;
|
2368 | this.resources._invokeHook('beforeUnbind', this);
|
2369 |
|
2370 | if (this.controller !== null) {
|
2371 | this.controller.unbind();
|
2372 | }
|
2373 |
|
2374 | bindings = this.bindings;
|
2375 | for (i = 0, ii = bindings.length; i < ii; ++i) {
|
2376 | bindings[i].unbind();
|
2377 | }
|
2378 |
|
2379 | controllers = this.controllers;
|
2380 | for (i = 0, ii = controllers.length; i < ii; ++i) {
|
2381 | controllers[i].unbind();
|
2382 | }
|
2383 |
|
2384 | children = this.children;
|
2385 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2386 | children[i].unbind();
|
2387 | }
|
2388 |
|
2389 | this.bindingContext = null;
|
2390 | this.overrideContext = null;
|
2391 | }
|
2392 | }
|
2393 |
|
2394 | |
2395 |
|
2396 |
|
2397 |
|
2398 | insertNodesBefore(refNode: Node): void {
|
2399 | refNode.parentNode.insertBefore(this.fragment, refNode);
|
2400 | }
|
2401 |
|
2402 | |
2403 |
|
2404 |
|
2405 |
|
2406 | appendNodesTo(parent: Element): void {
|
2407 | parent.appendChild(this.fragment);
|
2408 | }
|
2409 |
|
2410 | |
2411 |
|
2412 |
|
2413 | removeNodes(): void {
|
2414 | let fragment = this.fragment;
|
2415 | let current = this.firstChild;
|
2416 | let end = this.lastChild;
|
2417 | let next;
|
2418 |
|
2419 | while (current) {
|
2420 | next = current.nextSibling;
|
2421 | fragment.appendChild(current);
|
2422 |
|
2423 | if (current === end) {
|
2424 | break;
|
2425 | }
|
2426 |
|
2427 | current = next;
|
2428 | }
|
2429 | }
|
2430 |
|
2431 | |
2432 |
|
2433 |
|
2434 | attached(): void {
|
2435 | let controllers;
|
2436 | let children;
|
2437 | let i;
|
2438 | let ii;
|
2439 |
|
2440 | if (this.isAttached) {
|
2441 | return;
|
2442 | }
|
2443 |
|
2444 | this.isAttached = true;
|
2445 |
|
2446 | if (this.controller !== null) {
|
2447 | this.controller.attached();
|
2448 | }
|
2449 |
|
2450 | controllers = this.controllers;
|
2451 | for (i = 0, ii = controllers.length; i < ii; ++i) {
|
2452 | controllers[i].attached();
|
2453 | }
|
2454 |
|
2455 | children = this.children;
|
2456 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2457 | children[i].attached();
|
2458 | }
|
2459 | }
|
2460 |
|
2461 | |
2462 |
|
2463 |
|
2464 | detached(): void {
|
2465 | let controllers;
|
2466 | let children;
|
2467 | let i;
|
2468 | let ii;
|
2469 |
|
2470 | if (this.isAttached) {
|
2471 | this.isAttached = false;
|
2472 |
|
2473 | if (this.controller !== null) {
|
2474 | this.controller.detached();
|
2475 | }
|
2476 |
|
2477 | controllers = this.controllers;
|
2478 | for (i = 0, ii = controllers.length; i < ii; ++i) {
|
2479 | controllers[i].detached();
|
2480 | }
|
2481 |
|
2482 | children = this.children;
|
2483 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2484 | children[i].detached();
|
2485 | }
|
2486 | }
|
2487 | }
|
2488 | }
|
2489 |
|
2490 |
|
2491 |
|
2492 |
|
2493 | interface ComponentCreated {
|
2494 | |
2495 |
|
2496 |
|
2497 |
|
2498 |
|
2499 |
|
2500 |
|
2501 | created(owningView: View, myView: View): void;
|
2502 | }
|
2503 |
|
2504 |
|
2505 |
|
2506 |
|
2507 | interface ComponentBind {
|
2508 | |
2509 |
|
2510 |
|
2511 |
|
2512 |
|
2513 |
|
2514 | bind(bindingContext: any, overrideContext: any): void;
|
2515 | }
|
2516 |
|
2517 |
|
2518 |
|
2519 |
|
2520 | interface ComponentAttached {
|
2521 | |
2522 |
|
2523 |
|
2524 | attached(): void;
|
2525 | }
|
2526 |
|
2527 |
|
2528 |
|
2529 |
|
2530 | interface ComponentDetached {
|
2531 | |
2532 |
|
2533 |
|
2534 | detached(): void;
|
2535 | }
|
2536 |
|
2537 |
|
2538 |
|
2539 |
|
2540 | interface ComponentUnbind {
|
2541 | |
2542 |
|
2543 |
|
2544 | unbind(): void;
|
2545 | }
|
2546 |
|
2547 |
|
2548 |
|
2549 |
|
2550 | interface DynamicComponentGetViewStrategy {
|
2551 | |
2552 |
|
2553 |
|
2554 | getViewStrategy(): string|ViewStrategy;
|
2555 | }
|
2556 |
|
2557 | function getAnimatableElement(view) {
|
2558 | if (view.animatableElement !== undefined) {
|
2559 | return view.animatableElement;
|
2560 | }
|
2561 |
|
2562 | let current = view.firstChild;
|
2563 |
|
2564 | while (current && current.nodeType !== 1) {
|
2565 | current = current.nextSibling;
|
2566 | }
|
2567 |
|
2568 | if (current && current.nodeType === 1) {
|
2569 | return (view.animatableElement = current.classList.contains('au-animate') ? current : null);
|
2570 | }
|
2571 |
|
2572 | return (view.animatableElement = null);
|
2573 | }
|
2574 |
|
2575 |
|
2576 |
|
2577 |
|
2578 |
|
2579 | export class ViewSlot {
|
2580 | |
2581 |
|
2582 |
|
2583 |
|
2584 |
|
2585 |
|
2586 | constructor(anchor: Node, anchorIsContainer: boolean, animator?: Animator = Animator.instance) {
|
2587 | this.anchor = anchor;
|
2588 | this.anchorIsContainer = anchorIsContainer;
|
2589 | this.bindingContext = null;
|
2590 | this.overrideContext = null;
|
2591 | this.animator = animator;
|
2592 | this.children = [];
|
2593 | this.isBound = false;
|
2594 | this.isAttached = false;
|
2595 | this.contentSelectors = null;
|
2596 | anchor.viewSlot = this;
|
2597 | anchor.isContentProjectionSource = false;
|
2598 | }
|
2599 |
|
2600 | |
2601 |
|
2602 |
|
2603 |
|
2604 |
|
2605 |
|
2606 | animateView(view: View, direction: string = 'enter'): void | Promise<any> {
|
2607 | let animatableElement = getAnimatableElement(view);
|
2608 |
|
2609 | if (animatableElement !== null) {
|
2610 | switch (direction) {
|
2611 | case 'enter':
|
2612 | return this.animator.enter(animatableElement);
|
2613 | case 'leave':
|
2614 | return this.animator.leave(animatableElement);
|
2615 | default:
|
2616 | throw new Error('Invalid animation direction: ' + direction);
|
2617 | }
|
2618 | }
|
2619 | }
|
2620 |
|
2621 | |
2622 |
|
2623 |
|
2624 |
|
2625 | transformChildNodesIntoView(): void {
|
2626 | let parent = this.anchor;
|
2627 |
|
2628 | this.children.push({
|
2629 | fragment: parent,
|
2630 | firstChild: parent.firstChild,
|
2631 | lastChild: parent.lastChild,
|
2632 | returnToCache() {},
|
2633 | removeNodes() {
|
2634 | let last;
|
2635 |
|
2636 | while (last = parent.lastChild) {
|
2637 | parent.removeChild(last);
|
2638 | }
|
2639 | },
|
2640 | created() {},
|
2641 | bind() {},
|
2642 | unbind() {},
|
2643 | attached() {},
|
2644 | detached() {}
|
2645 | });
|
2646 | }
|
2647 |
|
2648 | |
2649 |
|
2650 |
|
2651 |
|
2652 |
|
2653 | bind(bindingContext: Object, overrideContext: Object): void {
|
2654 | let i;
|
2655 | let ii;
|
2656 | let children;
|
2657 |
|
2658 | if (this.isBound) {
|
2659 | if (this.bindingContext === bindingContext) {
|
2660 | return;
|
2661 | }
|
2662 |
|
2663 | this.unbind();
|
2664 | }
|
2665 |
|
2666 | this.isBound = true;
|
2667 | this.bindingContext = bindingContext = bindingContext || this.bindingContext;
|
2668 | this.overrideContext = overrideContext = overrideContext || this.overrideContext;
|
2669 |
|
2670 | children = this.children;
|
2671 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2672 | children[i].bind(bindingContext, overrideContext, true);
|
2673 | }
|
2674 | }
|
2675 |
|
2676 | |
2677 |
|
2678 |
|
2679 | unbind(): void {
|
2680 | if (this.isBound) {
|
2681 | let i;
|
2682 | let ii;
|
2683 | let children = this.children;
|
2684 |
|
2685 | this.isBound = false;
|
2686 | this.bindingContext = null;
|
2687 | this.overrideContext = null;
|
2688 |
|
2689 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2690 | children[i].unbind();
|
2691 | }
|
2692 | }
|
2693 | }
|
2694 |
|
2695 | |
2696 |
|
2697 |
|
2698 |
|
2699 |
|
2700 | add(view: View): void | Promise<any> {
|
2701 | if (this.anchorIsContainer) {
|
2702 | view.appendNodesTo(this.anchor);
|
2703 | } else {
|
2704 | view.insertNodesBefore(this.anchor);
|
2705 | }
|
2706 |
|
2707 | this.children.push(view);
|
2708 |
|
2709 | if (this.isAttached) {
|
2710 | view.attached();
|
2711 | return this.animateView(view, 'enter');
|
2712 | }
|
2713 | }
|
2714 |
|
2715 | |
2716 |
|
2717 |
|
2718 |
|
2719 |
|
2720 |
|
2721 | insert(index: number, view: View): void | Promise<any> {
|
2722 | let children = this.children;
|
2723 | let length = children.length;
|
2724 |
|
2725 | if ((index === 0 && length === 0) || index >= length) {
|
2726 | return this.add(view);
|
2727 | }
|
2728 |
|
2729 | view.insertNodesBefore(children[index].firstChild);
|
2730 | children.splice(index, 0, view);
|
2731 |
|
2732 | if (this.isAttached) {
|
2733 | view.attached();
|
2734 | return this.animateView(view, 'enter');
|
2735 | }
|
2736 | }
|
2737 |
|
2738 | |
2739 |
|
2740 |
|
2741 |
|
2742 |
|
2743 | move(sourceIndex, targetIndex) {
|
2744 | if (sourceIndex === targetIndex) {
|
2745 | return;
|
2746 | }
|
2747 |
|
2748 | const children = this.children;
|
2749 | const view = children[sourceIndex];
|
2750 |
|
2751 | view.removeNodes();
|
2752 | view.insertNodesBefore(children[targetIndex].firstChild);
|
2753 | children.splice(sourceIndex, 1);
|
2754 | children.splice(targetIndex, 0, view);
|
2755 | }
|
2756 |
|
2757 | |
2758 |
|
2759 |
|
2760 |
|
2761 |
|
2762 |
|
2763 |
|
2764 | remove(view: View, returnToCache?: boolean, skipAnimation?: boolean): View | Promise<View> {
|
2765 | return this.removeAt(this.children.indexOf(view), returnToCache, skipAnimation);
|
2766 | }
|
2767 |
|
2768 | |
2769 |
|
2770 |
|
2771 |
|
2772 |
|
2773 |
|
2774 |
|
2775 | removeMany(viewsToRemove: View[], returnToCache?: boolean, skipAnimation?: boolean): void | Promise<void> {
|
2776 | const children = this.children;
|
2777 | let ii = viewsToRemove.length;
|
2778 | let i;
|
2779 | let rmPromises = [];
|
2780 |
|
2781 | viewsToRemove.forEach(child => {
|
2782 | if (skipAnimation) {
|
2783 | child.removeNodes();
|
2784 | return;
|
2785 | }
|
2786 |
|
2787 | let animation = this.animateView(child, 'leave');
|
2788 | if (animation) {
|
2789 | rmPromises.push(animation.then(() => child.removeNodes()));
|
2790 | } else {
|
2791 | child.removeNodes();
|
2792 | }
|
2793 | });
|
2794 |
|
2795 | let removeAction = () => {
|
2796 | if (this.isAttached) {
|
2797 | for (i = 0; i < ii; ++i) {
|
2798 | viewsToRemove[i].detached();
|
2799 | }
|
2800 | }
|
2801 |
|
2802 | if (returnToCache) {
|
2803 | for (i = 0; i < ii; ++i) {
|
2804 | viewsToRemove[i].returnToCache();
|
2805 | }
|
2806 | }
|
2807 |
|
2808 | for (i = 0; i < ii; ++i) {
|
2809 | const index = children.indexOf(viewsToRemove[i]);
|
2810 | if (index >= 0) {
|
2811 | children.splice(index, 1);
|
2812 | }
|
2813 | }
|
2814 | };
|
2815 |
|
2816 | if (rmPromises.length > 0) {
|
2817 | return Promise.all(rmPromises).then(() => removeAction());
|
2818 | }
|
2819 |
|
2820 | return removeAction();
|
2821 | }
|
2822 |
|
2823 | |
2824 |
|
2825 |
|
2826 |
|
2827 |
|
2828 |
|
2829 |
|
2830 | removeAt(index: number, returnToCache?: boolean, skipAnimation?: boolean): View | Promise<View> {
|
2831 | let view = this.children[index];
|
2832 |
|
2833 | let removeAction = () => {
|
2834 | index = this.children.indexOf(view);
|
2835 | view.removeNodes();
|
2836 | this.children.splice(index, 1);
|
2837 |
|
2838 | if (this.isAttached) {
|
2839 | view.detached();
|
2840 | }
|
2841 |
|
2842 | if (returnToCache) {
|
2843 | view.returnToCache();
|
2844 | }
|
2845 |
|
2846 | return view;
|
2847 | };
|
2848 |
|
2849 | if (!skipAnimation) {
|
2850 | let animation = this.animateView(view, 'leave');
|
2851 | if (animation) {
|
2852 | return animation.then(() => removeAction());
|
2853 | }
|
2854 | }
|
2855 |
|
2856 | return removeAction();
|
2857 | }
|
2858 |
|
2859 | |
2860 |
|
2861 |
|
2862 |
|
2863 |
|
2864 |
|
2865 | removeAll(returnToCache?: boolean, skipAnimation?: boolean): void | Promise<any> {
|
2866 | let children = this.children;
|
2867 | let ii = children.length;
|
2868 | let i;
|
2869 | let rmPromises = [];
|
2870 |
|
2871 | children.forEach(child => {
|
2872 | if (skipAnimation) {
|
2873 | child.removeNodes();
|
2874 | return;
|
2875 | }
|
2876 |
|
2877 | let animation = this.animateView(child, 'leave');
|
2878 | if (animation) {
|
2879 | rmPromises.push(animation.then(() => child.removeNodes()));
|
2880 | } else {
|
2881 | child.removeNodes();
|
2882 | }
|
2883 | });
|
2884 |
|
2885 | let removeAction = () => {
|
2886 | if (this.isAttached) {
|
2887 | for (i = 0; i < ii; ++i) {
|
2888 | children[i].detached();
|
2889 | }
|
2890 | }
|
2891 |
|
2892 | if (returnToCache) {
|
2893 | for (i = 0; i < ii; ++i) {
|
2894 | const child = children[i];
|
2895 |
|
2896 | if (child) {
|
2897 | child.returnToCache();
|
2898 | }
|
2899 | }
|
2900 | }
|
2901 |
|
2902 | this.children = [];
|
2903 | };
|
2904 |
|
2905 | if (rmPromises.length > 0) {
|
2906 | return Promise.all(rmPromises).then(() => removeAction());
|
2907 | }
|
2908 |
|
2909 | return removeAction();
|
2910 | }
|
2911 |
|
2912 | |
2913 |
|
2914 |
|
2915 | attached(): void {
|
2916 | let i;
|
2917 | let ii;
|
2918 | let children;
|
2919 | let child;
|
2920 |
|
2921 | if (this.isAttached) {
|
2922 | return;
|
2923 | }
|
2924 |
|
2925 | this.isAttached = true;
|
2926 |
|
2927 | children = this.children;
|
2928 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2929 | child = children[i];
|
2930 | child.attached();
|
2931 | this.animateView(child, 'enter');
|
2932 | }
|
2933 | }
|
2934 |
|
2935 | |
2936 |
|
2937 |
|
2938 | detached(): void {
|
2939 | let i;
|
2940 | let ii;
|
2941 | let children;
|
2942 |
|
2943 | if (this.isAttached) {
|
2944 | this.isAttached = false;
|
2945 | children = this.children;
|
2946 | for (i = 0, ii = children.length; i < ii; ++i) {
|
2947 | children[i].detached();
|
2948 | }
|
2949 | }
|
2950 | }
|
2951 |
|
2952 | projectTo(slots: Object): void {
|
2953 | this.projectToSlots = slots;
|
2954 | this.add = this._projectionAdd;
|
2955 | this.insert = this._projectionInsert;
|
2956 | this.move = this._projectionMove;
|
2957 | this.remove = this._projectionRemove;
|
2958 | this.removeAt = this._projectionRemoveAt;
|
2959 | this.removeMany = this._projectionRemoveMany;
|
2960 | this.removeAll = this._projectionRemoveAll;
|
2961 | this.children.forEach(view => ShadowDOM.distributeView(view, slots, this));
|
2962 | }
|
2963 |
|
2964 | _projectionAdd(view) {
|
2965 | ShadowDOM.distributeView(view, this.projectToSlots, this);
|
2966 |
|
2967 | this.children.push(view);
|
2968 |
|
2969 | if (this.isAttached) {
|
2970 | view.attached();
|
2971 | }
|
2972 | }
|
2973 |
|
2974 | _projectionInsert(index, view) {
|
2975 | if ((index === 0 && !this.children.length) || index >= this.children.length) {
|
2976 | this.add(view);
|
2977 | } else {
|
2978 | ShadowDOM.distributeView(view, this.projectToSlots, this, index);
|
2979 |
|
2980 | this.children.splice(index, 0, view);
|
2981 |
|
2982 | if (this.isAttached) {
|
2983 | view.attached();
|
2984 | }
|
2985 | }
|
2986 | }
|
2987 |
|
2988 | _projectionMove(sourceIndex, targetIndex) {
|
2989 | if (sourceIndex === targetIndex) {
|
2990 | return;
|
2991 | }
|
2992 |
|
2993 | const children = this.children;
|
2994 | const view = children[sourceIndex];
|
2995 |
|
2996 | ShadowDOM.undistributeView(view, this.projectToSlots, this);
|
2997 | ShadowDOM.distributeView(view, this.projectToSlots, this, targetIndex);
|
2998 |
|
2999 | children.splice(sourceIndex, 1);
|
3000 | children.splice(targetIndex, 0, view);
|
3001 | }
|
3002 |
|
3003 | _projectionRemove(view, returnToCache) {
|
3004 | ShadowDOM.undistributeView(view, this.projectToSlots, this);
|
3005 | this.children.splice(this.children.indexOf(view), 1);
|
3006 |
|
3007 | if (this.isAttached) {
|
3008 | view.detached();
|
3009 | }
|
3010 | if (returnToCache) {
|
3011 | view.returnToCache();
|
3012 | }
|
3013 | }
|
3014 |
|
3015 | _projectionRemoveAt(index, returnToCache) {
|
3016 | let view = this.children[index];
|
3017 |
|
3018 | ShadowDOM.undistributeView(view, this.projectToSlots, this);
|
3019 | this.children.splice(index, 1);
|
3020 |
|
3021 | if (this.isAttached) {
|
3022 | view.detached();
|
3023 | }
|
3024 | if (returnToCache) {
|
3025 | view.returnToCache();
|
3026 | }
|
3027 | }
|
3028 |
|
3029 | _projectionRemoveMany(viewsToRemove, returnToCache?) {
|
3030 | viewsToRemove.forEach(view => this.remove(view, returnToCache));
|
3031 | }
|
3032 |
|
3033 | _projectionRemoveAll(returnToCache) {
|
3034 | ShadowDOM.undistributeAll(this.projectToSlots, this);
|
3035 |
|
3036 | let children = this.children;
|
3037 | let ii = children.length;
|
3038 |
|
3039 | if (this.isAttached) {
|
3040 | for (let i = 0; i < ii; ++i) {
|
3041 | if (returnToCache) {
|
3042 | children[i].returnToCache();
|
3043 | } else {
|
3044 | children[i].detached();
|
3045 | }
|
3046 | }
|
3047 | }
|
3048 |
|
3049 | this.children = [];
|
3050 | }
|
3051 | }
|
3052 |
|
3053 | @resolver
|
3054 | class ProviderResolver {
|
3055 | get(container, key) {
|
3056 | let id = key.__providerId__;
|
3057 | return id in container ? container[id] : (container[id] = container.invoke(key));
|
3058 | }
|
3059 | }
|
3060 |
|
3061 | let providerResolverInstance = new ProviderResolver();
|
3062 |
|
3063 | function elementContainerGet(key) {
|
3064 | if (key === DOM.Element) {
|
3065 | return this.element;
|
3066 | }
|
3067 |
|
3068 | if (key === BoundViewFactory) {
|
3069 | if (this.boundViewFactory) {
|
3070 | return this.boundViewFactory;
|
3071 | }
|
3072 |
|
3073 | let factory = this.instruction.viewFactory;
|
3074 | let partReplacements = this.partReplacements;
|
3075 |
|
3076 | if (partReplacements) {
|
3077 | factory = partReplacements[factory.part] || factory;
|
3078 | }
|
3079 |
|
3080 | this.boundViewFactory = new BoundViewFactory(this, factory, partReplacements);
|
3081 | return this.boundViewFactory;
|
3082 | }
|
3083 |
|
3084 | if (key === ViewSlot) {
|
3085 | if (this.viewSlot === undefined) {
|
3086 | this.viewSlot = new ViewSlot(this.element, this.instruction.anchorIsContainer);
|
3087 | this.element.isContentProjectionSource = this.instruction.lifting;
|
3088 | this.children.push(this.viewSlot);
|
3089 | }
|
3090 |
|
3091 | return this.viewSlot;
|
3092 | }
|
3093 |
|
3094 | if (key === ElementEvents) {
|
3095 | return this.elementEvents || (this.elementEvents = new ElementEvents(this.element));
|
3096 | }
|
3097 |
|
3098 | if (key === CompositionTransaction) {
|
3099 | return this.compositionTransaction || (this.compositionTransaction = this.parent.get(key));
|
3100 | }
|
3101 |
|
3102 | if (key === ViewResources) {
|
3103 | return this.viewResources;
|
3104 | }
|
3105 |
|
3106 | if (key === TargetInstruction) {
|
3107 | return this.instruction;
|
3108 | }
|
3109 |
|
3110 | return this.superGet(key);
|
3111 | }
|
3112 |
|
3113 | function createElementContainer(parent, element, instruction, children, partReplacements, resources) {
|
3114 | let container = parent.createChild();
|
3115 | let providers;
|
3116 | let i;
|
3117 |
|
3118 | container.element = element;
|
3119 | container.instruction = instruction;
|
3120 | container.children = children;
|
3121 | container.viewResources = resources;
|
3122 | container.partReplacements = partReplacements;
|
3123 |
|
3124 | providers = instruction.providers;
|
3125 | i = providers.length;
|
3126 |
|
3127 | while (i--) {
|
3128 | container._resolvers.set(providers[i], providerResolverInstance);
|
3129 | }
|
3130 |
|
3131 | container.superGet = container.get;
|
3132 | container.get = elementContainerGet;
|
3133 |
|
3134 | return container;
|
3135 | }
|
3136 |
|
3137 | function hasAttribute(name) {
|
3138 | return this._element.hasAttribute(name);
|
3139 | }
|
3140 |
|
3141 | function getAttribute(name) {
|
3142 | return this._element.getAttribute(name);
|
3143 | }
|
3144 |
|
3145 | function setAttribute(name, value) {
|
3146 | this._element.setAttribute(name, value);
|
3147 | }
|
3148 |
|
3149 | function makeElementIntoAnchor(element, elementInstruction) {
|
3150 | let anchor = DOM.createComment('anchor');
|
3151 |
|
3152 | if (elementInstruction) {
|
3153 | let firstChild = element.firstChild;
|
3154 |
|
3155 | if (firstChild && firstChild.tagName === 'AU-CONTENT') {
|
3156 | anchor.contentElement = firstChild;
|
3157 | }
|
3158 |
|
3159 | anchor._element = element;
|
3160 |
|
3161 | anchor.hasAttribute = hasAttribute;
|
3162 | anchor.getAttribute = getAttribute;
|
3163 | anchor.setAttribute = setAttribute;
|
3164 | }
|
3165 |
|
3166 | DOM.replaceNode(anchor, element);
|
3167 |
|
3168 | return anchor;
|
3169 | }
|
3170 |
|
3171 | function applyInstructions(containers, element, instruction, controllers, bindings, children, shadowSlots, partReplacements, resources) {
|
3172 | let behaviorInstructions = instruction.behaviorInstructions;
|
3173 | let expressions = instruction.expressions;
|
3174 | let elementContainer;
|
3175 | let i;
|
3176 | let ii;
|
3177 | let current;
|
3178 | let instance;
|
3179 |
|
3180 | if (instruction.contentExpression) {
|
3181 | bindings.push(instruction.contentExpression.createBinding(element.nextSibling));
|
3182 | element.nextSibling.auInterpolationTarget = true;
|
3183 | element.parentNode.removeChild(element);
|
3184 | return;
|
3185 | }
|
3186 |
|
3187 | if (instruction.shadowSlot) {
|
3188 | let commentAnchor = DOM.createComment('slot');
|
3189 | let slot;
|
3190 |
|
3191 | if (instruction.slotDestination) {
|
3192 | slot = new PassThroughSlot(commentAnchor, instruction.slotName, instruction.slotDestination, instruction.slotFallbackFactory);
|
3193 | } else {
|
3194 | slot = new ShadowSlot(commentAnchor, instruction.slotName, instruction.slotFallbackFactory);
|
3195 | }
|
3196 |
|
3197 | DOM.replaceNode(commentAnchor, element);
|
3198 | shadowSlots[instruction.slotName] = slot;
|
3199 | controllers.push(slot);
|
3200 | return;
|
3201 | }
|
3202 |
|
3203 | if (behaviorInstructions.length) {
|
3204 | if (!instruction.anchorIsContainer) {
|
3205 | element = makeElementIntoAnchor(element, instruction.elementInstruction);
|
3206 | }
|
3207 |
|
3208 | containers[instruction.injectorId] = elementContainer =
|
3209 | createElementContainer(
|
3210 | containers[instruction.parentInjectorId],
|
3211 | element,
|
3212 | instruction,
|
3213 | children,
|
3214 | partReplacements,
|
3215 | resources
|
3216 | );
|
3217 |
|
3218 | for (i = 0, ii = behaviorInstructions.length; i < ii; ++i) {
|
3219 | current = behaviorInstructions[i];
|
3220 | instance = current.type.create(elementContainer, current, element, bindings);
|
3221 | controllers.push(instance);
|
3222 | }
|
3223 | }
|
3224 |
|
3225 | for (i = 0, ii = expressions.length; i < ii; ++i) {
|
3226 | bindings.push(expressions[i].createBinding(element));
|
3227 | }
|
3228 | }
|
3229 |
|
3230 | function styleStringToObject(style, target) {
|
3231 | let attributes = style.split(';');
|
3232 | let firstIndexOfColon;
|
3233 | let i;
|
3234 | let current;
|
3235 | let key;
|
3236 | let value;
|
3237 |
|
3238 | target = target || {};
|
3239 |
|
3240 | for (i = 0; i < attributes.length; i++) {
|
3241 | current = attributes[i];
|
3242 | firstIndexOfColon = current.indexOf(':');
|
3243 | key = current.substring(0, firstIndexOfColon).trim();
|
3244 | value = current.substring(firstIndexOfColon + 1).trim();
|
3245 | target[key] = value;
|
3246 | }
|
3247 |
|
3248 | return target;
|
3249 | }
|
3250 |
|
3251 | function styleObjectToString(obj) {
|
3252 | let result = '';
|
3253 |
|
3254 | for (let key in obj) {
|
3255 | result += key + ':' + obj[key] + ';';
|
3256 | }
|
3257 |
|
3258 | return result;
|
3259 | }
|
3260 |
|
3261 | function applySurrogateInstruction(container, element, instruction, controllers, bindings, children) {
|
3262 | let behaviorInstructions = instruction.behaviorInstructions;
|
3263 | let expressions = instruction.expressions;
|
3264 | let providers = instruction.providers;
|
3265 | let values = instruction.values;
|
3266 | let i;
|
3267 | let ii;
|
3268 | let current;
|
3269 | let instance;
|
3270 | let currentAttributeValue;
|
3271 |
|
3272 | i = providers.length;
|
3273 | while (i--) {
|
3274 | container._resolvers.set(providers[i], providerResolverInstance);
|
3275 | }
|
3276 |
|
3277 |
|
3278 | for (let key in values) {
|
3279 | currentAttributeValue = element.getAttribute(key);
|
3280 |
|
3281 | if (currentAttributeValue) {
|
3282 | if (key === 'class') {
|
3283 |
|
3284 | element.setAttribute('class', currentAttributeValue + ' ' + values[key]);
|
3285 | } else if (key === 'style') {
|
3286 |
|
3287 | let styleObject = styleStringToObject(values[key]);
|
3288 | styleStringToObject(currentAttributeValue, styleObject);
|
3289 | element.setAttribute('style', styleObjectToString(styleObject));
|
3290 | }
|
3291 |
|
3292 |
|
3293 | } else {
|
3294 |
|
3295 | element.setAttribute(key, values[key]);
|
3296 | }
|
3297 | }
|
3298 |
|
3299 |
|
3300 | if (behaviorInstructions.length) {
|
3301 | for (i = 0, ii = behaviorInstructions.length; i < ii; ++i) {
|
3302 | current = behaviorInstructions[i];
|
3303 | instance = current.type.create(container, current, element, bindings);
|
3304 |
|
3305 | if (instance.contentView) {
|
3306 | children.push(instance.contentView);
|
3307 | }
|
3308 |
|
3309 | controllers.push(instance);
|
3310 | }
|
3311 | }
|
3312 |
|
3313 |
|
3314 | for (i = 0, ii = expressions.length; i < ii; ++i) {
|
3315 | bindings.push(expressions[i].createBinding(element));
|
3316 | }
|
3317 | }
|
3318 |
|
3319 |
|
3320 |
|
3321 |
|
3322 | export class BoundViewFactory {
|
3323 | |
3324 |
|
3325 |
|
3326 |
|
3327 |
|
3328 |
|
3329 | constructor(parentContainer: Container, viewFactory: ViewFactory, partReplacements?: Object) {
|
3330 | this.parentContainer = parentContainer;
|
3331 | this.viewFactory = viewFactory;
|
3332 | this.factoryCreateInstruction = { partReplacements: partReplacements };
|
3333 | }
|
3334 |
|
3335 | |
3336 |
|
3337 |
|
3338 |
|
3339 | create(): View {
|
3340 | let view = this.viewFactory.create(this.parentContainer.createChild(), this.factoryCreateInstruction);
|
3341 | view._isUserControlled = true;
|
3342 | return view;
|
3343 | }
|
3344 |
|
3345 | |
3346 |
|
3347 |
|
3348 | get isCaching() {
|
3349 | return this.viewFactory.isCaching;
|
3350 | }
|
3351 |
|
3352 | |
3353 |
|
3354 |
|
3355 |
|
3356 |
|
3357 | setCacheSize(size: number | string, doNotOverrideIfAlreadySet: boolean): void {
|
3358 | this.viewFactory.setCacheSize(size, doNotOverrideIfAlreadySet);
|
3359 | }
|
3360 |
|
3361 | |
3362 |
|
3363 |
|
3364 |
|
3365 | getCachedView(): View {
|
3366 | return this.viewFactory.getCachedView();
|
3367 | }
|
3368 |
|
3369 | |
3370 |
|
3371 |
|
3372 |
|
3373 | returnViewToCache(view: View): void {
|
3374 | this.viewFactory.returnViewToCache(view);
|
3375 | }
|
3376 | }
|
3377 |
|
3378 |
|
3379 |
|
3380 |
|
3381 | export class ViewFactory {
|
3382 | |
3383 |
|
3384 |
|
3385 | isCaching = false;
|
3386 |
|
3387 | |
3388 |
|
3389 |
|
3390 |
|
3391 |
|
3392 |
|
3393 | constructor(template: DocumentFragment, instructions: Object, resources: ViewResources) {
|
3394 | this.template = template;
|
3395 | this.instructions = instructions;
|
3396 | this.resources = resources;
|
3397 | this.cacheSize = -1;
|
3398 | this.cache = null;
|
3399 | }
|
3400 |
|
3401 | |
3402 |
|
3403 |
|
3404 |
|
3405 |
|
3406 | setCacheSize(size: number | string, doNotOverrideIfAlreadySet: boolean): void {
|
3407 | if (size) {
|
3408 | if (size === '*') {
|
3409 | size = Number.MAX_VALUE;
|
3410 | } else if (typeof size === 'string') {
|
3411 | size = parseInt(size, 10);
|
3412 | }
|
3413 | }
|
3414 |
|
3415 | if (this.cacheSize === -1 || !doNotOverrideIfAlreadySet) {
|
3416 | this.cacheSize = size;
|
3417 | }
|
3418 |
|
3419 | if (this.cacheSize > 0) {
|
3420 | this.cache = [];
|
3421 | } else {
|
3422 | this.cache = null;
|
3423 | }
|
3424 |
|
3425 | this.isCaching = this.cacheSize > 0;
|
3426 | }
|
3427 |
|
3428 | |
3429 |
|
3430 |
|
3431 |
|
3432 | getCachedView(): View {
|
3433 | return this.cache !== null ? (this.cache.pop() || null) : null;
|
3434 | }
|
3435 |
|
3436 | |
3437 |
|
3438 |
|
3439 |
|
3440 | returnViewToCache(view: View): void {
|
3441 | if (view.isAttached) {
|
3442 | view.detached();
|
3443 | }
|
3444 |
|
3445 | if (view.isBound) {
|
3446 | view.unbind();
|
3447 | }
|
3448 |
|
3449 | if (this.cache !== null && this.cache.length < this.cacheSize) {
|
3450 | view.fromCache = true;
|
3451 | this.cache.push(view);
|
3452 | }
|
3453 | }
|
3454 |
|
3455 | |
3456 |
|
3457 |
|
3458 |
|
3459 |
|
3460 |
|
3461 |
|
3462 | create(container: Container, createInstruction?: ViewCreateInstruction, element?: Element): View {
|
3463 | createInstruction = createInstruction || BehaviorInstruction.normal;
|
3464 |
|
3465 | let cachedView = this.getCachedView();
|
3466 | if (cachedView !== null) {
|
3467 | return cachedView;
|
3468 | }
|
3469 |
|
3470 | let fragment = createInstruction.enhance ? this.template : this.template.cloneNode(true);
|
3471 | let instructables = fragment.querySelectorAll('.au-target');
|
3472 | let instructions = this.instructions;
|
3473 | let resources = this.resources;
|
3474 | let controllers = [];
|
3475 | let bindings = [];
|
3476 | let children = [];
|
3477 | let shadowSlots = Object.create(null);
|
3478 | let containers = { root: container };
|
3479 | let partReplacements = createInstruction.partReplacements;
|
3480 | let i;
|
3481 | let ii;
|
3482 | let view;
|
3483 | let instructable;
|
3484 | let instruction;
|
3485 |
|
3486 | this.resources._invokeHook('beforeCreate', this, container, fragment, createInstruction);
|
3487 |
|
3488 | if (element && this.surrogateInstruction !== null) {
|
3489 | applySurrogateInstruction(container, element, this.surrogateInstruction, controllers, bindings, children);
|
3490 | }
|
3491 |
|
3492 | if (createInstruction.enhance && fragment.hasAttribute('au-target-id')) {
|
3493 | instructable = fragment;
|
3494 | instruction = instructions[instructable.getAttribute('au-target-id')];
|
3495 | applyInstructions(containers, instructable, instruction, controllers, bindings, children, shadowSlots, partReplacements, resources);
|
3496 | }
|
3497 |
|
3498 | for (i = 0, ii = instructables.length; i < ii; ++i) {
|
3499 | instructable = instructables[i];
|
3500 | instruction = instructions[instructable.getAttribute('au-target-id')];
|
3501 | applyInstructions(containers, instructable, instruction, controllers, bindings, children, shadowSlots, partReplacements, resources);
|
3502 | }
|
3503 |
|
3504 | view = new View(container, this, fragment, controllers, bindings, children, shadowSlots);
|
3505 |
|
3506 |
|
3507 | if (!createInstruction.initiatedByBehavior) {
|
3508 | view.created();
|
3509 | }
|
3510 |
|
3511 | this.resources._invokeHook('afterCreate', view);
|
3512 |
|
3513 | return view;
|
3514 | }
|
3515 | }
|
3516 |
|
3517 | let nextInjectorId = 0;
|
3518 | function getNextInjectorId() {
|
3519 | return ++nextInjectorId;
|
3520 | }
|
3521 |
|
3522 | let lastAUTargetID = 0;
|
3523 | function getNextAUTargetID() {
|
3524 | return (++lastAUTargetID).toString();
|
3525 | }
|
3526 |
|
3527 | function makeIntoInstructionTarget(element) {
|
3528 | let value = element.getAttribute('class');
|
3529 | let auTargetID = getNextAUTargetID();
|
3530 |
|
3531 | element.setAttribute('class', (value ? value + ' au-target' : 'au-target'));
|
3532 | element.setAttribute('au-target-id', auTargetID);
|
3533 |
|
3534 | return auTargetID;
|
3535 | }
|
3536 |
|
3537 | function makeShadowSlot(compiler, resources, node, instructions, parentInjectorId) {
|
3538 | let auShadowSlot = DOM.createElement('au-shadow-slot');
|
3539 | DOM.replaceNode(auShadowSlot, node);
|
3540 |
|
3541 | let auTargetID = makeIntoInstructionTarget(auShadowSlot);
|
3542 | let instruction = TargetInstruction.shadowSlot(parentInjectorId);
|
3543 |
|
3544 | instruction.slotName = node.getAttribute('name') || ShadowDOM.defaultSlotKey;
|
3545 | instruction.slotDestination = node.getAttribute('slot');
|
3546 |
|
3547 | if (node.innerHTML.trim()) {
|
3548 | let fragment = DOM.createDocumentFragment();
|
3549 | let child;
|
3550 |
|
3551 | while (child = node.firstChild) {
|
3552 | fragment.appendChild(child);
|
3553 | }
|
3554 |
|
3555 | instruction.slotFallbackFactory = compiler.compile(fragment, resources);
|
3556 | }
|
3557 |
|
3558 | instructions[auTargetID] = instruction;
|
3559 |
|
3560 | return auShadowSlot;
|
3561 | }
|
3562 |
|
3563 |
|
3564 |
|
3565 |
|
3566 | @inject(BindingLanguage, ViewResources)
|
3567 | export class ViewCompiler {
|
3568 | |
3569 |
|
3570 |
|
3571 |
|
3572 |
|
3573 | constructor(bindingLanguage: BindingLanguage, resources: ViewResources) {
|
3574 | this.bindingLanguage = bindingLanguage;
|
3575 | this.resources = resources;
|
3576 | }
|
3577 |
|
3578 | |
3579 |
|
3580 |
|
3581 |
|
3582 |
|
3583 |
|
3584 |
|
3585 | compile(source: Element|DocumentFragment|string, resources?: ViewResources, compileInstruction?: ViewCompileInstruction): ViewFactory {
|
3586 | resources = resources || this.resources;
|
3587 | compileInstruction = compileInstruction || ViewCompileInstruction.normal;
|
3588 | source = typeof source === 'string' ? DOM.createTemplateFromMarkup(source) : source;
|
3589 |
|
3590 | let content;
|
3591 | let part;
|
3592 | let cacheSize;
|
3593 |
|
3594 | if (source.content) {
|
3595 | part = source.getAttribute('part');
|
3596 | cacheSize = source.getAttribute('view-cache');
|
3597 | content = DOM.adoptNode(source.content);
|
3598 | } else {
|
3599 | content = source;
|
3600 | }
|
3601 |
|
3602 | compileInstruction.targetShadowDOM = compileInstruction.targetShadowDOM && FEATURE.shadowDOM;
|
3603 | resources._invokeHook('beforeCompile', content, resources, compileInstruction);
|
3604 |
|
3605 | let instructions = {};
|
3606 | this._compileNode(content, resources, instructions, source, 'root', !compileInstruction.targetShadowDOM);
|
3607 |
|
3608 | let firstChild = content.firstChild;
|
3609 | if (firstChild && firstChild.nodeType === 1) {
|
3610 | let targetId = firstChild.getAttribute('au-target-id');
|
3611 | if (targetId) {
|
3612 | let ins = instructions[targetId];
|
3613 |
|
3614 | if (ins.shadowSlot || ins.lifting || (ins.elementInstruction && !ins.elementInstruction.anchorIsContainer)) {
|
3615 | content.insertBefore(DOM.createComment('view'), firstChild);
|
3616 | }
|
3617 | }
|
3618 | }
|
3619 |
|
3620 | let factory = new ViewFactory(content, instructions, resources);
|
3621 |
|
3622 | factory.surrogateInstruction = compileInstruction.compileSurrogate ? this._compileSurrogate(source, resources) : null;
|
3623 | factory.part = part;
|
3624 |
|
3625 | if (cacheSize) {
|
3626 | factory.setCacheSize(cacheSize);
|
3627 | }
|
3628 |
|
3629 | resources._invokeHook('afterCompile', factory);
|
3630 |
|
3631 | return factory;
|
3632 | }
|
3633 |
|
3634 | _compileNode(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM) {
|
3635 | switch (node.nodeType) {
|
3636 | case 1:
|
3637 | return this._compileElement(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM);
|
3638 | case 3:
|
3639 |
|
3640 | let expression = resources.getBindingLanguage(this.bindingLanguage).inspectTextContent(resources, node.wholeText);
|
3641 | if (expression) {
|
3642 | let marker = DOM.createElement('au-marker');
|
3643 | let auTargetID = makeIntoInstructionTarget(marker);
|
3644 | (node.parentNode || parentNode).insertBefore(marker, node);
|
3645 | node.textContent = ' ';
|
3646 | instructions[auTargetID] = TargetInstruction.contentExpression(expression);
|
3647 |
|
3648 | while (node.nextSibling && node.nextSibling.nodeType === 3) {
|
3649 | (node.parentNode || parentNode).removeChild(node.nextSibling);
|
3650 | }
|
3651 | } else {
|
3652 |
|
3653 | while (node.nextSibling && node.nextSibling.nodeType === 3) {
|
3654 | node = node.nextSibling;
|
3655 | }
|
3656 | }
|
3657 | return node.nextSibling;
|
3658 | case 11:
|
3659 | let currentChild = node.firstChild;
|
3660 | while (currentChild) {
|
3661 | currentChild = this._compileNode(currentChild, resources, instructions, node, parentInjectorId, targetLightDOM);
|
3662 | }
|
3663 | break;
|
3664 | default:
|
3665 | break;
|
3666 | }
|
3667 |
|
3668 | return node.nextSibling;
|
3669 | }
|
3670 |
|
3671 | _compileSurrogate(node, resources) {
|
3672 | let tagName = node.tagName.toLowerCase();
|
3673 | let attributes = node.attributes;
|
3674 | let bindingLanguage = resources.getBindingLanguage(this.bindingLanguage);
|
3675 | let knownAttribute;
|
3676 | let property;
|
3677 | let instruction;
|
3678 | let i;
|
3679 | let ii;
|
3680 | let attr;
|
3681 | let attrName;
|
3682 | let attrValue;
|
3683 | let info;
|
3684 | let type;
|
3685 | let expressions = [];
|
3686 | let expression;
|
3687 | let behaviorInstructions = [];
|
3688 | let values = {};
|
3689 | let hasValues = false;
|
3690 | let providers = [];
|
3691 |
|
3692 | for (i = 0, ii = attributes.length; i < ii; ++i) {
|
3693 | attr = attributes[i];
|
3694 | attrName = attr.name;
|
3695 | attrValue = attr.value;
|
3696 |
|
3697 | info = bindingLanguage.inspectAttribute(resources, tagName, attrName, attrValue);
|
3698 | type = resources.getAttribute(info.attrName);
|
3699 |
|
3700 | if (type) {
|
3701 | knownAttribute = resources.mapAttribute(info.attrName);
|
3702 | if (knownAttribute) {
|
3703 | property = type.attributes[knownAttribute];
|
3704 |
|
3705 | if (property) {
|
3706 | info.defaultBindingMode = property.defaultBindingMode;
|
3707 |
|
3708 | if (!info.command && !info.expression) {
|
3709 | info.command = property.hasOptions ? 'options' : null;
|
3710 | }
|
3711 |
|
3712 |
|
3713 |
|
3714 |
|
3715 | if (info.command && (info.command !== 'options') && type.primaryProperty) {
|
3716 | const primaryProperty = type.primaryProperty;
|
3717 | attrName = info.attrName = primaryProperty.attribute;
|
3718 |
|
3719 |
|
3720 | info.defaultBindingMode = primaryProperty.defaultBindingMode;
|
3721 | }
|
3722 | }
|
3723 | }
|
3724 | }
|
3725 |
|
3726 | instruction = bindingLanguage.createAttributeInstruction(resources, node, info, undefined, type);
|
3727 |
|
3728 | if (instruction) {
|
3729 | if (instruction.alteredAttr) {
|
3730 | type = resources.getAttribute(instruction.attrName);
|
3731 | }
|
3732 |
|
3733 | if (instruction.discrete) {
|
3734 | expressions.push(instruction);
|
3735 | } else {
|
3736 | if (type) {
|
3737 | instruction.type = type;
|
3738 | this._configureProperties(instruction, resources);
|
3739 |
|
3740 | if (type.liftsContent) {
|
3741 | throw new Error('You cannot place a template controller on a surrogate element.');
|
3742 | } else {
|
3743 | behaviorInstructions.push(instruction);
|
3744 | }
|
3745 | } else {
|
3746 | expressions.push(instruction.attributes[instruction.attrName]);
|
3747 | }
|
3748 | }
|
3749 | } else {
|
3750 | if (type) {
|
3751 | instruction = BehaviorInstruction.attribute(attrName, type);
|
3752 | instruction.attributes[resources.mapAttribute(attrName)] = attrValue;
|
3753 |
|
3754 | if (type.liftsContent) {
|
3755 | throw new Error('You cannot place a template controller on a surrogate element.');
|
3756 | } else {
|
3757 | behaviorInstructions.push(instruction);
|
3758 | }
|
3759 | } else if (attrName !== 'id' && attrName !== 'part' && attrName !== 'replace-part') {
|
3760 | hasValues = true;
|
3761 | values[attrName] = attrValue;
|
3762 | }
|
3763 | }
|
3764 | }
|
3765 |
|
3766 | if (expressions.length || behaviorInstructions.length || hasValues) {
|
3767 | for (i = 0, ii = behaviorInstructions.length; i < ii; ++i) {
|
3768 | instruction = behaviorInstructions[i];
|
3769 | instruction.type.compile(this, resources, node, instruction);
|
3770 | providers.push(instruction.type.target);
|
3771 | }
|
3772 |
|
3773 | for (i = 0, ii = expressions.length; i < ii; ++i) {
|
3774 | expression = expressions[i];
|
3775 | if (expression.attrToRemove !== undefined) {
|
3776 | node.removeAttribute(expression.attrToRemove);
|
3777 | }
|
3778 | }
|
3779 |
|
3780 | return TargetInstruction.surrogate(providers, behaviorInstructions, expressions, values);
|
3781 | }
|
3782 |
|
3783 | return null;
|
3784 | }
|
3785 |
|
3786 | _compileElement(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM) {
|
3787 | let tagName = node.tagName.toLowerCase();
|
3788 | let attributes = node.attributes;
|
3789 | let expressions = [];
|
3790 | let expression;
|
3791 | let behaviorInstructions = [];
|
3792 | let providers = [];
|
3793 | let bindingLanguage = resources.getBindingLanguage(this.bindingLanguage);
|
3794 | let liftingInstruction;
|
3795 | let viewFactory;
|
3796 | let type;
|
3797 | let elementInstruction;
|
3798 | let elementProperty;
|
3799 | let i;
|
3800 | let ii;
|
3801 | let attr;
|
3802 | let attrName;
|
3803 | let attrValue;
|
3804 | let originalAttrName;
|
3805 | let instruction;
|
3806 | let info;
|
3807 | let property;
|
3808 | let knownAttribute;
|
3809 | let auTargetID;
|
3810 | let injectorId;
|
3811 |
|
3812 | if (tagName === 'slot') {
|
3813 | if (targetLightDOM) {
|
3814 | node = makeShadowSlot(this, resources, node, instructions, parentInjectorId);
|
3815 | }
|
3816 | return node.nextSibling;
|
3817 | } else if (tagName === 'template') {
|
3818 | if (!('content' in node)) {
|
3819 | throw new Error('You cannot place a template element within ' + node.namespaceURI + ' namespace');
|
3820 | }
|
3821 | viewFactory = this.compile(node, resources);
|
3822 | viewFactory.part = node.getAttribute('part');
|
3823 | } else {
|
3824 | type = resources.getElement(node.getAttribute('as-element') || tagName);
|
3825 | if (type) {
|
3826 | elementInstruction = BehaviorInstruction.element(node, type);
|
3827 | type.processAttributes(this, resources, node, attributes, elementInstruction);
|
3828 | behaviorInstructions.push(elementInstruction);
|
3829 | }
|
3830 | }
|
3831 |
|
3832 | for (i = 0, ii = attributes.length; i < ii; ++i) {
|
3833 | attr = attributes[i];
|
3834 | originalAttrName = attrName = attr.name;
|
3835 | attrValue = attr.value;
|
3836 | info = bindingLanguage.inspectAttribute(resources, tagName, attrName, attrValue);
|
3837 |
|
3838 | if (targetLightDOM && info.attrName === 'slot') {
|
3839 | info.attrName = attrName = 'au-slot';
|
3840 | }
|
3841 |
|
3842 | type = resources.getAttribute(info.attrName);
|
3843 | elementProperty = null;
|
3844 |
|
3845 | if (type) {
|
3846 | knownAttribute = resources.mapAttribute(info.attrName);
|
3847 | if (knownAttribute) {
|
3848 | property = type.attributes[knownAttribute];
|
3849 |
|
3850 | if (property) {
|
3851 | info.defaultBindingMode = property.defaultBindingMode;
|
3852 |
|
3853 | if (!info.command && !info.expression) {
|
3854 | info.command = property.hasOptions ? 'options' : null;
|
3855 | }
|
3856 |
|
3857 |
|
3858 |
|
3859 |
|
3860 | if (info.command && (info.command !== 'options') && type.primaryProperty) {
|
3861 | const primaryProperty = type.primaryProperty;
|
3862 | attrName = info.attrName = primaryProperty.attribute;
|
3863 |
|
3864 |
|
3865 | info.defaultBindingMode = primaryProperty.defaultBindingMode;
|
3866 | }
|
3867 | }
|
3868 | }
|
3869 | } else if (elementInstruction) {
|
3870 | elementProperty = elementInstruction.type.attributes[info.attrName];
|
3871 | if (elementProperty) {
|
3872 | info.defaultBindingMode = elementProperty.defaultBindingMode;
|
3873 | }
|
3874 | }
|
3875 |
|
3876 | if (elementProperty) {
|
3877 | instruction = bindingLanguage.createAttributeInstruction(resources, node, info, elementInstruction);
|
3878 | } else {
|
3879 | instruction = bindingLanguage.createAttributeInstruction(resources, node, info, undefined, type);
|
3880 | }
|
3881 |
|
3882 | if (instruction) {
|
3883 | if (instruction.alteredAttr) {
|
3884 | type = resources.getAttribute(instruction.attrName);
|
3885 | }
|
3886 |
|
3887 | if (instruction.discrete) {
|
3888 | expressions.push(instruction);
|
3889 | } else {
|
3890 | if (type) {
|
3891 | instruction.type = type;
|
3892 | this._configureProperties(instruction, resources);
|
3893 |
|
3894 | if (type.liftsContent) {
|
3895 | instruction.originalAttrName = originalAttrName;
|
3896 | liftingInstruction = instruction;
|
3897 | break;
|
3898 | } else {
|
3899 | behaviorInstructions.push(instruction);
|
3900 | }
|
3901 | } else if (elementProperty) {
|
3902 | elementInstruction.attributes[info.attrName].targetProperty = elementProperty.name;
|
3903 | } else {
|
3904 | expressions.push(instruction.attributes[instruction.attrName]);
|
3905 | }
|
3906 | }
|
3907 | } else {
|
3908 | if (type) {
|
3909 | instruction = BehaviorInstruction.attribute(attrName, type);
|
3910 | instruction.attributes[resources.mapAttribute(attrName)] = attrValue;
|
3911 |
|
3912 | if (type.liftsContent) {
|
3913 | instruction.originalAttrName = originalAttrName;
|
3914 | liftingInstruction = instruction;
|
3915 | break;
|
3916 | } else {
|
3917 | behaviorInstructions.push(instruction);
|
3918 | }
|
3919 | } else if (elementProperty) {
|
3920 | elementInstruction.attributes[attrName] = attrValue;
|
3921 | }
|
3922 |
|
3923 |
|
3924 | }
|
3925 | }
|
3926 |
|
3927 | if (liftingInstruction) {
|
3928 | liftingInstruction.viewFactory = viewFactory;
|
3929 | node = liftingInstruction.type.compile(this, resources, node, liftingInstruction, parentNode);
|
3930 | auTargetID = makeIntoInstructionTarget(node);
|
3931 | instructions[auTargetID] = TargetInstruction.lifting(parentInjectorId, liftingInstruction);
|
3932 | } else {
|
3933 | let skipContentProcessing = false;
|
3934 |
|
3935 | if (expressions.length || behaviorInstructions.length) {
|
3936 | injectorId = behaviorInstructions.length ? getNextInjectorId() : false;
|
3937 |
|
3938 | for (i = 0, ii = behaviorInstructions.length; i < ii; ++i) {
|
3939 | instruction = behaviorInstructions[i];
|
3940 | instruction.type.compile(this, resources, node, instruction, parentNode);
|
3941 | providers.push(instruction.type.target);
|
3942 | skipContentProcessing = skipContentProcessing || instruction.skipContentProcessing;
|
3943 | }
|
3944 |
|
3945 | for (i = 0, ii = expressions.length; i < ii; ++i) {
|
3946 | expression = expressions[i];
|
3947 | if (expression.attrToRemove !== undefined) {
|
3948 | node.removeAttribute(expression.attrToRemove);
|
3949 | }
|
3950 | }
|
3951 |
|
3952 | auTargetID = makeIntoInstructionTarget(node);
|
3953 | instructions[auTargetID] = TargetInstruction.normal(
|
3954 | injectorId,
|
3955 | parentInjectorId,
|
3956 | providers,
|
3957 | behaviorInstructions,
|
3958 | expressions,
|
3959 | elementInstruction
|
3960 | );
|
3961 | }
|
3962 |
|
3963 | if (skipContentProcessing) {
|
3964 | return node.nextSibling;
|
3965 | }
|
3966 |
|
3967 | let currentChild = node.firstChild;
|
3968 | while (currentChild) {
|
3969 | currentChild = this._compileNode(currentChild, resources, instructions, node, injectorId || parentInjectorId, targetLightDOM);
|
3970 | }
|
3971 | }
|
3972 |
|
3973 | return node.nextSibling;
|
3974 | }
|
3975 |
|
3976 | _configureProperties(instruction, resources) {
|
3977 | let type = instruction.type;
|
3978 | let attrName = instruction.attrName;
|
3979 | let attributes = instruction.attributes;
|
3980 | let property;
|
3981 | let key;
|
3982 | let value;
|
3983 |
|
3984 | let knownAttribute = resources.mapAttribute(attrName);
|
3985 | if (knownAttribute && attrName in attributes && knownAttribute !== attrName) {
|
3986 | attributes[knownAttribute] = attributes[attrName];
|
3987 | delete attributes[attrName];
|
3988 | }
|
3989 |
|
3990 | for (key in attributes) {
|
3991 | value = attributes[key];
|
3992 |
|
3993 | if (value !== null && typeof value === 'object') {
|
3994 | property = type.attributes[key];
|
3995 |
|
3996 | if (property !== undefined) {
|
3997 | value.targetProperty = property.name;
|
3998 | } else {
|
3999 | value.targetProperty = key;
|
4000 | }
|
4001 | }
|
4002 | }
|
4003 | }
|
4004 | }
|
4005 |
|
4006 |
|
4007 |
|
4008 |
|
4009 | export class ResourceModule {
|
4010 | |
4011 |
|
4012 |
|
4013 |
|
4014 | constructor(moduleId: string) {
|
4015 | this.id = moduleId;
|
4016 | this.moduleInstance = null;
|
4017 | this.mainResource = null;
|
4018 | this.resources = null;
|
4019 | this.viewStrategy = null;
|
4020 | this.isInitialized = false;
|
4021 | this.onLoaded = null;
|
4022 | this.loadContext = null;
|
4023 | }
|
4024 |
|
4025 | |
4026 |
|
4027 |
|
4028 |
|
4029 | initialize(container: Container): void {
|
4030 | let current = this.mainResource;
|
4031 | let resources = this.resources;
|
4032 | let vs = this.viewStrategy;
|
4033 |
|
4034 | if (this.isInitialized) {
|
4035 | return;
|
4036 | }
|
4037 |
|
4038 | this.isInitialized = true;
|
4039 |
|
4040 | if (current !== undefined) {
|
4041 | current.metadata.viewStrategy = vs;
|
4042 | current.initialize(container);
|
4043 | }
|
4044 |
|
4045 | for (let i = 0, ii = resources.length; i < ii; ++i) {
|
4046 | current = resources[i];
|
4047 | current.metadata.viewStrategy = vs;
|
4048 | current.initialize(container);
|
4049 | }
|
4050 | }
|
4051 |
|
4052 | |
4053 |
|
4054 |
|
4055 |
|
4056 |
|
4057 | register(registry:ViewResources, name?:string): void {
|
4058 | let main = this.mainResource;
|
4059 | let resources = this.resources;
|
4060 |
|
4061 | if (main !== undefined) {
|
4062 | main.register(registry, name);
|
4063 | name = null;
|
4064 | }
|
4065 |
|
4066 | for (let i = 0, ii = resources.length; i < ii; ++i) {
|
4067 | resources[i].register(registry, name);
|
4068 | name = null;
|
4069 | }
|
4070 | }
|
4071 |
|
4072 | |
4073 |
|
4074 |
|
4075 |
|
4076 |
|
4077 |
|
4078 | load(container: Container, loadContext?: ResourceLoadContext): Promise<void> {
|
4079 | if (this.onLoaded !== null) {
|
4080 |
|
4081 | return this.loadContext === loadContext ? Promise.resolve() : this.onLoaded;
|
4082 | }
|
4083 |
|
4084 | let main = this.mainResource;
|
4085 | let resources = this.resources;
|
4086 | let loads;
|
4087 |
|
4088 | if (main !== undefined) {
|
4089 | loads = new Array(resources.length + 1);
|
4090 | loads[0] = main.load(container, loadContext);
|
4091 | for (let i = 0, ii = resources.length; i < ii; ++i) {
|
4092 | loads[i + 1] = resources[i].load(container, loadContext);
|
4093 | }
|
4094 | } else {
|
4095 | loads = new Array(resources.length);
|
4096 | for (let i = 0, ii = resources.length; i < ii; ++i) {
|
4097 | loads[i] = resources[i].load(container, loadContext);
|
4098 | }
|
4099 | }
|
4100 |
|
4101 | this.loadContext = loadContext;
|
4102 | this.onLoaded = Promise.all(loads);
|
4103 | return this.onLoaded;
|
4104 | }
|
4105 | }
|
4106 |
|
4107 |
|
4108 |
|
4109 |
|
4110 | export class ResourceDescription {
|
4111 | |
4112 |
|
4113 |
|
4114 |
|
4115 |
|
4116 |
|
4117 | constructor(key: string, exportedValue: any, resourceTypeMeta?: Object) {
|
4118 | if (!resourceTypeMeta) {
|
4119 | resourceTypeMeta = metadata.get(metadata.resource, exportedValue);
|
4120 |
|
4121 | if (!resourceTypeMeta) {
|
4122 | resourceTypeMeta = new HtmlBehaviorResource();
|
4123 | resourceTypeMeta.elementName = _hyphenate(key);
|
4124 | metadata.define(metadata.resource, resourceTypeMeta, exportedValue);
|
4125 | }
|
4126 | }
|
4127 |
|
4128 | if (resourceTypeMeta instanceof HtmlBehaviorResource) {
|
4129 | if (resourceTypeMeta.elementName === undefined) {
|
4130 |
|
4131 | resourceTypeMeta.elementName = _hyphenate(key);
|
4132 | } else if (resourceTypeMeta.attributeName === undefined) {
|
4133 |
|
4134 | resourceTypeMeta.attributeName = _hyphenate(key);
|
4135 | } else if (resourceTypeMeta.attributeName === null && resourceTypeMeta.elementName === null) {
|
4136 |
|
4137 | HtmlBehaviorResource.convention(key, resourceTypeMeta);
|
4138 | }
|
4139 | } else if (!resourceTypeMeta.name) {
|
4140 | resourceTypeMeta.name = _hyphenate(key);
|
4141 | }
|
4142 |
|
4143 | this.metadata = resourceTypeMeta;
|
4144 | this.value = exportedValue;
|
4145 | }
|
4146 |
|
4147 | |
4148 |
|
4149 |
|
4150 |
|
4151 | initialize(container: Container): void {
|
4152 | this.metadata.initialize(container, this.value);
|
4153 | }
|
4154 |
|
4155 | |
4156 |
|
4157 |
|
4158 |
|
4159 |
|
4160 | register(registry: ViewResources, name?: string): void {
|
4161 | this.metadata.register(registry, name);
|
4162 | }
|
4163 |
|
4164 | |
4165 |
|
4166 |
|
4167 |
|
4168 |
|
4169 |
|
4170 | load(container: Container, loadContext?: ResourceLoadContext): Promise<void> | void {
|
4171 | return this.metadata.load(container, this.value, loadContext);
|
4172 | }
|
4173 | }
|
4174 |
|
4175 |
|
4176 |
|
4177 |
|
4178 | export class ModuleAnalyzer {
|
4179 | |
4180 |
|
4181 |
|
4182 | constructor() {
|
4183 | this.cache = Object.create(null);
|
4184 | }
|
4185 |
|
4186 | |
4187 |
|
4188 |
|
4189 |
|
4190 |
|
4191 | getAnalysis(moduleId: string): ResourceModule {
|
4192 | return this.cache[moduleId];
|
4193 | }
|
4194 |
|
4195 | |
4196 |
|
4197 |
|
4198 |
|
4199 |
|
4200 |
|
4201 |
|
4202 | analyze(moduleId: string, moduleInstance: any, mainResourceKey?: string): ResourceModule {
|
4203 | let mainResource;
|
4204 | let fallbackValue;
|
4205 | let fallbackKey;
|
4206 | let resourceTypeMeta;
|
4207 | let key;
|
4208 | let exportedValue;
|
4209 | let resources = [];
|
4210 | let conventional;
|
4211 | let vs;
|
4212 | let resourceModule;
|
4213 |
|
4214 | resourceModule = this.cache[moduleId];
|
4215 | if (resourceModule) {
|
4216 | return resourceModule;
|
4217 | }
|
4218 |
|
4219 | resourceModule = new ResourceModule(moduleId);
|
4220 | this.cache[moduleId] = resourceModule;
|
4221 |
|
4222 | if (typeof moduleInstance === 'function') {
|
4223 | moduleInstance = {'default': moduleInstance};
|
4224 | }
|
4225 |
|
4226 | if (mainResourceKey) {
|
4227 | mainResource = new ResourceDescription(mainResourceKey, moduleInstance[mainResourceKey]);
|
4228 | }
|
4229 |
|
4230 | for (key in moduleInstance) {
|
4231 | exportedValue = moduleInstance[key];
|
4232 |
|
4233 | if (key === mainResourceKey || typeof exportedValue !== 'function') {
|
4234 | continue;
|
4235 | }
|
4236 |
|
4237 |
|
4238 |
|
4239 | resourceTypeMeta = metadata.get(metadata.resource, exportedValue);
|
4240 |
|
4241 | if (resourceTypeMeta) {
|
4242 | if (resourceTypeMeta instanceof HtmlBehaviorResource) {
|
4243 |
|
4244 | ViewResources.convention(exportedValue, resourceTypeMeta);
|
4245 |
|
4246 | if (resourceTypeMeta.attributeName === null && resourceTypeMeta.elementName === null) {
|
4247 |
|
4248 | HtmlBehaviorResource.convention(key, resourceTypeMeta);
|
4249 | }
|
4250 |
|
4251 | if (resourceTypeMeta.attributeName === null && resourceTypeMeta.elementName === null) {
|
4252 |
|
4253 | resourceTypeMeta.elementName = _hyphenate(key);
|
4254 | }
|
4255 | }
|
4256 |
|
4257 | if (!mainResource && resourceTypeMeta instanceof HtmlBehaviorResource && resourceTypeMeta.elementName !== null) {
|
4258 | mainResource = new ResourceDescription(key, exportedValue, resourceTypeMeta);
|
4259 | } else {
|
4260 | resources.push(new ResourceDescription(key, exportedValue, resourceTypeMeta));
|
4261 | }
|
4262 | } else if (viewStrategy.decorates(exportedValue)) {
|
4263 | vs = exportedValue;
|
4264 | } else if (exportedValue instanceof TemplateRegistryEntry) {
|
4265 | vs = new TemplateRegistryViewStrategy(moduleId, exportedValue);
|
4266 | } else {
|
4267 | if (conventional = ViewResources.convention(exportedValue)) {
|
4268 | if (conventional.elementName !== null && !mainResource) {
|
4269 | mainResource = new ResourceDescription(key, exportedValue, conventional);
|
4270 | } else {
|
4271 | resources.push(new ResourceDescription(key, exportedValue, conventional));
|
4272 | }
|
4273 | metadata.define(metadata.resource, conventional, exportedValue);
|
4274 | } else if (conventional = HtmlBehaviorResource.convention(key)) {
|
4275 | if (conventional.elementName !== null && !mainResource) {
|
4276 | mainResource = new ResourceDescription(key, exportedValue, conventional);
|
4277 | } else {
|
4278 | resources.push(new ResourceDescription(key, exportedValue, conventional));
|
4279 | }
|
4280 |
|
4281 | metadata.define(metadata.resource, conventional, exportedValue);
|
4282 | } else if (conventional = ValueConverterResource.convention(key)
|
4283 | || BindingBehaviorResource.convention(key)
|
4284 | || ViewEngineHooksResource.convention(key)) {
|
4285 | resources.push(new ResourceDescription(key, exportedValue, conventional));
|
4286 | metadata.define(metadata.resource, conventional, exportedValue);
|
4287 | } else if (!fallbackValue) {
|
4288 | fallbackValue = exportedValue;
|
4289 | fallbackKey = key;
|
4290 | }
|
4291 | }
|
4292 | }
|
4293 |
|
4294 | if (!mainResource && fallbackValue) {
|
4295 | mainResource = new ResourceDescription(fallbackKey, fallbackValue);
|
4296 | }
|
4297 |
|
4298 | resourceModule.moduleInstance = moduleInstance;
|
4299 | resourceModule.mainResource = mainResource;
|
4300 | resourceModule.resources = resources;
|
4301 | resourceModule.viewStrategy = vs;
|
4302 |
|
4303 | return resourceModule;
|
4304 | }
|
4305 | }
|
4306 |
|
4307 | let logger = LogManager.getLogger('templating');
|
4308 |
|
4309 | function ensureRegistryEntry(loader, urlOrRegistryEntry) {
|
4310 | if (urlOrRegistryEntry instanceof TemplateRegistryEntry) {
|
4311 | return Promise.resolve(urlOrRegistryEntry);
|
4312 | }
|
4313 |
|
4314 | return loader.loadTemplate(urlOrRegistryEntry);
|
4315 | }
|
4316 |
|
4317 | class ProxyViewFactory {
|
4318 | constructor(promise) {
|
4319 | promise.then(x => this.viewFactory = x);
|
4320 | }
|
4321 |
|
4322 | create(container: Container, bindingContext?: Object, createInstruction?: ViewCreateInstruction, element?: Element): View {
|
4323 | return this.viewFactory.create(container, bindingContext, createInstruction, element);
|
4324 | }
|
4325 |
|
4326 | get isCaching() {
|
4327 | return this.viewFactory.isCaching;
|
4328 | }
|
4329 |
|
4330 | setCacheSize(size: number | string, doNotOverrideIfAlreadySet: boolean): void {
|
4331 | this.viewFactory.setCacheSize(size, doNotOverrideIfAlreadySet);
|
4332 | }
|
4333 |
|
4334 | getCachedView(): View {
|
4335 | return this.viewFactory.getCachedView();
|
4336 | }
|
4337 |
|
4338 | returnViewToCache(view: View): void {
|
4339 | this.viewFactory.returnViewToCache(view);
|
4340 | }
|
4341 | }
|
4342 |
|
4343 | let auSlotBehavior = null;
|
4344 |
|
4345 |
|
4346 |
|
4347 |
|
4348 | @inject(Loader, Container, ViewCompiler, ModuleAnalyzer, ViewResources)
|
4349 | export class ViewEngine {
|
4350 | |
4351 |
|
4352 |
|
4353 | static viewModelRequireMetadataKey = 'aurelia:view-model-require';
|
4354 |
|
4355 | |
4356 |
|
4357 |
|
4358 |
|
4359 |
|
4360 |
|
4361 |
|
4362 |
|
4363 | constructor(loader: Loader, container: Container, viewCompiler: ViewCompiler, moduleAnalyzer: ModuleAnalyzer, appResources: ViewResources) {
|
4364 | this.loader = loader;
|
4365 | this.container = container;
|
4366 | this.viewCompiler = viewCompiler;
|
4367 | this.moduleAnalyzer = moduleAnalyzer;
|
4368 | this.appResources = appResources;
|
4369 | this._pluginMap = {};
|
4370 |
|
4371 | if (auSlotBehavior === null) {
|
4372 | auSlotBehavior = new HtmlBehaviorResource();
|
4373 | auSlotBehavior.attributeName = 'au-slot';
|
4374 | metadata.define(metadata.resource, auSlotBehavior, SlotCustomAttribute);
|
4375 | }
|
4376 |
|
4377 | auSlotBehavior.initialize(container, SlotCustomAttribute);
|
4378 | auSlotBehavior.register(appResources);
|
4379 | }
|
4380 |
|
4381 | |
4382 |
|
4383 |
|
4384 |
|
4385 |
|
4386 | addResourcePlugin(extension: string, implementation: Object): void {
|
4387 | let name = extension.replace('.', '') + '-resource-plugin';
|
4388 | this._pluginMap[extension] = name;
|
4389 | this.loader.addPlugin(name, implementation);
|
4390 | }
|
4391 |
|
4392 | |
4393 |
|
4394 |
|
4395 |
|
4396 |
|
4397 |
|
4398 |
|
4399 |
|
4400 | loadViewFactory(urlOrRegistryEntry: string|TemplateRegistryEntry, compileInstruction?: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewFactory> {
|
4401 | loadContext = loadContext || new ResourceLoadContext();
|
4402 |
|
4403 | return ensureRegistryEntry(this.loader, urlOrRegistryEntry).then(registryEntry => {
|
4404 | const url = registryEntry.address;
|
4405 |
|
4406 | if (registryEntry.onReady) {
|
4407 | if (!loadContext.hasDependency(url)) {
|
4408 | loadContext.addDependency(url);
|
4409 | return registryEntry.onReady;
|
4410 | }
|
4411 |
|
4412 | if (registryEntry.template === null) {
|
4413 |
|
4414 | return registryEntry.onReady;
|
4415 | }
|
4416 |
|
4417 | return Promise.resolve(new ProxyViewFactory(registryEntry.onReady));
|
4418 | }
|
4419 |
|
4420 | loadContext.addDependency(url);
|
4421 |
|
4422 | registryEntry.onReady = this.loadTemplateResources(registryEntry, compileInstruction, loadContext, target).then(resources => {
|
4423 | registryEntry.resources = resources;
|
4424 |
|
4425 | if (registryEntry.template === null) {
|
4426 |
|
4427 | return registryEntry.factory = null;
|
4428 | }
|
4429 |
|
4430 | let viewFactory = this.viewCompiler.compile(registryEntry.template, resources, compileInstruction);
|
4431 | return registryEntry.factory = viewFactory;
|
4432 | });
|
4433 |
|
4434 | return registryEntry.onReady;
|
4435 | });
|
4436 | }
|
4437 |
|
4438 | |
4439 |
|
4440 |
|
4441 |
|
4442 |
|
4443 |
|
4444 |
|
4445 |
|
4446 | loadTemplateResources(registryEntry: TemplateRegistryEntry, compileInstruction?: ViewCompileInstruction, loadContext?: ResourceLoadContext, target?: any): Promise<ViewResources> {
|
4447 | let resources = new ViewResources(this.appResources, registryEntry.address);
|
4448 | let dependencies = registryEntry.dependencies;
|
4449 | let importIds;
|
4450 | let names;
|
4451 |
|
4452 | compileInstruction = compileInstruction || ViewCompileInstruction.normal;
|
4453 |
|
4454 | if (dependencies.length === 0 && !compileInstruction.associatedModuleId) {
|
4455 | return Promise.resolve(resources);
|
4456 | }
|
4457 |
|
4458 | importIds = dependencies.map(x => x.src);
|
4459 | names = dependencies.map(x => x.name);
|
4460 | logger.debug(`importing resources for ${registryEntry.address}`, importIds);
|
4461 |
|
4462 | if (target) {
|
4463 | let viewModelRequires = metadata.get(ViewEngine.viewModelRequireMetadataKey, target);
|
4464 | if (viewModelRequires) {
|
4465 | let templateImportCount = importIds.length;
|
4466 | for (let i = 0, ii = viewModelRequires.length; i < ii; ++i) {
|
4467 | let req = viewModelRequires[i];
|
4468 | let importId = typeof req === 'function' ? Origin.get(req).moduleId : relativeToFile(req.src || req, registryEntry.address);
|
4469 |
|
4470 | if (importIds.indexOf(importId) === -1) {
|
4471 | importIds.push(importId);
|
4472 | names.push(req.as);
|
4473 | }
|
4474 | }
|
4475 | logger.debug(`importing ViewModel resources for ${compileInstruction.associatedModuleId}`, importIds.slice(templateImportCount));
|
4476 | }
|
4477 | }
|
4478 |
|
4479 | return this.importViewResources(importIds, names, resources, compileInstruction, loadContext);
|
4480 | }
|
4481 |
|
4482 | |
4483 |
|
4484 |
|
4485 |
|
4486 |
|
4487 |
|
4488 | importViewModelResource(moduleImport: string, moduleMember: string): Promise<ResourceDescription> {
|
4489 | return this.loader.loadModule(moduleImport).then(viewModelModule => {
|
4490 | let normalizedId = Origin.get(viewModelModule).moduleId;
|
4491 | let resourceModule = this.moduleAnalyzer.analyze(normalizedId, viewModelModule, moduleMember);
|
4492 |
|
4493 | if (!resourceModule.mainResource) {
|
4494 | throw new Error(`No view model found in module "${moduleImport}".`);
|
4495 | }
|
4496 |
|
4497 | resourceModule.initialize(this.container);
|
4498 |
|
4499 | return resourceModule.mainResource;
|
4500 | });
|
4501 | }
|
4502 |
|
4503 | |
4504 |
|
4505 |
|
4506 |
|
4507 |
|
4508 |
|
4509 |
|
4510 |
|
4511 | importViewResources(moduleIds: string[], names: string[], resources: ViewResources, compileInstruction?: ViewCompileInstruction, loadContext?: ResourceLoadContext): Promise<ViewResources> {
|
4512 | loadContext = loadContext || new ResourceLoadContext();
|
4513 | compileInstruction = compileInstruction || ViewCompileInstruction.normal;
|
4514 |
|
4515 | moduleIds = moduleIds.map(x => this._applyLoaderPlugin(x));
|
4516 |
|
4517 | return this.loader.loadAllModules(moduleIds).then(imports => {
|
4518 | let i;
|
4519 | let ii;
|
4520 | let analysis;
|
4521 | let normalizedId;
|
4522 | let current;
|
4523 | let associatedModule;
|
4524 | let container = this.container;
|
4525 | let moduleAnalyzer = this.moduleAnalyzer;
|
4526 | let allAnalysis = new Array(imports.length);
|
4527 |
|
4528 |
|
4529 |
|
4530 |
|
4531 | for (i = 0, ii = imports.length; i < ii; ++i) {
|
4532 | current = imports[i];
|
4533 | normalizedId = Origin.get(current).moduleId;
|
4534 |
|
4535 | analysis = moduleAnalyzer.analyze(normalizedId, current);
|
4536 | analysis.initialize(container);
|
4537 | analysis.register(resources, names[i]);
|
4538 |
|
4539 | allAnalysis[i] = analysis;
|
4540 | }
|
4541 |
|
4542 | if (compileInstruction.associatedModuleId) {
|
4543 | associatedModule = moduleAnalyzer.getAnalysis(compileInstruction.associatedModuleId);
|
4544 |
|
4545 | if (associatedModule) {
|
4546 | associatedModule.register(resources);
|
4547 | }
|
4548 | }
|
4549 |
|
4550 |
|
4551 |
|
4552 | for (i = 0, ii = allAnalysis.length; i < ii; ++i) {
|
4553 | allAnalysis[i] = allAnalysis[i].load(container, loadContext);
|
4554 | }
|
4555 |
|
4556 | return Promise.all(allAnalysis).then(() => resources);
|
4557 | });
|
4558 | }
|
4559 |
|
4560 | _applyLoaderPlugin(id) {
|
4561 | let index = id.lastIndexOf('.');
|
4562 | if (index !== -1) {
|
4563 | let ext = id.substring(index);
|
4564 | let pluginName = this._pluginMap[ext];
|
4565 |
|
4566 | if (pluginName === undefined) {
|
4567 | return id;
|
4568 | }
|
4569 |
|
4570 | return this.loader.applyPluginToUrl(id, pluginName);
|
4571 | }
|
4572 |
|
4573 | return id;
|
4574 | }
|
4575 | }
|
4576 |
|
4577 |
|
4578 |
|
4579 |
|
4580 | export class Controller {
|
4581 | |
4582 |
|
4583 |
|
4584 | behavior: HtmlBehaviorResource;
|
4585 |
|
4586 | |
4587 |
|
4588 |
|
4589 | viewModel: Object;
|
4590 |
|
4591 | |
4592 |
|
4593 |
|
4594 |
|
4595 | view: View;
|
4596 |
|
4597 | |
4598 |
|
4599 |
|
4600 |
|
4601 |
|
4602 |
|
4603 |
|
4604 | constructor(behavior: HtmlBehaviorResource, instruction: BehaviorInstruction, viewModel: Object, container: Container) {
|
4605 | this.behavior = behavior;
|
4606 | this.instruction = instruction;
|
4607 | this.viewModel = viewModel;
|
4608 | this.isAttached = false;
|
4609 | this.view = null;
|
4610 | this.isBound = false;
|
4611 | this.scope = null;
|
4612 | this.container = container;
|
4613 | this.elementEvents = container.elementEvents || null;
|
4614 |
|
4615 | let observerLookup = behavior.observerLocator.getOrCreateObserversLookup(viewModel);
|
4616 | let handlesBind = behavior.handlesBind;
|
4617 | let attributes = instruction.attributes;
|
4618 | let boundProperties = this.boundProperties = [];
|
4619 | let properties = behavior.properties;
|
4620 | let i;
|
4621 | let ii;
|
4622 |
|
4623 | behavior._ensurePropertiesDefined(viewModel, observerLookup);
|
4624 |
|
4625 | for (i = 0, ii = properties.length; i < ii; ++i) {
|
4626 | properties[i]._initialize(viewModel, observerLookup, attributes, handlesBind, boundProperties);
|
4627 | }
|
4628 | }
|
4629 |
|
4630 | |
4631 |
|
4632 |
|
4633 |
|
4634 | created(owningView: View): void {
|
4635 | if (this.behavior.handlesCreated) {
|
4636 | this.viewModel.created(owningView, this.view);
|
4637 | }
|
4638 | }
|
4639 |
|
4640 | |
4641 |
|
4642 |
|
4643 |
|
4644 |
|
4645 |
|
4646 | automate(overrideContext?: Object, owningView?: View): void {
|
4647 | this.view.bindingContext = this.viewModel;
|
4648 | this.view.overrideContext = overrideContext || createOverrideContext(this.viewModel);
|
4649 | this.view._isUserControlled = true;
|
4650 |
|
4651 | if (this.behavior.handlesCreated) {
|
4652 | this.viewModel.created(owningView || null, this.view);
|
4653 | }
|
4654 |
|
4655 | this.bind(this.view);
|
4656 | }
|
4657 |
|
4658 | |
4659 |
|
4660 |
|
4661 |
|
4662 | bind(scope: Object): void {
|
4663 | let skipSelfSubscriber = this.behavior.handlesBind;
|
4664 | let boundProperties = this.boundProperties;
|
4665 | let i;
|
4666 | let ii;
|
4667 | let x;
|
4668 | let observer;
|
4669 | let selfSubscriber;
|
4670 |
|
4671 | if (this.isBound) {
|
4672 | if (this.scope === scope) {
|
4673 | return;
|
4674 | }
|
4675 |
|
4676 | this.unbind();
|
4677 | }
|
4678 |
|
4679 | this.isBound = true;
|
4680 | this.scope = scope;
|
4681 |
|
4682 | for (i = 0, ii = boundProperties.length; i < ii; ++i) {
|
4683 | x = boundProperties[i];
|
4684 | observer = x.observer;
|
4685 | selfSubscriber = observer.selfSubscriber;
|
4686 | observer.publishing = false;
|
4687 |
|
4688 | if (skipSelfSubscriber) {
|
4689 | observer.selfSubscriber = null;
|
4690 | }
|
4691 |
|
4692 | x.binding.bind(scope);
|
4693 | observer.call();
|
4694 |
|
4695 | observer.publishing = true;
|
4696 | observer.selfSubscriber = selfSubscriber;
|
4697 | }
|
4698 |
|
4699 | let overrideContext;
|
4700 | if (this.view !== null) {
|
4701 | if (skipSelfSubscriber) {
|
4702 | this.view.viewModelScope = scope;
|
4703 | }
|
4704 |
|
4705 |
|
4706 | if (this.viewModel === scope.overrideContext.bindingContext) {
|
4707 | overrideContext = scope.overrideContext;
|
4708 |
|
4709 | } else if (this.instruction.inheritBindingContext) {
|
4710 | overrideContext = createOverrideContext(this.viewModel, scope.overrideContext);
|
4711 |
|
4712 |
|
4713 | } else {
|
4714 | overrideContext = createOverrideContext(this.viewModel);
|
4715 | overrideContext.__parentOverrideContext = scope.overrideContext;
|
4716 | }
|
4717 |
|
4718 | this.view.bind(this.viewModel, overrideContext);
|
4719 | } else if (skipSelfSubscriber) {
|
4720 | overrideContext = scope.overrideContext;
|
4721 |
|
4722 |
|
4723 |
|
4724 |
|
4725 |
|
4726 |
|
4727 | if (scope.overrideContext.__parentOverrideContext !== undefined
|
4728 | && this.viewModel.viewFactory && this.viewModel.viewFactory.factoryCreateInstruction.partReplacements) {
|
4729 |
|
4730 | overrideContext = Object.assign({}, scope.overrideContext);
|
4731 | overrideContext.parentOverrideContext = scope.overrideContext.__parentOverrideContext;
|
4732 | }
|
4733 | this.viewModel.bind(scope.bindingContext, overrideContext);
|
4734 | }
|
4735 | }
|
4736 |
|
4737 | |
4738 |
|
4739 |
|
4740 | unbind(): void {
|
4741 | if (this.isBound) {
|
4742 | let boundProperties = this.boundProperties;
|
4743 | let i;
|
4744 | let ii;
|
4745 |
|
4746 | this.isBound = false;
|
4747 | this.scope = null;
|
4748 |
|
4749 | if (this.view !== null) {
|
4750 | this.view.unbind();
|
4751 | }
|
4752 |
|
4753 | if (this.behavior.handlesUnbind) {
|
4754 | this.viewModel.unbind();
|
4755 | }
|
4756 |
|
4757 | if (this.elementEvents !== null) {
|
4758 | this.elementEvents.disposeAll();
|
4759 | }
|
4760 |
|
4761 | for (i = 0, ii = boundProperties.length; i < ii; ++i) {
|
4762 | boundProperties[i].binding.unbind();
|
4763 | }
|
4764 | }
|
4765 | }
|
4766 |
|
4767 | |
4768 |
|
4769 |
|
4770 | attached(): void {
|
4771 | if (this.isAttached) {
|
4772 | return;
|
4773 | }
|
4774 |
|
4775 | this.isAttached = true;
|
4776 |
|
4777 | if (this.behavior.handlesAttached) {
|
4778 | this.viewModel.attached();
|
4779 | }
|
4780 |
|
4781 | if (this.view !== null) {
|
4782 | this.view.attached();
|
4783 | }
|
4784 | }
|
4785 |
|
4786 | |
4787 |
|
4788 |
|
4789 | detached(): void {
|
4790 | if (this.isAttached) {
|
4791 | this.isAttached = false;
|
4792 |
|
4793 | if (this.view !== null) {
|
4794 | this.view.detached();
|
4795 | }
|
4796 |
|
4797 | if (this.behavior.handlesDetached) {
|
4798 | this.viewModel.detached();
|
4799 | }
|
4800 | }
|
4801 | }
|
4802 | }
|
4803 |
|
4804 |
|
4805 |
|
4806 |
|
4807 | @subscriberCollection()
|
4808 | export class BehaviorPropertyObserver {
|
4809 | |
4810 |
|
4811 |
|
4812 |
|
4813 |
|
4814 |
|
4815 |
|
4816 |
|
4817 | constructor(taskQueue: TaskQueue, obj: Object, propertyName: string, selfSubscriber: Function, initialValue: any) {
|
4818 | this.taskQueue = taskQueue;
|
4819 | this.obj = obj;
|
4820 | this.propertyName = propertyName;
|
4821 | this.notqueued = true;
|
4822 | this.publishing = false;
|
4823 | this.selfSubscriber = selfSubscriber;
|
4824 | this.currentValue = this.oldValue = initialValue;
|
4825 | }
|
4826 |
|
4827 | |
4828 |
|
4829 |
|
4830 | getValue(): any {
|
4831 | return this.currentValue;
|
4832 | }
|
4833 |
|
4834 | |
4835 |
|
4836 |
|
4837 |
|
4838 | setValue(newValue: any): void {
|
4839 | let oldValue = this.currentValue;
|
4840 |
|
4841 | if (!Object.is(newValue, oldValue)) {
|
4842 | this.oldValue = oldValue;
|
4843 | this.currentValue = newValue;
|
4844 |
|
4845 | if (this.publishing && this.notqueued) {
|
4846 | if (this.taskQueue.flushing) {
|
4847 | this.call();
|
4848 | } else {
|
4849 | this.notqueued = false;
|
4850 | this.taskQueue.queueMicroTask(this);
|
4851 | }
|
4852 | }
|
4853 | }
|
4854 | }
|
4855 |
|
4856 | |
4857 |
|
4858 |
|
4859 | call(): void {
|
4860 | let oldValue = this.oldValue;
|
4861 | let newValue = this.currentValue;
|
4862 |
|
4863 | this.notqueued = true;
|
4864 |
|
4865 | if (Object.is(newValue, oldValue)) {
|
4866 | return;
|
4867 | }
|
4868 |
|
4869 | if (this.selfSubscriber) {
|
4870 | this.selfSubscriber(newValue, oldValue);
|
4871 | }
|
4872 |
|
4873 | this.callSubscribers(newValue, oldValue);
|
4874 | this.oldValue = newValue;
|
4875 | }
|
4876 |
|
4877 | |
4878 |
|
4879 |
|
4880 |
|
4881 |
|
4882 | subscribe(context: any, callable: Function): void {
|
4883 | this.addSubscriber(context, callable);
|
4884 | }
|
4885 |
|
4886 | |
4887 |
|
4888 |
|
4889 |
|
4890 |
|
4891 | unsubscribe(context: any, callable: Function): void {
|
4892 | this.removeSubscriber(context, callable);
|
4893 | }
|
4894 | }
|
4895 |
|
4896 | function getObserver(instance, name) {
|
4897 | let lookup = instance.__observers__;
|
4898 |
|
4899 | if (lookup === undefined) {
|
4900 |
|
4901 |
|
4902 |
|
4903 |
|
4904 |
|
4905 |
|
4906 | let ctor = Object.getPrototypeOf(instance).constructor;
|
4907 | let behavior = metadata.get(metadata.resource, ctor);
|
4908 | if (!behavior.isInitialized) {
|
4909 | behavior.initialize(Container.instance || new Container(), instance.constructor);
|
4910 | }
|
4911 |
|
4912 | lookup = behavior.observerLocator.getOrCreateObserversLookup(instance);
|
4913 | behavior._ensurePropertiesDefined(instance, lookup);
|
4914 | }
|
4915 |
|
4916 | return lookup[name];
|
4917 | }
|
4918 |
|
4919 |
|
4920 |
|
4921 |
|
4922 | export class BindableProperty {
|
4923 | |
4924 |
|
4925 |
|
4926 |
|
4927 | constructor(nameOrConfig: string | Object) {
|
4928 | if (typeof nameOrConfig === 'string') {
|
4929 | this.name = nameOrConfig;
|
4930 | } else {
|
4931 | Object.assign(this, nameOrConfig);
|
4932 | }
|
4933 |
|
4934 | this.attribute = this.attribute || _hyphenate(this.name);
|
4935 | let defaultBindingMode = this.defaultBindingMode;
|
4936 | if (defaultBindingMode === null || defaultBindingMode === undefined) {
|
4937 | this.defaultBindingMode = bindingMode.oneWay;
|
4938 | } else if (typeof defaultBindingMode === 'string') {
|
4939 |
|
4940 | this.defaultBindingMode = bindingMode[defaultBindingMode] || bindingMode.oneWay;
|
4941 | }
|
4942 | this.changeHandler = this.changeHandler || null;
|
4943 | this.owner = null;
|
4944 | this.descriptor = null;
|
4945 | }
|
4946 |
|
4947 | |
4948 |
|
4949 |
|
4950 |
|
4951 |
|
4952 |
|
4953 | registerWith(target: Function, behavior: HtmlBehaviorResource, descriptor?: Object): void {
|
4954 | behavior.properties.push(this);
|
4955 | behavior.attributes[this.attribute] = this;
|
4956 | this.owner = behavior;
|
4957 |
|
4958 | if (descriptor) {
|
4959 | this.descriptor = descriptor;
|
4960 | return this._configureDescriptor(descriptor);
|
4961 | }
|
4962 |
|
4963 | return undefined;
|
4964 | }
|
4965 |
|
4966 | _configureDescriptor(descriptor: Object): Object {
|
4967 | let name = this.name;
|
4968 |
|
4969 | descriptor.configurable = true;
|
4970 | descriptor.enumerable = true;
|
4971 |
|
4972 | if ('initializer' in descriptor) {
|
4973 | this.defaultValue = descriptor.initializer;
|
4974 | delete descriptor.initializer;
|
4975 | delete descriptor.writable;
|
4976 | }
|
4977 |
|
4978 | if ('value' in descriptor) {
|
4979 | this.defaultValue = descriptor.value;
|
4980 | delete descriptor.value;
|
4981 | delete descriptor.writable;
|
4982 | }
|
4983 |
|
4984 | descriptor.get = function() {
|
4985 | return getObserver(this, name).getValue();
|
4986 | };
|
4987 |
|
4988 | descriptor.set = function(value) {
|
4989 | getObserver(this, name).setValue(value);
|
4990 | };
|
4991 |
|
4992 | descriptor.get.getObserver = function(obj) {
|
4993 | return getObserver(obj, name);
|
4994 | };
|
4995 |
|
4996 | return descriptor;
|
4997 | }
|
4998 |
|
4999 | |
5000 |
|
5001 |
|
5002 |
|
5003 |
|
5004 | defineOn(target: Function, behavior: HtmlBehaviorResource): void {
|
5005 | let name = this.name;
|
5006 | let handlerName;
|
5007 |
|
5008 | if (this.changeHandler === null) {
|
5009 | handlerName = name + 'Changed';
|
5010 | if (handlerName in target.prototype) {
|
5011 | this.changeHandler = handlerName;
|
5012 | }
|
5013 | }
|
5014 |
|
5015 | if (this.descriptor === null) {
|
5016 | Object.defineProperty(target.prototype, name, this._configureDescriptor(behavior, {}));
|
5017 | }
|
5018 | }
|
5019 |
|
5020 | |
5021 |
|
5022 |
|
5023 |
|
5024 |
|
5025 | createObserver(viewModel: Object): BehaviorPropertyObserver {
|
5026 | let selfSubscriber = null;
|
5027 | let defaultValue = this.defaultValue;
|
5028 | let changeHandlerName = this.changeHandler;
|
5029 | let name = this.name;
|
5030 | let initialValue;
|
5031 |
|
5032 | if (this.hasOptions) {
|
5033 | return undefined;
|
5034 | }
|
5035 |
|
5036 | if (changeHandlerName in viewModel) {
|
5037 | if ('propertyChanged' in viewModel) {
|
5038 | selfSubscriber = (newValue, oldValue) => {
|
5039 | viewModel[changeHandlerName](newValue, oldValue);
|
5040 | viewModel.propertyChanged(name, newValue, oldValue);
|
5041 | };
|
5042 | } else {
|
5043 | selfSubscriber = (newValue, oldValue) => viewModel[changeHandlerName](newValue, oldValue);
|
5044 | }
|
5045 | } else if ('propertyChanged' in viewModel) {
|
5046 | selfSubscriber = (newValue, oldValue) => viewModel.propertyChanged(name, newValue, oldValue);
|
5047 | } else if (changeHandlerName !== null) {
|
5048 | throw new Error(`Change handler ${changeHandlerName} was specified but not declared on the class.`);
|
5049 | }
|
5050 |
|
5051 | if (defaultValue !== undefined) {
|
5052 | initialValue = typeof defaultValue === 'function' ? defaultValue.call(viewModel) : defaultValue;
|
5053 | }
|
5054 |
|
5055 | return new BehaviorPropertyObserver(this.owner.taskQueue, viewModel, this.name, selfSubscriber, initialValue);
|
5056 | }
|
5057 |
|
5058 | _initialize(viewModel, observerLookup, attributes, behaviorHandlesBind, boundProperties): void {
|
5059 | let selfSubscriber;
|
5060 | let observer;
|
5061 | let attribute;
|
5062 | let defaultValue = this.defaultValue;
|
5063 |
|
5064 | if (this.isDynamic) {
|
5065 | for (let key in attributes) {
|
5066 | this._createDynamicProperty(viewModel, observerLookup, behaviorHandlesBind, key, attributes[key], boundProperties);
|
5067 | }
|
5068 | } else if (!this.hasOptions) {
|
5069 | observer = observerLookup[this.name];
|
5070 |
|
5071 | if (attributes !== null) {
|
5072 | selfSubscriber = observer.selfSubscriber;
|
5073 | attribute = attributes[this.attribute];
|
5074 |
|
5075 | if (behaviorHandlesBind) {
|
5076 | observer.selfSubscriber = null;
|
5077 | }
|
5078 |
|
5079 | if (typeof attribute === 'string') {
|
5080 | viewModel[this.name] = attribute;
|
5081 | observer.call();
|
5082 | } else if (attribute) {
|
5083 | boundProperties.push({observer: observer, binding: attribute.createBinding(viewModel)});
|
5084 | } else if (defaultValue !== undefined) {
|
5085 | observer.call();
|
5086 | }
|
5087 |
|
5088 | observer.selfSubscriber = selfSubscriber;
|
5089 | }
|
5090 |
|
5091 | observer.publishing = true;
|
5092 | }
|
5093 | }
|
5094 |
|
5095 | _createDynamicProperty(viewModel, observerLookup, behaviorHandlesBind, name, attribute, boundProperties) {
|
5096 | let changeHandlerName = name + 'Changed';
|
5097 | let selfSubscriber = null;
|
5098 | let observer;
|
5099 | let info;
|
5100 |
|
5101 | if (changeHandlerName in viewModel) {
|
5102 | if ('propertyChanged' in viewModel) {
|
5103 | selfSubscriber = (newValue, oldValue) => {
|
5104 | viewModel[changeHandlerName](newValue, oldValue);
|
5105 | viewModel.propertyChanged(name, newValue, oldValue);
|
5106 | };
|
5107 | } else {
|
5108 | selfSubscriber = (newValue, oldValue) => viewModel[changeHandlerName](newValue, oldValue);
|
5109 | }
|
5110 | } else if ('propertyChanged' in viewModel) {
|
5111 | selfSubscriber = (newValue, oldValue) => viewModel.propertyChanged(name, newValue, oldValue);
|
5112 | }
|
5113 |
|
5114 | observer = observerLookup[name] = new BehaviorPropertyObserver(
|
5115 | this.owner.taskQueue,
|
5116 | viewModel,
|
5117 | name,
|
5118 | selfSubscriber
|
5119 | );
|
5120 |
|
5121 | Object.defineProperty(viewModel, name, {
|
5122 | configurable: true,
|
5123 | enumerable: true,
|
5124 | get: observer.getValue.bind(observer),
|
5125 | set: observer.setValue.bind(observer)
|
5126 | });
|
5127 |
|
5128 | if (behaviorHandlesBind) {
|
5129 | observer.selfSubscriber = null;
|
5130 | }
|
5131 |
|
5132 | if (typeof attribute === 'string') {
|
5133 | viewModel[name] = attribute;
|
5134 | observer.call();
|
5135 | } else if (attribute) {
|
5136 | info = {observer: observer, binding: attribute.createBinding(viewModel)};
|
5137 | boundProperties.push(info);
|
5138 | }
|
5139 |
|
5140 | observer.publishing = true;
|
5141 | observer.selfSubscriber = selfSubscriber;
|
5142 | }
|
5143 | }
|
5144 |
|
5145 | let lastProviderId = 0;
|
5146 |
|
5147 | function nextProviderId() {
|
5148 | return ++lastProviderId;
|
5149 | }
|
5150 |
|
5151 | function doProcessContent() { return true; }
|
5152 | function doProcessAttributes() {}
|
5153 |
|
5154 |
|
5155 |
|
5156 |
|
5157 |
|
5158 | export class HtmlBehaviorResource {
|
5159 | |
5160 |
|
5161 |
|
5162 | constructor() {
|
5163 | this.elementName = null;
|
5164 | this.attributeName = null;
|
5165 | this.attributeDefaultBindingMode = undefined;
|
5166 | this.liftsContent = false;
|
5167 | this.targetShadowDOM = false;
|
5168 | this.shadowDOMOptions = null;
|
5169 | this.processAttributes = doProcessAttributes;
|
5170 | this.processContent = doProcessContent;
|
5171 | this.usesShadowDOM = false;
|
5172 | this.childBindings = null;
|
5173 | this.hasDynamicOptions = false;
|
5174 | this.containerless = false;
|
5175 | this.properties = [];
|
5176 | this.attributes = {};
|
5177 | this.isInitialized = false;
|
5178 | this.primaryProperty = null;
|
5179 | }
|
5180 |
|
5181 | |
5182 |
|
5183 |
|
5184 |
|
5185 |
|
5186 | static convention(name: string, existing?: HtmlBehaviorResource): HtmlBehaviorResource {
|
5187 | let behavior;
|
5188 |
|
5189 | if (name.endsWith('CustomAttribute')) {
|
5190 | behavior = existing || new HtmlBehaviorResource();
|
5191 | behavior.attributeName = _hyphenate(name.substring(0, name.length - 15));
|
5192 | }
|
5193 |
|
5194 | if (name.endsWith('CustomElement')) {
|
5195 | behavior = existing || new HtmlBehaviorResource();
|
5196 | behavior.elementName = _hyphenate(name.substring(0, name.length - 13));
|
5197 | }
|
5198 |
|
5199 | return behavior;
|
5200 | }
|
5201 |
|
5202 | |
5203 |
|
5204 |
|
5205 |
|
5206 | addChildBinding(behavior: Object): void {
|
5207 | if (this.childBindings === null) {
|
5208 | this.childBindings = [];
|
5209 | }
|
5210 |
|
5211 | this.childBindings.push(behavior);
|
5212 | }
|
5213 |
|
5214 | |
5215 |
|
5216 |
|
5217 |
|
5218 |
|
5219 |
|
5220 | initialize(container: Container, target: Function): void {
|
5221 | let proto = target.prototype;
|
5222 | let properties = this.properties;
|
5223 | let attributeName = this.attributeName;
|
5224 | let attributeDefaultBindingMode = this.attributeDefaultBindingMode;
|
5225 | let i;
|
5226 | let ii;
|
5227 | let current;
|
5228 |
|
5229 | if (this.isInitialized) {
|
5230 | return;
|
5231 | }
|
5232 |
|
5233 | this.isInitialized = true;
|
5234 | target.__providerId__ = nextProviderId();
|
5235 |
|
5236 | this.observerLocator = container.get(ObserverLocator);
|
5237 | this.taskQueue = container.get(TaskQueue);
|
5238 |
|
5239 | this.target = target;
|
5240 | this.usesShadowDOM = this.targetShadowDOM && FEATURE.shadowDOM;
|
5241 | this.handlesCreated = ('created' in proto);
|
5242 | this.handlesBind = ('bind' in proto);
|
5243 | this.handlesUnbind = ('unbind' in proto);
|
5244 | this.handlesAttached = ('attached' in proto);
|
5245 | this.handlesDetached = ('detached' in proto);
|
5246 | this.htmlName = this.elementName || this.attributeName;
|
5247 |
|
5248 | if (attributeName !== null) {
|
5249 | if (properties.length === 0) {
|
5250 | new BindableProperty({
|
5251 | name: 'value',
|
5252 | changeHandler: 'valueChanged' in proto ? 'valueChanged' : null,
|
5253 | attribute: attributeName,
|
5254 | defaultBindingMode: attributeDefaultBindingMode
|
5255 | }).registerWith(target, this);
|
5256 | }
|
5257 |
|
5258 | current = properties[0];
|
5259 |
|
5260 | if (properties.length === 1 && current.name === 'value') {
|
5261 | current.isDynamic = current.hasOptions = this.hasDynamicOptions;
|
5262 | current.defineOn(target, this);
|
5263 | } else {
|
5264 | for (i = 0, ii = properties.length; i < ii; ++i) {
|
5265 | properties[i].defineOn(target, this);
|
5266 | if (properties[i].primaryProperty) {
|
5267 | if (this.primaryProperty) {
|
5268 | throw new Error('Only one bindable property on a custom element can be defined as the default');
|
5269 | }
|
5270 | this.primaryProperty = properties[i];
|
5271 | }
|
5272 | }
|
5273 |
|
5274 | current = new BindableProperty({
|
5275 | name: 'value',
|
5276 | changeHandler: 'valueChanged' in proto ? 'valueChanged' : null,
|
5277 | attribute: attributeName,
|
5278 | defaultBindingMode: attributeDefaultBindingMode
|
5279 | });
|
5280 |
|
5281 | current.hasOptions = true;
|
5282 | current.registerWith(target, this);
|
5283 | }
|
5284 | } else {
|
5285 | for (i = 0, ii = properties.length; i < ii; ++i) {
|
5286 | properties[i].defineOn(target, this);
|
5287 | }
|
5288 |
|
5289 |
|
5290 |
|
5291 | this._copyInheritedProperties(container, target);
|
5292 | }
|
5293 | }
|
5294 |
|
5295 | |
5296 |
|
5297 |
|
5298 |
|
5299 |
|
5300 |
|
5301 |
|
5302 | register(registry: ViewResources, name?: string): void {
|
5303 | if (this.attributeName !== null) {
|
5304 | registry.registerAttribute(name || this.attributeName, this, this.attributeName);
|
5305 |
|
5306 | if (Array.isArray(this.aliases)) {
|
5307 | this.aliases
|
5308 | .forEach( (alias) => {
|
5309 | registry.registerAttribute(alias, this, this.attributeName);
|
5310 | });
|
5311 | }
|
5312 | }
|
5313 |
|
5314 | if (this.elementName !== null) {
|
5315 | registry.registerElement(name || this.elementName, this);
|
5316 | }
|
5317 | }
|
5318 |
|
5319 | |
5320 |
|
5321 |
|
5322 |
|
5323 |
|
5324 |
|
5325 |
|
5326 |
|
5327 |
|
5328 |
|
5329 | load(container: Container, target: Function, loadContext?: ResourceLoadContext, viewStrategy?: ViewStrategy, transientView?: boolean): Promise<HtmlBehaviorResource> {
|
5330 | let options;
|
5331 |
|
5332 | if (this.elementName !== null) {
|
5333 | viewStrategy = container.get(ViewLocator).getViewStrategy(viewStrategy || this.viewStrategy || target);
|
5334 | options = new ViewCompileInstruction(this.targetShadowDOM, true);
|
5335 |
|
5336 | if (!viewStrategy.moduleId) {
|
5337 | viewStrategy.moduleId = Origin.get(target).moduleId;
|
5338 | }
|
5339 |
|
5340 | return viewStrategy.loadViewFactory(container.get(ViewEngine), options, loadContext, target).then(viewFactory => {
|
5341 | if (!transientView || !this.viewFactory) {
|
5342 | this.viewFactory = viewFactory;
|
5343 | }
|
5344 |
|
5345 | return viewFactory;
|
5346 | });
|
5347 | }
|
5348 |
|
5349 | return Promise.resolve(this);
|
5350 | }
|
5351 |
|
5352 | |
5353 |
|
5354 |
|
5355 |
|
5356 |
|
5357 |
|
5358 |
|
5359 |
|
5360 |
|
5361 | compile(compiler: ViewCompiler, resources: ViewResources, node: Node, instruction: BehaviorInstruction, parentNode?: Node): Node {
|
5362 | if (this.liftsContent) {
|
5363 | if (!instruction.viewFactory) {
|
5364 | let template = DOM.createElement('template');
|
5365 | let fragment = DOM.createDocumentFragment();
|
5366 | let cacheSize = node.getAttribute('view-cache');
|
5367 | let part = node.getAttribute('part');
|
5368 |
|
5369 | node.removeAttribute(instruction.originalAttrName);
|
5370 | DOM.replaceNode(template, node, parentNode);
|
5371 | fragment.appendChild(node);
|
5372 | instruction.viewFactory = compiler.compile(fragment, resources);
|
5373 |
|
5374 | if (part) {
|
5375 | instruction.viewFactory.part = part;
|
5376 | node.removeAttribute('part');
|
5377 | }
|
5378 |
|
5379 | if (cacheSize) {
|
5380 | instruction.viewFactory.setCacheSize(cacheSize);
|
5381 | node.removeAttribute('view-cache');
|
5382 | }
|
5383 |
|
5384 | node = template;
|
5385 | }
|
5386 | } else if (this.elementName !== null) {
|
5387 | let partReplacements = {};
|
5388 |
|
5389 | if (this.processContent(compiler, resources, node, instruction) && node.hasChildNodes()) {
|
5390 | let currentChild = node.firstChild;
|
5391 | let contentElement = this.usesShadowDOM ? null : DOM.createElement('au-content');
|
5392 | let nextSibling;
|
5393 | let toReplace;
|
5394 |
|
5395 | while (currentChild) {
|
5396 | nextSibling = currentChild.nextSibling;
|
5397 |
|
5398 | if (currentChild.tagName === 'TEMPLATE' && (toReplace = currentChild.getAttribute('replace-part'))) {
|
5399 | partReplacements[toReplace] = compiler.compile(currentChild, resources);
|
5400 | DOM.removeNode(currentChild, parentNode);
|
5401 | instruction.partReplacements = partReplacements;
|
5402 | } else if (contentElement !== null) {
|
5403 | if (currentChild.nodeType === 3 && _isAllWhitespace(currentChild)) {
|
5404 | DOM.removeNode(currentChild, parentNode);
|
5405 | } else {
|
5406 | contentElement.appendChild(currentChild);
|
5407 | }
|
5408 | }
|
5409 |
|
5410 | currentChild = nextSibling;
|
5411 | }
|
5412 |
|
5413 | if (contentElement !== null && contentElement.hasChildNodes()) {
|
5414 | node.appendChild(contentElement);
|
5415 | }
|
5416 |
|
5417 | instruction.skipContentProcessing = false;
|
5418 | } else {
|
5419 | instruction.skipContentProcessing = true;
|
5420 | }
|
5421 | } else if (!this.processContent(compiler, resources, node, instruction)) {
|
5422 | instruction.skipContentProcessing = true;
|
5423 | }
|
5424 |
|
5425 | return node;
|
5426 | }
|
5427 |
|
5428 | |
5429 |
|
5430 |
|
5431 |
|
5432 |
|
5433 |
|
5434 |
|
5435 |
|
5436 | create(container: Container, instruction?: BehaviorInstruction, element?: Element, bindings?: Binding[]): Controller {
|
5437 | let viewHost;
|
5438 | let au = null;
|
5439 |
|
5440 | instruction = instruction || BehaviorInstruction.normal;
|
5441 | element = element || null;
|
5442 | bindings = bindings || null;
|
5443 |
|
5444 | if (this.elementName !== null && element) {
|
5445 | if (this.usesShadowDOM) {
|
5446 | viewHost = element.attachShadow(this.shadowDOMOptions);
|
5447 | container.registerInstance(DOM.boundary, viewHost);
|
5448 | } else {
|
5449 | viewHost = element;
|
5450 | if (this.targetShadowDOM) {
|
5451 | container.registerInstance(DOM.boundary, viewHost);
|
5452 | }
|
5453 | }
|
5454 | }
|
5455 |
|
5456 | if (element !== null) {
|
5457 | element.au = au = element.au || {};
|
5458 | }
|
5459 |
|
5460 | let viewModel = instruction.viewModel || container.get(this.target);
|
5461 | let controller = new Controller(this, instruction, viewModel, container);
|
5462 | let childBindings = this.childBindings;
|
5463 | let viewFactory;
|
5464 |
|
5465 | if (this.liftsContent) {
|
5466 |
|
5467 | au.controller = controller;
|
5468 | } else if (this.elementName !== null) {
|
5469 |
|
5470 | viewFactory = instruction.viewFactory || this.viewFactory;
|
5471 | container.viewModel = viewModel;
|
5472 |
|
5473 | if (viewFactory) {
|
5474 | controller.view = viewFactory.create(container, instruction, element);
|
5475 | }
|
5476 |
|
5477 | if (element !== null) {
|
5478 | au.controller = controller;
|
5479 |
|
5480 | if (controller.view) {
|
5481 | if (!this.usesShadowDOM && (element.childNodes.length === 1 || element.contentElement)) {
|
5482 | let contentElement = element.childNodes[0] || element.contentElement;
|
5483 | controller.view.contentView = { fragment: contentElement };
|
5484 | contentElement.parentNode && DOM.removeNode(contentElement);
|
5485 | }
|
5486 |
|
5487 | if (instruction.anchorIsContainer) {
|
5488 | if (childBindings !== null) {
|
5489 | for (let i = 0, ii = childBindings.length; i < ii; ++i) {
|
5490 | controller.view.addBinding(childBindings[i].create(element, viewModel, controller));
|
5491 | }
|
5492 | }
|
5493 |
|
5494 | controller.view.appendNodesTo(viewHost);
|
5495 | } else {
|
5496 | controller.view.insertNodesBefore(viewHost);
|
5497 | }
|
5498 | } else if (childBindings !== null) {
|
5499 | for (let i = 0, ii = childBindings.length; i < ii; ++i) {
|
5500 | bindings.push(childBindings[i].create(element, viewModel, controller));
|
5501 | }
|
5502 | }
|
5503 | } else if (controller.view) {
|
5504 |
|
5505 | controller.view.controller = controller;
|
5506 |
|
5507 | if (childBindings !== null) {
|
5508 | for (let i = 0, ii = childBindings.length; i < ii; ++i) {
|
5509 | controller.view.addBinding(childBindings[i].create(instruction.host, viewModel, controller));
|
5510 | }
|
5511 | }
|
5512 | } else if (childBindings !== null) {
|
5513 |
|
5514 | for (let i = 0, ii = childBindings.length; i < ii; ++i) {
|
5515 | bindings.push(childBindings[i].create(instruction.host, viewModel, controller));
|
5516 | }
|
5517 | }
|
5518 | } else if (childBindings !== null) {
|
5519 |
|
5520 | for (let i = 0, ii = childBindings.length; i < ii; ++i) {
|
5521 | bindings.push(childBindings[i].create(element, viewModel, controller));
|
5522 | }
|
5523 | }
|
5524 |
|
5525 | if (au !== null) {
|
5526 | au[this.htmlName] = controller;
|
5527 | }
|
5528 |
|
5529 | if (instruction.initiatedByBehavior && viewFactory) {
|
5530 | controller.view.created();
|
5531 | }
|
5532 |
|
5533 | return controller;
|
5534 | }
|
5535 |
|
5536 | _ensurePropertiesDefined(instance: Object, lookup: Object) {
|
5537 | let properties;
|
5538 | let i;
|
5539 | let ii;
|
5540 | let observer;
|
5541 |
|
5542 | if ('__propertiesDefined__' in lookup) {
|
5543 | return;
|
5544 | }
|
5545 |
|
5546 | lookup.__propertiesDefined__ = true;
|
5547 | properties = this.properties;
|
5548 |
|
5549 | for (i = 0, ii = properties.length; i < ii; ++i) {
|
5550 | observer = properties[i].createObserver(instance);
|
5551 |
|
5552 | if (observer !== undefined) {
|
5553 | lookup[observer.propertyName] = observer;
|
5554 | }
|
5555 | }
|
5556 | }
|
5557 |
|
5558 | _copyInheritedProperties(container: Container, target: Function) {
|
5559 |
|
5560 |
|
5561 |
|
5562 |
|
5563 |
|
5564 | let behavior;
|
5565 | let derived = target;
|
5566 |
|
5567 | while (true) {
|
5568 | let proto = Object.getPrototypeOf(target.prototype);
|
5569 | target = proto && proto.constructor;
|
5570 | if (!target) {
|
5571 | return;
|
5572 | }
|
5573 | behavior = metadata.getOwn(metadata.resource, target);
|
5574 | if (behavior) {
|
5575 | break;
|
5576 | }
|
5577 | }
|
5578 | behavior.initialize(container, target);
|
5579 | for (let i = 0, ii = behavior.properties.length; i < ii; ++i) {
|
5580 | let prop = behavior.properties[i];
|
5581 |
|
5582 | if (this.properties.some(p => p.name === prop.name)) {
|
5583 | continue;
|
5584 | }
|
5585 |
|
5586 |
|
5587 | new BindableProperty(prop).registerWith(derived, this);
|
5588 | }
|
5589 | }
|
5590 | }
|
5591 |
|
5592 | function createChildObserverDecorator(selectorOrConfig, all) {
|
5593 | return function(target, key, descriptor) {
|
5594 | let actualTarget = typeof key === 'string' ? target.constructor : target;
|
5595 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, actualTarget);
|
5596 |
|
5597 | if (typeof selectorOrConfig === 'string') {
|
5598 | selectorOrConfig = {
|
5599 | selector: selectorOrConfig,
|
5600 | name: key
|
5601 | };
|
5602 | }
|
5603 |
|
5604 | if (descriptor) {
|
5605 | descriptor.writable = true;
|
5606 | descriptor.configurable = true;
|
5607 | }
|
5608 |
|
5609 | selectorOrConfig.all = all;
|
5610 | r.addChildBinding(new ChildObserver(selectorOrConfig));
|
5611 | };
|
5612 | }
|
5613 |
|
5614 |
|
5615 |
|
5616 |
|
5617 | export function children(selectorOrConfig: string | Object): any {
|
5618 | return createChildObserverDecorator(selectorOrConfig, true);
|
5619 | }
|
5620 |
|
5621 |
|
5622 |
|
5623 |
|
5624 | export function child(selectorOrConfig: string | Object): any {
|
5625 | return createChildObserverDecorator(selectorOrConfig, false);
|
5626 | }
|
5627 |
|
5628 | class ChildObserver {
|
5629 | constructor(config) {
|
5630 | this.name = config.name;
|
5631 | this.changeHandler = config.changeHandler || this.name + 'Changed';
|
5632 | this.selector = config.selector;
|
5633 | this.all = config.all;
|
5634 | }
|
5635 |
|
5636 | create(viewHost, viewModel, controller) {
|
5637 | return new ChildObserverBinder(this.selector, viewHost, this.name, viewModel, controller, this.changeHandler, this.all);
|
5638 | }
|
5639 | }
|
5640 |
|
5641 | const noMutations = [];
|
5642 |
|
5643 | function trackMutation(groupedMutations, binder, record) {
|
5644 | let mutations = groupedMutations.get(binder);
|
5645 |
|
5646 | if (!mutations) {
|
5647 | mutations = [];
|
5648 | groupedMutations.set(binder, mutations);
|
5649 | }
|
5650 |
|
5651 | mutations.push(record);
|
5652 | }
|
5653 |
|
5654 | function onChildChange(mutations, observer) {
|
5655 | let binders = observer.binders;
|
5656 | let bindersLength = binders.length;
|
5657 | let groupedMutations = new Map();
|
5658 |
|
5659 | for (let i = 0, ii = mutations.length; i < ii; ++i) {
|
5660 | let record = mutations[i];
|
5661 | let added = record.addedNodes;
|
5662 | let removed = record.removedNodes;
|
5663 |
|
5664 | for (let j = 0, jj = removed.length; j < jj; ++j) {
|
5665 | let node = removed[j];
|
5666 | if (node.nodeType === 1) {
|
5667 | for (let k = 0; k < bindersLength; ++k) {
|
5668 | let binder = binders[k];
|
5669 | if (binder.onRemove(node)) {
|
5670 | trackMutation(groupedMutations, binder, record);
|
5671 | }
|
5672 | }
|
5673 | }
|
5674 | }
|
5675 |
|
5676 | for (let j = 0, jj = added.length; j < jj; ++j) {
|
5677 | let node = added[j];
|
5678 | if (node.nodeType === 1) {
|
5679 | for (let k = 0; k < bindersLength; ++k) {
|
5680 | let binder = binders[k];
|
5681 | if (binder.onAdd(node)) {
|
5682 | trackMutation(groupedMutations, binder, record);
|
5683 | }
|
5684 | }
|
5685 | }
|
5686 | }
|
5687 | }
|
5688 |
|
5689 | groupedMutations.forEach((value, key) => {
|
5690 | if (key.changeHandler !== null) {
|
5691 | key.viewModel[key.changeHandler](value);
|
5692 | }
|
5693 | });
|
5694 | }
|
5695 |
|
5696 | class ChildObserverBinder {
|
5697 | constructor(selector, viewHost, property, viewModel, controller, changeHandler, all) {
|
5698 | this.selector = selector;
|
5699 | this.viewHost = viewHost;
|
5700 | this.property = property;
|
5701 | this.viewModel = viewModel;
|
5702 | this.controller = controller;
|
5703 | this.changeHandler = changeHandler in viewModel ? changeHandler : null;
|
5704 | this.usesShadowDOM = controller.behavior.usesShadowDOM;
|
5705 | this.all = all;
|
5706 |
|
5707 | if (!this.usesShadowDOM && controller.view && controller.view.contentView) {
|
5708 | this.contentView = controller.view.contentView;
|
5709 | } else {
|
5710 | this.contentView = null;
|
5711 | }
|
5712 | }
|
5713 |
|
5714 | matches(element) {
|
5715 | if (element.matches(this.selector)) {
|
5716 | if (this.contentView === null) {
|
5717 | return true;
|
5718 | }
|
5719 |
|
5720 | let contentView = this.contentView;
|
5721 | let assignedSlot = element.auAssignedSlot;
|
5722 |
|
5723 | if (assignedSlot && assignedSlot.projectFromAnchors) {
|
5724 | let anchors = assignedSlot.projectFromAnchors;
|
5725 |
|
5726 | for (let i = 0, ii = anchors.length; i < ii; ++i) {
|
5727 | if (anchors[i].auOwnerView === contentView) {
|
5728 | return true;
|
5729 | }
|
5730 | }
|
5731 |
|
5732 | return false;
|
5733 | }
|
5734 |
|
5735 | return element.auOwnerView === contentView;
|
5736 | }
|
5737 |
|
5738 | return false;
|
5739 | }
|
5740 |
|
5741 | bind(source) {
|
5742 | let viewHost = this.viewHost;
|
5743 | let viewModel = this.viewModel;
|
5744 | let observer = viewHost.__childObserver__;
|
5745 |
|
5746 | if (!observer) {
|
5747 | observer = viewHost.__childObserver__ = DOM.createMutationObserver(onChildChange);
|
5748 |
|
5749 | let options = {
|
5750 | childList: true,
|
5751 | subtree: !this.usesShadowDOM
|
5752 | };
|
5753 |
|
5754 | observer.observe(viewHost, options);
|
5755 | observer.binders = [];
|
5756 | }
|
5757 |
|
5758 | observer.binders.push(this);
|
5759 |
|
5760 | if (this.usesShadowDOM) {
|
5761 | let current = viewHost.firstElementChild;
|
5762 |
|
5763 | if (this.all) {
|
5764 | let items = viewModel[this.property];
|
5765 | if (!items) {
|
5766 | items = viewModel[this.property] = [];
|
5767 | } else {
|
5768 |
|
5769 |
|
5770 | items.splice(0);
|
5771 | }
|
5772 |
|
5773 | while (current) {
|
5774 | if (this.matches(current)) {
|
5775 | items.push(current.au && current.au.controller ? current.au.controller.viewModel : current);
|
5776 | }
|
5777 |
|
5778 | current = current.nextElementSibling;
|
5779 | }
|
5780 |
|
5781 | if (this.changeHandler !== null) {
|
5782 | this.viewModel[this.changeHandler](noMutations);
|
5783 | }
|
5784 | } else {
|
5785 | while (current) {
|
5786 | if (this.matches(current)) {
|
5787 | let value = current.au && current.au.controller ? current.au.controller.viewModel : current;
|
5788 | this.viewModel[this.property] = value;
|
5789 |
|
5790 | if (this.changeHandler !== null) {
|
5791 | this.viewModel[this.changeHandler](value);
|
5792 | }
|
5793 |
|
5794 | break;
|
5795 | }
|
5796 |
|
5797 | current = current.nextElementSibling;
|
5798 | }
|
5799 | }
|
5800 | }
|
5801 | }
|
5802 |
|
5803 | onRemove(element) {
|
5804 | if (this.matches(element)) {
|
5805 | let value = element.au && element.au.controller ? element.au.controller.viewModel : element;
|
5806 |
|
5807 | if (this.all) {
|
5808 | let items = (this.viewModel[this.property] || (this.viewModel[this.property] = []));
|
5809 | let index = items.indexOf(value);
|
5810 |
|
5811 | if (index !== -1) {
|
5812 | items.splice(index, 1);
|
5813 | }
|
5814 |
|
5815 | return true;
|
5816 | }
|
5817 |
|
5818 | return false;
|
5819 | }
|
5820 |
|
5821 | return false;
|
5822 | }
|
5823 |
|
5824 | onAdd(element) {
|
5825 | if (this.matches(element)) {
|
5826 | let value = element.au && element.au.controller ? element.au.controller.viewModel : element;
|
5827 |
|
5828 | if (this.all) {
|
5829 | let items = (this.viewModel[this.property] || (this.viewModel[this.property] = []));
|
5830 |
|
5831 | if (this.selector === '*') {
|
5832 | items.push(value);
|
5833 | return true;
|
5834 | }
|
5835 |
|
5836 | let index = 0;
|
5837 | let prev = element.previousElementSibling;
|
5838 |
|
5839 | while (prev) {
|
5840 | if (this.matches(prev)) {
|
5841 | index++;
|
5842 | }
|
5843 |
|
5844 | prev = prev.previousElementSibling;
|
5845 | }
|
5846 |
|
5847 | items.splice(index, 0, value);
|
5848 | return true;
|
5849 | }
|
5850 |
|
5851 | this.viewModel[this.property] = value;
|
5852 |
|
5853 | if (this.changeHandler !== null) {
|
5854 | this.viewModel[this.changeHandler](value);
|
5855 | }
|
5856 | }
|
5857 |
|
5858 | return false;
|
5859 | }
|
5860 |
|
5861 | unbind() {
|
5862 | if (this.viewHost.__childObserver__) {
|
5863 | this.viewHost.__childObserver__.disconnect();
|
5864 | this.viewHost.__childObserver__ = null;
|
5865 | this.viewModel[this.property] = null;
|
5866 | }
|
5867 | }
|
5868 | }
|
5869 |
|
5870 | function remove(viewSlot, previous) {
|
5871 | return Array.isArray(previous)
|
5872 | ? viewSlot.removeMany(previous, true)
|
5873 | : viewSlot.remove(previous, true);
|
5874 | }
|
5875 |
|
5876 | export const SwapStrategies = {
|
5877 |
|
5878 | before(viewSlot, previous, callback) {
|
5879 | return (previous === undefined)
|
5880 | ? callback()
|
5881 | : callback().then(() => remove(viewSlot, previous));
|
5882 | },
|
5883 |
|
5884 |
|
5885 | with(viewSlot, previous, callback) {
|
5886 | return (previous === undefined)
|
5887 | ? callback()
|
5888 | : Promise.all([remove(viewSlot, previous), callback()]);
|
5889 | },
|
5890 |
|
5891 |
|
5892 | after(viewSlot, previous, callback) {
|
5893 | return Promise.resolve(viewSlot.removeAll(true)).then(callback);
|
5894 | }
|
5895 | };
|
5896 |
|
5897 |
|
5898 |
|
5899 |
|
5900 | interface CompositionContext {
|
5901 | |
5902 |
|
5903 |
|
5904 | container: Container;
|
5905 | |
5906 |
|
5907 |
|
5908 | childContainer?: Container;
|
5909 | |
5910 |
|
5911 |
|
5912 | bindingContext: any;
|
5913 | |
5914 |
|
5915 |
|
5916 | overrideContext?: any;
|
5917 | |
5918 |
|
5919 |
|
5920 | viewModel?: any;
|
5921 | |
5922 |
|
5923 |
|
5924 | model?: any;
|
5925 | |
5926 |
|
5927 |
|
5928 | viewModelResource?: HtmlBehaviorResource;
|
5929 | |
5930 |
|
5931 |
|
5932 | viewResources: ViewResources;
|
5933 | |
5934 |
|
5935 |
|
5936 | owningView?: View;
|
5937 | |
5938 |
|
5939 |
|
5940 | view?: string | ViewStrategy;
|
5941 | |
5942 |
|
5943 |
|
5944 | viewSlot: ViewSlot;
|
5945 | |
5946 |
|
5947 |
|
5948 | skipActivation?: boolean;
|
5949 | |
5950 |
|
5951 |
|
5952 |
|
5953 | host?: Element;
|
5954 | }
|
5955 |
|
5956 | function tryActivateViewModel(context) {
|
5957 | if (context.skipActivation || typeof context.viewModel.activate !== 'function') {
|
5958 | return Promise.resolve();
|
5959 | }
|
5960 |
|
5961 | return context.viewModel.activate(context.model) || Promise.resolve();
|
5962 | }
|
5963 |
|
5964 |
|
5965 |
|
5966 |
|
5967 | @inject(ViewEngine, ViewLocator)
|
5968 | export class CompositionEngine {
|
5969 | |
5970 |
|
5971 |
|
5972 |
|
5973 | constructor(viewEngine: ViewEngine, viewLocator: ViewLocator) {
|
5974 | this.viewEngine = viewEngine;
|
5975 | this.viewLocator = viewLocator;
|
5976 | }
|
5977 |
|
5978 | _swap(context, view) {
|
5979 | let swapStrategy = SwapStrategies[context.swapOrder] || SwapStrategies.after;
|
5980 | let previousViews = context.viewSlot.children.slice();
|
5981 |
|
5982 | return swapStrategy(context.viewSlot, previousViews, () => {
|
5983 | return Promise.resolve(context.viewSlot.add(view)).then(() => {
|
5984 | if (context.currentController) {
|
5985 | context.currentController.unbind();
|
5986 | }
|
5987 | });
|
5988 | }).then(() => {
|
5989 | if (context.compositionTransactionNotifier) {
|
5990 | context.compositionTransactionNotifier.done();
|
5991 | }
|
5992 | });
|
5993 | }
|
5994 |
|
5995 | _createControllerAndSwap(context) {
|
5996 | return this.createController(context).then(controller => {
|
5997 | if (context.compositionTransactionOwnershipToken) {
|
5998 | return context.compositionTransactionOwnershipToken
|
5999 | .waitForCompositionComplete()
|
6000 | .then(() => {
|
6001 | controller.automate(context.overrideContext, context.owningView);
|
6002 |
|
6003 | return this._swap(context, controller.view);
|
6004 | })
|
6005 | .then(() => controller);
|
6006 | }
|
6007 |
|
6008 | controller.automate(context.overrideContext, context.owningView);
|
6009 |
|
6010 | return this._swap(context, controller.view).then(() => controller);
|
6011 | });
|
6012 | }
|
6013 |
|
6014 | |
6015 |
|
6016 |
|
6017 |
|
6018 |
|
6019 | createController(context: CompositionContext): Promise<Controller> {
|
6020 | let childContainer;
|
6021 | let viewModel;
|
6022 | let viewModelResource;
|
6023 |
|
6024 | let m;
|
6025 |
|
6026 | return this
|
6027 | .ensureViewModel(context)
|
6028 | .then(tryActivateViewModel)
|
6029 | .then(() => {
|
6030 | childContainer = context.childContainer;
|
6031 | viewModel = context.viewModel;
|
6032 | viewModelResource = context.viewModelResource;
|
6033 | m = viewModelResource.metadata;
|
6034 |
|
6035 | let viewStrategy = this.viewLocator.getViewStrategy(context.view || viewModel);
|
6036 |
|
6037 | if (context.viewResources) {
|
6038 | viewStrategy.makeRelativeTo(context.viewResources.viewUrl);
|
6039 | }
|
6040 |
|
6041 | return m.load(
|
6042 | childContainer,
|
6043 | viewModelResource.value,
|
6044 | null,
|
6045 | viewStrategy,
|
6046 | true
|
6047 | );
|
6048 | }).then(viewFactory => m.create(
|
6049 | childContainer,
|
6050 | BehaviorInstruction.dynamic(context.host, viewModel, viewFactory)
|
6051 | ));
|
6052 | }
|
6053 |
|
6054 | |
6055 |
|
6056 |
|
6057 |
|
6058 |
|
6059 | ensureViewModel(context: CompositionContext): Promise<CompositionContext> {
|
6060 | let childContainer = context.childContainer = (context.childContainer || context.container.createChild());
|
6061 |
|
6062 | if (typeof context.viewModel === 'string') {
|
6063 | context.viewModel = context.viewResources
|
6064 | ? context.viewResources.relativeToView(context.viewModel)
|
6065 | : context.viewModel;
|
6066 |
|
6067 | return this.viewEngine.importViewModelResource(context.viewModel).then(viewModelResource => {
|
6068 | childContainer.autoRegister(viewModelResource.value);
|
6069 |
|
6070 | if (context.host) {
|
6071 | childContainer.registerInstance(DOM.Element, context.host);
|
6072 | }
|
6073 |
|
6074 | context.viewModel = childContainer.viewModel = childContainer.get(viewModelResource.value);
|
6075 | context.viewModelResource = viewModelResource;
|
6076 | return context;
|
6077 | });
|
6078 | }
|
6079 |
|
6080 |
|
6081 |
|
6082 | let ctor = context.viewModel.constructor;
|
6083 | let isClass = typeof context.viewModel === 'function';
|
6084 | if (isClass) {
|
6085 | ctor = context.viewModel;
|
6086 | childContainer.autoRegister(ctor);
|
6087 | }
|
6088 | let m = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, ctor);
|
6089 |
|
6090 |
|
6091 | m.elementName = m.elementName || 'dynamic-element';
|
6092 |
|
6093 |
|
6094 | m.initialize(isClass ? childContainer : (context.container || childContainer), ctor);
|
6095 |
|
6096 |
|
6097 | context.viewModelResource = { metadata: m, value: ctor };
|
6098 |
|
6099 | if (context.host) {
|
6100 | childContainer.registerInstance(DOM.Element, context.host);
|
6101 | }
|
6102 | childContainer.viewModel = context.viewModel = isClass ? childContainer.get(ctor) : context.viewModel;
|
6103 | return Promise.resolve(context);
|
6104 | }
|
6105 |
|
6106 | |
6107 |
|
6108 |
|
6109 |
|
6110 |
|
6111 | compose(context: CompositionContext): Promise<View | Controller> {
|
6112 | context.childContainer = context.childContainer || context.container.createChild();
|
6113 | context.view = this.viewLocator.getViewStrategy(context.view);
|
6114 |
|
6115 | let transaction = context.childContainer.get(CompositionTransaction);
|
6116 | let compositionTransactionOwnershipToken = transaction.tryCapture();
|
6117 |
|
6118 | if (compositionTransactionOwnershipToken) {
|
6119 | context.compositionTransactionOwnershipToken = compositionTransactionOwnershipToken;
|
6120 | } else {
|
6121 | context.compositionTransactionNotifier = transaction.enlist();
|
6122 | }
|
6123 |
|
6124 | if (context.viewModel) {
|
6125 | return this._createControllerAndSwap(context);
|
6126 | } else if (context.view) {
|
6127 | if (context.viewResources) {
|
6128 | context.view.makeRelativeTo(context.viewResources.viewUrl);
|
6129 | }
|
6130 |
|
6131 | return context.view.loadViewFactory(this.viewEngine, new ViewCompileInstruction()).then(viewFactory => {
|
6132 | let result = viewFactory.create(context.childContainer);
|
6133 | result.bind(context.bindingContext, context.overrideContext);
|
6134 |
|
6135 | if (context.compositionTransactionOwnershipToken) {
|
6136 | return context.compositionTransactionOwnershipToken.waitForCompositionComplete()
|
6137 | .then(() => this._swap(context, result))
|
6138 | .then(() => result);
|
6139 | }
|
6140 |
|
6141 | return this._swap(context, result).then(() => result);
|
6142 | });
|
6143 | } else if (context.viewSlot) {
|
6144 | context.viewSlot.removeAll();
|
6145 |
|
6146 | if (context.compositionTransactionNotifier) {
|
6147 | context.compositionTransactionNotifier.done();
|
6148 | }
|
6149 |
|
6150 | return Promise.resolve(null);
|
6151 | }
|
6152 |
|
6153 | return Promise.resolve(null);
|
6154 | }
|
6155 | }
|
6156 |
|
6157 |
|
6158 |
|
6159 |
|
6160 |
|
6161 |
|
6162 | export class ElementConfigResource {
|
6163 | |
6164 |
|
6165 |
|
6166 |
|
6167 |
|
6168 |
|
6169 | initialize(container: Container, target: Function): void {}
|
6170 |
|
6171 | |
6172 |
|
6173 |
|
6174 |
|
6175 |
|
6176 |
|
6177 |
|
6178 | register(registry: ViewResources, name?: string): void {}
|
6179 |
|
6180 | |
6181 |
|
6182 |
|
6183 |
|
6184 |
|
6185 |
|
6186 | load(container: Container, target: Function): void {
|
6187 | let config = new target();
|
6188 | let eventManager = container.get(EventManager);
|
6189 | eventManager.registerElementConfig(config);
|
6190 | }
|
6191 | }
|
6192 |
|
6193 |
|
6194 |
|
6195 |
|
6196 |
|
6197 | export function resource(instanceOrConfig: string | object): any {
|
6198 | return function(target) {
|
6199 | let isConfig = typeof instanceOrConfig === 'string' || Object.getPrototypeOf(instanceOrConfig) === Object.prototype;
|
6200 | if (isConfig) {
|
6201 | target.$resource = instanceOrConfig;
|
6202 | } else {
|
6203 | metadata.define(metadata.resource, instanceOrConfig, target);
|
6204 | }
|
6205 | };
|
6206 | }
|
6207 |
|
6208 |
|
6209 |
|
6210 |
|
6211 |
|
6212 | export function behavior(override: HtmlBehaviorResource | Object): any {
|
6213 | return function(target) {
|
6214 | if (override instanceof HtmlBehaviorResource) {
|
6215 | metadata.define(metadata.resource, override, target);
|
6216 | } else {
|
6217 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, target);
|
6218 | Object.assign(r, override);
|
6219 | }
|
6220 | };
|
6221 | }
|
6222 |
|
6223 |
|
6224 |
|
6225 |
|
6226 |
|
6227 | export function customElement(name: string): any {
|
6228 | return function(target) {
|
6229 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, target);
|
6230 | r.elementName = validateBehaviorName(name, 'custom element');
|
6231 | };
|
6232 | }
|
6233 |
|
6234 |
|
6235 |
|
6236 |
|
6237 |
|
6238 |
|
6239 |
|
6240 | export function customAttribute(name: string, defaultBindingMode?: number, aliases?: string[]): any {
|
6241 | return function(target) {
|
6242 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, target);
|
6243 | r.attributeName = validateBehaviorName(name, 'custom attribute');
|
6244 | r.attributeDefaultBindingMode = defaultBindingMode;
|
6245 | r.aliases = aliases;
|
6246 | };
|
6247 | }
|
6248 |
|
6249 |
|
6250 |
|
6251 |
|
6252 |
|
6253 |
|
6254 | export function templateController(target?): any {
|
6255 | let deco = function(t) {
|
6256 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, t);
|
6257 | r.liftsContent = true;
|
6258 | };
|
6259 |
|
6260 | return target ? deco(target) : deco;
|
6261 | }
|
6262 |
|
6263 |
|
6264 |
|
6265 |
|
6266 |
|
6267 | export function bindable(nameOrConfigOrTarget?: string | Object, key?, descriptor?): any {
|
6268 | let deco = function(target, key2, descriptor2) {
|
6269 | let actualTarget = key2 ? target.constructor : target;
|
6270 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, actualTarget);
|
6271 | let prop;
|
6272 |
|
6273 | if (key2) {
|
6274 | nameOrConfigOrTarget = nameOrConfigOrTarget || {};
|
6275 | nameOrConfigOrTarget.name = key2;
|
6276 | }
|
6277 |
|
6278 | prop = new BindableProperty(nameOrConfigOrTarget);
|
6279 | return prop.registerWith(actualTarget, r, descriptor2);
|
6280 | };
|
6281 |
|
6282 | if (!nameOrConfigOrTarget) {
|
6283 | return deco;
|
6284 | }
|
6285 |
|
6286 | if (key) {
|
6287 | let target = nameOrConfigOrTarget;
|
6288 | nameOrConfigOrTarget = null;
|
6289 | return deco(target, key, descriptor);
|
6290 | }
|
6291 |
|
6292 | return deco;
|
6293 | }
|
6294 |
|
6295 |
|
6296 |
|
6297 |
|
6298 |
|
6299 | export function dynamicOptions(target?): any {
|
6300 | let deco = function(t) {
|
6301 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, t);
|
6302 | r.hasDynamicOptions = true;
|
6303 | };
|
6304 |
|
6305 | return target ? deco(target) : deco;
|
6306 | }
|
6307 |
|
6308 | const defaultShadowDOMOptions = { mode: 'open' };
|
6309 |
|
6310 |
|
6311 |
|
6312 |
|
6313 | export function useShadowDOM(targetOrOptions?): any {
|
6314 | let options = typeof targetOrOptions === 'function' || !targetOrOptions
|
6315 | ? defaultShadowDOMOptions
|
6316 | : targetOrOptions;
|
6317 |
|
6318 | let deco = function(t) {
|
6319 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, t);
|
6320 | r.targetShadowDOM = true;
|
6321 | r.shadowDOMOptions = options;
|
6322 | };
|
6323 |
|
6324 | return typeof targetOrOptions === 'function' ? deco(targetOrOptions) : deco;
|
6325 | }
|
6326 |
|
6327 |
|
6328 |
|
6329 |
|
6330 |
|
6331 | export function processAttributes(processor: Function): any {
|
6332 | return function(t) {
|
6333 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, t);
|
6334 | r.processAttributes = function(compiler, resources, node, attributes, elementInstruction) {
|
6335 | try {
|
6336 | processor(compiler, resources, node, attributes, elementInstruction);
|
6337 | } catch (error) {
|
6338 | LogManager.getLogger('templating').error(error);
|
6339 | }
|
6340 | };
|
6341 | };
|
6342 | }
|
6343 |
|
6344 | function doNotProcessContent() { return false; }
|
6345 |
|
6346 |
|
6347 |
|
6348 |
|
6349 |
|
6350 |
|
6351 |
|
6352 |
|
6353 |
|
6354 | export function processContent(processor: boolean | Function): any {
|
6355 | return function(t) {
|
6356 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, t);
|
6357 | r.processContent = processor ? function(compiler, resources, node, instruction) {
|
6358 | try {
|
6359 | return processor(compiler, resources, node, instruction);
|
6360 | } catch (error) {
|
6361 | LogManager.getLogger('templating').error(error);
|
6362 | return false;
|
6363 | }
|
6364 | } : doNotProcessContent;
|
6365 | };
|
6366 | }
|
6367 |
|
6368 |
|
6369 |
|
6370 |
|
6371 |
|
6372 | export function containerless(target?): any {
|
6373 | let deco = function(t) {
|
6374 | let r = metadata.getOrCreateOwn(metadata.resource, HtmlBehaviorResource, t);
|
6375 | r.containerless = true;
|
6376 | };
|
6377 |
|
6378 | return target ? deco(target) : deco;
|
6379 | }
|
6380 |
|
6381 |
|
6382 |
|
6383 |
|
6384 |
|
6385 | export function useViewStrategy(strategy: Object): any {
|
6386 | return function(target) {
|
6387 | metadata.define(ViewLocator.viewStrategyMetadataKey, strategy, target);
|
6388 | };
|
6389 | }
|
6390 |
|
6391 |
|
6392 |
|
6393 |
|
6394 |
|
6395 | export function useView(path: string): any {
|
6396 | return useViewStrategy(new RelativeViewStrategy(path));
|
6397 | }
|
6398 |
|
6399 |
|
6400 |
|
6401 |
|
6402 |
|
6403 |
|
6404 |
|
6405 |
|
6406 | export function inlineView(markup:string, dependencies?:Array<string|Function|Object>, dependencyBaseUrl?:string): any {
|
6407 | return useViewStrategy(new InlineViewStrategy(markup, dependencies, dependencyBaseUrl));
|
6408 | }
|
6409 |
|
6410 |
|
6411 |
|
6412 |
|
6413 | export function noView(targetOrDependencies?:Function|Array<any>, dependencyBaseUrl?:string): any {
|
6414 | let target;
|
6415 | let dependencies;
|
6416 | if (typeof targetOrDependencies === 'function') {
|
6417 | target = targetOrDependencies;
|
6418 | } else {
|
6419 | dependencies = targetOrDependencies;
|
6420 | target = undefined;
|
6421 | }
|
6422 |
|
6423 | let deco = function(t) {
|
6424 | metadata.define(ViewLocator.viewStrategyMetadataKey, new NoViewStrategy(dependencies, dependencyBaseUrl), t);
|
6425 | };
|
6426 |
|
6427 | return target ? deco(target) : deco;
|
6428 | }
|
6429 |
|
6430 | interface IStaticViewStrategyConfig {
|
6431 | template: string | HTMLTemplateElement;
|
6432 | dependencies?: Function[] | { (): (Promise<Record<string, Function>> | Function)[] }
|
6433 | }
|
6434 |
|
6435 |
|
6436 |
|
6437 |
|
6438 | export function view(templateOrConfig:string|HTMLTemplateElement|IStaticViewStrategyConfig): any {
|
6439 | return function(target) {
|
6440 | target.$view = templateOrConfig;
|
6441 | };
|
6442 | }
|
6443 |
|
6444 |
|
6445 |
|
6446 |
|
6447 |
|
6448 | export function elementConfig(target?): any {
|
6449 | let deco = function(t) {
|
6450 | metadata.define(metadata.resource, new ElementConfigResource(), t);
|
6451 | };
|
6452 |
|
6453 | return target ? deco(target) : deco;
|
6454 | }
|
6455 |
|
6456 |
|
6457 |
|
6458 |
|
6459 |
|
6460 |
|
6461 | export function viewResources(...resources) {
|
6462 | return function(target) {
|
6463 | metadata.define(ViewEngine.viewModelRequireMetadataKey, resources, target);
|
6464 | };
|
6465 | }
|
6466 |
|
6467 |
|
6468 |
|
6469 |
|
6470 | interface EnhanceInstruction {
|
6471 | |
6472 |
|
6473 |
|
6474 | container?: Container;
|
6475 | |
6476 |
|
6477 |
|
6478 | element: Element;
|
6479 | |
6480 |
|
6481 |
|
6482 | resources?: ViewResources;
|
6483 | |
6484 |
|
6485 |
|
6486 | bindingContext?: Object;
|
6487 | |
6488 |
|
6489 |
|
6490 | overrideContext?: any;
|
6491 | }
|
6492 |
|
6493 |
|
6494 |
|
6495 |
|
6496 | @inject(Container, ModuleAnalyzer, ViewCompiler, CompositionEngine)
|
6497 | export class TemplatingEngine {
|
6498 | |
6499 |
|
6500 |
|
6501 |
|
6502 |
|
6503 |
|
6504 |
|
6505 | constructor(container: Container, moduleAnalyzer: ModuleAnalyzer, viewCompiler: ViewCompiler, compositionEngine: CompositionEngine) {
|
6506 | this._container = container;
|
6507 | this._moduleAnalyzer = moduleAnalyzer;
|
6508 | this._viewCompiler = viewCompiler;
|
6509 | this._compositionEngine = compositionEngine;
|
6510 | container.registerInstance(Animator, Animator.instance = new Animator());
|
6511 | }
|
6512 |
|
6513 | |
6514 |
|
6515 |
|
6516 |
|
6517 | configureAnimator(animator: Animator): void {
|
6518 | this._container.unregister(Animator);
|
6519 | this._container.registerInstance(Animator, Animator.instance = animator);
|
6520 | }
|
6521 |
|
6522 | |
6523 |
|
6524 |
|
6525 |
|
6526 |
|
6527 |
|
6528 | compose(context: CompositionContext): Promise<View | Controller> {
|
6529 | return this._compositionEngine.compose(context);
|
6530 | }
|
6531 |
|
6532 | |
6533 |
|
6534 |
|
6535 |
|
6536 |
|
6537 |
|
6538 | enhance(instruction: Element | EnhanceInstruction): View {
|
6539 | if (instruction instanceof DOM.Element) {
|
6540 | instruction = { element: instruction };
|
6541 | }
|
6542 |
|
6543 | let compilerInstructions = {};
|
6544 | let resources = instruction.resources || this._container.get(ViewResources);
|
6545 |
|
6546 | this._viewCompiler._compileNode(instruction.element, resources, compilerInstructions, instruction.element.parentNode, 'root', true);
|
6547 |
|
6548 | let factory = new ViewFactory(instruction.element, compilerInstructions, resources);
|
6549 | let container = instruction.container || this._container.createChild();
|
6550 | let view = factory.create(container, BehaviorInstruction.enhance());
|
6551 |
|
6552 | view.bind(instruction.bindingContext || {}, instruction.overrideContext);
|
6553 |
|
6554 | view.firstChild = view.lastChild = view.fragment;
|
6555 | view.fragment = DOM.createDocumentFragment();
|
6556 | view.attached();
|
6557 |
|
6558 | return view;
|
6559 | }
|
6560 | }
|