import { TreeNode } from '@farris/ui-treetable'; import { LookupGridComponent } from '../lookup-grid.component'; import { TreeInfo } from '../lookup-grid-options'; export class TreeNodeHelper { treeInfo: TreeInfo; flatAllNodes = []; constructor(private instance: LookupGridComponent) { } getTreeInfo(treeNode: TreeNode) { if (treeNode.data[this.treeInfo.dataField]) { return treeNode.data[this.treeInfo.dataField]; } const data = treeNode.data; if (data && this.treeInfo.dataField) { const treeInfoDataField = this.treeInfo.dataField.toLowerCase(); const dataField = Object.keys(treeNode.data).find(n => { return n.toLowerCase() === treeInfoDataField; }); if (dataField) { return data[dataField]; } else { console.error(`未找到树形信息数据字段【${this.treeInfo.dataField}】`); } } else { console.error(`未找到树形信息数据字段【${this.treeInfo.dataField}】`); } } getTreeNodeLayer(treeNode: TreeNode) { return this.getTreeInfo(treeNode)[this.treeInfo.layerField]; } /** 更新节点的展开状态。 根据组件中 expandLevel 的值决定 * -1:不展开,0:全部展开,>0 展开到指定级数 */ updateTreeNodeExpanded(treeNodes: TreeNode[], treeInfo: TreeInfo = null) { if (treeInfo) { this.treeInfo = treeInfo; } else { this.treeInfo = this.instance.treeInfo; } const expandLevel = this.instance.expandLevel; if (expandLevel === -1) { return; } if (!this.flatAllNodes.length) { this.flatAllNodes = this.treeData2Flat(null, treeNodes, 0, []); } treeNodes.forEach((tn: TreeNode) => { tn.expanded = this.shoudExpand(expandLevel, this.getTreeNodeLayer(tn)); if (this.isSelectNodeParent(tn)) { tn.expanded = true; } if (tn.children && tn.children.length) { this.updateTreeNodeExpanded(tn.children, treeInfo); } else { tn.leaf = true; } }); } private treeData2Flat(parent, nodes, level, parentIds) { const idField = this.instance.idField; let arr = []; if (nodes && nodes.length) { nodes.forEach((node, index) => { // node.parent = parent; let parents = []; if (parent) { const parentID = parent.data[idField]; const _parents = parentIds || []; parents = parents.concat(_parents.map(n => n)); parents.push(parentID); } const rowNode = { id: node.data[idField], node, level, parents, }; arr.push(rowNode); arr = arr.concat(this.treeData2Flat(node, node.children, level + 1, parents)); }); } return arr; } private shoudExpand(expandLevel: number, nodeLayer: number) { if (expandLevel === -1) { // -1 为不展开 return false; } else if (expandLevel === 0) { // 0 为全部展开 return true; } else { // 没有启用分层加载,通过展开层级确定是否展开该节点 return nodeLayer <= expandLevel; } } private isSelectNodeParent(treeNode) { if (this.instance.navSelectedIds) { const allParentIds: string[] = this.flatAllNodes.find(f => f.id === this.instance.navSelectedIds).parents; if (allParentIds && allParentIds.length) { return allParentIds.includes(treeNode.id); } return false; } return false; } getLeafNode(treeNode: TreeNode) { if (treeNode && (!treeNode.children || !treeNode.children.length)) { return treeNode; } else { if (treeNode.children.length === 1) { return this.getLeafNode(treeNode.children[0]); } else { return treeNode.children; } } } flatTreeNodes(items, result = []) { items = items || []; return items.reduce((c, n) => { c.push(n); if (n.children && n.children.length) { this.flatTreeNodes(n.children, c); } return c; }, result); } }