@@ -5,13 +5,11 @@ var cy_layout;
55var removed = [ ] ;
66var meta_node ;
77var meta_node_edges ;
8- const cy_pan = {
9- x : 0 ,
10- y : 0
11- } ;
8+ const cy_pan = { } ;
129const cy_zoom = {
13- level : 0
10+ value : 0 ,
1411} ;
12+ var highlighted_node ;
1513
1614function selectionChanged ( ) {
1715 removed . toReversed ( ) . forEach ( eles => eles . restore ( ) ) ;
@@ -42,10 +40,13 @@ function layoutNodes() {
4240 name : "fcose" ,
4341 animate : "end" ,
4442 padding : 50 ,
45- avoidOverlap : true ,
4643 nodeDimensionsIncludeLabels : true ,
4744 centerGraph : false ,
4845 numIter : 10000 ,
46+ fit : true ,
47+ stop : store_positions ,
48+ nodeRepulsion : node => 15000 ,
49+ quality : "proof" ,
4950 } ) ;
5051 cy_layout . run ( ) ;
5152}
@@ -93,6 +94,9 @@ function highlightNode(node) {
9394 if ( node . id ( ) == "simulators" ) {
9495 return ;
9596 }
97+ // track highlighted node
98+ highlighted_node = node ;
99+
96100 // Swap out center/uncenter buttons
97101 const centerButton = document . getElementById ( "center_button" ) ;
98102 const uncenterButton = document . getElementById ( "uncenter_button" ) ;
@@ -203,29 +207,48 @@ function unhighlightNode(event, unselect) {
203207
204208 // return graph to initial state
205209 const return_graph_to_init = ( ) => {
206- cy . edges ( ) . forEach ( n => { n . style ( "curve-style" , "unbundled-bezier" ) ; n . style ( "min-zoomed-font-size" , 36 ) } ) ;
207- cy . animate (
208- {
209- pan : cy_pan ,
210+
211+ // reset edges
212+ var moved_edges = highlighted_node . closedNeighbourhood ( ( el ) => { return el . isEdge ( ) } ) ;
213+ moved_edges . forEach ( n => { n . style ( "curve-style" , "unbundled-bezier" ) ; n . style ( "min-zoomed-font-size" , 36 ) } ) ;
214+
215+ // reset moved nodes
216+ var moved_nodes = highlighted_node . closedNeighbourhood ( ( el ) => { return el . isNode ( ) } ) ;
217+ const node_animations = moved_nodes . map ( n => n . animation ( {
218+ position : n . initial_position ,
210219 duration : 1500 ,
211220 easing : 'ease' ,
212- zoom : {
213- level : cy_zoom . level ,
221+ queue : false ,
222+ fit : {
223+ eles : cy . nodes ( ) ,
224+ padding : 50 ,
214225 } ,
215- complete : ( ) => {
216- console . log ( "New pan: " + JSON . stringify ( cy . pan ( ) ) + ", zoom: " + cy . zoom ( ) ) ;
226+ } )
227+ ) ;
228+
229+ const viewport_animation = cy . animation (
230+ {
231+ pan : cy_pan ,
232+ duration : 1500 ,
233+ easing : 'ease' ,
234+ queue : false ,
235+ zoom : {
236+ level : cy_zoom . value ,
237+ } ,
238+ fit : {
239+ eles : cy . nodes ( ) ,
240+ padding : 50 ,
241+ } ,
242+ complete : ( ) => {
243+ console . log ( "New pan: " + JSON . stringify ( cy . pan ( ) ) + ", zoom: " + cy . zoom ( ) ) ;
244+ }
217245 }
218- } ) ;
219- cy . nodes ( ) . forEach ( n => n . animation ( {
220- position : n . initial_position ,
221- duration : 1500 ,
222- easing : 'ease' ,
223- complete : ( ) => {
224- console . log ( "Init pos: " + n . id ( ) + ": " + n . initial_position . x + ", " + n . initial_position . y ) ;
225- console . log ( "New pos: " + n . id ( ) + ": " + n . position ( ) . x + ", " + n . position ( ) . y ) ;
226- }
227- } ) . play ( ) ) ;
246+ ) ;
228247
248+ const node_promises = node_animations . map ( an => an . play ( ) . promise ( ) ) ;
249+ Promise . all ( [ ...node_promises , viewport_animation . play ( ) . promise ] ) . then ( ( ) => {
250+ console . log ( "Apparently " + moved_nodes . length + " nodes and viewport animations all have completed" ) ;
251+ } ) ;
229252 } ;
230253
231254 return_graph_to_init ( ) ;
@@ -345,9 +368,6 @@ function create_cy_elements(data, style) {
345368 cy . on ( "drag" , "node" , store_positions ) ;
346369 cy . $ ( "#simulators" ) . select ( ) ;
347370 selectionChanged ( ) ;
348-
349- // when the layout stops the first time, we store positions of the nodes
350- cy_layout . one ( 'layoutstop' , store_positions ) ;
351371}
352372
353373function store_positions ( event ) {
@@ -357,22 +377,20 @@ function store_positions(event) {
357377 if ( event . type === "drag" )
358378 {
359379 n = event_target ;
360- const new_pos = { x : n . renderedPosition ( ) . x , y : n . renderedPosition ( ) . y } ;
380+ const new_pos = { x : n . position ( ) . x , y : n . position ( ) . y } ;
361381 n . initial_position = new_pos ;
362382
363383 console . log ( "Node was dragged" ) ;
364384 console . log ( "New pos: " + n . id ( ) + ": " + n . initial_position . x + ", " + n . initial_position . y ) ;
365385 }
366386 else {
367- cy . nodes ( ) . forEach ( n => { const init_pos = { x : n . renderedPosition ( ) . x , y : n . renderedPosition ( ) . y } ; n . initial_position = init_pos ; } ) ;
387+ cy . nodes ( ) . forEach ( n => { const init_pos = { x : n . position ( ) . x , y : n . position ( ) . y } ; n . initial_position = init_pos ; } ) ;
368388 cy . nodes ( ) . forEach ( n => { console . log ( "Init pos: " + n . id ( ) + ": " + n . initial_position . x + ", " + n . initial_position . y ) ; } ) ;
369- //
370- // store the initial pan values
371- cy_pan . x = cy . pan ( ) . x ;
372- cy_pan . y = cy . pan ( ) . y ;
373389
374- // store the initial zoom values
375- cy_zoom . level = cy . zoom ( ) ;
390+ Object . assign ( cy_pan , cy . pan ( ) ) ;
391+ // cy.zoom does not return an object
392+ cy_zoom . value = cy . zoom ( ) ;
393+
376394 console . log ( "Initial pan: " + JSON . stringify ( cy_pan ) + ", zoom: " + JSON . stringify ( cy_zoom ) ) ;
377395 }
378396}
0 commit comments