create-gojs-kit
Version:
A CLI for downloading GoJS samples, extensions, and docs
293 lines (259 loc) • 11.4 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="Example custom PanelLayout." />
<meta itemprop="description" content="Example custom PanelLayout." />
<meta property="og:description" content="Example custom PanelLayout." />
<meta name="twitter:description" content="Example custom PanelLayout." />
<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 Panel Layout: Cascading Elements" />
<meta property="og:title" content="Custom Panel Layout: Cascading Elements" />
<meta name="twitter:title" content="Custom Panel Layout: Cascading Elements" />
<meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/panellayout.png" />
<meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/panellayout.png" />
<meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/panellayout.png" />
<meta property="og:url" content="https://gojs.net/latest/samples/panelLayout.html" />
<meta property="twitter:url" content="https://gojs.net/latest/samples/panelLayout.html" />
<meta name="twitter:card" content="summary_large_image" />
<meta property="og:type" content="website" />
<meta property="twitter:domain" content="gojs.net" />
<title>
Custom Panel Layout: Cascading Elements | 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">
class PanelLayoutCascading extends go.PanelLayout {
constructor(init) {
super();
this.name = 'Cascading';
if (init) Object.assign(this, init);
}
/**
* Given the available size, measure the Panel and
* determine its expected drawing size. Sets the measuredBounds of the object.
*
* This must call {@link #measureElement} with each Panel element.
*
* This must also construct the union.width and union.height of the passed in union Rect argument.
*
* @this {PanelLayout}
* @param {Panel} panel Panel which called this layout
* @param {number} width expected width of the panel
* @param {number} height expected width of the panel
* @param {Array.<GraphObject>} elements Array of Panel elements
* @param {Rect} union rectangle to contain the expected union bounds of every element in the Panel. Useful for arrange.
* @param {number} minw minimum width of the panel
* @param {number} minh minimum height of the panel
*/
measure(panel, width, height, elements, union, minw, minh) {
const l = elements.length;
for (let i = 0; i < l; i++) {
const elem = elements[i];
this.measureElement(elem, width, height, minw, minh);
const mb = elem.measuredBounds;
union.width += mb.width;
union.height += mb.height;
}
}
/**
* Given the panel and its list of elements, arrange each element.
*
* This must call {@link #arrangeElement} with each Panel element, which will set that element's {@link GraphObject#actualBounds}.
*
* For arranging some elements, it is useful to know the total unioned area of every element.
* This Rect can be used to right-align or center-align, etc, elements within an area.
*
* @this {PanelLayout}
* @param {Panel} panel Panel which called this layout
* @param {Array.<GraphObject>} elements Array of Panel elements
* @param {Rect} union rectangle, if properly constructed in {@link #measure}, that contains the expected union bounds of every element in the Panel.
*/
arrange(panel, elements, union) {
const l = elements.length;
let x = 0;
let y = 0;
for (let i = 0; i < l; i++) {
const elem = elements[i];
const mb = elem.measuredBounds;
this.arrangeElement(elem, x, y, mb.width, mb.height);
/*
* By incrementing the arranged x and y by the width and height, we arrange each object in a diagonal fashion:
* A
* B
* C
* D
*/
x += mb.width;
y += mb.height;
}
}
}
// end of PanelLayoutCascading
// Register the PanelLayout so that go.GraphObject.build can use it by name:
go.Panel.definePanelLayout('Cascading', new PanelLayoutCascading());
function init() {
myDiagram = new go.Diagram('myDiagramDiv', { 'undoManager.isEnabled': true });
// Define a simple Node template that includes a Cascading Panel holding
// some number of item Panels holding Shapes, based on data.items being an Array
// of item descriptor objects.
myDiagram.nodeTemplate =
new go.Node('Auto', { width: 100, height: 100 })
.add(
new go.Shape({ fill: 'transparent', strokeWidth: 2 })
.bind('stroke', 'color'),
new go.Panel('Cascading', {
itemTemplate:
new go.Panel()
.add(
new go.Shape({ width: 20, height: 20, strokeWidth: 0 })
.bind('figure', 'fig')
.bind('fill', 'color')
)
})
.bind('itemArray', 'items')
);
myDiagram.model = new go.GraphLinksModel([
{
key: 1,
color: 'lightgreen',
items: [
{ fig: 'RoundedRectangle', color: 'lightblue' },
{ fig: 'Triangle', color: 'pink' }
]
},
{
key: 2,
color: 'lightblue',
items: [
{ fig: 'RoundedRectangle', color: 'lightgray' },
{ fig: 'Square', color: 'yellow' },
{ fig: 'Circle', color: 'orange' }
]
},
{
key: 3,
color: 'purple',
items: [{ fig: 'Diamond', color: 'red' }]
},
{
key: 4,
color: 'orange',
items: [
{ fig: 'Circle', color: 'green' },
{ fig: 'Square', color: 'blue' },
{ fig: 'Triangle', color: 'red' },
{ fig: 'TriangleRight', color: 'green' }
]
}
]);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<!-- The DIV for the Diagram needs an explicit size or else we won't see anything.
This also adds a border to help see the edges of the viewport. -->
<div id="myDiagramDiv" style="border: solid 1px black; width: 800px; height: 800px"></div>
<p>
This sample demonstrates creating a simple custom <a>PanelLayout</a>. It merely cascades the elements diagonally, as if combining a Horizontal and Vertical
panel.
</p>
<p>
Creating your own Panel layouts is very uncommon, and you should not need to create your own to use GoJS effectively. However there may be apps that require
creating a custom Panel layout because the standard panel layouts do not offer the precise behavior that you want.
</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>
</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>