1
1
export const deepClone = x => JSON . parse ( JSON . stringify ( x ) ) ;
2
2
3
- // TODO: add description on the top of all functions
4
- // TODO: replace deepclone with { ...rootnode } and make sure it still works
5
- // TODO: implement isValidCheckedStatus()
6
-
7
- // assign uniq ids to each node
8
- export const addUniqIds = rootNode => {
3
+ // deepclone the initial data for internal use, and assign uniq ids to each node
4
+ // deepclone only happens once at initialization, other operations will be in-place
5
+ export const initStateWithUniqIds = rootNode => {
9
6
let curId = 0 ;
10
7
const _addId = node => {
11
8
node . _id = curId ; // eslint-disable-line
@@ -33,9 +30,14 @@ const setStatusDown = (node, status) => {
33
30
setStatusDown ( child , status ) ;
34
31
}
35
32
}
36
- return node ;
33
+ return { ... node } ;
37
34
} ;
38
35
36
+ // set checked status for all nodes
37
+ export const setAllCheckedStatus = ( rootNode , status ) => (
38
+ setStatusDown ( rootNode , status )
39
+ ) ;
40
+
39
41
// calculate the check status of a node based on the check status of it's children
40
42
export const getNewCheckStatus = node => {
41
43
const { children } = node ;
@@ -66,15 +68,9 @@ export const updateStatusUp = nodes => {
66
68
updateStatusUp ( nodes ) ;
67
69
} ;
68
70
69
- // set checked status for all nodes
70
- export const setAllCheckedStatus = ( rootNode , status ) => (
71
- setStatusDown ( deepClone ( rootNode ) , status )
72
- ) ;
73
-
74
71
// handle state change when user (un)check a TreeNode
75
72
export const checkNode = ( rootNode , path , status ) => {
76
- const _rootNode = deepClone ( rootNode ) ;
77
- let curNode = _rootNode ;
73
+ let curNode = rootNode ;
78
74
const parentNodes = [ curNode ] ; // parent nodes for getNewCheckStatus() upwards
79
75
80
76
for ( const idx of path ) {
@@ -87,33 +83,30 @@ export const checkNode = (rootNode, path, status) => {
87
83
parentNodes . pop ( ) ; // don't need to check this node's level
88
84
updateStatusUp ( parentNodes ) ; // update check status up, from this nodes parent, in place
89
85
90
- return _rootNode ;
86
+ return { ... rootNode } ;
91
87
} ;
92
88
93
89
export const renameNode = ( rootNode , path , newName ) => {
94
- const _rootNode = deepClone ( rootNode ) ;
95
- let curNode = _rootNode ;
90
+ let curNode = rootNode ;
96
91
for ( const idx of path ) {
97
92
curNode = curNode . children [ idx ] ;
98
93
}
99
94
curNode . name = newName ;
100
95
101
- return _rootNode ;
96
+ return { ... rootNode } ;
102
97
} ;
103
98
104
99
export const deleteNode = ( rootNode , path ) => {
105
- const _rootNode = deepClone ( rootNode ) ;
106
-
100
+ let curNode = rootNode ;
107
101
if ( path . length === 0 ) {
108
102
// this is root node
109
103
// just remove every children and reset check status to 0
110
- _rootNode . children = [ ] ;
111
- _rootNode . checked = 0 ;
104
+ curNode . children = [ ] ;
105
+ curNode . checked = 0 ;
112
106
113
- return _rootNode ;
107
+ return curNode ;
114
108
}
115
109
116
- let curNode = _rootNode ;
117
110
const parentNodes = [ curNode ] ;
118
111
const lastIdx = path . pop ( ) ;
119
112
@@ -125,7 +118,7 @@ export const deleteNode = (rootNode, path) => {
125
118
curNode . children . splice ( lastIdx , 1 ) ; // remove target node
126
119
updateStatusUp ( parentNodes ) ; // update check status up, from this nodes
127
120
128
- return _rootNode ;
121
+ return { ... rootNode } ;
129
122
} ;
130
123
131
124
export const findMaxId = rootNode => {
@@ -141,8 +134,7 @@ export const addNode = (rootNode, path, type = 'file') => {
141
134
const id = findMaxId ( rootNode ) + 1 ;
142
135
const isFile = type === 'file' ;
143
136
144
- const _rootNode = deepClone ( rootNode ) ;
145
- let curNode = _rootNode ;
137
+ let curNode = rootNode ;
146
138
for ( const idx of path ) {
147
139
curNode = curNode . children [ idx ] ;
148
140
}
@@ -167,12 +159,11 @@ export const addNode = (rootNode, path, type = 'file') => {
167
159
}
168
160
}
169
161
170
- return _rootNode ;
162
+ return { ... rootNode } ;
171
163
} ;
172
164
173
165
export const toggleOpen = ( rootNode , path , isOpen ) => {
174
- const _rootNode = deepClone ( rootNode ) ;
175
- let curNode = _rootNode ;
166
+ let curNode = rootNode ;
176
167
for ( const idx of path ) {
177
168
curNode = curNode . children [ idx ] ;
178
169
}
@@ -182,7 +173,7 @@ export const toggleOpen = (rootNode, path, isOpen) => {
182
173
curNode . isOpen = isOpen ;
183
174
}
184
175
185
- return _rootNode ;
176
+ return { ... rootNode } ;
186
177
} ;
187
178
188
179
export const setAllOpenStatus = ( node , isOpen ) => {
@@ -216,4 +207,5 @@ export const isValidOpenStatus = node => {
216
207
} ;
217
208
218
209
// check if the initial custom checked status is valid
210
+ // TODO: implement isValidCheckedStatus()
219
211
export const isValidCheckedStatus = rootNode => true ; /* eslint-disable-line */
0 commit comments