util.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {
  2. isNull,
  3. isUndefined,
  4. isEqual
  5. } from 'lodash';
  6. import { strings, VALUE_SPLIT } from './constants';
  7. function getPosition(level: any, index: any) {
  8. return `${level}-${index}`;
  9. }
  10. export function isValid(val: any) {
  11. return !isNull(val) && !isUndefined(val);
  12. }
  13. export function normalizedArr(val: any) {
  14. if (!Array.isArray(val)) {
  15. return [val];
  16. } else {
  17. return val;
  18. }
  19. }
  20. /**
  21. * @returns whether option includes sugInput.
  22. * When filterTreeNode is a function,returns the result of filterTreeNode which called with (sugInput, target, option).
  23. */
  24. export function filter(sugInput: string, option: any, filterTreeNode: any, filteredPath?: string[]) {
  25. if (!filterTreeNode) {
  26. return true;
  27. }
  28. let filterFn = filterTreeNode;
  29. let target: string;
  30. if (typeof filterTreeNode === 'boolean') {
  31. filterFn = (targetVal: string, val: string) => {
  32. const input = targetVal.toLowerCase();
  33. return val
  34. .toLowerCase()
  35. .includes(input);
  36. };
  37. // 当 filterTreeNode 是 bool 类型时,由 Cascader 内部判断是否符合筛选条件,使用 join('') 修复搜索英文逗号导致所有数据被匹配问题
  38. // When the type of of filterTreeNode is bool, Cascader internally determines whether it meets the filtering conditions.
  39. // Use join('') to fix the problem that searching for English commas causes all data to be matched.
  40. target = filteredPath.join('');
  41. } else {
  42. // 当 filterTreeNode 为函数类型时,由用户判断是否符合筛选条件,使用 join(), 和原来保持一致
  43. // When the type of of filterTreeNode is function, the user determines whether it meets the filtering conditions,
  44. // uses join() to be consistent with the previous version.
  45. target = filteredPath.join();
  46. }
  47. return filterFn(sugInput, target, option);
  48. }
  49. /**
  50. * Traverse all the data by `treeData`.
  51. */
  52. function traverseDataNodes(treeNodes: any, callback: any) {
  53. const processNode = (node: any, ind?: any, parent?: any) => {
  54. const children = node ? node.children : treeNodes;
  55. let item: any = null;
  56. // Process node if is not root
  57. if (node) {
  58. const key = parent ? `${parent.key}${VALUE_SPLIT}${node.value}` : node.value;
  59. const pos = parent ? getPosition(parent.pos, ind) : `${ind}`;
  60. item = {
  61. data: { ...node },
  62. ind,
  63. key,
  64. pos,
  65. level: parent ? parent.level + 1 : 0,
  66. parentKey: parent ? parent.key : null,
  67. path: parent ? [...parent.path, key] : [key],
  68. valuePath: parent ? [...parent.valuePath, node.value] : [node.value]
  69. };
  70. callback(item);
  71. }
  72. // Process children node
  73. if (children) {
  74. children.forEach((subNode: any, subIndex: any) => {
  75. processNode(subNode, subIndex, item);
  76. });
  77. }
  78. };
  79. processNode(null);
  80. }
  81. export function getKeysByValuePath(valuePath: (string | number)[][] | (string | number)[]) {
  82. if (valuePath?.length) {
  83. if (Array.isArray(valuePath[0])) {
  84. return valuePath.map((item) => getKeyByValuePath(item));
  85. } else {
  86. return [getKeyByValuePath(valuePath as (string | number)[])];
  87. }
  88. }
  89. return [];
  90. }
  91. export function getKeyByValuePath(valuePath: (string | number)[]) {
  92. return valuePath.join(VALUE_SPLIT);
  93. }
  94. export function getValuePathByKey(key: string) {
  95. return key.split(VALUE_SPLIT);
  96. }
  97. export function getKeyByPos(pos: string, treeData: any) {
  98. const posArr = pos.split('-').map(item => Number(item));
  99. let resultData = treeData;
  100. let valuePath = [];
  101. posArr.forEach((item, index) => {
  102. resultData = index === 0 ? resultData[item] : resultData?.children?.[item];
  103. valuePath.push(resultData?.value);
  104. });
  105. return getKeyByValuePath(valuePath);
  106. }
  107. export function convertDataToEntities(dataNodes: any) {
  108. const keyEntities: any = {};
  109. traverseDataNodes(dataNodes, (data: any) => {
  110. const { key, parentKey } = data;
  111. const entity = { ...data };
  112. keyEntities[key] = entity;
  113. // Fill children
  114. entity.parent = keyEntities[parentKey];
  115. if (entity.parent) {
  116. entity.parent.children = entity.parent.children || [];
  117. entity.parent.children.push(entity);
  118. }
  119. });
  120. return keyEntities;
  121. }
  122. export function calcMergeType(autoMergeValue: boolean, leafOnly: boolean): string {
  123. let mergeType: string;
  124. if (leafOnly) {
  125. mergeType = strings.LEAF_ONLY_MERGE_TYPE;
  126. } else if (autoMergeValue) {
  127. mergeType = strings.AUTO_MERGE_VALUE_MERGE_TYPE;
  128. } else {
  129. mergeType = strings.NONE_MERGE_TYPE;
  130. }
  131. return mergeType;
  132. }