-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraph.js
More file actions
378 lines (348 loc) · 12.1 KB
/
graph.js
File metadata and controls
378 lines (348 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// vim set sw=4:
var elements = [];
var cy;
var cy_layout;
var removed = [];
var meta_node;
var meta_node_edges;
const cy_pan = {
x: 0,
y: 0
};
const cy_zoom = {
level: 0
};
function selectionChanged() {
removed.toReversed().forEach(eles => eles.restore());
removed = [];
removed.push(cy.filter(function(element, i){
return element.isNode() && element.data("features").includes("simulator") && !selected.includes(element.data("id"));
}).remove());
// Hide all nodes that do not have any (even indirect) connection to a visible simulator node
removed.push(cy.filter(function(element, i){
return element.isNode() && !element.data("features").includes("simulator") && !element.connectedEdges().some(edge => edge.source().data("features").includes("simulator") || edge.target().data("features").includes("simulator"));
}).remove());
// Hide all edges that are not connected to a visible node
removed.push(cy.filter(function(element, i){
return element.isEdge() && !(element.source().visible() && element.target().visible);
}).remove());
// Hide all nodes that are not connected to a visible edge, except for simulators
removed.push(cy.filter(function(element, i){
return element.isNode() && !element.data("features").includes("simulator") && !element.connectedEdges().some(edge => edge.visible());
}).remove());
if (selected.length > 0) {
layoutNodes();
}
}
function layoutNodes() {
cy_layout = cy.layout({
name: "fcose",
animate: "end",
padding: 50,
avoidOverlap: true,
nodeDimensionsIncludeLabels: true,
centerGraph: false,
numIter: 10000,
});
cy_layout.run();
}
const BUTTON_ICONS = {
"source": "github.svg",
"documentation": "book.svg",
"homepage": "home.svg",
"download": "download.svg",
"chat": "messages.svg",
"issue tracker": "check-circle.svg",
"forum": "users.svg",
"examples": "code.svg",
"tutorial": "user.svg",
"installation": "package.svg",
"email": "mail.svg"
}
const BUTTON_ROWS = [
["homepage", "download", "source"],
["documentation", "installation", "tutorial", "examples"],
["forum", "issue tracker", "chat", "email"]
];
function urlButton(type, url, btnClass) {
const button = document.createElement("button");
let iconFile = BUTTON_ICONS[type];
button.type = "button"
button.classList.add('btn', 'm-1');
let icon = `<img aria-hidden='true' focusable='false' class='icon' src='assets/${iconFile}'></img>`;
button.innerHTML = icon + " " + type;
if (url !== undefined) {
button.classList.add(btnClass);
button.onclick = function() {
window.open(url, "_blank");
}
} else {
button.classList.add('btn-secondary');
button.disabled = true;
}
return button;
}
function highlightNode(node) {
if (node.id() == "simulators") {
return;
}
// Swap out center/uncenter buttons
const centerButton = document.getElementById("center_button");
const uncenterButton = document.getElementById("uncenter_button");
if (centerButton) {
centerButton.classList.add("d-none");
uncenterButton.classList.remove("d-none");
}
// Ignore the meta node
meta_node.deselect();
meta_node.remove();
// change opacity if node or edge is not connected to the clicked node
const nhood = node.closedNeighbourhood();
const connectedEdges = node.connectedEdges();
cy.elements().forEach(n => n.style("opacity", 0.1));
nhood.forEach(n => n.style("opacity", 1));
connectedEdges.forEach(n => {n.style("curve-style", "bezier"); n.style("min-zoomed-font-size", 12)});
const layout = nhood.layout({
name: 'concentric',
fit: true,
concentric: function(ele) {
if (ele.same(node)) {
return 2;
} else {
return 1;
}
},
minNodeSpacing: 75,
avoidOverlap: true,
levelWidth: () => {return 1; },
animate: true,
//animationDuration: 50,
animationEasing: 'ease',
});
layout.run();
}
function showNodeDetails(node) {
if (node.id() == "simulators") {
showDetails(null, null);
} else {
showDetails(node.data(), node.outgoers("edge").map((edge) => {
return { type: "outgoing", target: edge.target().id(), label: edge.data("label"), source: edge.source().id() };
}).concat(
node.incomers("edge").map((edge) => {
return { type: "incoming", target: edge.target().id(), label: edge.data("label"), source: edge.source().id() }
})
)
);
}
}
function highlightEdge(edge) {
console.log("Edge double tapped: no op");
}
function highlightElement(event) {
if (event.target === cy) {
// Only unhilight node if double tapped on background
// Single tap is too error prone
if (event.type === "dbltap") {
unhighlightNode(null, true);
}
else {
console.log("No-op: single tap on background");
}
}
else {
if (event.target.group() === "nodes") {
const node = event.target;
if (event.type === "dbltap") {
highlightNode(node);
}
else if (event.type === "select") {
showNodeDetails(node);
}
} else if (event.target.group() === "edges") {
const edge = event.target;
if (event.type === "select") {
// do nothing special
}
else if (event.type === "dbltap") {
highlightEdge(edge);
}
}
}
}
function unhighlightNode(event, unselect) {
// Swap out center/uncenter buttons
const centerButton = document.getElementById("center_button");
const uncenterButton = document.getElementById("uncenter_button");
if (centerButton) {
centerButton.classList.remove("d-none");
uncenterButton.classList.add("d-none");
}
// Ignore the meta node
meta_node.restore();
meta_node_edges.restore();
// Re-add the edges
cy.elements().forEach(n => n.style("opacity", 1));
// return graph to initial state
const return_graph_to_init = () => {
cy.edges().forEach(n => {n.style("curve-style", "unbundled-bezier"); n.style("min-zoomed-font-size", 36)});
cy.animate(
{
pan: cy_pan,
duration: 1500,
easing: 'ease',
zoom: {
level: cy_zoom.level,
},
complete: () => {
console.log("New pan: " + JSON.stringify(cy.pan()) + ", zoom: " + cy.zoom());
}
});
cy.nodes().forEach(n => n.animation({
position: n.initial_position,
duration: 1500,
easing: 'ease',
complete: () => {
console.log("Init pos: " + n.id() + ": " + n.initial_position.x + ", " + n.initial_position.y);
console.log("New pos: " + n.id() + ": " + n.position().x + ", " + n.position().y);
}
}).play());
};
return_graph_to_init();
if (unselect) {
showDetails(null, null);
}
}
function updateHighlights() {
const search = document.getElementById("simulator_search_input");
const searchValue = search.value.toLowerCase();
for (node of cy.nodes()) {
node.style("opacity", 1);
}
if (search) {
for (node of cy.nodes()) {
const full_name = node.data("full_name");
const description = node.data("description");
if (full_name && !(full_name.toLowerCase().includes(searchValue) || description && description.toLowerCase().includes(searchValue))) {
node.style("opacity", 0.2);
}
}
}
if (criteria.length > 0) {
for (node of cy.nodes()) {
let levels = node.data("levels");
if (levels && criteria.every(c => levels.includes(c))) {
// do nothing
} else if (levels && criteria.some(c => levels.includes(c))) {
if (node.style("opacity") > 0.6) {
node.style("opacity", 0.6);
}
} else {
node.style("opacity", 0.2);
}
}
}
}
function newNode(name, description) {
const features = description["features"].split(",").map(x => x.trim());
let bio_levels = description["biological_level"];
if (bio_levels === undefined) {
bio_levels = [];
}
let comp_levels = description["computing_scale"];
if (comp_levels === undefined) {
comp_levels = [];
}
let position = undefined;
return {
group: 'nodes',
data: {
id: name,
full_name: description["full_name"],
short_name: description["short_name"],
description: description["summary"],
levels: bio_levels.concat(comp_levels),
features: description["features"],
urls: description["urls"]
},
position: position,
initial_position: position,
classes: features.join(" ")
}
}
function newEdge(name, relation) {
return {
group: 'edges',
data: {
id: name + " → " + relation["name"],
source: name,
target: relation["name"],
label: relation["description"],
style: {
"event": "no",
"selectable": false,
"grabbable": false
},
}
}
}
function create_cy_elements(data, style) {
// Create a "meta-node" for all simulators
elements.push(newNode("simulators", {full_name: "Simulators", features: "meta"}));
for (const [name, description] of Object.entries(data)) {
elements.push(newNode(name, description));
// Connect all simulators to the meta node
if (description["features"].includes("simulator")) {
elements.push(newEdge("simulators", {name: name, description: "simulator"}));
}
if (description["relations"] !== undefined) {
for (let relation of description["relations"]){
if (relation["description"] === undefined)
continue;
elements.push(newEdge(name, relation));
}
}
}
cy = window.cy = cytoscape({
container: document.getElementById('cy'),
elements: elements,
layout: { name: 'random' },
style: style,
minZoom: 0.75,
maxZoom: 4,
wheelSensitivity: 0.2,
});
// store the meta_node, since we need to remove it when highlighting nodes
meta_node = cy.$("#simulators");
meta_node_edges = meta_node.connectedEdges();
cy.on("select tap dbltap", highlightElement);
//
// if a user drags a node, we want to remember this
cy.on("drag", "node", store_positions);
cy.$("#simulators").select();
selectionChanged();
// when the layout stops the first time, we store positions of the nodes
cy_layout.one('layoutstop', store_positions);
}
function store_positions(event) {
event_target = event.target;
// must be a dragged node
if (event.type === "drag")
{
n = event_target;
const new_pos = {x: n.renderedPosition().x, y: n.renderedPosition().y};
n.initial_position = new_pos;
console.log("Node was dragged");
console.log("New pos: " + n.id() + ": " + n.initial_position.x + ", " + n.initial_position.y);
}
else {
cy.nodes().forEach(n => {const init_pos = {x: n.renderedPosition().x, y: n.renderedPosition().y}; n.initial_position = init_pos;});
cy.nodes().forEach(n => {console.log("Init pos: " + n.id() + ": " + n.initial_position.x + ", " + n.initial_position.y);});
//
// store the initial pan values
cy_pan.x = cy.pan().x;
cy_pan.y = cy.pan().y;
// store the initial zoom values
cy_zoom.level = cy.zoom();
console.log("Initial pan: " + JSON.stringify(cy_pan) + ", zoom: " + JSON.stringify(cy_zoom));
}
}