@@ -48,9 +48,26 @@ class GraphData extends Data {
48
48
this . isWeighted = isWeighted ;
49
49
}
50
50
51
- addNode ( id , weight = null , visitedCount = 0 , selectedCount = 0 , x = 0 , y = 0 ) {
51
+ addNode ( id , weight = null , x = 0 , y = 0 , visitedCount = 0 , selectedCount = 0 ) {
52
52
if ( this . findNode ( id ) ) return ;
53
- this . nodes . push ( { id, weight, visitedCount, selectedCount, x, y } ) ;
53
+ this . nodes . push ( { id, weight, x, y, visitedCount, selectedCount } ) ;
54
+ this . layout ( ) ;
55
+ }
56
+
57
+ updateNode ( id , weight , x , y , visitedCount , selectedCount ) {
58
+ const node = this . findNode ( id ) ;
59
+ const update = { weight, x, y, visitedCount, selectedCount } ;
60
+ Object . keys ( update ) . forEach ( key => {
61
+ if ( update [ key ] === undefined ) delete update [ key ] ;
62
+ } ) ;
63
+ Object . assign ( node , update ) ;
64
+ }
65
+
66
+ removeNode ( id ) {
67
+ const node = this . findNode ( id ) ;
68
+ if ( ! node ) return ;
69
+ const index = this . nodes . indexOf ( node ) ;
70
+ this . nodes . splice ( index , 1 ) ;
54
71
this . layout ( ) ;
55
72
}
56
73
@@ -60,9 +77,21 @@ class GraphData extends Data {
60
77
this . layout ( ) ;
61
78
}
62
79
63
- updateNode ( id , update ) {
64
- const node = this . findNode ( id ) ;
65
- Object . assign ( node , update ) ;
80
+ updateEdge ( source , target , weight , visitedCount , selectedCount ) {
81
+ const edge = this . findEdge ( source , target ) ;
82
+ const update = { weight, visitedCount, selectedCount } ;
83
+ Object . keys ( update ) . forEach ( key => {
84
+ if ( update [ key ] === undefined ) delete update [ key ] ;
85
+ } ) ;
86
+ Object . assign ( edge , update ) ;
87
+ }
88
+
89
+ removeEdge ( source , target ) {
90
+ const edge = this . findEdge ( source , target ) ;
91
+ if ( ! edge ) return ;
92
+ const index = this . edges . indexOf ( edge ) ;
93
+ this . edges . splice ( index , 1 ) ;
94
+ this . layout ( ) ;
66
95
}
67
96
68
97
findNode ( id ) {
@@ -195,11 +224,11 @@ class GraphData extends Data {
195
224
this . visitOrLeave ( false , target , source , weight ) ;
196
225
}
197
226
198
- visitOrLeave ( visit , target , source = null , weight = null ) {
227
+ visitOrLeave ( visit , target , source = null , weight ) {
199
228
const edge = this . findEdge ( source , target ) ;
200
229
if ( edge ) edge . visitedCount += visit ? 1 : - 1 ;
201
230
const node = this . findNode ( target ) ;
202
- node . weight = weight ;
231
+ if ( weight !== undefined ) node . weight = weight ;
203
232
node . visitedCount += visit ? 1 : - 1 ;
204
233
if ( this . logData ) {
205
234
this . logData . print ( visit ? ( source || '' ) + ' -> ' + target : ( source || '' ) + ' <- ' + target ) ;
0 commit comments