create-gojs-kit
Version:
A CLI for downloading GoJS samples, extensions, and docs
323 lines (288 loc) • 13.2 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="Custom policy for collapsing and expanding subtrees, different than TreeExpanderButton." />
<meta itemprop="description" content="Custom policy for collapsing and expanding subtrees, different than TreeExpanderButton." />
<meta property="og:description" content="Custom policy for collapsing and expanding subtrees, different than TreeExpanderButton." />
<meta name="twitter:description" content="Custom policy for collapsing and expanding subtrees, different than TreeExpanderButton." />
<link rel="preconnect" href="https://rsms.me/">
<link rel="stylesheet" href="../assets/css/style.css">
<!-- Copyright 1998-2025 by Northwoods Software Corporation. -->
<meta itemprop="name" content="Custom Expand and Collapse Buttons for Non-Tree Graphs" />
<meta property="og:title" content="Custom Expand and Collapse Buttons for Non-Tree Graphs" />
<meta name="twitter:title" content="Custom Expand and Collapse Buttons for Non-Tree Graphs" />
<meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/customexpandcollapse.png" />
<meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/customexpandcollapse.png" />
<meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/customexpandcollapse.png" />
<meta property="og:url" content="https://gojs.net/latest/samples/customExpandCollapse.html" />
<meta property="twitter:url" content="https://gojs.net/latest/samples/customExpandCollapse.html" />
<meta name="twitter:card" content="summary_large_image" />
<meta property="og:type" content="website" />
<meta property="twitter:domain" content="gojs.net" />
<title>
Custom Expand and Collapse Buttons for Non-Tree Graphs | GoJS Diagramming Library
</title>
</head>
<body>
<!-- This top nav is not part of the sample code -->
<nav id="navTop" class=" w-full h-[var(--topnav-h)] z-30 bg-white border-b border-b-gray-200">
<div class="max-w-screen-xl mx-auto flex flex-wrap items-start justify-between px-4">
<a class="text-white bg-nwoods-primary font-bold !leading-[calc(var(--topnav-h)_-_1px)] my-0 px-2 text-4xl lg:text-5xl logo"
href="../">
GoJS
</a>
<div class="relative">
<button id="topnavButton" class="h-[calc(var(--topnav-h)_-_1px)] px-2 m-0 text-gray-900 bg-inherit shadow-none md:hidden hover:!bg-inherit hover:!text-nwoods-accent hover:!shadow-none" aria-label="Navigation">
<svg class="h-7 w-7 block" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<div id="topnavList" class="hidden md:block">
<div class="absolute right-0 z-30 flex flex-col items-end rounded border border-gray-200 p-4 pl-12 shadow bg-white text-gray-900 font-semibold
md:flex-row md:space-x-4 md:items-start md:border-0 md:p-0 md:shadow-none md:bg-inherit">
<a href="../learn/">Learn</a>
<a href="../samples/">Samples</a>
<a href="../intro/">Intro</a>
<a href="../api/">API</a>
<a href="../download.html">Download</a>
<a href="https://forum.nwoods.com/c/gojs/11" target="_blank" rel="noopener">Forum</a>
<a id="tc" href="https://nwoods.com/contact.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://nwoods.com/contact.html', 'contact');">Contact</a>
<a id="tb" href="https://nwoods.com/sales/index.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://nwoods.com/sales/index.html', 'buy');">Buy</a>
</div>
</div>
</div>
</div>
</nav>
<script>
window.addEventListener("DOMContentLoaded", function () {
// topnav
var topButton = document.getElementById("topnavButton");
var topnavList = document.getElementById("topnavList");
if (topButton && topnavList) {
topButton.addEventListener("click", function (e) {
topnavList
.classList
.toggle("hidden");
e.stopPropagation();
});
document.addEventListener("click", function (e) {
// if the clicked element isn't the list, close the list
if (!topnavList.classList.contains("hidden") && !e.target.closest("#topnavList")) {
topButton.click();
}
});
// set active <a> element
var url = window
.location
.href
.toLowerCase();
var aTags = topnavList.getElementsByTagName('a');
for (var i = 0; i < aTags.length; i++) {
var lowerhref = aTags[i]
.href
.toLowerCase();
if (lowerhref.endsWith('.html'))
lowerhref = lowerhref.slice(0, -5);
if (url.startsWith(lowerhref)) {
aTags[i]
.classList
.add('active');
break;
}
}
}
});
</script>
<div class="flex flex-col prose">
<div class="w-full max-w-screen-xl mx-auto">
<!-- * * * * * * * * * * * * * -->
<!-- Start of GoJS sample code -->
<script src="https://cdn.jsdelivr.net/npm/gojs@3.1.0"></script>
<div id="allSampleContent" class="p-4 w-full">
<script id="code">
function init() {
myDiagram = new go.Diagram('myDiagramDiv', {
padding: 10,
layout: new go.LayeredDigraphLayout({
direction: 90,
layeringOption: go.LayeredDigraphLayering.LongestPathSource
}),
'undoManager.isEnabled': true
});
myDiagram.nodeTemplate =
new go.Node('Vertical', {
portId: '',
fromLinkable: true,
toLinkable: true
})
.bind('visible')
.add(
new go.Panel('Auto')
.add(
new go.Shape({
fill: 'white',
minSize: new go.Size(30, 30),
strokeWidth: 0,
cursor: 'pointer' // indicate that linking may start here
})
.bind('fill', 'color'),
new go.TextBlock({
margin: 2,
fromLinkable: false,
toLinkable: false // don't start drawing a link from the text
})
.bind('text', 'key')
),
go.GraphObject.build('Button', {
// a replacement for "TreeExpanderButton" that works for non-tree-structured graphs
visible: false, // assume initially not visible because there are no links coming out
click: (e, obj) => {
e.diagram.startTransaction();
var node = obj.part;
if (node.data.isCollapsed) {
expandFrom(node, node);
} else {
collapseFrom(node, node);
}
e.diagram.commitTransaction('toggled visibility of dependencies');
}
})
// bind the button visibility to whether it's not a leaf node
.bindObject('visible', 'isTreeLeaf', leaf => !leaf)
.add(
new go.Shape({
name: 'ButtonIcon',
figure: 'MinusLine',
desiredSize: new go.Size(6, 6)
})
.bind('figure', 'isCollapsed', // data.isCollapsed remembers "collapsed" or "expanded"
collapsed => collapsed ? 'PlusLine' : 'MinusLine')
)
);
function collapseFrom(node, start) {
if (node.data.isCollapsed) return;
node.diagram.model.setDataProperty(node.data, 'isCollapsed', true);
if (node !== start) {
node.diagram.model.setDataProperty(node.data, 'visible', false);
}
node.findNodesOutOf().each(collapseFrom);
}
function expandFrom(node, start) {
if (!node.data.isCollapsed) return;
node.diagram.model.setDataProperty(node.data, 'isCollapsed', false);
if (node !== start) {
node.diagram.model.setDataProperty(node.data, 'visible', true);
}
node.findNodesOutOf().each(expandFrom);
}
myDiagram.linkTemplate =
new go.Link({
relinkableFrom: true,
relinkableTo: true,
corner: 10
})
.add(
new go.Shape(),
new go.Shape({ toArrow: 'Standard' })
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: 'A', color: 'lightgreen' },
{ key: 'B1', color: 'yellow' },
{ key: 'B2', color: 'yellow' },
{ key: 'C', color: 'lightblue' },
{ key: 'D1', color: 'orange' },
{ key: 'D2', color: 'orange' },
{ key: 'E', color: 'pink' },
{ key: 'F', color: 'lightgreen' },
{ key: 'Z1', color: 'lightgreen' },
{ key: 'Z2', color: 'yellow' },
{ key: 'Z3', color: 'orange' },
{ key: 'Z4', color: 'pink' }
],
[
{ from: 'A', to: 'B1' },
{ from: 'B1', to: 'C' },
{ from: 'A', to: 'B2' },
{ from: 'B2', to: 'D2' },
{ from: 'C', to: 'D1' },
{ from: 'C', to: 'D2' },
{ from: 'D1', to: 'E' },
{ from: 'D2', to: 'E' },
{ from: 'D2', to: 'F' },
{ from: 'Z1', to: 'Z2' },
{ from: 'Z2', to: 'Z3' },
{ from: 'Z3', to: 'Z4' },
{ from: 'Z4', to: 'Z1' }
]
);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div
id="myDiagramDiv"
style="border: solid 1px black; width: 600px; height: 700px"></div>
<p>
The "TreeExpanderButton", which changes the
<a>Node.isTreeExpanded</a> property, really only works with tree structures.
When you want to hide/show the "downstream" nodes from a given node, using
the "TreeExpanderButton" might not do what you like, especially when there
are cycles in the graph structure.
</p>
<p>
Instead, this sample implements a "Button" with custom behavior to modify
the visibility of each Node. If this behavior is still not quite right for
your app, you can adapt the behavior implemented in the
<code>collapseFrom</code> and <code>expandFrom</code> functions to use
different criteria for when to stop recursion.
</p>
</div>
</div>
<!-- * * * * * * * * * * * * * -->
<!-- End of GoJS sample code -->
</div>
<div id="allTagDescriptions" class="p-4 w-full max-w-screen-xl mx-auto">
<hr/>
<h3 class="text-xl">GoJS Features in this sample</h3>
<!-- blacklist tags that do not correspond to a specific GoJS feature -->
<h4>Layered Digraph Layout</h4>
<p>
This predefined layout is used for placing Nodes of a general directed graph in layers (rows or columns). This is more general than <a href="../api/symbols/TreeLayout.html">TreeLayout</a>,
as it does not require that the graph be tree-structured.
More information can be found in the <a href="../intro/layouts.html#LayeredDigraphLayout">GoJS Intro</a>.
</p>
<p>
<a href="../samples/index.html#layered-digraph">Related samples</a>
</p>
<hr>
<!-- blacklist tags that do not correspond to a specific GoJS feature -->
<h4>Buttons</h4>
<p>
GoJS defines several <a href="../api/symbols/Panel.html" target="api">Panel</a>s for common uses.
These include "Button", "TreeExpanderButton", "SubGraphExpanderButton", "PanelExpanderButton", "ContextMenuButton", and "CheckBoxButton".
"ContextMenuButton"s are typically used inside of "ContextMenu" Panels;
"CheckBoxButton"s are used in the implementation of "CheckBox" Panels.
</p>
<p>
These predefined panels can be used as if they were <a href="../api/symbols/Panel.html" target="api">Panel</a>-derived classes in calls to <a href="../api/symbols/GraphObject.html#build" target="api">GraphObject.build</a>.
They are implemented as simple visual trees of <a href="../api/symbols/GraphObject.html" target="api">GraphObject</a>s in <a href="../api/symbols/Panel.html" target="api">Panel</a>s,
with pre-set properties and event handlers.
</p>
<p>
More information can be found in the <a href="../intro/buttons.html">GoJS Intro</a>.
</p>
<p>
<a href="../samples/index.html#buttons">Related samples</a>
</p>
<hr>
</div>
</div>
</body>
<!-- This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>