|
| 1 | +import { Directive, Input, ViewContainerRef, TemplateRef, EmbeddedViewRef, OnChanges, SimpleChanges, OnDestroy } from '@angular/core'; |
| 2 | + |
| 3 | +const COMPOSED_CONTEXT_TOKEN = '[COMPOSED_CONTEXT_TOKEN]'; |
| 4 | + |
| 5 | +interface ComposedContext { |
| 6 | + $implicit: any; |
| 7 | + templateRefs: TemplateRef<ComposedContext>[]; |
| 8 | + [key: string]: any; |
| 9 | +} |
| 10 | + |
| 11 | +class ComposedView { |
| 12 | + |
| 13 | + private context: ComposedContext = { |
| 14 | + $implicit: null, |
| 15 | + templateRefs: [], |
| 16 | + [COMPOSED_CONTEXT_TOKEN]: COMPOSED_CONTEXT_TOKEN |
| 17 | + }; |
| 18 | + private viewRef: EmbeddedViewRef<ComposedContext>; |
| 19 | + |
| 20 | + constructor(private viewContainerRef: ViewContainerRef) {} |
| 21 | + |
| 22 | + render($implicit: any, templateRefs: TemplateRef<ComposedContext>[]) { |
| 23 | + this.viewContainerRef.clear(); |
| 24 | + this.context.$implicit = $implicit; |
| 25 | + |
| 26 | + const [ templateRef, ...tail ] = templateRefs; |
| 27 | + this.context.templateRefs = tail; |
| 28 | + this.viewRef = |
| 29 | + this.viewContainerRef.createEmbeddedView(templateRef, this.context); |
| 30 | + } |
| 31 | + |
| 32 | + update($implicit: any) { |
| 33 | + if (this.viewRef) { |
| 34 | + this.context.$implicit = $implicit; |
| 35 | + this.viewRef.markForCheck(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + destroy() { |
| 40 | + this.viewContainerRef.clear(); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +@Directive({ selector: '[compose]' }) |
| 45 | +export class ComposeDirective implements OnChanges, OnDestroy { |
| 46 | + @Input() composeOf: TemplateRef<ComposedContext>[]; |
| 47 | + @Input() composeUse: any; |
| 48 | + |
| 49 | + private get templateRefs(): TemplateRef<ComposedContext>[] { |
| 50 | + return this.composeOf; |
| 51 | + } |
| 52 | + |
| 53 | + private composedView: ComposedView; |
| 54 | + |
| 55 | + constructor( |
| 56 | + private templateRef: TemplateRef<ComposedContext>, |
| 57 | + viewContainerRef: ViewContainerRef |
| 58 | + ) { |
| 59 | + this.composedView = new ComposedView(viewContainerRef); |
| 60 | + } |
| 61 | + |
| 62 | + ngOnChanges(changes: SimpleChanges) { |
| 63 | + if (changes.composeOf && this.composeOf) { |
| 64 | + return this.render(); |
| 65 | + } |
| 66 | + |
| 67 | + if (changes.composeUse && this.composeOf) { |
| 68 | + return this.update(); |
| 69 | + } |
| 70 | + |
| 71 | + if (changes.composeOf && !this.composeOf) { |
| 72 | + return this.destroy(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + ngOnDestroy() { |
| 77 | + this.destroy(); |
| 78 | + } |
| 79 | + |
| 80 | + private render() { |
| 81 | + const templateRefs = [...this.templateRefs, this.templateRef ]; |
| 82 | + this.composedView.render(this.composeUse, templateRefs); |
| 83 | + } |
| 84 | + |
| 85 | + private update() { |
| 86 | + this.composedView.update(this.composeUse); |
| 87 | + } |
| 88 | + |
| 89 | + private destroy() { |
| 90 | + this.composedView.destroy(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +@Directive({ selector: '[return]' }) |
| 95 | +export class ReturnDirective implements OnDestroy { |
| 96 | + @Input() set return(value: any) { |
| 97 | + if (this.templateRefsChanged) { |
| 98 | + this.previousTemplateRefs = this.templateRefs; |
| 99 | + this.composedView.render(value, this.templateRefs); |
| 100 | + } else { |
| 101 | + this.composedView.update(value); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private composedView: ComposedView; |
| 106 | + |
| 107 | + private previousTemplateRefs: TemplateRef<ComposedContext>[]; |
| 108 | + private get templateRefs(): TemplateRef<ComposedContext>[] { |
| 109 | + const view = (this.viewContainerRef.injector as any).view; |
| 110 | + const context: ComposedContext = findParentContext(view); |
| 111 | + return context.templateRefs; |
| 112 | + } |
| 113 | + private get templateRefsChanged(): boolean { |
| 114 | + return this.previousTemplateRefs !== this.templateRefs; |
| 115 | + } |
| 116 | + |
| 117 | + constructor(private viewContainerRef: ViewContainerRef) { |
| 118 | + this.composedView = new ComposedView(viewContainerRef); |
| 119 | + } |
| 120 | + |
| 121 | + ngOnDestroy() { |
| 122 | + this.composedView.destroy(); |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +function findParentContext(view: any): ComposedContext { |
| 128 | + const context: any = view.context; |
| 129 | + |
| 130 | + if (isComposedContext(context)) { |
| 131 | + return context; |
| 132 | + } |
| 133 | + |
| 134 | + return findParentContext(view.parent); |
| 135 | +} |
| 136 | + |
| 137 | +function isComposedContext(context: any): context is ComposedContext { |
| 138 | + return context && context[COMPOSED_CONTEXT_TOKEN]; |
| 139 | +} |
0 commit comments