dictUtil.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var Record;
  2. (function (Record) {
  3. Record[Record["None"] = 0] = "None";
  4. Record[Record["Start"] = 1] = "Start";
  5. Record[Record["End"] = 2] = "End";
  6. })(Record || (Record = {}));
  7. function traverseNodesKey(treeData, fieldNames, callback) {
  8. function processNode(dataNode) {
  9. const key = dataNode[fieldNames.key];
  10. const children = dataNode[fieldNames.children];
  11. if (callback(key, dataNode) !== false) {
  12. traverseNodesKey(children || [], fieldNames, callback);
  13. }
  14. }
  15. treeData.forEach(processNode);
  16. }
  17. /** 计算选中范围,只考虑expanded情况以优化性能 */
  18. export function calcRangeKeys(_ref) {
  19. let {
  20. treeData,
  21. expandedKeys,
  22. startKey,
  23. endKey,
  24. fieldNames = {
  25. title: 'title',
  26. key: 'key',
  27. children: 'children'
  28. }
  29. } = _ref;
  30. const keys = [];
  31. let record = Record.None;
  32. if (startKey && startKey === endKey) {
  33. return [startKey];
  34. }
  35. if (!startKey || !endKey) {
  36. return [];
  37. }
  38. function matchKey(key) {
  39. return key === startKey || key === endKey;
  40. }
  41. traverseNodesKey(treeData, fieldNames, key => {
  42. if (record === Record.End) {
  43. return false;
  44. }
  45. if (matchKey(key)) {
  46. // Match test
  47. keys.push(key);
  48. if (record === Record.None) {
  49. record = Record.Start;
  50. } else if (record === Record.Start) {
  51. record = Record.End;
  52. return false;
  53. }
  54. } else if (record === Record.Start) {
  55. // Append selection
  56. keys.push(key);
  57. }
  58. return expandedKeys.includes(key);
  59. });
  60. return keys;
  61. }
  62. export function convertDirectoryKeysToNodes(treeData, keys, fieldNames) {
  63. const restKeys = [...keys];
  64. const nodes = [];
  65. traverseNodesKey(treeData, fieldNames, (key, node) => {
  66. const index = restKeys.indexOf(key);
  67. if (index !== -1) {
  68. nodes.push(node);
  69. restKeys.splice(index, 1);
  70. }
  71. return !!restKeys.length;
  72. });
  73. return nodes;
  74. }