Skip to content

Commit

Permalink
fix: normalize dirty list to tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jsers committed May 15, 2020
1 parent fe9c84f commit e5cef8c
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions web/src/components/Layout/utils.tsx
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e5cef8c

Please sign in to comment.