index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @author 肖阳
  3. * @time 2020-9-10
  4. * @dec 公共方法
  5. */
  6. // https://github.com/reduxjs/redux/blob/master/src/compose.js
  7. export function compose(...funcs) {
  8. if (funcs.length === 0) {
  9. return arg => arg;
  10. }
  11. if (funcs.length === 1) {
  12. return funcs[0];
  13. }
  14. return funcs.reduce((a, b) => (...args) => a(b(...args)));
  15. }
  16. /**
  17. * 转化为el-tree树形结构数据
  18. */
  19. export function transToTreeDat(arr) {
  20. let list = arr;
  21. let colNodes = list.filter(i => !i.isRow);
  22. let colNodesGroup = getColNode(colNodes);
  23. let allNodes = list.concat(colNodesGroup);
  24. let tree = transTree(allNodes);
  25. getPidArr(arr);
  26. return tree;
  27. }
  28. export function getColNode(colNodeArrs) {
  29. let colNodes = colNodeArrs;
  30. let map = {};
  31. colNodes.forEach(i => {
  32. if (!map[i.groupId]) {
  33. map[i.groupId] = [];
  34. }
  35. map[i.groupId].push(i);
  36. });
  37. let colNodesArr = [];
  38. for (const groupId in map) {
  39. let obj = {
  40. id: groupId,
  41. groupId: map[groupId][0].groupPid,
  42. type: "route",
  43. isRow: true,
  44. isFlowTo: map[groupId][0].type === "6",
  45. conditionNodes: map[groupId]
  46. };
  47. colNodesArr.push(obj);
  48. }
  49. return colNodesArr;
  50. }
  51. /**
  52. *
  53. * @param {allNodes} arr 所有的整行元素
  54. * @param {*} list 所有的节点元素
  55. */
  56. export function getPidArr(list) {
  57. let colNodes = list.filter(i => !i.isRow);
  58. let rowNodes = list.filter(i => i.isRow);
  59. let colNodesGroup = getColNode(colNodes, list);
  60. let arr = colNodesGroup.concat(rowNodes);
  61. let map = {}; //所有整行元素的字典对象
  62. for (let item of arr) {
  63. map[item.id] = item;
  64. }
  65. //获取节点所在行
  66. for (let lis of list) {
  67. lis.pids = [];
  68. if (!lis.isRow) {
  69. let p = map[lis.groupPid];
  70. if (lis.groupPid === "root") {
  71. lis.pids.push(p.id);
  72. continue;
  73. }
  74. //当上一层为rowNode
  75. getColPid(p, lis);
  76. } else {
  77. let p = map[lis.groupId];
  78. getRowPid(p, lis);
  79. }
  80. }
  81. }
  82. //获取row的父节点id
  83. function getRowPid(p, lis) {
  84. if (!p) {
  85. lis.pids.push(lis.groupId);
  86. } else {
  87. if (p.conditionNodes) {
  88. p.conditionNodes.forEach(i => {
  89. loopGetPid(i, lis);
  90. });
  91. } else {
  92. lis.pids.push(p.id);
  93. }
  94. }
  95. }
  96. /**
  97. * 获取col节点的父节点
  98. */
  99. function getColPid(p, lis) {
  100. if (!p) {
  101. //当上一层为条件框元素
  102. lis.pids.push(lis.groupPid);
  103. } else {
  104. //当上一层为整行元素
  105. loopGetPidCol(p, lis);
  106. }
  107. }
  108. /**
  109. *
  110. * @param {*} parentRow
  111. * @param {*} lis
  112. * 单独处理一下col节点
  113. */
  114. export function loopGetPidCol(parentRow, lis) {
  115. if (parentRow.conditionNodes) {
  116. parentRow.conditionNodes.forEach(i => {
  117. loopGetPid(i, lis);
  118. });
  119. } else {
  120. lis.pids.push(parentRow.id);
  121. }
  122. }
  123. /**
  124. * 轮询节点获取pid
  125. * @param {*} node
  126. * @param {*} lis
  127. */
  128. export function loopGetPid(node, lis) {
  129. if (node.childNode) {
  130. loopGetPid(node.childNode, lis);
  131. } else if (node.conditionNodes) {
  132. node.conditionNodes.forEach(i => {
  133. loopGetPid(i, lis);
  134. });
  135. } else {
  136. lis.pids.push(node.id);
  137. }
  138. }
  139. /**
  140. * 转化为el-tree树形结构数据
  141. */
  142. export function transTree(arr) {
  143. let list = arr;
  144. if (!list || !list.length) return [];
  145. let map = {};
  146. for (let item of list) {
  147. map[item.id] = item;
  148. }
  149. let nodes = [];
  150. for (let lis of list) {
  151. if (!lis.isRow) {
  152. continue;
  153. }
  154. let p = map[lis.groupId];
  155. if (!p) {
  156. nodes.push(lis);
  157. continue;
  158. }
  159. p.isParent = true;
  160. p.childNode || (p.childNode = {});
  161. p.childNode = lis;
  162. if (
  163. p.childNode.conditionNodes &&
  164. p.childNode.conditionNodes[0].type === "6"
  165. ) {
  166. p.isFlowTo = true;
  167. }
  168. }
  169. return nodes;
  170. }
  171. /**
  172. * Hash 哈希值
  173. */
  174. export function HashCode(hashLength) {
  175. // 默认长度 24
  176. return (
  177. "a" +
  178. Array.from(Array(Number(hashLength) || 15), () =>
  179. Math.floor(Math.random() * 36).toString(36)
  180. ).join("")
  181. );
  182. }
  183. /**
  184. * 树结构转化为扁平化结构
  185. */
  186. export function deepTraversal(tree) {
  187. let list = [];
  188. tree.forEach(item => {
  189. const loop = data => {
  190. list.push(data);
  191. let children = data.children;
  192. children &&
  193. children.length &&
  194. children.forEach(child => {
  195. loop(child);
  196. });
  197. };
  198. loop(item);
  199. });
  200. return list;
  201. }