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 React from 'react';
import _ from 'lodash'; import _ from 'lodash';
import { Tree } from 'antd'; import { Tree } from 'antd';
import LTT from 'list-to-tree';
import { MenuConfItem, TreeNode } from '@interface'; import { MenuConfItem, TreeNode } from '@interface';
export function isAbsolutePath(url: string) { export function isAbsolutePath(url: string) {
@ -73,20 +74,37 @@ export function findNode(treeData: TreeNode[], node: TreeNode) {
export function normalizeTreeData(data: TreeNode[]) { export function normalizeTreeData(data: TreeNode[]) {
const treeData: TreeNode[] = []; const treeData: TreeNode[] = [];
_.each(data, (node) => { let tag = 0;
node = _.cloneDeep(node);
if (node.pid === 0) { function fn(_cache?: TreeNode[]) {
treeData.splice(_.sortedIndexBy(treeData, node, 'name'), 0, node); const cache: TreeNode[] = [];
} else { _.each(data, (node) => {
const findedNode = findNode(treeData, node); node = _.cloneDeep(node);
if (!findedNode) return; if (node.pid === 0) {
if (_.isArray(findedNode.children)) { if (tag === 0) {
findedNode.children.splice(_.sortedIndexBy(findedNode.children, node, 'name'), 0, node); treeData.splice(_.sortedIndexBy(treeData, node, 'name'), 0, node);
}
} else { } 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; return treeData;
} }