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.

85 lines 2.93 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(sourceNode, targetNode, position) { // 找到并移除 Source const sourceInfo = this.findNodeWithParent(this.root, sourceNode[this.key]); if (!sourceInfo) throw new Error('Source node not found'); const { parent: sourceParent, index: sourceIndex, node: sourceFound } = sourceInfo; if (sourceParent) { const childrenArr = sourceParent[this.childrenKey]; childrenArr.splice(sourceIndex, 1); if (childrenArr.length === 0) { delete sourceParent[this.childrenKey]; } } else { this.root.splice(sourceIndex, 1); } // 找到 Target const targetInfo = this.findNodeWithParent(this.root, targetNode[this.key]); if (!targetInfo) throw new Error('Target node not found'); const { parent: targetParent, index: targetIndex } = targetInfo; // 插入 if (position === 'before') { if (targetParent) { targetParent[this.childrenKey].splice(targetIndex, 0, sourceFound); } else { this.root.splice(targetIndex, 0, sourceFound); } } else if (position === 'after') { if (targetParent) { targetParent[this.childrenKey].splice(targetIndex + 1, 0, sourceFound); } else { this.root.splice(targetIndex + 1, 0, sourceFound); } } } /** 在树中找到节点及其父节点 */ findNodeWithParent(tree, nodeId, parent = null) { for (let i = 0; i < tree.length; i++) { const node = tree[i]; if (node[this.key] === nodeId) { return { parent, index: i, node }; } if (node[this.childrenKey]) { const found = this.findNodeWithParent(node[this.childrenKey], nodeId, node); if (found) return found; } } return null; } } //# sourceMappingURL=TreeUtil.js.map