Skip to content

Commit

Permalink
feat: Create "meta-node" to avoid unconnected nodes in graph
Browse files Browse the repository at this point in the history
  • Loading branch information
mstimberg committed Jan 13, 2025
1 parent 16a15cf commit 02ae0c9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
9 changes: 9 additions & 0 deletions assets/cy-style.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
"edge-text-rotation": "autorotate"
}
},
{
"selector": ".meta",
"style": {
"background-color": "#fff",
"font-style": "italic",
"shape": "hexagon",
"border-width": "3px"
}
},
{
"selector": ".standard",
"style": {
Expand Down
37 changes: 29 additions & 8 deletions graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var elements = [];
var cy;
var cy_layout;
var removed = [];
var meta_node;
var meta_node_edges;

function selectionChanged() {
removed.toReversed().forEach(eles => eles.restore());
Expand All @@ -28,9 +30,6 @@ function selectionChanged() {
}

function layoutNodes() {
unconnected = cy.filter(function(element, i) {
return element.isNode() && element.connectedEdges().length == 0
}).remove();
cy_layout = cy.layout({
name: "cola",
animate: "end",
Expand All @@ -39,7 +38,6 @@ function layoutNodes() {
nodeDimensionsIncludeLabels: true,
centerGraph: false,
});
unconnected.forEach((eles, i) => {eles.restore(); eles.position("x", 250); eles.position("y", 200 + i*50);});
cy_layout.run();
}

Expand Down Expand Up @@ -91,6 +89,12 @@ function urlButton(type, url) {
}

function highlightNode(node) {
if (node.id() == "simulators") {
return;
}
// 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();
Expand Down Expand Up @@ -123,9 +127,13 @@ function highlightNode(node) {
}

function showNodeDetails(node) {
showDetails(node.data(), node.outgoers("edge").map((edge) => {
return {target: edge.target().id(), label: edge.data("label"), source: edge.source().id()};
}));
if (node.id() == "simulators") {
showDetails(null, null);
} else {
showDetails(node.data(), node.outgoers("edge").map((edge) => {
return {target: edge.target().id(), label: edge.data("label"), source: edge.source().id()};
}));
}
}

function highlightEdge(edge) {
Expand Down Expand Up @@ -188,6 +196,10 @@ function highlightElement(event) {
}

function unhighlightNode(event) {
// Ignore the meta node
meta_node.restore();
meta_node_edges.restore();
// Re-add the edges
cy.elements().forEach(n => n.style("opacity", 1));
showDetails(null, null);
}
Expand Down Expand Up @@ -222,9 +234,14 @@ function newEdge(name, relation) {
}

function create_cy_elements(data, style) {
console.log("Creating", data);
// 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)
Expand All @@ -239,7 +256,11 @@ function create_cy_elements(data, style) {
layout: { name: 'random' },
style: style
});
// 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);
cy.on("unselect", "*", unhighlightNode);
cy.$("#simulators").select();
selectionChanged();
}
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const selected = [];

// If params are null, show a default message
function showDetails(data, outgoers) {
console.log("showDetails called")
// Show details about the simulator
const details = document.getElementById("details");
// Basic description
Expand Down Expand Up @@ -62,7 +61,11 @@ function showDetails(data, outgoers) {

const url = new URL(window.location.href);
const params = new URLSearchParams(url.search);
params.set('selected', data["full_name"]);
if (data === null)
params.delete('selected');
else {
params.set('selected', data["full_name"]);
}
url.search = params.toString();
window.history.pushState({}, "", url);
}
Expand Down

0 comments on commit 02ae0c9

Please sign in to comment.