I copied the definition from extensions/Buttons.js
and modified it to bind the Shape.geometryString rather than the Shape.figure. I also added the TextBlock and bound its text string based on the Group.isSubGraphExpanded property.
<!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">
go.GraphObject.defineBuilder('SubGraphExpanderButton2', function (args) {
var button = /** @type {Panel} */ (
go.GraphObject.make('Button',
go.GraphObject.make(go.Panel, "Horizontal",
go.GraphObject.make(go.Shape, // the icon
{
name: 'ButtonIcon',
stroke: '#424242',
strokeWidth: 1,
desiredSize: new go.Size(10, 10)
},
// bind the Shape.geometryString to the Group.isSubGraphExpanded value using this converter:
new go.Binding('geometryString', 'isSubGraphExpanded',
function (exp) {
if (exp) {
return "M0 0 L 4 4 M0 4 L4 4 4 0 M 6 10 L6 6 10 6 M 10 10 L 6 6";
} else {
return "M0 0 L 4 4 M0 4 L0 0 4 0 M 6 10 L10 10 10 6 M 10 10 L 6 6";
}
}
).ofObject()
),
go.GraphObject.make(go.TextBlock,
new go.Binding("text", "isSubGraphExpanded",
function(exp) { return exp ? "collapse" : "expand"; }
).ofObject())
)
)
);
// subgraph expand/collapse behavior
button.click = function (e, btn) {
var group = btn.part;
if (group instanceof go.Adornment) group = group.adornedPart;
if (!(group instanceof go.Group)) return;
var diagram = group.diagram;
if (diagram === null) return;
var cmd = diagram.commandHandler;
if (group.isSubGraphExpanded) {
if (!cmd.canCollapseSubGraph(group)) return;
} else {
if (!cmd.canExpandSubGraph(group)) return;
}
e.handled = true;
if (group.isSubGraphExpanded) {
cmd.collapseSubGraph(group);
} else {
cmd.expandSubGraph(group);
}
};
return button;
});
function init() {
var $ = go.GraphObject.make;
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
"undoManager.isEnabled": true,
"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: "white", portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 8, editable: true },
new go.Binding("text").makeTwoWay())
);
myDiagram.groupTemplate =
$(go.Group, "Vertical",
$(go.Panel, "Horizontal",
$("SubGraphExpanderButton2"),
$(go.TextBlock, new go.Binding("text")),
),
$(go.Placeholder, { padding: 10, background: "beige" })
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: 3, text: "Gamma", color: "lightgreen", group: 5 },
{ key: 4, text: "Delta", color: "pink", group: 5 },
{ key: 5, text: "GROUP", isGroup: true }
],
[
{ from: 3, to: 4 }
]);
}
</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>