UNPKG

create-gojs-kit

Version:

A CLI for downloading GoJS samples, extensions, and docs

401 lines (358 loc) 17.3 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 editable finite state machine chart that saves incremental changes." /> <meta itemprop="description" content="An editable finite state machine chart that saves incremental changes." /> <meta property="og:description" content="An editable finite state machine chart that saves incremental changes." /> <meta name="twitter:description" content="An editable finite state machine chart that saves incremental changes." /> <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="State Chart Editor Saving Incremental JSON" /> <meta property="og:title" content="State Chart Editor Saving Incremental JSON" /> <meta name="twitter:title" content="State Chart Editor Saving Incremental JSON" /> <meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/statechart.png" /> <meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/statechart.png" /> <meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/statechart.png" /> <meta property="og:url" content="https://gojs.net/latest/samples/stateChartIncremental.html" /> <meta property="twitter:url" content="https://gojs.net/latest/samples/stateChartIncremental.html" /> <meta name="twitter:card" content="summary_large_image" /> <meta property="og:type" content="website" /> <meta property="twitter:domain" content="gojs.net" /> <title> State Chart Editor Saving Incremental JSON | 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> <link rel="stylesheet" href="../assets/css/prism.css"/> <script src="../assets/js/prism.js"></script> <div id="allSampleContent" class="p-4 w-full"> <script id="code"> function init() { myDiagram = new go.Diagram('myDiagramDiv', { // have mouse wheel events zoom in and out instead of scroll up and down 'toolManager.mouseWheelBehavior': go.WheelMode.Zoom, // support double-click in background creating a new node 'clickCreatingTool.archetypeNodeData': { text: 'new node' }, ModelChanged: e => { if (e.change === go.ChangeType.Transaction && e.propertyName === 'CommittedTransaction' && e.oldValue !== 'Initial Layout') { // this records each Transaction as a JSON-format string showIncremental(myDiagram.model.toIncrementalJson(e)); } }, // enable undo & redo '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); } }); // define the Node template myDiagram.nodeTemplate = new go.Node('Auto') .bindTwoWay('location', 'loc', go.Point.parse, go.Point.stringify) .add( // define the node's outer shape, which will surround the TextBlock new go.Shape('RoundedRectangle', { parameter1: 20, // the corner has a large radius fill: new go.Brush('Linear', { 0: 'rgb(254, 201, 0)', 1: 'rgb(254, 162, 0)' }), stroke: 'black', portId: '', fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true, toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true, cursor: 'pointer' }), new go.TextBlock({ font: 'bold 11pt helvetica, bold arial, sans-serif', editable: true // editing the text automatically updates the model data }) .bindTwoWay('text', 'text') ); // unlike the normal selection Adornment, this one includes a Button myDiagram.nodeTemplate.selectionAdornmentTemplate = new go.Adornment('Spot') .add( new go.Panel('Auto') .add( new go.Shape({ fill: null, stroke: 'blue', strokeWidth: 2 }), new go.Placeholder() // this represents the selected Node ), // the button to create a "next" node, at the top-right corner go.GraphObject.build('Button', { alignment: go.Spot.TopRight, click: addNodeAndLink // this function is defined below }) // end button .add(new go.Shape('PlusLine', { desiredSize: new go.Size(6, 6) })) ); // end Adornment // clicking the button inserts a new node to the right of the selected node, // and adds a link to that new node function addNodeAndLink(e, obj) { var adorn = obj.part; e.handled = true; var diagram = adorn.diagram; diagram.startTransaction('Add State'); // get the node data for which the user clicked the button var fromNode = adorn.adornedPart; var fromData = fromNode.data; // create a new "State" data object, positioned off to the right of the adorned Node var toData = { text: 'new' }; var p = fromNode.location.copy(); p.x += 200; toData.loc = go.Point.stringify(p); // the "loc" property is a string, not a Point object // add the new node data to the model var model = diagram.model; model.addNodeData(toData); // create a link data from the old node data to the new node data var linkdata = { from: model.getKeyForNodeData(fromData), // or just: fromData.id to: model.getKeyForNodeData(toData), text: 'transition' }; // and add the link data to the model model.addLinkData(linkdata); // select the new Node var newnode = diagram.findNodeForData(toData); diagram.select(newnode); diagram.commitTransaction('Add State'); // if the new node is off-screen, scroll the diagram to show the new node if (!diagram.viewportBounds.containsRect(newnode.actualBounds)) { diagram.commandHandler.scrollToPart(newnode); } } // replace the default Link template in the linkTemplateMap myDiagram.linkTemplate = new go.Link({ // the whole link panel curve: go.Curve.Bezier, adjusting: go.LinkAdjusting.Stretch, reshapable: true, relinkableFrom: true, relinkableTo: true }) .bindTwoWay('points') .bind('curviness', 'curviness') .add( new go.Shape({ strokeWidth: 1.5 }), // the link shape new go.Shape({ toArrow: 'standard', stroke: null }), // the arrowhead new go.Panel('Auto') .add( new go.Shape({ // the label background, which becomes transparent around the edges fill: new go.Brush('Radial', { 0: 'rgb(240, 240, 240)', 0.3: 'rgb(240, 240, 240)', 1: 'rgba(240, 240, 240, 0)' }), stroke: null }), new go.TextBlock('transition', { // the label text textAlign: 'center', font: '10pt helvetica, arial, sans-serif', stroke: 'black', margin: 4, editable: true // editing the text automatically updates the model data }) .bindTwoWay('text', 'text') ) ); // read in the JSON-format data from the "mySavedModel" element load(); } // Show the diagram's model in JSON format function save() { document.getElementById('mySavedModel').value = myDiagram.model.toJson(); myDiagram.isModified = false; showIncremental(''); } function load() { var model = go.Model.fromJson(document.getElementById('mySavedModel').value); // establish GraphLinksModel functions: // node data id's are odd numbers model.makeUniqueKeyFunction = (model, data) => { var i = model.nodeDataArray.length * 2 + 1; while (model.findNodeDataForKey(i) !== null) i += 2; data.id = i; // assume Model.nodeKeyProperty === "id" return i; }; // link data id's are even numbers model.makeUniqueLinkKeyFunction = (model, data) => { var i = model.linkDataArray.length * 2 + 2; while (model.findLinkDataForKey(i) !== null) i += 2; data.id = i; // assume GraphLinksModel.linkKeyProperty === "id" return i; }; myDiagram.model = model; showIncremental(''); } function showIncremental(str) { console.log(str) // show the last transaction as an incremental update in JSON-formatted text var element = document.getElementById('myTransaction'); element.innerHTML = str; if (window.Prism) window.Prism.highlightAll(); } window.addEventListener('DOMContentLoaded', init); </script> <div id="sample"> <div id="myDiagramDiv" style="background-color: whitesmoke; border: solid 1px black; width: 100%; height: 400px"></div> <p> This sample is derived from the <a href="stateChart.html">State Chart</a> sample. This makes use of the new <a>GraphLinksModel.linkKeyProperty</a> property and the <a>Model.toIncrementalJson</a> and <a>Model.applyIncrementalJson</a> methods. It also demonstrates custom functions for <a>Model.makeUniqueKeyFunction</a> and <a>GraphLinksModel.makeUniqueLinkKeyFunction</a>, which assign odd numbers to new node data and even numbers to new link data. Unlike most models, this example uses "id" as the name of the <a>Model.nodeKeyProperty</a> rather than "key". </p> Last <a>Transaction</a> saved in incremental JSON format: <pre class="lang-js"><code id="myTransaction"></code></pre> <button id="SaveButton" onclick="save()">Save</button> <button onclick="load()">Load</button> Diagram Model saved in JSON format: <br /> <textarea id="mySavedModel" style="width: 100%; height: 300px"> { "class": "go.GraphLinksModel", "nodeKeyProperty": "id", "linkKeyProperty": "id", "pointsDigits": 1, "nodeDataArray": [ { "id": 1, "loc": "120 120", "text": "Initial" }, { "id": 3, "loc": "330 120", "text": "First down" }, { "id": 5, "loc": "226 376", "text": "First up" }, { "id": 7, "loc": "60 276", "text": "Second down" }, { "id": 9, "loc": "226 226", "text": "Wait" } ], "linkDataArray": [ { "id": 2, "from": 1, "to": 1, "text": "up or timer", "curviness": -20 }, { "id": 4, "from": 1, "to": 3, "text": "down", "curviness": 20 }, { "id": 6, "from": 3, "to": 1, "text": "up (moved)\nPOST", "curviness": 20 }, { "id": 8, "from": 3, "to": 3, "text": "down", "curviness": -20 }, { "id": 10, "from": 3, "to": 5, "text": "up (no move)" }, { "id": 12, "from": 3, "to": 9, "text": "timer" }, { "id": 14, "from": 5, "to": 1, "text": "timer\nPOST" }, { "id": 16, "from": 5, "to": 7, "text": "down" }, { "id": 18, "from": 7, "to": 1, "text": "up\nPOST\n(dblclick\nif no move)" }, { "id": 20, "from": 7, "to": 7, "text": "down or timer", "curviness": 20 }, { "id": 22, "from": 9, "to": 1, "text": "up\nPOST" }, { "id": 24, "from": 9, "to": 9, "text": "down" } ] } </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>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> <!-- blacklist tags that do not correspond to a specific GoJS feature --> </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>