@@ -14,19 +14,29 @@ class DirectedGraphConstructTracer extends Tracer {
14
14
constructor ( name , nodePlacement = null ) {
15
15
super ( name ) ;
16
16
this . nodePlacement = nodePlacement ;
17
+ this . nodeCollection = [ ] ;
17
18
if ( this . isNew ) initView ( this ) ;
18
19
}
19
20
20
- _addNode ( G , root , element , parentElement = null ) {
21
+ _addRoot ( root ) {
22
+ this . manager . pushStep ( this . capsule , {
23
+ type : 'addRoot' ,
24
+ arguments : arguments
25
+ } ) ;
26
+ return this ;
27
+ }
28
+
29
+ _addNode ( element , parentElement = null ) {
21
30
this . manager . pushStep ( this . capsule , {
22
31
type : 'addNode' ,
23
32
arguments : arguments
24
33
} ) ;
25
34
return this ;
26
35
}
27
36
28
- _findNode ( G , val ) {
37
+ _findNode ( val ) {
29
38
var idToFind = this . n ( val ) ;
39
+ var G = this . nodeCollection ;
30
40
var result = null ;
31
41
for ( let i = 0 ; i < G . length ; i ++ ) {
32
42
if ( G [ i ] . id === idToFind ) {
@@ -37,10 +47,9 @@ class DirectedGraphConstructTracer extends Tracer {
37
47
return result ;
38
48
}
39
49
40
- _visit ( G , target , source ) {
50
+ _visit ( target , source ) {
41
51
this . manager . pushStep ( this . capsule , {
42
52
type : 'visit' ,
43
- G : G ,
44
53
target : target ,
45
54
source : source
46
55
} ) ;
@@ -77,13 +86,16 @@ class DirectedGraphConstructTracer extends Tracer {
77
86
node . y = position . y ;
78
87
} ) ;
79
88
break ;
89
+ case 'addRoot' :
90
+ this . addRoot . apply ( this , step . arguments ) ;
91
+ break ;
80
92
case 'addNode' :
81
93
this . addNode . apply ( this , step . arguments ) ;
82
94
break ;
83
95
case 'visit' :
84
96
case 'leave' :
85
97
var visit = step . type == 'visit' ;
86
- var nodeObject = this . _findNode ( step . G , step . target ) ;
98
+ var nodeObject = this . _findNode ( step . target ) ;
87
99
nodeObject . visited = visit ;
88
100
nodeObject . isNew = false ;
89
101
var targetNode = this . graph . nodes ( this . n ( step . target ) ) ;
@@ -108,9 +120,20 @@ class DirectedGraphConstructTracer extends Tracer {
108
120
}
109
121
}
110
122
111
- addNode ( G , root , node , parent ) {
123
+ addRoot ( root ) {
124
+ if ( this . rootObject ) throw 'Root for this graph is already added' ;
125
+ this . rootObject = this . createGraphNode ( root ) ;
126
+ this . drawGraph ( this . rootObject . level ) ;
127
+ }
128
+
129
+ addNode ( node , parent ) {
130
+ var nodeObject = this . createGraphNode ( node , parent )
131
+ this . drawGraph ( nodeObject . level ) ;
132
+ }
133
+
134
+ createGraphNode ( node , parent ) {
112
135
var nodeObject = this . nodeConstruct ( node ) ;
113
- var parentObject = this . _findNode ( G , parent ) ;
136
+ var parentObject = this . _findNode ( parent ) ;
114
137
if ( parentObject ) {
115
138
nodeObject . parent = parentObject ;
116
139
nodeObject . level = parentObject . level + 1 ;
@@ -131,18 +154,14 @@ class DirectedGraphConstructTracer extends Tracer {
131
154
}
132
155
if ( isSpliced ) {
133
156
parentObject . children . splice ( insertIndex , 0 , nodeObject ) ;
134
- console . log ( 'exchange Happened' ) ;
135
157
} else {
136
158
parentObject . children . push ( nodeObject ) ;
137
159
}
138
160
}
139
161
}
140
162
nodeObject . updateBreadth ( ) ;
141
- G . push ( nodeObject ) ;
142
- this . drawGraph ( G , root , nodeObject . level ) ;
143
- if ( node === 4 ) {
144
- console . log ( this . graph . nodes ) ;
145
- }
163
+ this . nodeCollection . push ( nodeObject ) ;
164
+ return nodeObject ;
146
165
}
147
166
148
167
nodeConstruct ( val ) {
@@ -171,7 +190,7 @@ class DirectedGraphConstructTracer extends Tracer {
171
190
return nodeObject ;
172
191
}
173
192
174
- drawGraph ( G , root , nodeLevel ) {
193
+ drawGraph ( nodeLevel ) {
175
194
const nodes = [ ] ;
176
195
const edges = [ ] ;
177
196
var tracer = this ;
@@ -210,9 +229,8 @@ class DirectedGraphConstructTracer extends Tracer {
210
229
occupiedBreadth += node . breadth ;
211
230
}
212
231
}
213
- }
214
- var rootObject = this . _findNode ( G , root ) ;
215
- drawNode ( rootObject ) ;
232
+ } ;
233
+ drawNode ( this . rootObject ) ;
216
234
217
235
this . graph . clear ( ) ;
218
236
this . graph . read ( {
@@ -251,8 +269,10 @@ class DirectedGraphConstructTracer extends Tracer {
251
269
}
252
270
253
271
clearGraphColor ( ) {
254
- var tracer = this ;
255
-
272
+ this . nodeCollection . forEach ( function ( node ) {
273
+ node . isNew = false ;
274
+ } ) ;
275
+
256
276
this . graph . nodes ( ) . forEach ( function ( node ) {
257
277
node . color = tracer . color . default ;
258
278
} ) ;
0 commit comments