<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Editor</title>
<!-- Copyright 1998-2023 by Northwoods Software Corporation. -->
</head>
<body>
<div style="width: 100%; display: flex; justify-content: space-between">
<div style="display: flex; flex-direction: column; margin: 0 2px 0 0">
<div id="myPaletteDiv" style="flex-grow: 1; width: 100px; background-color: floralwhite; border: solid 1px black"></div>
<div id="myOverviewDiv" style="margin: 2px 0 0 0; width: 100px; height: 100px; background-color: whitesmoke; border: solid 1px black"></div>
</div>
<div id="myDiagramDiv" style="flex-grow: 1; height: 400px; border: solid 1px black"></div>
</div>
<div>
<button id="myLoadButton">Load</button>
<button id="mySaveButton">Save</button>
</div>
<textarea id="mySavedModel" style="width:100%;height:200px">
{ "class": "go.GraphLinksModel",
"nodeDataArray": [
{"key":1, "text":"hello", "color":"green", "location":"0 0"},
{"key":2, "text":"world", "color":"red", "location":"70 0"}
],
"linkDataArray": [
{"from":1, "to":2}
]}
</textarea>
<script src="https://unpkg.com/gojs"></script>
<script id="code">
const $ = go.GraphObject.make;
// initialize main Diagram
const myDiagram =
$(go.Diagram, "myDiagramDiv",
{
"undoManager.isEnabled": true
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{ locationSpot: go.Spot.Center },
new go.Binding("location", "location", go.Point.parse).makeTwoWay(go.Point.stringify),
$(go.Shape,
{
fill: "white", stroke: "gray", strokeWidth: 2,
portId: "", fromLinkable: true, toLinkable: true,
fromLinkableDuplicates: true, toLinkableDuplicates: true,
fromLinkableSelfNode: true, toLinkableSelfNode: true
},
new go.Binding("stroke", "color")),
$(go.TextBlock,
{
margin: new go.Margin(5, 5, 3, 5), font: "10pt sans-serif",
minSize: new go.Size(16, 16), maxSize: new go.Size(120, NaN),
editable: true
},
new go.Binding("text").makeTwoWay())
);
// initialize Palette
myPalette =
$(go.Palette, "myPaletteDiv",
{
padding: 0,
layout: $(go.GridLayout,
{
wrappingColumn: 1,
alignment: go.GridLayout.Position,
cellSize: new go.Size(1, 1),
spacing: new go.Size(0, 0)
}),
hasHorizontalScrollbar: false,
hasVerticalScrollbar: false,
isReadOnly: false,
allowDelete: false,
allowInsert: false,
allowMove: false,
nodeTemplate:
$(go.Node, "Vertical",
{ locationSpot: go.Spot.Center },
$(go.Shape, { width: 30, height: 30, fill: "white" },
new go.Binding("fill", "color")),
$(go.TextBlock,
new go.Binding("text"))
),
groupTemplate:
$(go.Group, "Vertical",
{ selectable: false, copyable: false },
{ layout: $(go.GridLayout, { wrappingColumn: 1 }) },
$(go.Panel, "Auto",
{ name: "HEADER", width: 100, height: 30 },
$(go.Shape, { fill: "slateblue", strokeWidth: 0 }),
$(go.TextBlock, { stroke: "white" },
new go.Binding("text")),
$("SubGraphExpanderButton", { alignment: go.Spot.Right })
),
$(go.Placeholder, { padding: new go.Margin(10, 0, 0, 0) })
),
"ViewportBoundsChanged": e => {
if (e.subject.bounds.width !== e.diagram.viewportBounds.width) {
e.diagram.findTopLevelGroups().each(g => {
const head = g.findObject("HEADER");
head.width = e.diagram.viewportBounds.width;
});
}
},
model: new go.GraphLinksModel([
{ key: 1, isGroup: true, text: "Group 1" },
{ group: 1, text: "red node", color: "red" },
{ group: 1, text: "green node", color: "green" },
{ group: 1, text: "blue node", color: "blue" },
{ group: 1, text: "orange node", color: "orange" },
{ key: 2, isGroup: true, text: "Group 2" },
{ group: 2, text: "pink", color: "pink" },
{ group: 2, text: "lightgreen", color: "lightgreen" },
{ group: 2, text: "lightblue", color: "lightblue" },
{ group: 2, text: "gold", color: "gold" }
])
});
// initialize Overview
myOverview =
$(go.Overview, "myOverviewDiv",
{
observed: myDiagram,
contentAlignment: go.Spot.Center
});
// save a model to and load a model from Json text, displayed below the Diagram
function save() {
var str = myDiagram.model.toJson();
document.getElementById("mySavedModel").value = str;
}
document.getElementById("mySaveButton").addEventListener("click", save);
function load() {
var str = document.getElementById("mySavedModel").value;
myDiagram.model = go.Model.fromJson(str);
}
document.getElementById("myLoadButton").addEventListener("click", load);
load();
</script>
</body>
</html>