Skip to content

Commit 4f4fb23

Browse files
authored
Merge pull request #74 from OCNS/feat-graph-concentric-restore
Feat: restore graph to initial state when clicked outside
2 parents 2d96c7b + 5a28d49 commit 4f4fb23

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

graph.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ var cy_layout;
55
var removed = [];
66
var meta_node;
77
var meta_node_edges;
8+
const cy_pan = {
9+
x: 0,
10+
y: 0
11+
};
12+
const cy_zoom = {
13+
level: 0
14+
};
815

916
function selectionChanged() {
1017
removed.toReversed().forEach(eles => eles.restore());
@@ -212,6 +219,33 @@ function unhighlightNode(event) {
212219
meta_node_edges.restore();
213220
// Re-add the edges
214221
cy.elements().forEach(n => n.style("opacity", 1));
222+
223+
// return graph to initial state
224+
const return_graph_to_init = () => {
225+
cy.animate(
226+
{
227+
pan: cy_pan,
228+
duration: 1500,
229+
easing: 'ease',
230+
zoom: {
231+
level: cy_zoom.level,
232+
},
233+
complete: () => {
234+
console.log("New pan: " + JSON.stringify(cy.pan()) + ", zoom: " + cy.zoom());
235+
}
236+
});
237+
cy.nodes().forEach(n => n.animation({
238+
position: n.initial_position,
239+
duration: 1500,
240+
easing: 'ease',
241+
complete: () => {
242+
console.log("Init pos: " + n.id() + ": " + n.initial_position.x + ", " + n.initial_position.y);
243+
console.log("New pos: " + n.id() + ": " + n.position().x + ", " + n.position().y);
244+
}
245+
}).play());
246+
};
247+
248+
return_graph_to_init();
215249
showDetails(null, null);
216250
}
217251

@@ -269,6 +303,7 @@ function newNode(name, description) {
269303
urls: description["urls"]
270304
},
271305
position: position,
306+
initial_position: position,
272307
classes: features.join(" ")
273308
}
274309
}
@@ -312,6 +347,39 @@ function create_cy_elements(data, style) {
312347
meta_node = cy.$("#simulators");
313348
meta_node_edges = meta_node.connectedEdges();
314349
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);
315353
cy.$("#simulators").select();
316354
selectionChanged();
355+
356+
// when the layout stops the first time, we store positions of the nodes
357+
cy_layout.one('layoutstop', store_positions);
358+
}
359+
360+
function store_positions(event) {
361+
event_target = event.target;
362+
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;
369+
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+
}
317385
}

0 commit comments

Comments
 (0)