UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

101 lines (96 loc) 3.65 kB
import { isObservable, of } from 'rxjs'; import { DataSource } from './_data-source-chunk.mjs'; import { InjectionToken } from '@angular/core'; class ArrayDataSource extends DataSource { _data; constructor(_data) { super(); this._data = _data; } connect() { return isObservable(this._data) ? this._data : of(this._data); } disconnect() {} } var _ViewRepeaterOperation; (function (_ViewRepeaterOperation) { _ViewRepeaterOperation[_ViewRepeaterOperation["REPLACED"] = 0] = "REPLACED"; _ViewRepeaterOperation[_ViewRepeaterOperation["INSERTED"] = 1] = "INSERTED"; _ViewRepeaterOperation[_ViewRepeaterOperation["MOVED"] = 2] = "MOVED"; _ViewRepeaterOperation[_ViewRepeaterOperation["REMOVED"] = 3] = "REMOVED"; })(_ViewRepeaterOperation || (_ViewRepeaterOperation = {})); const _VIEW_REPEATER_STRATEGY = new InjectionToken('_ViewRepeater'); class _RecycleViewRepeaterStrategy { viewCacheSize = 20; _viewCache = []; applyChanges(changes, viewContainerRef, itemContextFactory, itemValueResolver, itemViewChanged) { changes.forEachOperation((record, adjustedPreviousIndex, currentIndex) => { let view; let operation; if (record.previousIndex == null) { const viewArgsFactory = () => itemContextFactory(record, adjustedPreviousIndex, currentIndex); view = this._insertView(viewArgsFactory, currentIndex, viewContainerRef, itemValueResolver(record)); operation = view ? _ViewRepeaterOperation.INSERTED : _ViewRepeaterOperation.REPLACED; } else if (currentIndex == null) { this._detachAndCacheView(adjustedPreviousIndex, viewContainerRef); operation = _ViewRepeaterOperation.REMOVED; } else { view = this._moveView(adjustedPreviousIndex, currentIndex, viewContainerRef, itemValueResolver(record)); operation = _ViewRepeaterOperation.MOVED; } if (itemViewChanged) { itemViewChanged({ context: view?.context, operation, record }); } }); } detach() { for (const view of this._viewCache) { view.destroy(); } this._viewCache = []; } _insertView(viewArgsFactory, currentIndex, viewContainerRef, value) { const cachedView = this._insertViewFromCache(currentIndex, viewContainerRef); if (cachedView) { cachedView.context.$implicit = value; return undefined; } const viewArgs = viewArgsFactory(); return viewContainerRef.createEmbeddedView(viewArgs.templateRef, viewArgs.context, viewArgs.index); } _detachAndCacheView(index, viewContainerRef) { const detachedView = viewContainerRef.detach(index); this._maybeCacheView(detachedView, viewContainerRef); } _moveView(adjustedPreviousIndex, currentIndex, viewContainerRef, value) { const view = viewContainerRef.get(adjustedPreviousIndex); viewContainerRef.move(view, currentIndex); view.context.$implicit = value; return view; } _maybeCacheView(view, viewContainerRef) { if (this._viewCache.length < this.viewCacheSize) { this._viewCache.push(view); } else { const index = viewContainerRef.indexOf(view); if (index === -1) { view.destroy(); } else { viewContainerRef.remove(index); } } } _insertViewFromCache(index, viewContainerRef) { const cachedView = this._viewCache.pop(); if (cachedView) { viewContainerRef.insert(cachedView, index); } return cachedView || null; } } export { ArrayDataSource, _RecycleViewRepeaterStrategy, _VIEW_REPEATER_STRATEGY, _ViewRepeaterOperation }; //# sourceMappingURL=_recycle-view-repeater-strategy-chunk.mjs.map