@@ -347,21 +347,39 @@ function create_cy_elements(data, style) {
347347 meta_node = cy . $ ( "#simulators" ) ;
348348 meta_node_edges = meta_node . connectedEdges ( ) ;
349349 cy . on ( "select tap dbltap" , highlightElement ) ;
350+ //
351+ // if a user drags a node, we want to remember this
352+ cy . on ( "drag" , "node" , store_positions ) ;
350353 cy . $ ( "#simulators" ) . select ( ) ;
351354 selectionChanged ( ) ;
352355
356+ // when the layout stops the first time, we store positions of the nodes
353357 cy_layout . one ( 'layoutstop' , store_positions ) ;
354358}
355359
356- function store_positions ( ) {
357- cy . nodes ( ) . forEach ( n => { const init_pos = { x : n . renderedPosition ( ) . x , y : n . renderedPosition ( ) . y } ; n . initial_position = init_pos ; } ) ;
358- cy . nodes ( ) . forEach ( n => { console . log ( "Init pos: " + n . id ( ) + ": " + n . initial_position . x + ", " + n . initial_position . y ) ; } ) ;
360+ function store_positions ( event ) {
361+ event_target = event . target ;
359362
360- // store the initial pan values
361- cy_pan . x = cy . pan ( ) . x ;
362- cy_pan . y = cy . pan ( ) . y ;
363+ // must be a dragged node
364+ if ( event . type === "drag" )
365+ {
366+ n = event_target ;
367+ const new_pos = { x : n . renderedPosition ( ) . x , y : n . renderedPosition ( ) . y } ;
368+ n . initial_position = new_pos ;
363369
364- // store the initial zoom values
365- cy_zoom . level = cy . zoom ( ) ;
366- console . log ( "Initial pan: " + JSON . stringify ( cy_pan ) + ", zoom: " + JSON . stringify ( cy_zoom ) ) ;
370+ console . log ( "Node was dragged" ) ;
371+ console . log ( "New pos: " + n . id ( ) + ": " + n . initial_position . x + ", " + n . initial_position . y ) ;
372+ }
373+ else {
374+ cy . nodes ( ) . forEach ( n => { const init_pos = { x : n . renderedPosition ( ) . x , y : n . renderedPosition ( ) . y } ; n . initial_position = init_pos ; } ) ;
375+ cy . nodes ( ) . forEach ( n => { console . log ( "Init pos: " + n . id ( ) + ": " + n . initial_position . x + ", " + n . initial_position . y ) ; } ) ;
376+ //
377+ // store the initial pan values
378+ cy_pan . x = cy . pan ( ) . x ;
379+ cy_pan . y = cy . pan ( ) . y ;
380+
381+ // store the initial zoom values
382+ cy_zoom . level = cy . zoom ( ) ;
383+ console . log ( "Initial pan: " + JSON . stringify ( cy_pan ) + ", zoom: " + JSON . stringify ( cy_zoom ) ) ;
384+ }
367385}
0 commit comments