@@ -19,11 +19,10 @@ import { Colors, ZIndexLevels } from "../../utils";
1919import { Position } from "../common" ;
2020import { DeviceInfo } from "../../graphics/renderables/device_info" ;
2121import { IpAddress } from "../../packets/ip" ;
22- import { DeviceId } from "../graphs/datagraph" ;
22+ import { DeviceId , RemovedNodeData } from "../graphs/datagraph" ;
2323import { DragDeviceMove , AddEdgeMove } from "../undo-redo" ;
2424import { Layer } from "./layer" ;
2525import { Packet } from "../packet" ;
26- import { CreateDevice } from "./utils" ;
2726import { MacAddress } from "../../packets/ethernet" ;
2827import { GlobalContext } from "../../context" ;
2928
@@ -131,17 +130,11 @@ export abstract class Device extends Container {
131130 this . addChild ( idText ) ; // Add the ID text as a child of the device
132131 }
133132
134- /// Returns the data needed to create the device
135- getCreateDevice ( ) : CreateDevice {
136- const node = this . viewgraph . getDataGraph ( ) . getDevice ( this . id ) ;
137- const connections = this . viewgraph . getDataGraph ( ) . getConnections ( this . id ) ;
138- return { id : this . id , node, connections } ;
139- }
140-
141- delete ( ) : void {
142- this . viewgraph . removeDevice ( this . id ) ;
133+ delete ( ) : RemovedNodeData {
134+ const deviceData = this . viewgraph . removeDevice ( this . id ) ;
143135 console . log ( `Device ${ this . id } deleted` ) ;
144136 this . destroy ( ) ;
137+ return deviceData ;
145138 }
146139
147140 onPointerDown ( event : FederatedPointerEvent ) : void {
@@ -159,36 +152,22 @@ export abstract class Device extends Container {
159152 this . parent . on ( "pointerup" , onPointerUp ) ;
160153 }
161154
162- // TODO: why is this even here??
163- connectTo ( adjacentId : DeviceId ) : boolean {
164- // Connects both devices with an edge.
165- const edgeId = this . viewgraph . addEdge ( this . id , adjacentId ) ;
166- if ( edgeId ) {
167- // Register move
168- const move = new AddEdgeMove ( this . viewgraph . getLayer ( ) , {
169- n1 : this . id ,
170- n2 : adjacentId ,
171- } ) ;
172- urManager . push ( move ) ;
173-
174- return true ;
175- }
176- return false ;
177- }
178-
179155 onClick ( e : FederatedPointerEvent ) {
180156 e . stopPropagation ( ) ;
181157
182158 if ( ! Device . connectionTarget ) {
183159 selectElement ( this ) ;
184160 return ;
185161 }
186- // If the stored device is this, reset it
162+ // If the stored device is this, ignore
187163 if ( Device . connectionTarget === this ) {
188164 return ;
189165 }
190- // The "LineStart" device ends up as the end of the drawing but it's the same
191- if ( this . connectTo ( Device . connectionTarget . id ) ) {
166+ // Connect both devices
167+ const n1 = Device . connectionTarget . id ;
168+ const n2 = this . id ;
169+ const move = new AddEdgeMove ( this . viewgraph . getLayer ( ) , { n1, n2 } ) ;
170+ if ( urManager . push ( this . viewgraph , move ) ) {
192171 refreshElement ( ) ;
193172 Device . connectionTarget = null ;
194173 }
@@ -258,7 +237,6 @@ export abstract class Device extends Container {
258237 abstract getLayer ( ) : Layer ;
259238
260239 destroy ( ) {
261- // Clear connections
262240 deselectElement ( ) ;
263241 super . destroy ( ) ;
264242 }
@@ -278,13 +256,14 @@ function onPointerMove(event: FederatedPointerEvent): void {
278256}
279257
280258function onPointerUp ( ) : void {
281- if ( Device . dragTarget && Device . startPosition ) {
259+ const target = Device . dragTarget ;
260+ if ( target && Device . startPosition ) {
282261 const endPosition : Position = {
283- x : Device . dragTarget . x ,
284- y : Device . dragTarget . y ,
262+ x : target . x ,
263+ y : target . y ,
285264 } ;
286265 console . log ( "Finalizing move for device:" , {
287- id : Device . dragTarget . id ,
266+ id : target . id ,
288267 startPosition : Device . startPosition ,
289268 endPosition,
290269 } ) ;
@@ -294,24 +273,24 @@ function onPointerUp(): void {
294273 Device . startPosition . y === endPosition . y
295274 ) {
296275 console . log (
297- `No movement detected for device ID ${ Device . dragTarget . id } . Move not registered.` ,
276+ `No movement detected for device ID ${ target . id } . Move not registered.` ,
298277 ) ;
299278 } else {
300279 const move = new DragDeviceMove (
301- Device . dragTarget . viewgraph . getLayer ( ) ,
302- Device . dragTarget . id ,
280+ target . viewgraph . getLayer ( ) ,
281+ target . id ,
303282 Device . startPosition ,
304283 endPosition ,
305284 ) ;
306- urManager . push ( move ) ;
285+ urManager . push ( target . viewgraph , move ) ;
307286 }
308287
309- // Resetear variables estáticas
288+ // Reset static variables
310289 Device . startPosition = null ;
311290
312291 // Remove global pointermove and pointerup events
313- Device . dragTarget . parent . off ( "pointermove" , onPointerMove ) ;
314- Device . dragTarget . parent . off ( "pointerup" , onPointerUp ) ;
292+ target . parent . off ( "pointermove" , onPointerMove ) ;
293+ target . parent . off ( "pointerup" , onPointerUp ) ;
315294 Device . dragTarget = null ;
316295 }
317296}
0 commit comments