|
1 | | -import { ITreeItem } from '../types/ITreeItem'; |
| 1 | +import { ITreeItem, ITreeItemWithParent } from '../types/ITreeItem'; |
| 2 | +import { TaskModelProxy } from '../modules/tasks/models/TaskModelProxy'; |
| 3 | +import TaskModel from '../modules/tasks/models/TaskModel'; |
2 | 4 |
|
3 | 5 | const TreeModelHelper = { |
| 6 | + getPathToNode<T extends ITreeItemWithParent>(node: T) { |
| 7 | + const result: string[] = []; |
| 8 | + |
| 9 | + let ptrNode = node; |
| 10 | + while (ptrNode) { |
| 11 | + result.unshift(ptrNode.key); |
| 12 | + // @ts-ignore |
| 13 | + ptrNode = ptrNode.parent; |
| 14 | + } |
| 15 | + |
| 16 | + return result; |
| 17 | + }, |
| 18 | + copyItemsToTree( |
| 19 | + sourceTree: TaskModel[], |
| 20 | + destTree: TaskModelProxy[], |
| 21 | + keysToTask: string[] |
| 22 | + ) { |
| 23 | + let keyIdx = 0; |
| 24 | + let sourceChild = sourceTree; |
| 25 | + let destChild = destTree; |
| 26 | + |
| 27 | + if (keysToTask.length === 1) { |
| 28 | + const source = sourceChild.find((node) => node.key === keysToTask[0]); |
| 29 | + destChild.push( |
| 30 | + // @ts-ignore |
| 31 | + new TaskModelProxy({ |
| 32 | + ...source, |
| 33 | + children: [], |
| 34 | + }) |
| 35 | + ); |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + do { |
| 40 | + const nextSourceNode = sourceChild.find( |
| 41 | + (task) => task.key === keysToTask[keyIdx] |
| 42 | + ); |
| 43 | + if (!nextSourceNode) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + const nextDestNode = destChild.find( |
| 48 | + (task) => task.key === keysToTask[keyIdx] |
| 49 | + ); |
| 50 | + |
| 51 | + if (nextDestNode) { |
| 52 | + keyIdx++; |
| 53 | + destChild = nextDestNode.children; |
| 54 | + } else { |
| 55 | + const restKeysToTask = keysToTask.slice(keyIdx); |
| 56 | + return TreeModelHelper.copySubItemsToTree( |
| 57 | + sourceChild, |
| 58 | + destChild, |
| 59 | + restKeysToTask |
| 60 | + ); |
| 61 | + } |
| 62 | + } while (keyIdx >= keysToTask.length); |
| 63 | + |
| 64 | + return true; |
| 65 | + }, |
| 66 | + |
| 67 | + copySubItemsToTree( |
| 68 | + sourceTree: TaskModel[], |
| 69 | + destTree: TaskModelProxy[], |
| 70 | + keysToTask: string[] |
| 71 | + ) { |
| 72 | + if (!sourceTree) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + let keyIdx = 0; |
| 77 | + let dest = destTree; |
| 78 | + let source = sourceTree.find((node) => node.key === keysToTask[keyIdx]); |
| 79 | + |
| 80 | + if (!source) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + while (true) { |
| 85 | + // @ts-ignore |
| 86 | + let copy = new TaskModelProxy({ |
| 87 | + ...source, |
| 88 | + children: [], |
| 89 | + }); |
| 90 | + |
| 91 | + dest.push(copy); |
| 92 | + |
| 93 | + keyIdx++; |
| 94 | + if (keyIdx === keysToTask.length) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + dest = copy.children; |
| 99 | + source = source.children.find((node) => node.key === keysToTask[keyIdx]); |
| 100 | + if (!source) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + |
4 | 106 | walkRecursive<T extends ITreeItem<any>>( |
5 | 107 | fn: (t: T, p?: T) => void, |
6 | 108 | treeItems: T[], |
|
0 commit comments