@@ -126,20 +126,6 @@ export class ViewGraph {
126126 return null ;
127127 }
128128
129- // Check if an edge already exists between these two devices
130- for ( const edge of this . edges . values ( ) ) {
131- const { n1, n2 } = edge . connectedNodes ;
132- if (
133- ( n1 === device1Id && n2 === device2Id ) ||
134- ( n1 === device2Id && n2 === device1Id )
135- ) {
136- console . warn (
137- `Connection between ID ${ device1Id } and ID ${ device2Id } already exists.` ,
138- ) ;
139- return null ;
140- }
141- }
142-
143129 const device1 = this . devices . get ( device1Id ) ;
144130 const device2 = this . devices . get ( device2Id ) ;
145131
@@ -160,19 +146,19 @@ export class ViewGraph {
160146
161147 deviceMoved ( deviceId : DeviceId ) {
162148 const device : Device = this . devices . get ( deviceId ) ;
163- device . getConnections ( ) . forEach ( ( adyacentId ) => {
149+ device . getConnections ( ) . forEach ( ( adjacentId ) => {
164150 const edge = this . edges . get (
165- Edge . generateConnectionKey ( { n1 : deviceId , n2 : adyacentId } ) ,
151+ Edge . generateConnectionKey ( { n1 : deviceId , n2 : adjacentId } ) ,
166152 ) ;
167153 // Get start and end devices directly
168154 const startDevice =
169155 edge . connectedNodes . n1 === device . id
170156 ? device
171- : this . devices . get ( adyacentId ) ;
157+ : this . devices . get ( adjacentId ) ;
172158
173159 const endDevice =
174160 edge . connectedNodes . n1 === device . id
175- ? this . devices . get ( adyacentId )
161+ ? this . devices . get ( adjacentId )
176162 : device ;
177163
178164 if ( startDevice && endDevice ) {
@@ -188,6 +174,17 @@ export class ViewGraph {
188174 return this . layer ;
189175 }
190176
177+ changeCurrLayer ( newLayer : Layer ) {
178+ this . layer = newLayer ;
179+ this . clear ( ) ;
180+ this . constructView ( ) ;
181+ const layerSelect = document . getElementById (
182+ "layer-select" ,
183+ ) as HTMLSelectElement ;
184+ const event = new CustomEvent ( "layerChanged" ) ;
185+ layerSelect . dispatchEvent ( event ) ;
186+ }
187+
191188 getSpeed ( ) : SpeedMultiplier {
192189 if ( ! this . speedMultiplier ) {
193190 this . speedMultiplier = new SpeedMultiplier ( 1 ) ;
@@ -211,8 +208,8 @@ export class ViewGraph {
211208 }
212209 const connections = device
213210 . getConnections ( )
214- . map ( ( adyacentId ) =>
215- this . edges . get ( Edge . generateConnectionKey ( { n1 : id , n2 : adyacentId } ) ) ,
211+ . map ( ( adjacentId ) =>
212+ this . edges . get ( Edge . generateConnectionKey ( { n1 : id , n2 : adjacentId } ) ) ,
216213 ) ;
217214 return connections ;
218215 }
@@ -250,11 +247,9 @@ export class ViewGraph {
250247 return ;
251248 }
252249
253- device . destroy ( ) ;
254-
255- // Remove connection from adyacent’s devices
256- device . getConnections ( ) . forEach ( ( adyacentId ) => {
257- const edgeId = Edge . generateConnectionKey ( { n1 : id , n2 : adyacentId } ) ;
250+ // Remove connection from adjacent’s devices
251+ device . getConnections ( ) . forEach ( ( adjacentId ) => {
252+ const edgeId = Edge . generateConnectionKey ( { n1 : id , n2 : adjacentId } ) ;
258253 const edge = this . edges . get ( edgeId ) ;
259254 if ( edge ) {
260255 edge . delete ( ) ;
@@ -346,14 +341,14 @@ export class ViewGraph {
346341 const unvisitedNodes = [ ] ;
347342 const connectingEdges = new Map < DeviceId , EdgeId > ( [ [ a . id , null ] ] ) ;
348343 while ( current . id !== idB ) {
349- for ( const adyacentId of current . connections ) {
344+ for ( const adjacentId of current . connections ) {
350345 const edgeId = Edge . generateConnectionKey ( {
351346 n1 : current . id ,
352- n2 : adyacentId ,
347+ n2 : adjacentId ,
353348 } ) ;
354- if ( ! connectingEdges . has ( adyacentId ) ) {
355- connectingEdges . set ( adyacentId , edgeId ) ;
356- unvisitedNodes . push ( this . devices . get ( adyacentId ) ) ;
349+ if ( ! connectingEdges . has ( adjacentId ) ) {
350+ connectingEdges . set ( adjacentId , edgeId ) ;
351+ unvisitedNodes . push ( this . devices . get ( adjacentId ) ) ;
357352 }
358353 }
359354 if ( unvisitedNodes . length === 0 ) {
@@ -393,12 +388,12 @@ export class ViewGraph {
393388 if ( visited . has ( w ) ) {
394389 return ;
395390 }
396- const adyacent = this . datagraph . getDevice ( w ) ;
391+ const adjacent = this . datagraph . getDevice ( w ) ;
397392 // mark node as visited
398393 visited . add ( w ) ;
399394
400- if ( layerIncluded ( layerFromType ( adyacent . type ) , this . layer ) ) {
401- // add connection between v and w
395+ if ( layerIncluded ( layerFromType ( adjacent . type ) , this . layer ) ) {
396+ // add connection between s and w
402397 const connectionKey : string = Edge . generateConnectionKey ( {
403398 n1 : w ,
404399 n2 : s ,
@@ -413,7 +408,11 @@ export class ViewGraph {
413408 } ) ;
414409 }
415410
416- destroy ( ) {
411+ clear ( ) {
412+ this . viewport . clear ( ) ;
417413 this . devices . forEach ( ( device ) => device . destroy ( ) ) ;
414+ this . devices . clear ( ) ;
415+ this . edges . forEach ( ( edge ) => edge . destroy ( ) ) ;
416+ this . edges . clear ( ) ;
418417 }
419418}
0 commit comments