fix: normalize dirty list to tree

This commit is contained in:
jsers 2020-05-15 18:24:00 +08:00
parent fe9c84f263
commit e5cef8ccc0
1 changed files with 29 additions and 11 deletions

View File

@ -1,6 +1,7 @@
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;
}