<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Vis Network | Manipulation | Manipulation with Localization</title>
<style type="text/css">
body,
select {
font: 10pt sans;
}
#mynetwork {
position: relative;
width: 640px;
height: 480px;
border: 1px solid lightgray;
}
table.legend_table {
font-size: 11px;
border-width: 1px;
border-color: #d3d3d3;
border-style: solid;
}
table.legend_table,
td {
border-width: 1px;
border-color: #d3d3d3;
border-style: solid;
padding: 2px;
}
div.table_content {
width: 80px;
text-align: center;
}
div.table_description {
width: 100px;
}
#operation {
font-size: 28px;
}
#network-popUp {
display: none;
position: absolute;
top: 350px;
left: 170px;
z-index: 299;
width: 250px;
height: 120px;
background-color: #f9f9f9;
border-style: solid;
border-width: 3px;
border-color: #5394ed;
padding: 10px;
text-align: center;
}
</style>
<script
type="text/javascript"
src="../../../standalone/umd/vis-network.min.js"
></script>
<script
type="text/javascript"
src="https://unpkg.com/alea@1.0.0/alea.js"
></script>
<script type="text/javascript" src="../exampleUtil.js"></script>
<script type="text/javascript">
var network;
var container;
var exportArea;
var importButton;
var exportButton;
function init() {
container = document.getElementById("mynetwork");
exportArea = document.getElementById("input_output");
importButton = document.getElementById("import_button");
exportButton = document.getElementById("export_button");
draw();
}
var nodes = null;
var edges = null;
var network = null;
// randomly create some nodes and edges
var data = getScaleFreeNetwork(25);
var seed = 2;
function destroy() {
if (network !== null) {
network.destroy();
network = null;
}
}
function draw() {
destroy();
nodes = [];
edges = [];
// create a network
var container = document.getElementById("mynetwork");
var options = {
layout: {randomSeed: seed}, // just to make sure the layout is the same when the locale is changed
locale: "cn",
interaction: {keyboard: true},
manipulation: {
addNode: function (data, callback) {
// filling in the popup DOM elements
document.getElementById("operation").innerText = "Add Node";
document.getElementById("node-id").value = data.id;
document.getElementById("node-label").value = data.label;
document.getElementById("saveButton").onclick = saveData.bind(
this,
data,
callback
);
document.getElementById("cancelButton").onclick =
clearPopUp.bind();
document.getElementById("network-popUp").style.display = "block";
},
editNode: function (data, callback) {
// filling in the popup DOM elements
document.getElementById("operation").innerText = "Edit Node";
document.getElementById("node-id").value = data.id;
document.getElementById("node-label").value = data.label;
document.getElementById("saveButton").onclick = saveData.bind(
this,
data,
callback
);
document.getElementById("cancelButton").onclick = cancelEdit.bind(
this,
callback
);
document.getElementById("network-popUp").style.display = "block";
},
addEdge: function (data, callback) {
if (data.from == data.to) {
var r = confirm("Do you want to connect the node to itself?");
if (r == true) {
callback(data);
}
} else {
callback(data);
}
},
},
};
network = new vis.Network(container, data, options);
clearOutputArea();
}
function clearPopUp() {
document.getElementById("saveButton").onclick = null;
document.getElementById("cancelButton").onclick = null;
document.getElementById("network-popUp").style.display = "none";
}
function cancelEdit(callback) {
clearPopUp();
callback(null);
}
function saveData(data, callback) {
data.id = document.getElementById("node-id").value;
data.label = document.getElementById("node-label").value;
clearPopUp();
callback(data);
}
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);
}
function destroyNetwork() {
network.destroy();
}
function clearOutputArea() {
exportArea.value = "";
}
function exportNetwork() {
clearOutputArea();
var nodes = objectToArray(network.getPositions());
nodes.forEach(addConnections);
// pretty print node data
var exportValue = JSON.stringify(nodes, undefined, 2);
exportArea.value = exportValue;
resizeExportArea();
}
function importNetwork() {
var inputValue = exportArea.value;
var inputData = JSON.parse(inputValue);
var data = {
nodes: getNodeData(inputData),
edges: getEdgeData(inputData),
};
var options = {
layout: {randomSeed: seed}, // just to make sure the layout is the same when the locale is changed
locale: "cn",
interaction: {keyboard: true},
manipulation: {
addNode: function (data, callback) {
// filling in the popup DOM elements
document.getElementById("operation").innerText = "Add Node";
document.getElementById("node-id").value = data.id;
document.getElementById("node-label").value = data.label;
document.getElementById("saveButton").onclick = saveData.bind(
this,
data,
callback
);
document.getElementById("cancelButton").onclick =
clearPopUp.bind();
document.getElementById("network-popUp").style.display = "block";
},
editNode: function (data, callback) {
// filling in the popup DOM elements
document.getElementById("operation").innerText = "Edit Node";
document.getElementById("node-id").value = data.id;
document.getElementById("node-label").value = data.label;
document.getElementById("saveButton").onclick = saveData.bind(
this,
data,
callback
);
document.getElementById("cancelButton").onclick = cancelEdit.bind(
this,
callback
);
document.getElementById("network-popUp").style.display = "block";
},
addEdge: function (data, callback) {
if (data.from == data.to) {
var r = confirm("Do you want to connect the node to itself?");
if (r == true) {
callback(data);
}
} else {
callback(data);
}
},
},
};
network = new vis.Network(container, data, options);
resizeExportArea();
}
function getNodeData(data) {
var networkNodes = [];
data.forEach(function (elem, index, array) {
networkNodes.push({
id: elem.id,
label: elem.id,
x: elem.x,
y: elem.y,
});
});
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) {
// add the connection
node.connections.forEach(function (connId, cIndex, conns) {
networkEdges.push({from: node.id, to: connId});
let cNode = getNodeById(data, connId);
var elementConnections = cNode.connections;
// remove the connection from the other node to prevent duplicate connections
var duplicateIndex = elementConnections.findIndex(function (
connection
) {
return connection == node.id; // double equals since id can be numeric or string
});
if (duplicateIndex != -1) {
elementConnections.splice(duplicateIndex, 1);
}
});
});
return new vis.DataSet(networkEdges);
}
function objectToArray(obj) {
return Object.keys(obj).map(function (key) {
obj[key].id = key;
return obj[key];
});
}
function resizeExportArea() {
exportArea.style.height = 1 + exportArea.scrollHeight + "px";
}
</script>
</head>
<body onload="init();">
<h2>Editing the nodes and edges (localized)</h2>
<div id="network-popUp">
<span id="operation">node</span> <br />
<table style="margin: auto">
<tr>
<td>id</td>
<td><input id="node-id" value="new value" /></td>
</tr>
<tr>
<td>label</td>
<td><input id="node-label" value="new value" /></td>
</tr>
</table>
<input type="button" value="save" id="saveButton" />
<input type="button" value="cancel" id="cancelButton" />
</div>
<br />
<div id="mynetwork"></div>
<div>
<input
type="button"
id="import_button"
onclick="importNetwork()"
value="import"
/>
<input
type="button"
id="export_button"
onclick="exportNetwork()"
value="export"
/>
<input
type="button"
id="destroy_button"
onclick="destroyNetwork()"
value="destroy"
/>
<br>
<textarea id="input_output"></textarea>
</div>
</body>
</html>