UNPKG

create-gojs-kit

Version:

A CLI for downloading GoJS samples, extensions, and docs

693 lines (632 loc) 28.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="An editor of flow diagrams that supports deletion by dropping onto a particular node and relinking by dragging a link." /> <meta itemprop="description" content="An editor of flow diagrams that supports deletion by dropping onto a particular node and relinking by dragging a link." /> <meta property="og:description" content="An editor of flow diagrams that supports deletion by dropping onto a particular node and relinking by dragging a link." /> <meta name="twitter:description" content="An editor of flow diagrams that supports deletion by dropping onto a particular node and relinking by dragging a link." /> <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="Flow Builder for Building Acyclic Graphs" /> <meta property="og:title" content="Flow Builder for Building Acyclic Graphs" /> <meta name="twitter:title" content="Flow Builder for Building Acyclic Graphs" /> <meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/flowbuilder.png" /> <meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/flowbuilder.png" /> <meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/flowbuilder.png" /> <meta property="og:url" content="https://gojs.net/latest/samples/flowBuilder.html" /> <meta property="twitter:url" content="https://gojs.net/latest/samples/flowBuilder.html" /> <meta name="twitter:card" content="summary_large_image" /> <meta property="og:type" content="website" /> <meta property="twitter:domain" content="gojs.net" /> <title> Flow Builder for Building Acyclic 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"> // Define a custom tool that changes a drag operation on a Link to a relinking operation, // but that operates like a normal DraggingTool otherwise. class DragLinkingTool extends go.DraggingTool { constructor(init) { super(); this.isGridSnapEnabled = true; this.isGridSnapRealtime = false; this.gridSnapCellSize = new go.Size(182, 1); this.gridSnapOrigin = new go.Point(5.5, 0); if (init) Object.assign(this, init); } // Handle dragging a link specially -- by starting the RelinkingTool on that Link doActivate() { const diagram = this.diagram; if (diagram === null) return; this.standardMouseSelect(); const main = this.currentPart; // this is set by the standardMouseSelect if (main instanceof go.Link) { // maybe start relinking instead of dragging const relinkingtool = diagram.toolManager.relinkingTool; // tell the RelinkingTool to work on this Link, not what is under the mouse relinkingtool.originalLink = main; // start the RelinkingTool diagram.currentTool = relinkingtool; // can activate it right now, because it already has the originalLink to reconnect relinkingtool.doActivate(); relinkingtool.doMouseMove(); } else { super.doActivate(); } } } // end DragLinkingTool function init() { myDiagram = new go.Diagram('myDiagramDiv', { allowCopy: false, layout: new go.LayeredDigraphLayout({ setsPortSpots: false, // Links already know their fromSpot and toSpot columnSpacing: 5, isInitial: false, isOngoing: false }), validCycle: go.CycleMode.NotDirected, 'undoManager.isEnabled': true }); // when the document is modified, add a "*" to the title and enable the "Save" button myDiagram.addDiagramListener('Modified', e => { const button = document.getElementById('SaveButton'); if (button) button.disabled = !myDiagram.isModified; const idx = document.title.indexOf('*'); if (myDiagram.isModified) { if (idx < 0) document.title += '*'; } else { if (idx >= 0) document.title = document.title.slice(0, idx); } }); const graygrad = new go.Brush('Linear', { 0: 'white', 0.1: 'whitesmoke', 0.9: 'whitesmoke', 1: 'lightgray' }); myDiagram.nodeTemplate = new go.Node('Spot', { // the default node template selectionAdorned: false, textEditable: true, locationObjectName: 'BODY' }) .bindTwoWay('location', 'loc', go.Point.parse, go.Point.stringify) // the main body consists of a Rectangle surrounding the text .add( new go.Panel('Auto', { name: 'BODY' }) .add( new go.Shape('Rectangle', { fill: graygrad, stroke: 'gray', minSize: new go.Size(120, 21) }) .bindObject('fill', 'isSelected', s => s ? 'dodgerblue' : graygrad), new go.TextBlock({ stroke: 'black', font: '12px sans-serif', editable: true, margin: new go.Margin(3, 3 + 11, 3, 3 + 4), alignment: go.Spot.Left }) .bindTwoWay('text') ), // output port new go.Panel('Auto', { alignment: go.Spot.Right, portId: 'from', fromLinkable: true, cursor: 'pointer', click: addNodeAndLink }) .add( new go.Shape('Circle', { width: 22, height: 22, fill: 'white', stroke: 'dodgerblue', strokeWidth: 3 }), new go.Shape('PlusLine', { width: 11, height: 11, fill: null, stroke: 'dodgerblue', strokeWidth: 3 }) ), // input port new go.Panel('Auto', { alignment: go.Spot.Left, portId: 'to', toLinkable: true }) .add( new go.Shape('Circle', { width: 8, height: 8, fill: 'white', stroke: 'gray' }), new go.Shape('Circle', { width: 4, height: 4, fill: 'dodgerblue', stroke: null }) ) ); myDiagram.nodeTemplate.contextMenu = go.GraphObject.build('ContextMenu') .add( go.GraphObject.build('ContextMenuButton', { click: (e, obj) => e.diagram.commandHandler.editTextBlock() }) .bindObject('visible', '', o => o.diagram && o.diagram.commandHandler.canEditTextBlock()) .add(new go.TextBlock('Rename')), // add one for Editing... go.GraphObject.build('ContextMenuButton', { click: (e, obj) => e.diagram.commandHandler.deleteSelection() }) .bindObject('visible', '', o => o.diagram && o.diagram.commandHandler.canDeleteSelection()) .add(new go.TextBlock('Delete')) ); myDiagram.nodeTemplateMap.add('Loading', new go.Node('Spot', { selectionAdorned: false, textEditable: true, locationObjectName: 'BODY' }) .bindTwoWay('location', 'loc', go.Point.parse, go.Point.stringify) .add( // the main body consists of a Rectangle surrounding the text new go.Panel('Auto', { name: 'BODY' }) .add( new go.Shape('Rectangle', { fill: graygrad, stroke: 'gray', minSize: new go.Size(120, 21) }) .bindObject('fill', 'isSelected', s => s ? 'dodgerblue' : graygrad), new go.TextBlock({ stroke: 'black', font: '12px sans-serif', editable: true, margin: new go.Margin(3, 3 + 11, 3, 3 + 4), alignment: go.Spot.Left }) .bind('text') ), // output port new go.Panel('Auto', { alignment: go.Spot.Right, portId: 'from', fromLinkable: true, click: addNodeAndLink }) .add( new go.Shape('Circle', { width: 22, height: 22, fill: 'white', stroke: 'dodgerblue', strokeWidth: 3 }), new go.Shape('PlusLine', { width: 11, height: 11, fill: null, stroke: 'dodgerblue', strokeWidth: 3 }) ) ) ); myDiagram.nodeTemplateMap.add('End', new go.Node('Spot', { selectionAdorned: false, textEditable: true, locationObjectName: 'BODY' }) .bindTwoWay('location', 'loc', go.Point.parse, go.Point.stringify) .add( // the main body consists of a Rectangle surrounding the text new go.Panel('Auto', { name: 'BODY' }) .add( new go.Shape('Rectangle', { fill: graygrad, stroke: 'gray', minSize: new go.Size(120, 21) }) .bindObject('fill', 'isSelected', s => s ? 'dodgerblue' : graygrad), new go.TextBlock({ stroke: 'black', font: '12px sans-serif', editable: true, margin: new go.Margin(3, 3 + 11, 3, 3 + 4), alignment: go.Spot.Left }) .bind('text') ), // input port new go.Panel('Auto', { alignment: go.Spot.Left, portId: 'to', toLinkable: true }) .add( new go.Shape('Circle', { width: 8, height: 8, fill: 'white', stroke: 'gray' }), new go.Shape('Circle', { width: 4, height: 4, fill: 'dodgerblue', stroke: null }) ) ) ); // dropping a node on this special node will cause the selection to be deleted; // linking or relinking to this special node will cause the link to be deleted myDiagram.nodeTemplateMap.add('Recycle', new go.Node('Auto', { portId: 'to', toLinkable: true, deletable: false, layerName: 'Background', locationSpot: go.Spot.Center, dragComputation: (node, pt, gridpt) => pt, mouseDrop: (e, obj) => e.diagram.commandHandler.deleteSelection() }) .bindTwoWay('location', 'loc', go.Point.parse, go.Point.stringify) .add( new go.Shape({ fill: 'lightgray', stroke: 'gray' }), new go.TextBlock('Drop Here\nTo Delete', { margin: 5, textAlign: 'center' }) ) ); // this is a click event handler that adds a node and a link to the diagram, // connecting with the node on which the click occurred function addNodeAndLink(e, obj) { const fromNode = obj.part; const diagram = fromNode.diagram; diagram.startTransaction('Add State'); // get the node data for which the user clicked the button const fromData = fromNode.data; // create a new "State" data object, positioned off to the right of the fromNode const p = fromNode.location.copy(); p.x += diagram.toolManager.draggingTool.gridSnapCellSize.width; const toData = { text: 'new', loc: go.Point.stringify(p) }; // add the new node data to the model const model = diagram.model; model.addNodeData(toData); // create a link data from the old node data to the new node data const linkdata = { from: model.getKeyForNodeData(fromData), to: model.getKeyForNodeData(toData) }; // and add the link data to the model model.addLinkData(linkdata); // select the new Node const newnode = diagram.findNodeForData(toData); diagram.select(newnode); // snap the new node to a valid location newnode.location = diagram.toolManager.draggingTool.computeMove(newnode, p); // then account for any overlap shiftNodesToEmptySpaces(); diagram.commitTransaction('Add State'); } // Highlight ports when they are targets for linking or relinking. let OldTarget = null; // remember the last highlit port function highlight(port) { if (OldTarget !== port) { lowlight(); // remove highlight from any old port OldTarget = port; port.scale = 1.3; // highlight by enlarging } } function lowlight() { // remove any highlight if (OldTarget) { OldTarget.scale = 1.0; OldTarget = null; } } // Connecting a link with the Recycle node removes the link myDiagram.addDiagramListener('LinkDrawn', e => { const link = e.subject; if (link.toNode.category === 'Recycle') myDiagram.remove(link); lowlight(); }); myDiagram.addDiagramListener('LinkRelinked', e => { const link = e.subject; if (link.toNode.category === 'Recycle') myDiagram.remove(link); lowlight(); }); myDiagram.linkTemplate = new go.Link({ selectionAdorned: false, fromPortId: 'from', toPortId: 'to', relinkableTo: true }) .add( new go.Shape({ stroke: 'gray', strokeWidth: 2, mouseEnter: (e, obj) => { obj.strokeWidth = 5; obj.stroke = 'dodgerblue'; }, mouseLeave: (e, obj) => { obj.strokeWidth = 2; obj.stroke = 'gray'; } }) ); function commonLinkingToolInit(tool) { // the temporary link drawn during a link drawing operation (LinkingTool) is thick and blue tool.temporaryLink = new go.Link({ layerName: 'Tool' }) .add( new go.Shape({ stroke: 'dodgerblue', strokeWidth: 5 }) ); // change the standard proposed ports feedback from blue rectangles to transparent circles tool.temporaryFromPort.figure = 'Circle'; tool.temporaryFromPort.stroke = null; tool.temporaryFromPort.strokeWidth = 0; tool.temporaryToPort.figure = 'Circle'; tool.temporaryToPort.stroke = null; tool.temporaryToPort.strokeWidth = 0; // provide customized visual feedback as ports are targeted or not tool.portTargeted = (realnode, realport, tempnode, tempport, toend) => { if (realport === null) { // no valid port nearby lowlight(); } else if (toend) { highlight(realport); } }; } const ltool = myDiagram.toolManager.linkingTool; commonLinkingToolInit(ltool); // do not allow links to be drawn starting at the "to" port ltool.direction = go.LinkingDirection.ForwardsOnly; const rtool = myDiagram.toolManager.relinkingTool; commonLinkingToolInit(rtool); // change the standard relink handle to be a shape that takes the shape of the link rtool.toHandleArchetype = new go.Shape({ isPanelMain: true, fill: null, stroke: 'dodgerblue', strokeWidth: 5 }); // use a special DraggingTool to cause the dragging of a Link to start relinking it myDiagram.toolManager.draggingTool = new DragLinkingTool(); // detect when dropped onto an occupied cell myDiagram.addDiagramListener('SelectionMoved', shiftNodesToEmptySpaces); function shiftNodesToEmptySpaces() { myDiagram.selection.each(node => { if (!(node instanceof go.Node)) return; // look for Parts overlapping the node while (true) { const exist = myDiagram .findObjectsIn( node.actualBounds, // only consider Parts obj => obj.part, // ignore Links and the dropped node itself part => part instanceof go.Node && part !== node, // check for any overlap, not complete containment true ) .first(); if (exist === null) break; // try shifting down beyond the existing node to see if there's empty space node.moveTo(node.actualBounds.x, exist.actualBounds.bottom + 10); } }); } // prevent nodes from being dragged to the left of where the layout placed them myDiagram.addDiagramListener('LayoutCompleted', e => { myDiagram.nodes.each(node => { if (node.category === 'Recycle') return; node.minLocation = new go.Point(node.location.x, -Infinity); }); }); load(); // load initial diagram from the mySavedModel textarea } function save() { document.getElementById('mySavedModel').value = myDiagram.model.toJson(); myDiagram.isModified = false; } function load() { myDiagram.model = go.Model.fromJson(document.getElementById('mySavedModel').value); // if any nodes don't have a real location, explicitly do a layout if (myDiagram.nodes.any(n => !n.location.isReal())) layout(); } function layout() { myDiagram.layoutDiagram(true); } window.addEventListener('DOMContentLoaded', init); </script> <div id="sample"> <div id="myDiagramDiv" style="border: solid 1px black; width: 100%; height: 500px"></div> <button onclick="layout()">Diagram Layout</button> <p> This sample let's users construct a flowing diagrmam with a few quality of life features. <a>Node</a>s can be deleted by dragging them over the "Drop Here To Delete" box. </p> <p> This sample implements a custom DragLinkingTool that allows <a>Link</a>s to be re-linked by dragging them to different <a>Node</a>s. </p> <p> To create a new <a>Node</a>, click the "+". This will create and link a new <a>Node</a>. </p> <p> When <a>Nodes</a> are dragged to new locations they will be snapped to the nearest lane. </p> <p> All <a>Node</a>s <a>TextBlock</a>s are editable. </p> <button id="SaveButton" onclick="save()">Save</button> <button onclick="load()">Load</button> <br /> <textarea id="mySavedModel" style="width: 100%; height: 300px"> { "class": "go.GraphLinksModel", "nodeDataArray": [ { "key":1, "text":"Loading Screen", "category":"Loading" }, { "key":2, "text":"Beginning" }, { "key":3, "text":"Segment 1" }, { "key":4, "text":"Segment 2" }, { "key":5, "text":"Segment 3"}, { "key":6, "text":"End Screen", "category":"End" }, { "key":-2, "category": "Recycle" } ], "linkDataArray": [ { "from":1, "to":2 }, { "from":2, "to":3 }, { "from":2, "to":5 }, { "from":3, "to":4 }, { "from":4, "to":6 } ] } </textarea> </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>Context Menus</h4> <p> A GoJS context menu is an <a href="../api/symbols/Adornment.html" target="api">Adornment</a> that is shown when the user context-clicks (right mouse click or long touch hold) an object that has its <a href="../api/symbols/GraphObject.html#contextMenu" target="api">GraphObject.contextMenu</a> set. The context menu is bound to the same data as the part itself. </p> <p> It is typical to implement a context menu as a "ContextMenu" Panel containing "ContextMenuButton"s, as you can see in the code below in the assignment of the Node's <a href="../api/symbols/GraphObject.html#contextMenu" target="api">GraphObject.contextMenu</a> and <a href="../api/symbols/Diagram.html#contextMenu" target="api">Diagram.contextMenu</a> properties. Each "ContextMenu" is just a "Vertical" Panel <a href="../api/symbols/Adornment.html" target="api">Adornment</a> that is shadowed. Each "ContextMenuButton" is a Panel on which you can set the <a href="../api/symbols/GraphObject.html#click" target="api">GraphObject.click</a> event handler. In the event handler <code>obj.part</code> will be the whole context menu Adornment. <code>obj.part.adornedPart</code> will be the adorned Node or Link. The bound data is <code>obj.part.data</code>, which will be the same as <code>obj.part.adornedPart.data</code>. </p> <p> More information can be found in the <a href="../intro/contextmenus.html">GoJS Intro</a>. </p> <p> <a href="../samples/index.html#contextmenus">Related samples</a> </p> <hr> <!-- blacklist tags that do not correspond to a specific GoJS feature --> <h4>Tools</h4> <p> <a href="../api/symbols/Tool.html" target="api">Tool</a>s handle all input events, such as mouse and keyboard interactions, in a Diagram. There are many kinds of predefined Tool classes that implement all of the common operations that users do. </p> <p> For flexibility and simplicity, all input events are canonicalized as <a href="../api/symbols/InputEvent.html" target="api">InputEvent</a>s and redirected by the diagram to go to the <a href="../api/symbols/Diagram.html#currentTool" target="api">Diagram.currentTool</a>. By default the Diagram.currentTool is an instance of <a href="../api/symbols/ToolManager.html" target="api">ToolManager</a> held as the <a href="../api/symbols/Diagram.html#toolManager" target="api">Diagram.toolManager</a>. The ToolManager implements support for all mode-less tools. The ToolManager is responsible for finding another tool that is ready to run and then making it the new current tool. This causes the new tool to process all of the input events (mouse, keyboard, and touch) until the tool decides that it is finished, at which time the diagram's current tool reverts back to the <a href="../api/symbols/Diagram.html#defaultTool" target="api">Diagram.defaultTool</a>, which is normally the ToolManager, again. </p> <p> More information can be found in the <a href="../intro/tools.html">GoJS Intro</a>. </p> <p> <a href="../samples/index.html#tools">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>