Line data Source code
1 : import type { Ref } from 'vue'
2 :
3 : import type { TreeNode } from '@/types/treeExplorerTypes'
4 :
5 236 : export function useTreeExpansion(expandedKeys: Ref<Record<string, boolean>>) {
6 236 : const toggleNode = (node: TreeNode) => {
7 48 : if (node.key && typeof node.key === 'string') {
8 48 : if (node.key in expandedKeys.value) {
9 0 : delete expandedKeys.value[node.key]
10 : } else {
11 48 : expandedKeys.value[node.key] = true
12 : }
13 : }
14 : }
15 :
16 236 : const toggleNodeRecursive = (node: TreeNode) => {
17 0 : if (node.key && typeof node.key === 'string') {
18 0 : if (node.key in expandedKeys.value) {
19 0 : collapseNode(node)
20 : } else {
21 0 : expandNode(node)
22 : }
23 : }
24 : }
25 :
26 236 : const expandNode = (node: TreeNode) => {
27 102 : if (node.key && typeof node.key === 'string' && !node.leaf) {
28 31 : expandedKeys.value[node.key] = true
29 :
30 31 : for (const child of node.children ?? []) {
31 96 : expandNode(child)
32 : }
33 : }
34 : }
35 :
36 236 : const collapseNode = (node: TreeNode) => {
37 0 : if (node.key && typeof node.key === 'string' && !node.leaf) {
38 0 : delete expandedKeys.value[node.key]
39 :
40 0 : for (const child of node.children ?? []) {
41 0 : collapseNode(child)
42 : }
43 : }
44 : }
45 :
46 236 : const toggleNodeOnEvent = (e: MouseEvent | KeyboardEvent, node: TreeNode) => {
47 48 : if (e.ctrlKey) {
48 0 : toggleNodeRecursive(node)
49 : } else {
50 48 : toggleNode(node)
51 : }
52 : }
53 :
54 236 : return {
55 : toggleNode,
56 : toggleNodeRecursive,
57 : expandNode,
58 : collapseNode,
59 : toggleNodeOnEvent
60 : }
61 : }
|