|
| 1 | +import { |
| 2 | + ChangeDetectorRef, |
| 3 | + Directive, |
| 4 | + EmbeddedViewRef, |
| 5 | + Input, |
| 6 | + IterableChangeRecord, |
| 7 | + IterableChanges, |
| 8 | + IterableDiffer, |
| 9 | + IterableDiffers, |
| 10 | + NgIterable, |
| 11 | + OnDestroy, |
| 12 | + OnInit, |
| 13 | + TemplateRef, |
| 14 | + TrackByFunction, |
| 15 | + ViewContainerRef |
| 16 | +} from '@angular/core'; |
| 17 | + |
| 18 | +import { Observable, ObservableInput, ReplaySubject, Subscription, Unsubscribable } from 'rxjs'; |
| 19 | +import { distinctUntilChanged, filter, map, switchAll, tap } from 'rxjs/operators'; |
| 20 | + |
| 21 | +interface RecordViewTuple<T, U extends NgIterable<T>> { |
| 22 | + record: any; |
| 23 | + view: EmbeddedViewRef<Poc5Locv5ViewContext<T, U>>; |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +export class Poc5Locv5ViewContext<T, U extends NgIterable<T> = NgIterable<T>> { |
| 28 | + |
| 29 | + localVariableProjections: CustomVariablesProjectors = {}; |
| 30 | + |
| 31 | + constructor(public $implicit: T, public pocLet: U, public index: number, public count: number) { |
| 32 | + } |
| 33 | + |
| 34 | + get first(): boolean { |
| 35 | + return this.index === 0; |
| 36 | + } |
| 37 | + |
| 38 | + get last(): boolean { |
| 39 | + return this.index === this.count - 1; |
| 40 | + } |
| 41 | + |
| 42 | + get even(): boolean { |
| 43 | + return this.index % 2 === 0; |
| 44 | + } |
| 45 | + |
| 46 | + get odd(): boolean { |
| 47 | + return !this.even; |
| 48 | + } |
| 49 | + |
| 50 | + get customVariable(): unknown { |
| 51 | + return Object.entries(this.localVariableProjections) |
| 52 | + .reduce((acc, [name, fn]) => { |
| 53 | + return { ...acc, [name]: fn(this) }; |
| 54 | + }, {}); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +interface CustomVariablesProjectors { |
| 59 | + [variableName: string]: (context) => unknown; |
| 60 | +} |
| 61 | + |
| 62 | +@Directive({ |
| 63 | + // tslint:disable-next-line:directive-selector |
| 64 | + selector: '[poc5LocV]' |
| 65 | +}) |
| 66 | +export class Poc5Locv5<T, U extends NgIterable<T> = NgIterable<T>> implements OnInit, OnDestroy { |
| 67 | + private differ: IterableDiffer<T> | null = null; |
| 68 | + private subscription: Unsubscribable = new Subscription(); |
| 69 | + |
| 70 | + observables$ = new ReplaySubject<ObservableInput<U & NgIterable<T>>>(1); |
| 71 | + values: U & NgIterable<T>; |
| 72 | + values$ = this.observables$ |
| 73 | + .pipe( |
| 74 | + distinctUntilChanged(), |
| 75 | + switchAll() |
| 76 | + ); |
| 77 | + |
| 78 | + private _trackByFn: TrackByFunction<T>; |
| 79 | + |
| 80 | + _localVariableProjections; |
| 81 | + @Input() |
| 82 | + set poc5LocVLocalVariableProjections(o: CustomVariablesProjectors) { |
| 83 | + this._localVariableProjections = o; |
| 84 | + } |
| 85 | + @Input() |
| 86 | + set poc5LocVIterableTrackBy(fn: TrackByFunction<T>) { |
| 87 | + this._trackByFn = fn; |
| 88 | + } |
| 89 | + |
| 90 | + @Input() |
| 91 | + set poc5LocV(potentialObservable: ObservableInput<U & NgIterable<T>> | null | undefined) { |
| 92 | + this.observables$.next(potentialObservable); |
| 93 | + } |
| 94 | + |
| 95 | + constructor( |
| 96 | + private cdRef: ChangeDetectorRef, |
| 97 | + private readonly nextTemplateRef: TemplateRef<Poc5Locv5ViewContext<T, U>>, |
| 98 | + private readonly viewContainerRef: ViewContainerRef, |
| 99 | + private iterableDiffers: IterableDiffers |
| 100 | + ) { |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + ngOnInit() { |
| 105 | + this.subscription = this.values$.pipe( |
| 106 | + // the actual values arrive here |
| 107 | + tap(value => { |
| 108 | + // set helper variable for applyChanges method |
| 109 | + this.values = value; |
| 110 | + // set new differ if there is none yet |
| 111 | + if (!this.differ && value) { |
| 112 | + this.differ = this.iterableDiffers.find(value).create(this._trackByFn); |
| 113 | + } |
| 114 | + }), |
| 115 | + // if there is no differ, we don't need to apply changes |
| 116 | + filter(() => !!this.differ), |
| 117 | + // apply differ -> return changes |
| 118 | + map(value => this.differ.diff(value)), |
| 119 | + // filter out no changes |
| 120 | + filter(changes => !!changes) |
| 121 | + ) |
| 122 | + .subscribe( |
| 123 | + changes => { |
| 124 | + this.applyChanges(changes); |
| 125 | + } |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + ngOnDestroy() { |
| 130 | + this.subscription.unsubscribe(); |
| 131 | + } |
| 132 | + |
| 133 | + private applyChanges(changes: IterableChanges<T>) { |
| 134 | + // behavior like *ngFor |
| 135 | + const insertTuples: RecordViewTuple<T, U>[] = []; |
| 136 | + // TODO: dig into `IterableDiffer` |
| 137 | + changes.forEachOperation( |
| 138 | + ( |
| 139 | + changeRecord: IterableChangeRecord<T>, |
| 140 | + previousIndex: number | null, |
| 141 | + currentIndex: number | null |
| 142 | + ) => { |
| 143 | + if (changeRecord.previousIndex == null) { |
| 144 | + // this is basically the first run |
| 145 | + // create the embedded view for each value with default values |
| 146 | + const view = this.viewContainerRef.createEmbeddedView( |
| 147 | + this.nextTemplateRef, |
| 148 | + new Poc5Locv5ViewContext<T, U>(null, this.values, -1, -1), |
| 149 | + currentIndex === null ? undefined : currentIndex |
| 150 | + ); |
| 151 | + insertTuples.push({ |
| 152 | + view, |
| 153 | + record: changeRecord |
| 154 | + }); |
| 155 | + |
| 156 | + } else if (currentIndex == null) { |
| 157 | + |
| 158 | + this.viewContainerRef.remove( |
| 159 | + previousIndex === null ? undefined : previousIndex); |
| 160 | + |
| 161 | + } else if (previousIndex !== null) { |
| 162 | + |
| 163 | + const view = <EmbeddedViewRef<Poc5Locv5ViewContext<T, U>>>this.viewContainerRef.get(previousIndex); |
| 164 | + this.viewContainerRef.move(view, currentIndex); |
| 165 | + insertTuples.push({ |
| 166 | + view, |
| 167 | + record: changeRecord |
| 168 | + }); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + for (let i = 0; i < insertTuples.length; i++) { |
| 173 | + this._perViewChange(insertTuples[i].view, insertTuples[i].record); |
| 174 | + } |
| 175 | + |
| 176 | + for (let i = 0, ilen = this.viewContainerRef.length; i < ilen; i++) { |
| 177 | + const viewRef = <EmbeddedViewRef<Poc5Locv5ViewContext<T, U>>>this.viewContainerRef.get(i); |
| 178 | + viewRef.context.localVariableProjections = this._localVariableProjections; |
| 179 | + |
| 180 | + viewRef.context.index = i; |
| 181 | + viewRef.context.count = ilen; |
| 182 | + viewRef.context.pocLet = this.values; |
| 183 | + } |
| 184 | + |
| 185 | + changes.forEachIdentityChange((record: IterableChangeRecord<T>) => { |
| 186 | + const viewRef = |
| 187 | + <EmbeddedViewRef<Poc5Locv5ViewContext<T, U>>>this.viewContainerRef.get(record.currentIndex); |
| 188 | + viewRef.context.$implicit = record.item; |
| 189 | + viewRef.detectChanges(); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + private _perViewChange( |
| 194 | + view: EmbeddedViewRef<Poc5Locv5ViewContext<T, U>>, record: IterableChangeRecord<T>) { |
| 195 | + view.context.$implicit = record.item; |
| 196 | + view.detectChanges(); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments