You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.8 KiB

  1. /**
  2. * uniqueId
  3. * @param {Array} {menuTree }
  4. * @param {return}} expandedPaths uniqueId组成的数组
  5. */
  6. const expandedPaths = [];
  7. export function extractPathList(menuTree) {
  8. if (!Array.isArray(menuTree)) {
  9. console.warn("menuTree must be an array");
  10. return;
  11. }
  12. if (!menuTree || menuTree.length === 0) return;
  13. for (const node of menuTree) {
  14. const hasChildren = node.children && node.children.length > 0;
  15. if (hasChildren) {
  16. extractPathList(node.children);
  17. }
  18. expandedPaths.push(node.uniqueId);
  19. }
  20. return expandedPaths;
  21. }
  22. /**
  23. * children的length为1children并自动组建唯一uniqueId
  24. * @param {Array} {menuTree }
  25. * @param {Array} {pathList id组成的数组}
  26. * @param {return}}
  27. */
  28. export function deleteChildren(menuTree, pathList = []) {
  29. if (!Array.isArray(menuTree)) {
  30. console.warn("menuTree must be an array");
  31. return;
  32. }
  33. if (!menuTree || menuTree.length === 0) return;
  34. for (const [key, node] of menuTree.entries()) {
  35. if (node.children && node.children.length === 1) delete node.children;
  36. node.id = key;
  37. node.parentId = pathList.length ? pathList[pathList.length - 1] : null;
  38. node.pathList = [...pathList, node.id];
  39. node.uniqueId =
  40. node.pathList.length > 1 ? node.pathList.join("-") : node.pathList[0];
  41. const hasChildren = node.children && node.children.length > 0;
  42. if (hasChildren) {
  43. deleteChildren(node.children, node.pathList);
  44. }
  45. }
  46. return menuTree;
  47. }
  48. // 创建层级关系
  49. export function buildHierarchyTree(menuTree, pathList = []) {
  50. if (!Array.isArray(menuTree)) {
  51. console.warn("menuTree must be an array");
  52. return;
  53. }
  54. if (!menuTree || menuTree.length === 0) return;
  55. for (const [key, node] of menuTree.entries()) {
  56. node.id = key;
  57. node.parentId = pathList.length ? pathList[pathList.length - 1] : null;
  58. node.pathList = [...pathList, node.id];
  59. const hasChildren = node.children && node.children.length > 0;
  60. if (hasChildren) {
  61. buildHierarchyTree(node.children, node.pathList);
  62. }
  63. }
  64. return menuTree;
  65. }
  66. /**
  67. * 广
  68. * @param {Array} tree
  69. * @param {Number|String} uniqueId uniqueId
  70. * @return {Object} node
  71. */
  72. export function getNodeByUniqueId(menuTree, uniqueId) {
  73. if (!Array.isArray(menuTree)) {
  74. console.warn("menuTree must be an array");
  75. return;
  76. }
  77. if (!menuTree || menuTree.length === 0) return;
  78. const item = menuTree.find(node => node.uniqueId === uniqueId);
  79. if (item) return item;
  80. const childrenList = menuTree
  81. .filter(node => node.children)
  82. .map(i => i.children)
  83. .flat(1);
  84. return getNodeByUniqueId(childrenList, uniqueId);
  85. }
  86. /**
  87. * uniqueId节点追加字段
  88. * @param {Array} {menuTree }
  89. * @param {Number|String} uniqueId uniqueId
  90. * @param {Object} fields uniqueId
  91. * @return {menuTree}
  92. */
  93. export function appendFieldByUniqueId(
  94. menuTree: Array<any>,
  95. uniqueId: Number | String,
  96. fields: Object
  97. ) {
  98. if (!Array.isArray(menuTree)) {
  99. console.warn("menuTree must be an array");
  100. return;
  101. }
  102. if (!menuTree || menuTree.length === 0) return {};
  103. for (const node of menuTree) {
  104. const hasChildren = node.children && node.children.length > 0;
  105. if (
  106. node.uniqueId === uniqueId &&
  107. Object.prototype.toString.call(fields) === "[object Object]"
  108. )
  109. Object.assign(node, fields);
  110. if (hasChildren) {
  111. appendFieldByUniqueId(node.children, uniqueId, fields);
  112. }
  113. }
  114. return menuTree;
  115. }
  116. /**
  117. *
  118. * @param {*} data
  119. * @param {*} id id字段 'id'
  120. * @param {*} parentId 'parentId'
  121. * @param {*} children 'children'
  122. */
  123. export function handleTree(
  124. data,
  125. id?: string,
  126. parentId?: string,
  127. children?: string
  128. ) {
  129. const config = {
  130. id: id || "id",
  131. parentId: parentId || "parentId",
  132. childrenList: children || "children"
  133. };
  134. const childrenListMap = {};
  135. const nodeIds = {};
  136. const tree = [];
  137. for (const d of data) {
  138. const parentId = d[config.parentId];
  139. if (childrenListMap[parentId] == null) {
  140. childrenListMap[parentId] = [];
  141. }
  142. nodeIds[d[config.id]] = d;
  143. childrenListMap[parentId].push(d);
  144. }
  145. for (const d of data) {
  146. const parentId = d[config.parentId];
  147. if (nodeIds[parentId] == null) {
  148. tree.push(d);
  149. }
  150. }
  151. for (const t of tree) {
  152. adaptToChildrenList(t);
  153. }
  154. function adaptToChildrenList(o) {
  155. if (childrenListMap[o[config.id]] !== null) {
  156. o[config.childrenList] = childrenListMap[o[config.id]];
  157. }
  158. if (o[config.childrenList]) {
  159. for (const c of o[config.childrenList]) {
  160. adaptToChildrenList(c);
  161. }
  162. }
  163. }
  164. return tree;
  165. }