UNPKG

e-virt-table

Version:

A powerful data table based on canvas. You can use it as data grid、Microsoft Excel or Google sheets. It supports virtual scroll、cell edit etc.

94 lines 3.27 kB
export class TreeUtil { constructor(initialData, config) { Object.defineProperty(this, "root", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "key", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "childrenKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.root = initialData; this.key = config?.key || 'key'; this.childrenKey = config?.childrenKey || 'children'; } getTree() { return this.root; } treeMove(sourceKey, targetKey, position) { if (position === 'none') { return; } if (sourceKey === targetKey) { return; } const sourceInfo = this.findNodeWithParent(this.root, sourceKey); if (!sourceInfo) return; const targetInfo = this.findNodeWithParent(this.root, targetKey); if (!targetInfo) return; const { parent: sourceParent, index: sourceIndex, node: sourceNode } = sourceInfo; const { parent: targetParent, index: targetIndex, node: targetNode } = targetInfo; if (this.isDescendant(sourceNode, targetKey)) { return; } const sourceArray = sourceParent ? sourceParent[this.childrenKey] : this.root; sourceArray.splice(sourceIndex, 1); if (sourceParent && sourceArray.length === 0) { delete sourceParent[this.childrenKey]; } if (position === 'inside') { if (!Array.isArray(targetNode[this.childrenKey])) { targetNode[this.childrenKey] = []; } targetNode[this.childrenKey].push(sourceNode); return; } const targetArray = targetParent ? targetParent[this.childrenKey] : this.root; let insertIndex = targetIndex; if (sourceParent === targetParent && sourceIndex < targetIndex) { insertIndex--; } if (position === 'after') insertIndex++; targetArray.splice(insertIndex, 0, sourceNode); } findNodeWithParent(tree, nodeKey, parent = null) { for (let i = 0; i < tree.length; i++) { const node = tree[i]; if (node[this.key] === nodeKey) return { parent, index: i, node }; const children = node[this.childrenKey]; if (Array.isArray(children)) { const found = this.findNodeWithParent(children, nodeKey, node); if (found) return found; } } return null; } isDescendant(node, targetKey) { const children = node[this.childrenKey]; if (!Array.isArray(children)) return false; for (const child of children) { if (child[this.key] === targetKey) return true; if (this.isDescendant(child, targetKey)) return true; } return false; } } //# sourceMappingURL=TreeUtil.js.map