@@ -34,6 +34,7 @@ export function serializeState(state) {
3434 return JSON . parse ( JSON . stringify ( state ) ) ;
3535 } catch ( e ) {
3636 // if there is an error, that means there is circular state i.e state that depends on itself
37+ console . log ( 'circular' ) ;
3738 return 'circularState' ;
3839 }
3940}
@@ -51,7 +52,7 @@ export function serializeState(state) {
5152 * @param route - an object representing the route associated with the node.
5253 */
5354class Tree {
54- state : string | { } ;
55+ state : string | { } ; // TODO: should change this to stateless || statefull
5556
5657 name : string ;
5758
@@ -77,16 +78,10 @@ class Tree {
7778 // 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
7879 // EXAMPLE OF COMPONENTNAMES OBJECT: {editableInput: 1, Provider: 0, etc}
7980
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 ) {
8782 this . state = state === 'root' ? 'root' : serializeState ( state ) ;
8883 this . name = name ;
89- this . componentData = componentData ? { ... JSON . parse ( JSON . stringify ( componentData ) ) } : { } ;
84+ this . componentData = JSON . parse ( JSON . stringify ( componentData ) ) ;
9085 this . children = [ ] ;
9186 this . parent = null ; // ref to parent so we can add siblings
9287 this . isExpanded = true ;
@@ -100,26 +95,24 @@ class Tree {
10095 * @returns
10196 */
10297 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+ */
103102 // 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+
123116 return name ;
124117 }
125118 /**
@@ -131,9 +124,10 @@ class Tree {
131124 * @returns - return new tree instance that is child
132125 */
133126 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
135128 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
137131 const newChild : Tree = new Tree ( state , uniqueName , componentData , rtid ) ;
138132 // updating newChild parent to "this"
139133 newChild . parent = this ;
@@ -153,6 +147,7 @@ class Tree {
153147 addSibling ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
154148 // gets unique name by calling checkForDuplicates method
155149 const uniqueName = this . checkForDuplicates ( name ) ;
150+ console . log ( 'SIBILING' , { name : uniqueName , this : this } ) ;
156151 // instantiate new sibilng tree with state, uniqueName, componentName and rtid
157152 const newSibling : Tree = new Tree ( state , uniqueName , componentData , rtid ) ;
158153 // updating newSibling's parent to be the parent of "this" which refers to sibling node
0 commit comments