@progress/kendo-angular-treeview
Version:
Kendo UI TreeView for Angular
90 lines (89 loc) • 3.13 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Injectable } from '@angular/core';
import { nodeIndex } from './utils';
import * as i0 from "@angular/core";
const INDEX_REGEX = /\d+$/;
/**
* @hidden
*/
export class TreeViewLookupService {
map = new Map();
reset() {
this.map.clear();
}
registerItem(item, parent) {
const currentLookup = {
children: [],
item,
parent: this.item(nodeIndex(parent))
};
this.map.set(item.index, currentLookup);
}
registerChildren(index, children) {
const item = this.item(index);
if (!item) {
return;
}
item.children = children;
}
unregisterItem(index, dataItem) {
const current = this.item(index);
if (current && current.item.dataItem === dataItem) {
this.map.delete(index);
if (current.parent && current.parent.children) {
current.parent.children = current.parent.children.filter(item => item.dataItem !== dataItem);
}
}
}
replaceItem(index, item, parent) {
if (!item) {
return;
}
this.unregisterItem(index, item.dataItem);
this.registerItem(item, parent);
this.addToParent(item, parent);
}
itemLookup(index) {
const item = this.item(index);
if (!item) {
return null;
}
return {
children: this.mapChildren(item.children),
item: item.item,
parent: item.parent
};
}
hasItem(index) {
return this.map.has(index);
}
item(index) {
return this.map.get(index) || null;
}
addToParent(item, parent) {
if (parent) {
const parentItem = this.item(parent.index);
const index = parseInt(INDEX_REGEX.exec(item.index)[0], 10);
parentItem.children = parentItem.children || [];
parentItem.children.splice(index, 0, item);
}
}
mapChildren(children = []) {
return children.map(c => {
const { item, parent, children } = this.item(c.index);
return {
children: this.mapChildren(children),
item,
parent
};
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TreeViewLookupService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TreeViewLookupService });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TreeViewLookupService, decorators: [{
type: Injectable
}] });