Browse Source

Fix handling of node id's in saveAndLoad example (#2943)

* Fix handling of node id's in saveAndLoad example

* Code cleanup

* Fixes for review
gemini
wimrijnders 7 years ago
committed by yotamberk
parent
commit
a58b92c123
1 changed files with 25 additions and 17 deletions
  1. +25
    -17
      examples/network/other/saveAndLoad.html

+ 25
- 17
examples/network/other/saveAndLoad.html View File

@ -71,15 +71,6 @@
draw();
}
function addContextualInformation(elem, index, array) {
addId(elem, index);
addConnections(elem, index);
}
function addId(elem, index) {
elem.id = index;
}
function addConnections(elem, index) {
// need to replace this with a tree of the network, then get child direct children of the element
elem.connections = network.getConnectedNodes(index);
@ -107,7 +98,7 @@
var nodes = objectToArray(network.getPositions());
nodes.forEach(addContextualInformation);
nodes.forEach(addConnections);
// pretty print node data
var exportValue = JSON.stringify(nodes, undefined, 2);
@ -141,30 +132,47 @@
return new vis.DataSet(networkNodes);
}
function getNodeById(data, id) {
for (var n = 0; n < data.length; n++) {
if (data[n].id == id) { // double equals since id can be numeric or string
return data[n];
}
};
throw 'Can not find id \'' + id + '\' in data';
}
function getEdgeData(data) {
var networkEdges = [];
data.forEach(function(node, index, array) {
data.forEach(function(node) {
// add the connection
node.connections.forEach(function(connId, cIndex, conns) {
networkEdges.push({from: node.id, to: connId});
let cNode = getNodeById(data, connId);
var elementConnections = array[connId].connections;
var elementConnections = cNode.connections;
// remove the connection from the other node to prevent duplicate connections
var duplicateIndex = elementConnections.findIndex(function(connection) {
connection === node.id;
return connection == node.id; // double equals since id can be numeric or string
});
elementConnections = elementConnections.splice(0, duplicateIndex - 1).concat(elementConnections.splice(duplicateIndex + 1, elementConnections.length))
});
if (duplicateIndex != -1) {
elementConnections.splice(duplicateIndex, 1);
};
});
});
return new vis.DataSet(networkEdges);
}
function objectToArray(obj) {
return Object.keys(obj).map(function (key) { return obj[key]; });
return Object.keys(obj).map(function (key) {
obj[key].id = key;
return obj[key];
});
}
function resizeExportArea() {
@ -174,4 +182,4 @@
init();
</script>
</body>
</html>
</html>

Loading…
Cancel
Save