<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2020 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="go.js"></script>
<script id="code">
function init() {
var $ = go.GraphObject.make;
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
//"undoManager.isEnabled": true, // don't turn on the UndoManager
"commandHandler.deleteSelection": function() {
// remember the keys of the Parts to be deleted
var todelete = { nodes: [], links: [] };
this.diagram.selection.each(function(part) {
if (part instanceof go.Link) todelete.links.push(part.key);
else todelete.nodes.push(part.key);
part.opacity = 0.3;
});
// show what the user selected for deletion
console.log(JSON.stringify(todelete));
setTimeout(function() {
// Execute this when getting a message from the server that some nodes should be deleted.
// Assume todelete is passed from the server, not just a closed-over local variable here.
myDiagram.commit(function(diag) {
todelete.links.forEach(function(key) {
var link = diag.findLinkForKey(key);
if (link !== null) diag.remove(link);
});
todelete.nodes.forEach(function(key) {
var node = diag.findNodeForKey(key);
if (node !== null) diag.remove(node);
});
}, "execute deletion");
}, 3000); // pretend to wait for a server response
},
"ModelChanged": function(e) { // just for demonstration purposes,
if (e.isTransactionFinished) { // show the model data in the page's TextArea
document.getElementById("mySavedModel").textContent = e.model.toJson();
}
}
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, { fill: "lightblue" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 8, editable: true },
new go.Binding("text").makeTwoWay())
);
myDiagram.linkTemplate =
$(go.Link,
$(go.Shape),
$(go.Shape, { toArrow: "Standard" })
);
myDiagram.model = $(go.GraphLinksModel, {
linkKeyProperty: "key",
nodeDataArray: [
{ key: 1, text: "Alpha", color: "lightblue" },
{ key: 2, text: "Beta", color: "orange" },
{ key: 3, text: "Gamma", color: "lightgreen" },
{ key: 4, text: "Delta", color: "pink" }
],
linkDataArray: [
{ from: 1, to: 2 },
{ from: 1, to: 3 },
{ from: 2, to: 2 },
{ from: 3, to: 4 },
{ from: 4, to: 1 }
]
});
}
</script>
</head>
<body onload="init()">
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
<textarea id="mySavedModel" style="width:100%;height:250px"></textarea>
</body>
</html>