From e5cef8ccc001ba0e5987816bfc7015a18a6cf310 Mon Sep 17 00:00:00 2001 From: jsers Date: Fri, 15 May 2020 18:24:00 +0800 Subject: [PATCH] fix: normalize dirty list to tree --- web/src/components/Layout/utils.tsx | 40 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/web/src/components/Layout/utils.tsx b/web/src/components/Layout/utils.tsx index 7883b515..a197c1b9 100644 --- a/web/src/components/Layout/utils.tsx +++ b/web/src/components/Layout/utils.tsx @@ -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; }