UNPKG

gojs

Version:

Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams

311 lines (293 loc) 16.6 kB
<!DOCTYPE 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="TypeScript: Use the TableLayout extension to arrange nodes in a tabular or grid-like form."/> <link rel="stylesheet" href="../assets/css/style.css"/> <!-- Copyright 1998-2023 by Northwoods Software Corporation. --> <title>Northwoods Software</title> </head> <body> <!-- This top nav is not part of the sample code --> <nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary"> <div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2"> <div class="md:pl-4"> <a class="text-white hover:text-white no-underline hover:no-underline font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../"> <h1 class="my-0 p-1 ">GoJS</h1> </a> </div> <button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation"> <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6"> <path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path> <path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path> </svg> </button> <div id="topnavList" class="hidden sm:block items-center w-auto mt-0 text-white p-0 z-20"> <ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0"> <li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html" target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li> <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html" target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li> </ul> </div> </div> <hr class="border-b border-gray-600 opacity-50 my-0 py-0" /> </nav> <div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto"> <div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div> <!-- * * * * * * * * * * * * * --> <!-- Start of GoJS sample code --> <div id="allSampleContent" class="p-4 w-full"> <div id="sample"> <div style="width: 100%; display: flex; justify-content: space-between"> <div id="myPaletteDiv" style="width: 120px; height: 600px; margin-right: 2px; border: solid 1px black"></div> <div id="myDiagramDiv" style="flex-grow: 1; height: 600px; border: solid 1px black"></div> </div> <p> This sample demonstrates a custom Layout, TableLayout, that is very much like a simplified "Table" Panel layout, but working on non-Link Parts in a Diagram or a Group rather than on GraphObjects in a Panel. The layout is defined in its own file, as <a href="TableLayout.js" target="_blank">TableLayout.js</a>. </p> <p> You can drag-and-drop nodes from the Palette into any Group. Dragging into a Group highlights the Group. Drops must occur inside Groups; otherwise the action is cancelled. </p> <p> Each row and each column is <a>Part.resizable</a> and has a custom <a>Part.resizeAdornmentTemplate</a> showing a single resize handle on the right side or on the bottom. There is a custom LaneResizingTool to provide a minimum width or height based on the contents of all of the groups (cells) in that column or row. </p> <p> This example assumes the Groups are predefined and exist in each cell at a particular row/column, but this sample could be extended to allow adding and removing rows and/or columns. </p> </div> <script type="module" id="code"> import * as go from "../release/go-module.js"; import { TableLayout } from './TableLayout.js'; if (window.goSamples) goSamples(); // init for the samples -- you don't need to call this const $ = go.GraphObject.make; // for conciseness in defining templates // define a custom ResizingTool to limit how far one can shrink a row or column class LaneResizingTool extends go.ResizingTool { computeMinSize() { const diagram = this.diagram; if (this.adornedObject === null) return new go.Size(); const lane = this.adornedObject.part; // might be row or column if (lane === null) return new go.Size(); const horiz = (lane.category === 'Column Header'); // or "Row Header" const margin = diagram.nodeTemplate.margin; let bounds = new go.Rect(); diagram.findTopLevelGroups().each((g) => { if (lane === null) return; if (horiz ? (g.column === lane.column) : (g.row === lane.row)) { const b = diagram.computePartsBounds(g.memberParts); if (b.isEmpty()) return; // nothing in there? ignore it b.unionPoint(g.location); // keep any empty space on the left and top b.addMargin(margin); // assume the same node margin applies to all nodes if (bounds.isEmpty()) { bounds = b; } else { bounds.unionRect(b); } } }); // limit the result by the standard value of computeMinSize const msz = super.computeMinSize(); if (bounds.isEmpty()) return msz; return new go.Size(Math.max(msz.width, bounds.width), Math.max(msz.height, bounds.height)); } resize(newr) { const diagram = this.diagram; if (this.adornedObject === null) return; const lane = this.adornedObject.part; if (lane === null) return; const horiz = (lane.category === 'Column Header'); const lay = diagram.layout; // the TableLayout if (horiz) { const col = lane.column; const coldef = lay.getColumnDefinition(col); coldef.width = newr.width; } else { const row = lane.row; const rowdef = lay.getRowDefinition(row); rowdef.height = newr.height; } lay.invalidateLayout(); } } const myDiagram = new go.Diagram('myDiagramDiv', { layout: $(TableLayout, $(go.RowColumnDefinition, { row: 1, height: 22 }), // fixed size column headers $(go.RowColumnDefinition, { column: 1, width: 22 }) // fixed size row headers ), 'SelectionMoved': (e) => { e.diagram.layoutDiagram(true); }, 'resizingTool': new LaneResizingTool(), // feedback that dropping in the background is not allowed mouseDragOver: (e) => { e.diagram.currentCursor = 'not-allowed'; }, // when dropped in the background, not on a Node or a Group, cancel the drop mouseDrop: (e) => { e.diagram.currentTool.doCancel(); }, 'animationManager.isInitial': false, 'undoManager.isEnabled': true }); myDiagram.nodeTemplateMap.add('Header', // an overall table header, at the top $(go.Part, 'Auto', { row: 0, column: 1, columnSpan: 9999, stretch: go.GraphObject.Horizontal, selectable: false, pickable: false }, $(go.Shape, { fill: 'transparent', strokeWidth: 0 }), $(go.TextBlock, { alignment: go.Spot.Center, font: 'bold 12pt sans-serif' }, new go.Binding('text')))); myDiagram.nodeTemplateMap.add('Sider', // an overall table header, on the left side $(go.Part, 'Auto', { row: 1, rowSpan: 9999, column: 0, stretch: go.GraphObject.Vertical, selectable: false, pickable: false }, $(go.Shape, { fill: 'transparent', strokeWidth: 0 }), $(go.TextBlock, { alignment: go.Spot.Center, font: 'bold 12pt sans-serif', angle: 270 }, new go.Binding('text')))); myDiagram.nodeTemplateMap.add('Column Header', // for each column header $(go.Part, 'Spot', { row: 1, rowSpan: 9999, column: 2, minSize: new go.Size(100, NaN), stretch: go.GraphObject.Fill, movable: false, resizable: true, resizeAdornmentTemplate: $(go.Adornment, 'Spot', $(go.Placeholder), $(go.Shape, // for changing the length of a lane { alignment: go.Spot.Right, desiredSize: new go.Size(7, 50), fill: 'lightblue', stroke: 'dodgerblue', cursor: 'col-resize' })) }, new go.Binding('column', 'col'), $(go.Shape, { fill: null }, new go.Binding('fill', 'color')), $(go.Panel, 'Auto', { alignment: go.Spot.Top, alignmentFocus: go.Spot.Bottom, stretch: go.GraphObject.Horizontal, height: myDiagram.layout.getRowDefinition(1).height }, $(go.Shape, { fill: 'transparent', strokeWidth: 0 }), $(go.TextBlock, { font: 'bold 10pt sans-serif', isMultiline: false, wrap: go.TextBlock.None, overflow: go.TextBlock.OverflowEllipsis }, new go.Binding('text'))))); myDiagram.nodeTemplateMap.add('Row Sider', // for each row header $(go.Part, 'Spot', { row: 2, column: 1, columnSpan: 9999, minSize: new go.Size(NaN, 100), stretch: go.GraphObject.Fill, movable: false, resizable: true, resizeAdornmentTemplate: $(go.Adornment, 'Spot', $(go.Placeholder), $(go.Shape, // for changing the breadth of a lane { alignment: go.Spot.Bottom, desiredSize: new go.Size(50, 7), fill: 'lightblue', stroke: 'dodgerblue', cursor: 'row-resize' })) }, new go.Binding('row'), $(go.Shape, { fill: null }, new go.Binding('fill', 'color')), $(go.Panel, 'Auto', { alignment: go.Spot.Left, alignmentFocus: go.Spot.Right, stretch: go.GraphObject.Vertical, angle: 270, height: myDiagram.layout.getColumnDefinition(1).width }, $(go.Shape, { fill: 'transparent', strokeWidth: 0 }), $(go.TextBlock, { font: 'bold 10pt sans-serif', isMultiline: false, wrap: go.TextBlock.None, overflow: go.TextBlock.OverflowEllipsis }, new go.Binding('text'))))); myDiagram.nodeTemplate = // for regular nodes within cells (groups); you'll want to extend this $(go.Node, 'Auto', { width: 100, height: 50, margin: 4 }, // assume uniform Margin, all around new go.Binding('row'), new go.Binding('column', 'col'), $(go.Shape, { fill: 'white' }, new go.Binding('fill', 'color')), $(go.TextBlock, new go.Binding('text', 'key'))); myDiagram.groupTemplate = // for cells $(go.Group, 'Auto', { layerName: 'Background', stretch: go.GraphObject.Fill, selectable: false, computesBoundsAfterDrag: true, computesBoundsIncludingLocation: true, handlesDragDropForMembers: true, mouseDragEnter: (e, group) => { if (group instanceof go.Group) group.isHighlighted = true; }, mouseDragLeave: (e, group) => { if (group instanceof go.Group) group.isHighlighted = false; }, mouseDrop: (e, group) => { if (!(group instanceof go.Group)) return; // if any dropped part wasn't already a member of this group, we'll want to let the group's row // column allow themselves to be resized automatically, in case the row height or column width // had been set manually by the LaneResizingTool const anynew = e.diagram.selection.any((p) => p.containingGroup !== group); // Don't allow headers/siders to be dropped const anyHeadersSiders = e.diagram.selection.any((p) => { return p.category === 'Column Header' || p.category === 'Row Sider'; }); if (!anyHeadersSiders && group.addMembers(e.diagram.selection, true)) { if (anynew) { e.diagram.layout.getRowDefinition(group.row).height = NaN; e.diagram.layout.getColumnDefinition(group.column).width = NaN; } } else { // failure upon trying to add parts to this group e.diagram.currentTool.doCancel(); } } }, new go.Binding('row'), new go.Binding('column', 'col'), // the group is normally unseen -- it is completely transparent except when given a color or when highlighted $(go.Shape, { fill: 'transparent', stroke: 'transparent', strokeWidth: myDiagram.nodeTemplate.margin.left, stretch: go.GraphObject.Fill }, new go.Binding('fill', 'color'), new go.Binding('stroke', 'isHighlighted', (h) => h ? 'red' : 'transparent').ofObject()), $(go.Placeholder, { alignment: ((m) => new go.Spot(0, 0, m.top, m.left))(myDiagram.nodeTemplate.margin), padding: ((m) => new go.Margin(0, m.right, m.bottom, 0))(myDiagram.nodeTemplate.margin) })); myDiagram.model = new go.GraphLinksModel([ // headers { key: 'Header', text: 'Vacation Procedures', category: 'Header' }, { key: 'Sider', text: 'Personnel', category: 'Sider' }, // column and row headers { key: 'Request', text: 'Request', col: 2, category: 'Column Header' }, { key: 'Approval', text: 'Approval', col: 3, category: 'Column Header' }, { key: 'Employee', text: 'Employee', row: 2, category: 'Row Sider' }, { key: 'Manager', text: 'Manager', row: 3, category: 'Row Sider' }, { key: 'Administrator', text: 'Administrator', row: 4, category: 'Row Sider' }, // cells, each a group assigned to a row and column { key: 'EmpReq', row: 2, col: 2, isGroup: true, color: 'lightyellow' }, { key: 'EmpApp', row: 2, col: 3, isGroup: true, color: 'lightgreen' }, { key: 'ManReq', row: 3, col: 2, isGroup: true, color: 'lightgreen' }, { key: 'ManApp', row: 3, col: 3, isGroup: true, color: 'lightyellow' }, { key: 'AdmReq', row: 4, col: 2, isGroup: true, color: 'lightyellow' }, { key: 'AdmApp', row: 4, col: 3, isGroup: true, color: 'lightgreen' }, // nodes, each assigned to a group/cell { key: 'Delta', color: 'orange', size: '100 100', group: 'EmpReq' }, { key: 'Epsilon', color: 'coral', size: '100 50', group: 'EmpReq' }, { key: 'Zeta', color: 'tomato', size: '50 70', group: 'ManReq' }, { key: 'Eta', color: 'coral', size: '50 50', group: 'ManApp' }, { key: 'Theta', color: 'tomato', size: '100 50', group: 'AdmApp' } ]); const myPalette = new go.Palette('myPaletteDiv', { nodeTemplateMap: myDiagram.nodeTemplateMap, 'model.nodeDataArray': [ { key: 'Alpha', color: 'orange' }, { key: 'Beta', color: 'tomato' }, { key: 'Gamma', color: 'goldenrod' } ] }); window.myDiagram = myDiagram; // Attach to the window for console debugging </script> </div> <!-- * * * * * * * * * * * * * --> <!-- End of GoJS sample code --> </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>