提交 e5cef8cc 编写于 作者: J jsers

fix: normalize dirty list to tree

上级 fe9c84f2
import React from 'react';
import _ from 'lodash';
import { Tree } from 'antd';
import LTT from 'list-to-tree';
import { MenuConfItem, TreeNode } from '@interface';
export function isAbsolutePath(url: string) {
......@@ -73,20 +74,37 @@ export function findNode(treeData: TreeNode[], node: TreeNode) {
export function normalizeTreeData(data: TreeNode[]) {
const treeData: TreeNode[] = [];
_.each(data, (node) => {
node = _.cloneDeep(node);
if (node.pid === 0) {
treeData.splice(_.sortedIndexBy(treeData, node, 'name'), 0, node);
} else {
const findedNode = findNode(treeData, node);
if (!findedNode) return;
if (_.isArray(findedNode.children)) {
findedNode.children.splice(_.sortedIndexBy(findedNode.children, node, 'name'), 0, node);
let tag = 0;
function fn(_cache?: TreeNode[]) {
const cache: TreeNode[] = [];
_.each(data, (node) => {
node = _.cloneDeep(node);
if (node.pid === 0) {
if (tag === 0) {
treeData.splice(_.sortedIndexBy(treeData, node, 'name'), 0, node);
}
} else {
findedNode.children = [node];
const findedNode = findNode(treeData, node); // find parent node
if (!findedNode) {
cache.push(node);
return;
};
if (_.isArray(findedNode.children)) {
if (!_.find(findedNode.children, { id: node.id })) {
findedNode.children.splice(_.sortedIndexBy(findedNode.children, node, 'name'), 0, node);
}
} else {
findedNode.children = [node];
}
}
});
tag += 1;
if (cache.length && !_.isEqual(_cache, cache)) {
fn(cache);
}
});
}
fn();
return treeData;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册