mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.4 KiB
98 lines
3.4 KiB
import { isObservable, of } from 'rxjs';
|
|
import { DataSource } from './_data-source-chunk.mjs';
|
|
|
|
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 = {}));
|
|
|
|
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, _ViewRepeaterOperation };
|
|
//# sourceMappingURL=_recycle-view-repeater-strategy-chunk.mjs.map
|
|
|