OK, I’ve changed my sample to use two separate Group templates, “” for expanded, and “Collapsed”. It seems to work well, although I’m not sure what kinds of operations I should try.
<!DOCTYPE html>
<html>
<head>
<title>Simple Groups</title>
<!-- Copyright 1998-2024 by Northwoods Software Corporation. -->
</head>
<body>
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
Double-click on Groups to collapse or expand them.
<textarea id="mySavedModel" style="width:100%;height:250px"></textarea>
<script src="https://unpkg.com/gojs"></script>
<script id="code">
const myDiagram =
new go.Diagram("myDiagramDiv", {
"undoManager.isEnabled": true,
"ModelChanged": 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 =
new go.Node("Auto", { width: 200, height: 100, locationSpot: go.Spot.Center })
.bindTwoWay("location", "loc", go.Point.parse, go.Point.stringify)
.add(
new go.Shape({ fill: "white" })
.bind("fill", "color"),
new go.TextBlock()
.bind("text")
);
myDiagram.groupTemplate =
new go.Group("Auto", {
locationSpot: go.Spot.Center,
isSubGraphExpanded: true,
doubleClick: (e, grp) => {
e.diagram.model.commit(m => {
m.setCategoryForNodeData(grp.data, "Collapsed");
});
}
})
.bindTwoWay("location", "loc", go.Point.parse, go.Point.stringify)
.add(
new go.Shape({ fill: "transparent", strokeWidth: 2, strokeDashArray: [8, 2] }),
new go.Panel("Spot")
.add(
new go.Placeholder({ padding: 20, minSize: new go.Size(200, 80) }),
new go.TextBlock({
margin: 4,
font: "bold 12pt sans-serif",
alignment: go.Spot.Top,
alignmentFocus: go.Spot.Bottom
})
.bind("text")
)
);
myDiagram.groupTemplateMap.add("Collapsed",
new go.Group("Auto", {
locationSpot: go.Spot.Center,
isSubGraphExpanded: false,
doubleClick: (e, grp) => {
e.diagram.model.commit(m => {
m.setCategoryForNodeData(grp.data, "");
});
}
})
.bindTwoWay("location", "loc", go.Point.parse, go.Point.stringify)
.add(
new go.Shape({ fill: "white", strokeWidth: 2, strokeDashArray: [8, 2] })
.bind("fill", "color"),
new go.Panel("Spot")
.add(
new go.Placeholder({ padding: 20, minSize: new go.Size(200, 80) }),
new go.TextBlock({ margin: 4, font: "bold 12pt sans-serif" })
.bind("text")
)
));
myDiagram.model = new go.GraphLinksModel([
{"key":0,"isGroup":true,"category":"","text":"Outer","color":"whitesmoke","loc":"276.5 226"},
{"key":1,"isGroup":true,"category":"","group":0,"text":"Alpha Group","color":"lightblue","loc":"59 87"},
{"key":11,"group":1,"text":"Alpha","color":"lightblue","loc":"59 87"},
{"key":2,"isGroup":true,"category":"Collapsed","group":0,"text":"Beta Group","color":"orange","loc":"514 90"},
{"key":21,"group":2,"text":"Beta","color":"orange","loc":"514 90"},
{"key":3,"isGroup":true,"category":"Collapsed","group":0,"text":"Gamma Group","color":"lightgreen","loc":"261 417"},
{"key":31,"group":3,"text":"Gamma","color":"lightgreen","loc":"261 417"},
{"key":4,"isGroup":true,"category":"","group":0,"text":"Delta Group","color":"pink","loc":"424 318"},
{"key":41,"group":1,"text":"Delta","color":"pink","loc":"184 218"}
]);
</script>
</body>
</html>