123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- export const extractPathList = (tree: any[]): any => {
- if (!Array.isArray(tree)) {
- console.warn("tree must be an array");
- return [];
- }
- if (!tree || tree.length === 0) return [];
- const expandedPaths: Array<number | string> = [];
- for (const node of tree) {
- const hasChildren = node.children && node.children.length > 0;
- if (hasChildren) {
- extractPathList(node.children);
- }
- expandedPaths.push(node.uniqueId);
- }
- return expandedPaths;
- };
- export const deleteChildren = (tree: any[], pathList = []): any => {
- if (!Array.isArray(tree)) {
- console.warn("menuTree must be an array");
- return [];
- }
- if (!tree || tree.length === 0) return [];
- for (const [key, node] of tree.entries()) {
- if (node.children && node.children.length === 1) delete node.children;
- node.id = key;
- node.parentId = pathList.length ? pathList[pathList.length - 1] : null;
- node.pathList = [...pathList, node.id];
- node.uniqueId =
- node.pathList.length > 1 ? node.pathList.join("-") : node.pathList[0];
- const hasChildren = node.children && node.children.length > 0;
- if (hasChildren) {
- deleteChildren(node.children, node.pathList);
- }
- }
- return tree;
- };
- export const buildHierarchyTree = (tree: any[], pathList = []): any => {
- if (!Array.isArray(tree)) {
- console.warn("tree must be an array");
- return [];
- }
- if (!tree || tree.length === 0) return [];
- for (const [key, node] of tree.entries()) {
- node.id = key;
- node.parentId = pathList.length ? pathList[pathList.length - 1] : null;
- node.pathList = [...pathList, node.id];
- const hasChildren = node.children && node.children.length > 0;
- if (hasChildren) {
- buildHierarchyTree(node.children, node.pathList);
- }
- }
- return tree;
- };
- export const getNodeByUniqueId = (
- tree: any[],
- uniqueId: number | string
- ): any => {
- if (!Array.isArray(tree)) {
- console.warn("menuTree must be an array");
- return [];
- }
- if (!tree || tree.length === 0) return [];
- const item = tree.find(node => node.uniqueId === uniqueId);
- if (item) return item;
- const childrenList = tree
- .filter(node => node.children)
- .map(i => i.children)
- .flat(1) as unknown;
- return getNodeByUniqueId(childrenList as any[], uniqueId);
- };
- export const appendFieldByUniqueId = (
- tree: any[],
- uniqueId: number | string,
- fields: object
- ): any => {
- if (!Array.isArray(tree)) {
- console.warn("menuTree must be an array");
- return [];
- }
- if (!tree || tree.length === 0) return [];
- for (const node of tree) {
- const hasChildren = node.children && node.children.length > 0;
- if (
- node.uniqueId === uniqueId &&
- Object.prototype.toString.call(fields) === "[object Object]"
- )
- Object.assign(node, fields);
- if (hasChildren) {
- appendFieldByUniqueId(node.children, uniqueId, fields);
- }
- }
- return tree;
- };
- export const handleTree = (
- data: any[],
- id?: string,
- parentId?: string,
- children?: string
- ): any => {
- if (!Array.isArray(data)) {
- console.warn("data must be an array");
- return [];
- }
- const config = {
- id: id || "id",
- parentId: parentId || "parentId",
- childrenList: children || "children"
- };
- const childrenListMap: any = {};
- const nodeIds: any = {};
- const tree = [];
- for (const d of data) {
- const parentId = d[config.parentId];
- if (childrenListMap[parentId] == null) {
- childrenListMap[parentId] = [];
- }
- nodeIds[d[config.id]] = d;
- childrenListMap[parentId].push(d);
- }
- for (const d of data) {
- const parentId = d[config.parentId];
- if (nodeIds[parentId] == null) {
- tree.push(d);
- }
- }
- for (const t of tree) {
- adaptToChildrenList(t);
- }
- function adaptToChildrenList(o: Record<string, any>) {
- if (childrenListMap[o[config.id]] !== null) {
- o[config.childrenList] = childrenListMap[o[config.id]];
- }
- if (o[config.childrenList]) {
- for (const c of o[config.childrenList]) {
- adaptToChildrenList(c);
- }
- }
- }
- return tree;
- };
|