11export const deepClone = x => JSON . parse ( JSON . stringify ( x ) ) ;
22
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 => {
96 let curId = 0 ;
107 const _addId = node => {
118 node . _id = curId ; // eslint-disable-line
@@ -33,9 +30,14 @@ const setStatusDown = (node, status) => {
3330 setStatusDown ( child , status ) ;
3431 }
3532 }
36- return node ;
33+ return { ... node } ;
3734} ;
3835
36+ // set checked status for all nodes
37+ export const setAllCheckedStatus = ( rootNode , status ) => (
38+ setStatusDown ( rootNode , status )
39+ ) ;
40+
3941// calculate the check status of a node based on the check status of it's children
4042export const getNewCheckStatus = node => {
4143 const { children } = node ;
@@ -66,15 +68,9 @@ export const updateStatusUp = nodes => {
6668 updateStatusUp ( nodes ) ;
6769} ;
6870
69- // set checked status for all nodes
70- export const setAllCheckedStatus = ( rootNode , status ) => (
71- setStatusDown ( deepClone ( rootNode ) , status )
72- ) ;
73-
7471// handle state change when user (un)check a TreeNode
7572export const checkNode = ( rootNode , path , status ) => {
76- const _rootNode = deepClone ( rootNode ) ;
77- let curNode = _rootNode ;
73+ let curNode = rootNode ;
7874 const parentNodes = [ curNode ] ; // parent nodes for getNewCheckStatus() upwards
7975
8076 for ( const idx of path ) {
@@ -87,33 +83,30 @@ export const checkNode = (rootNode, path, status) => {
8783 parentNodes . pop ( ) ; // don't need to check this node's level
8884 updateStatusUp ( parentNodes ) ; // update check status up, from this nodes parent, in place
8985
90- return _rootNode ;
86+ return { ... rootNode } ;
9187} ;
9288
9389export const renameNode = ( rootNode , path , newName ) => {
94- const _rootNode = deepClone ( rootNode ) ;
95- let curNode = _rootNode ;
90+ let curNode = rootNode ;
9691 for ( const idx of path ) {
9792 curNode = curNode . children [ idx ] ;
9893 }
9994 curNode . name = newName ;
10095
101- return _rootNode ;
96+ return { ... rootNode } ;
10297} ;
10398
10499export const deleteNode = ( rootNode , path ) => {
105- const _rootNode = deepClone ( rootNode ) ;
106-
100+ let curNode = rootNode ;
107101 if ( path . length === 0 ) {
108102 // this is root node
109103 // just remove every children and reset check status to 0
110- _rootNode . children = [ ] ;
111- _rootNode . checked = 0 ;
104+ curNode . children = [ ] ;
105+ curNode . checked = 0 ;
112106
113- return _rootNode ;
107+ return curNode ;
114108 }
115109
116- let curNode = _rootNode ;
117110 const parentNodes = [ curNode ] ;
118111 const lastIdx = path . pop ( ) ;
119112
@@ -125,7 +118,7 @@ export const deleteNode = (rootNode, path) => {
125118 curNode . children . splice ( lastIdx , 1 ) ; // remove target node
126119 updateStatusUp ( parentNodes ) ; // update check status up, from this nodes
127120
128- return _rootNode ;
121+ return { ... rootNode } ;
129122} ;
130123
131124export const findMaxId = rootNode => {
@@ -141,8 +134,7 @@ export const addNode = (rootNode, path, type = 'file') => {
141134 const id = findMaxId ( rootNode ) + 1 ;
142135 const isFile = type === 'file' ;
143136
144- const _rootNode = deepClone ( rootNode ) ;
145- let curNode = _rootNode ;
137+ let curNode = rootNode ;
146138 for ( const idx of path ) {
147139 curNode = curNode . children [ idx ] ;
148140 }
@@ -167,12 +159,11 @@ export const addNode = (rootNode, path, type = 'file') => {
167159 }
168160 }
169161
170- return _rootNode ;
162+ return { ... rootNode } ;
171163} ;
172164
173165export const toggleOpen = ( rootNode , path , isOpen ) => {
174- const _rootNode = deepClone ( rootNode ) ;
175- let curNode = _rootNode ;
166+ let curNode = rootNode ;
176167 for ( const idx of path ) {
177168 curNode = curNode . children [ idx ] ;
178169 }
@@ -182,7 +173,7 @@ export const toggleOpen = (rootNode, path, isOpen) => {
182173 curNode . isOpen = isOpen ;
183174 }
184175
185- return _rootNode ;
176+ return { ... rootNode } ;
186177} ;
187178
188179export const setAllOpenStatus = ( node , isOpen ) => {
@@ -216,4 +207,5 @@ export const isValidOpenStatus = node => {
216207} ;
217208
218209// check if the initial custom checked status is valid
210+ // TODO: implement isValidCheckedStatus()
219211export const isValidCheckedStatus = rootNode => true ; /* eslint-disable-line */
0 commit comments