transferUtils.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { omit } from 'lodash';
  2. import { strings } from './constants';
  3. export function _generateGroupedData(dataSource: any[]): any[] {
  4. const newData: any[] = [];
  5. for (const group of dataSource) {
  6. group.children.forEach((item: any) => {
  7. const { children, ...rest } = group;
  8. newData.push({ ...item, _parent: rest });
  9. });
  10. }
  11. return newData;
  12. }
  13. // DFS
  14. export function _generateTreeData(dataSource: any[]): any[] {
  15. const newData = [];
  16. const stack = [...dataSource].reverse();
  17. while (stack.length) {
  18. const current = stack.pop();
  19. current.path = current.path || [omit(current, ['children'])];
  20. if (current.children && Array.isArray(current.children)) {
  21. const nodes = current.children;
  22. for (let i = nodes.length - 1; i >= 0; i--) {
  23. const child = { ...nodes[i] };
  24. child.path = [].concat(current.path).concat(omit(child, ['children']));
  25. stack.push(child);
  26. }
  27. } else {
  28. current.isLeaf = true;
  29. }
  30. newData.push(omit(current, ['children']));
  31. }
  32. return newData;
  33. }
  34. export function _generateDataByType(dataSource: any[], type: string): any[] {
  35. const newData = dataSource.slice() || [];
  36. if (type === strings.TYPE_GROUP_LIST) {
  37. return _generateGroupedData(newData);
  38. }
  39. if (type === strings.TYPE_TREE_TO_LIST) {
  40. return _generateTreeData(newData);
  41. }
  42. return newData;
  43. }
  44. export function _generateSelectedItems(value: any[], data: any[]): Map<any, any> {
  45. const selectedItems = new Map();
  46. value.forEach(val => {
  47. const index = data.findIndex(option => option.value === val);
  48. if (index !== -1) {
  49. const option = data[index];
  50. selectedItems.set(option.key, option);
  51. }
  52. });
  53. return selectedItems;
  54. }