|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ArrayDataSource, CollectionViewer, DataSource, Range} from '@angular/cdk/collections'; |
| 10 | +import { |
| 11 | + Directive, |
| 12 | + DoCheck, |
| 13 | + EmbeddedViewRef, |
| 14 | + Host, |
| 15 | + Input, |
| 16 | + IterableChangeRecord, |
| 17 | + IterableChanges, |
| 18 | + IterableDiffer, |
| 19 | + IterableDiffers, |
| 20 | + NgIterable, |
| 21 | + OnDestroy, |
| 22 | + TemplateRef, |
| 23 | + TrackByFunction, |
| 24 | + ViewContainerRef, |
| 25 | +} from '@angular/core'; |
| 26 | +import {Observable} from 'rxjs/Observable'; |
| 27 | +import {pairwise} from 'rxjs/operators/pairwise'; |
| 28 | +import {shareReplay} from 'rxjs/operators/shareReplay'; |
| 29 | +import {startWith} from 'rxjs/operators/startWith'; |
| 30 | +import {switchMap} from 'rxjs/operators/switchMap'; |
| 31 | +import {Subject} from 'rxjs/Subject'; |
| 32 | +import {CdkVirtualScrollViewport} from './virtual-scroll-viewport'; |
| 33 | + |
| 34 | + |
| 35 | +/** The context for an item rendered by `CdkForOf` */ |
| 36 | +export class CdkForOfContext<T> { |
| 37 | + constructor(public $implicit: T, public cdkForOf: NgIterable<T> | DataSource<T>, |
| 38 | + public index: number, public count: number) {} |
| 39 | + |
| 40 | + get first(): boolean { return this.index === 0; } |
| 41 | + |
| 42 | + get last(): boolean { return this.index === this.count - 1; } |
| 43 | + |
| 44 | + get even(): boolean { return this.index % 2 === 0; } |
| 45 | + |
| 46 | + get odd(): boolean { return !this.even; } |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +type RecordViewTuple<T> = { |
| 51 | + record: IterableChangeRecord<T> | null, |
| 52 | + view?: EmbeddedViewRef<CdkForOfContext<T>> |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | +/** |
| 57 | + * A directive similar to `ngForOf` to be used for rendering data inside a virtual scrolling |
| 58 | + * container. |
| 59 | + */ |
| 60 | +@Directive({ |
| 61 | + selector: '[cdkFor][cdkForOf]', |
| 62 | +}) |
| 63 | +export class CdkForOf<T> implements CollectionViewer, DoCheck, OnDestroy { |
| 64 | + /** Emits when the rendered view of the data changes. */ |
| 65 | + viewChange = new Subject<Range>(); |
| 66 | + |
| 67 | + /** Emits when the data source changes. */ |
| 68 | + private _dataSourceSubject = new Subject<DataSource<T>>(); |
| 69 | + |
| 70 | + /** The DataSource to display. */ |
| 71 | + @Input() |
| 72 | + get cdkForOf(): NgIterable<T> | DataSource<T> { return this._cdkForOf; } |
| 73 | + set cdkForOf(value: NgIterable<T> | DataSource<T>) { |
| 74 | + this._cdkForOf = value; |
| 75 | + let ds = value instanceof DataSource ? value : |
| 76 | + new ArrayDataSource<T>(Array.prototype.slice.call(value)); |
| 77 | + this._dataSourceSubject.next(ds); |
| 78 | + } |
| 79 | + _cdkForOf: NgIterable<T> | DataSource<T>; |
| 80 | + |
| 81 | + /** The trackBy function to use for tracking elements. */ |
| 82 | + @Input() |
| 83 | + get cdkForTrackBy(): TrackByFunction<T> { |
| 84 | + return this._cdkForOfTrackBy; |
| 85 | + } |
| 86 | + set cdkForTrackBy(fn: TrackByFunction<T>) { |
| 87 | + this._needsUpdate = true; |
| 88 | + this._cdkForOfTrackBy = |
| 89 | + (index, item) => fn(index + (this._renderedRange ? this._renderedRange.start : 0), item); |
| 90 | + } |
| 91 | + private _cdkForOfTrackBy: TrackByFunction<T>; |
| 92 | + |
| 93 | + /** The template used to stamp out new elements. */ |
| 94 | + @Input() |
| 95 | + set cdkForTemplate(value: TemplateRef<CdkForOfContext<T>>) { |
| 96 | + if (value) { |
| 97 | + this._needsUpdate = true; |
| 98 | + this._template = value; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** Emits whenever the data in the current DataSource changes. */ |
| 103 | + dataStream: Observable<T[]> = this._dataSourceSubject |
| 104 | + .pipe( |
| 105 | + startWith(null!), |
| 106 | + pairwise(), |
| 107 | + switchMap(([prev, cur]) => this._changeDataSource(prev, cur)), |
| 108 | + shareReplay(1)); |
| 109 | + |
| 110 | + private _differ: IterableDiffer<T> | null = null; |
| 111 | + |
| 112 | + private _data: T[]; |
| 113 | + |
| 114 | + private _renderedItems: T[]; |
| 115 | + |
| 116 | + private _renderedRange: Range; |
| 117 | + |
| 118 | + private _templateCache: EmbeddedViewRef<CdkForOfContext<T>>[] = []; |
| 119 | + |
| 120 | + private _needsUpdate = false; |
| 121 | + |
| 122 | + constructor( |
| 123 | + private _viewContainerRef: ViewContainerRef, |
| 124 | + private _template: TemplateRef<CdkForOfContext<T>>, |
| 125 | + private _differs: IterableDiffers, |
| 126 | + @Host() private _viewport: CdkVirtualScrollViewport) { |
| 127 | + this.dataStream.subscribe(data => this._data = data); |
| 128 | + this._viewport.renderedRangeStream.subscribe(range => this._onRenderedRangeChange(range)); |
| 129 | + this._viewport.connect(this); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Get the client rect for the given index. |
| 134 | + * @param index The index of the data element whose client rect we want to measure. |
| 135 | + * @return The combined client rect for all DOM elements rendered as part of the given index. |
| 136 | + * Or null if no DOM elements are rendered for the given index. |
| 137 | + * @throws If the given index is not in the rendered range. |
| 138 | + */ |
| 139 | + measureClientRect(index: number): ClientRect | null { |
| 140 | + if (index < this._renderedRange.start || index >= this._renderedRange.end) { |
| 141 | + throw Error('Error: attempted to measure an element that isn\'t rendered.'); |
| 142 | + } |
| 143 | + index -= this._renderedRange.start; |
| 144 | + let view = this._viewContainerRef.get(index) as EmbeddedViewRef<CdkForOfContext<T>> | null; |
| 145 | + if (view && view.rootNodes.length) { |
| 146 | + let minTop = Infinity; |
| 147 | + let minLeft = Infinity; |
| 148 | + let maxBottom = -Infinity; |
| 149 | + let maxRight = -Infinity; |
| 150 | + |
| 151 | + // There may be multiple root DOM elements for a single data element, so we merge their rects. |
| 152 | + for (let i = 0, ilen = view.rootNodes.length; i < ilen; i++) { |
| 153 | + let rect = (view.rootNodes[i] as Element).getBoundingClientRect(); |
| 154 | + minTop = Math.min(minTop, rect.top); |
| 155 | + minLeft = Math.min(minLeft, rect.left); |
| 156 | + maxBottom = Math.max(maxBottom, rect.bottom); |
| 157 | + maxRight = Math.max(maxRight, rect.right); |
| 158 | + } |
| 159 | + |
| 160 | + return { |
| 161 | + top: minTop, |
| 162 | + left: minLeft, |
| 163 | + bottom: maxBottom, |
| 164 | + right: maxRight, |
| 165 | + height: maxBottom - minTop, |
| 166 | + width: maxRight - minLeft |
| 167 | + }; |
| 168 | + } |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + ngDoCheck() { |
| 173 | + if (this._differ && this._needsUpdate) { |
| 174 | + const changes = this._differ.diff(this._renderedItems); |
| 175 | + this._applyChanges(changes); |
| 176 | + this._needsUpdate = false; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + ngOnDestroy() { |
| 181 | + this._viewport.disconnect(); |
| 182 | + |
| 183 | + this._dataSourceSubject.complete(); |
| 184 | + this.viewChange.complete(); |
| 185 | + |
| 186 | + for (let view of this._templateCache) { |
| 187 | + view.destroy(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /** React to scroll state changes in the viewport. */ |
| 192 | + private _onRenderedRangeChange(renderedRange: Range) { |
| 193 | + this._renderedRange = renderedRange; |
| 194 | + this.viewChange.next(this._renderedRange); |
| 195 | + this._renderedItems = this._data.slice(this._renderedRange.start, this._renderedRange.end); |
| 196 | + if (!this._differ) { |
| 197 | + this._differ = this._differs.find(this._renderedItems).create(this.cdkForTrackBy); |
| 198 | + } |
| 199 | + this._needsUpdate = true; |
| 200 | + } |
| 201 | + |
| 202 | + /** Swap out one `DataSource` for another. */ |
| 203 | + private _changeDataSource(oldDs: DataSource<T> | null, newDs: DataSource<T>): Observable<T[]> { |
| 204 | + if (oldDs) { |
| 205 | + oldDs.disconnect(this); |
| 206 | + } |
| 207 | + this._needsUpdate = true; |
| 208 | + return newDs.connect(this); |
| 209 | + } |
| 210 | + |
| 211 | + /** Apply changes to the DOM. */ |
| 212 | + private _applyChanges(changes: IterableChanges<T> | null) { |
| 213 | + // If there are no changes, just update the index and count on the view context and be done. |
| 214 | + if (!changes) { |
| 215 | + for (let i = 0, len = this._viewContainerRef.length; i < len; i++) { |
| 216 | + let view = this._viewContainerRef.get(i) as EmbeddedViewRef<CdkForOfContext<T>>; |
| 217 | + view.context.index = this._renderedRange.start + i; |
| 218 | + view.context.count = this._data.length; |
| 219 | + view.detectChanges(); |
| 220 | + } |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + // Detach all of the views and add them into an array to preserve their original order. |
| 225 | + const previousViews: EmbeddedViewRef<CdkForOfContext<T>>[] = []; |
| 226 | + for (let i = 0, len = this._viewContainerRef.length; i < len; i++) { |
| 227 | + previousViews.unshift( |
| 228 | + this._viewContainerRef.detach()! as EmbeddedViewRef<CdkForOfContext<T>>); |
| 229 | + } |
| 230 | + |
| 231 | + // Mark the removed indices so we can recycle their views. |
| 232 | + changes.forEachRemovedItem(record => { |
| 233 | + this._templateCache.push(previousViews[record.previousIndex!]); |
| 234 | + delete previousViews[record.previousIndex!]; |
| 235 | + }); |
| 236 | + |
| 237 | + // Queue up the newly added items to be inserted, recycling views from the cache if possible. |
| 238 | + const insertTuples: RecordViewTuple<T>[] = []; |
| 239 | + changes.forEachAddedItem(record => { |
| 240 | + insertTuples[record.currentIndex!] = {record, view: this._templateCache.pop()}; |
| 241 | + }); |
| 242 | + |
| 243 | + // Queue up moved items to be re-inserted. |
| 244 | + changes.forEachMovedItem(record => { |
| 245 | + insertTuples[record.currentIndex!] = {record, view: previousViews[record.previousIndex!]}; |
| 246 | + delete previousViews[record.previousIndex!]; |
| 247 | + }); |
| 248 | + |
| 249 | + // We have deleted all of the views that were removed or moved from previousViews. What is left |
| 250 | + // is the unchanged items that we queue up to be re-inserted. |
| 251 | + for (let i = 0, len = previousViews.length; i < len; i++) { |
| 252 | + if (previousViews[i]) { |
| 253 | + insertTuples[i] = {record: null, view: previousViews[i]}; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + // We now have a full list of everything to be inserted, so go ahead and insert them. |
| 258 | + for (let i = 0, len = insertTuples.length; i < len; i++) { |
| 259 | + let {view, record} = insertTuples[i]; |
| 260 | + if (view) { |
| 261 | + this._viewContainerRef.insert(view); |
| 262 | + } else { |
| 263 | + view = this._viewContainerRef.createEmbeddedView(this._template, |
| 264 | + new CdkForOfContext<T>(null!, this._cdkForOf, -1, -1)); |
| 265 | + } |
| 266 | + |
| 267 | + if (record) { |
| 268 | + view.context.$implicit = record.item as T; |
| 269 | + } |
| 270 | + view.context.index = this._renderedRange.start + i; |
| 271 | + view.context.count = this._data.length; |
| 272 | + view.detectChanges(); |
| 273 | + } |
| 274 | + } |
| 275 | +} |
0 commit comments