-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree-view.vue
113 lines (108 loc) · 2.19 KB
/
tree-view.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
<ol role="tree" class="tree-view-outline">
<connected-tree-node
:name="name"
:data="data"
:dataIterator="dataIterator"
:depth="0"
:path="DEFAULT_ROOT_PATH"
:nodeRenderer="nodeRenderer"
:expandedPaths="expandedPaths"
@expandedPathsChange="onExpandedPathsChange"
/>
</ol>
</template>
<script>
import TreeNode from '@/components/tree-node'
import ConnectedTreeNode from './connected-tree-node.vue'
import { DEFAULT_ROOT_PATH, getExpandedPaths } from '@/libs/pathUtils'
export default {
name: 'tree-view',
components: {
ConnectedTreeNode,
},
props: {
data: null,
expandLevel: {
type: Number,
default: 0,
},
expandPaths: {
type: [String, Array],
default: '',
},
name: {
type: String,
},
sortObjectKeys: {
type: [Boolean, Function],
default: false,
},
nodeRenderer: {
type: Function,
},
dataIterator: {
type: Function,
},
},
data() {
return {
DEFAULT_ROOT_PATH,
expandedPaths: {},
}
},
watch: {
expandLevel() {
this.calcExpandLevel()
},
expandPaths() {
this.calcExpandPaths()
},
},
beforeMount() {
this.reCalcExpandPaths()
},
mounted() {},
methods: {
reCalcExpandPaths() {
let paths = this.calcExpandPaths()
this.calcExpandLevel(paths)
},
calcExpandPaths() {
let paths = []
if (this.expandPaths) {
this.expandPaths.forEach((path) => {
const arr = path.split('.')
arr.forEach((part, index) => {
paths.push(arr.slice(0, index + 1).join('.'))
})
})
}
this.expandedPaths = getExpandedPaths(
this.data,
this.dataIterator,
paths,
0,
{}
)
return paths
},
calcExpandLevel(paths) {
if (!paths) {
paths = []
}
this.expandedPaths = getExpandedPaths(
this.data,
this.dataIterator,
paths,
this.expandLevel,
{}
)
},
onExpandedPathsChange(data) {
this.$set(this.expandedPaths, data.path, data.val)
},
},
}
</script>
<style lang="less"></style>