@@ -34,6 +34,7 @@ export function serializeState(state) {
34
34
return JSON . parse ( JSON . stringify ( state ) ) ;
35
35
} catch ( e ) {
36
36
// if there is an error, that means there is circular state i.e state that depends on itself
37
+ console . log ( 'circular' ) ;
37
38
return 'circularState' ;
38
39
}
39
40
}
@@ -51,7 +52,7 @@ export function serializeState(state) {
51
52
* @param route - an object representing the route associated with the node.
52
53
*/
53
54
class Tree {
54
- state : string | { } ;
55
+ state : string | { } ; // TODO: should change this to stateless || statefull
55
56
56
57
name : string ;
57
58
@@ -77,16 +78,10 @@ class Tree {
77
78
// If not, create the new component and also a new key: value pair in 'componentNames' with the component's name as the key and 0 as its value
78
79
// EXAMPLE OF COMPONENTNAMES OBJECT: {editableInput: 1, Provider: 0, etc}
79
80
80
- constructor (
81
- state : string | { } ,
82
- name = 'nameless' ,
83
- componentData : { } = { } ,
84
- rtid : any = null ,
85
- string : any = null ,
86
- ) {
81
+ constructor ( state : string | { } , name = 'nameless' , componentData : { } = { } , rtid : any = null ) {
87
82
this . state = state === 'root' ? 'root' : serializeState ( state ) ;
88
83
this . name = name ;
89
- this . componentData = componentData ? { ... JSON . parse ( JSON . stringify ( componentData ) ) } : { } ;
84
+ this . componentData = JSON . parse ( JSON . stringify ( componentData ) ) ;
90
85
this . children = [ ] ;
91
86
this . parent = null ; // ref to parent so we can add siblings
92
87
this . isExpanded = true ;
@@ -100,26 +95,24 @@ class Tree {
100
95
* @returns
101
96
*/
102
97
checkForDuplicates ( name : string ) : string {
98
+ /**
99
+ * The condition for check empty name does not seem to ever be reached
100
+ * Commented and did not remove for potential future use
101
+ */
103
102
// check for empty name
104
- if ( name === '' && typeof this . rtid === 'string' ) {
105
- name = this . rtid . replace ( 'fromLinkFiber' , '' ) ;
106
- }
107
- // if we are at root, then make sure componentNames is an empty object
108
- if ( this . state === 'root' ) componentNames = { } ;
109
- // check for duplicate
110
- else if ( componentNames [ name ] !== undefined ) {
111
- // if name exists in componentNames object, grab count and add 1
112
- const count : number = componentNames [ name ] + 1 ;
113
- // declare a new name variable
114
- const newName : string = name + count ;
115
- // update name count in object
116
- componentNames [ name ] = count ;
117
- // return newName
118
- return newName ;
119
- }
120
- // add name in componentsName with value of 0
121
- componentNames [ name ] = 0 ;
122
- // return name
103
+ // if (name === '' && typeof this.rtid === 'string') {
104
+ // name = this.rtid.replace('fromLinkFiber', '');
105
+ // }
106
+ // console.log('Tree', { name, parentName: this.name, this: this });
107
+ // if parent node is root, initialize the componentNames object
108
+ if ( this . name === 'root' ) componentNames = { } ;
109
+
110
+ // Numerize the component name if found duplicate
111
+ // Ex: A board has 3 rows => Row, Row1, Row2
112
+ // Ex: Each row has 3 boxes => Box, Box1, Box2, ..., Box8
113
+ componentNames [ name ] = componentNames [ name ] + 1 || 0 ;
114
+ name += componentNames [ name ] ? componentNames [ name ] : '' ;
115
+
123
116
return name ;
124
117
}
125
118
/**
@@ -131,9 +124,10 @@ class Tree {
131
124
* @returns - return new tree instance that is child
132
125
*/
133
126
addChild ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
134
- // gets unique name by calling checkForDuplicates method
127
+ // Get unique name by invoking checkForDuplicates method
135
128
const uniqueName = this . checkForDuplicates ( name ) ;
136
- // instantiates new child Tree with state, uniqueName, componentData and rtid
129
+ console . log ( 'CHILD' , { name : uniqueName , this : this } ) ;
130
+ // Instantiates new child Tree with state, uniqueName, componentData and rtid
137
131
const newChild : Tree = new Tree ( state , uniqueName , componentData , rtid ) ;
138
132
// updating newChild parent to "this"
139
133
newChild . parent = this ;
@@ -153,6 +147,7 @@ class Tree {
153
147
addSibling ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
154
148
// gets unique name by calling checkForDuplicates method
155
149
const uniqueName = this . checkForDuplicates ( name ) ;
150
+ console . log ( 'SIBILING' , { name : uniqueName , this : this } ) ;
156
151
// instantiate new sibilng tree with state, uniqueName, componentName and rtid
157
152
const newSibling : Tree = new Tree ( state , uniqueName , componentData , rtid ) ;
158
153
// updating newSibling's parent to be the parent of "this" which refers to sibling node
0 commit comments