UNPKG

gojs

Version:

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

333 lines (307 loc) 17.1 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="Interactive GoJS diagram demonstrating creating new nodes and links, reconnecting links, grouping and ungrouping, and context menus and tooltips for nodes, for links, and for the diagram background."/> <link rel="stylesheet" href="../assets/css/style.css"/> <!-- Copyright 1998-2023 by Northwoods Software Corporation. --> <title>Basic GoJS Sample</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 --> <script src="../release/go.js"></script> <div id="allSampleContent" class="p-4 w-full"> <script id="code"> function init() { // Since 2.2 you can also author concise templates with method chaining instead of GraphObject.make // For details, see https://gojs.net/latest/intro/buildingObjects.html const $ = go.GraphObject.make; // for conciseness in defining templates myDiagram = new go.Diagram("myDiagramDiv", // create a Diagram for the DIV HTML element { // allow double-click in background to create a new node "clickCreatingTool.archetypeNodeData": { text: "Node", color: "white" }, // allow Ctrl-G to call groupSelection() "commandHandler.archetypeGroupData": { text: "Group", isGroup: true, color: "blue" }, // enable undo & redo "undoManager.isEnabled": true }); // Define the appearance and behavior for Nodes: // First, define the shared context menu for all Nodes, Links, and Groups. // To simplify this code we define a function for creating a context menu button: function makeButton(text, action, visiblePredicate) { return $("ContextMenuButton", $(go.TextBlock, text), { click: action }, // don't bother with binding GraphObject.visible if there's no predicate visiblePredicate ? new go.Binding("visible", "", (o, e) => o.diagram ? visiblePredicate(o, e) : false).ofObject() : {}); } // a context menu is an Adornment with a bunch of buttons in them var partContextMenu = $("ContextMenu", makeButton("Properties", (e, obj) => { // OBJ is this Button var contextmenu = obj.part; // the Button is in the context menu Adornment var part = contextmenu.adornedPart; // the adornedPart is the Part that the context menu adorns // now can do something with PART, or with its data, or with the Adornment (the context menu) if (part instanceof go.Link) alert(linkInfo(part.data)); else if (part instanceof go.Group) alert(groupInfo(contextmenu)); else alert(nodeInfo(part.data)); }), makeButton("Cut", (e, obj) => e.diagram.commandHandler.cutSelection(), o => o.diagram.commandHandler.canCutSelection()), makeButton("Copy", (e, obj) => e.diagram.commandHandler.copySelection(), o => o.diagram.commandHandler.canCopySelection()), makeButton("Paste", (e, obj) => e.diagram.commandHandler.pasteSelection(e.diagram.toolManager.contextMenuTool.mouseDownPoint), o => o.diagram.commandHandler.canPasteSelection(o.diagram.toolManager.contextMenuTool.mouseDownPoint)), makeButton("Delete", (e, obj) => e.diagram.commandHandler.deleteSelection(), o => o.diagram.commandHandler.canDeleteSelection()), makeButton("Undo", (e, obj) => e.diagram.commandHandler.undo(), o => o.diagram.commandHandler.canUndo()), makeButton("Redo", (e, obj) => e.diagram.commandHandler.redo(), o => o.diagram.commandHandler.canRedo()), makeButton("Group", (e, obj) => e.diagram.commandHandler.groupSelection(), o => o.diagram.commandHandler.canGroupSelection()), makeButton("Ungroup", (e, obj) => e.diagram.commandHandler.ungroupSelection(), o => o.diagram.commandHandler.canUngroupSelection()) ); function nodeInfo(d) { // Tooltip info for a node data object var str = "Node " + d.key + ": " + d.text + "\n"; if (d.group) str += "member of " + d.group; else str += "top-level node"; return str; } // These nodes have text surrounded by a rounded rectangle // whose fill color is bound to the node data. // The user can drag a node by dragging its TextBlock label. // Dragging from the Shape will start drawing a new link. myDiagram.nodeTemplate = $(go.Node, "Auto", { locationSpot: go.Spot.Center }, $(go.Shape, "RoundedRectangle", { fill: "white", // the default fill, if there is no data bound value portId: "", cursor: "pointer", // the Shape is the port, not the whole Node // allow all kinds of links from and to this port fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true, toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true }, new go.Binding("fill", "color")), $(go.TextBlock, { font: "bold 14px sans-serif", stroke: '#333', margin: 6, // make some extra space for the shape around the text isMultiline: false, // don't allow newlines in text editable: true // allow in-place editing by user }, new go.Binding("text", "text").makeTwoWay()), // the label shows the node data's text { // this tooltip Adornment is shared by all nodes toolTip: $("ToolTip", $(go.TextBlock, { margin: 4 }, // the tooltip shows the result of calling nodeInfo(data) new go.Binding("text", "", nodeInfo)) ), // this context menu Adornment is shared by all nodes contextMenu: partContextMenu } ); // Define the appearance and behavior for Links: function linkInfo(d) { // Tooltip info for a link data object return "Link:\nfrom " + d.from + " to " + d.to; } // The link shape and arrowhead have their stroke brush data bound to the "color" property myDiagram.linkTemplate = $(go.Link, { toShortLength: 3, relinkableFrom: true, relinkableTo: true }, // allow the user to relink existing links $(go.Shape, { strokeWidth: 2 }, new go.Binding("stroke", "color")), $(go.Shape, { toArrow: "Standard", stroke: null }, new go.Binding("fill", "color")), { // this tooltip Adornment is shared by all links toolTip: $("ToolTip", $(go.TextBlock, { margin: 4 }, // the tooltip shows the result of calling linkInfo(data) new go.Binding("text", "", linkInfo)) ), // the same context menu Adornment is shared by all links contextMenu: partContextMenu } ); // Define the appearance and behavior for Groups: function groupInfo(adornment) { // takes the tooltip or context menu, not a group node data object var g = adornment.adornedPart; // get the Group that the tooltip adorns var mems = g.memberParts.count; var links = 0; g.memberParts.each(part => { if (part instanceof go.Link) links++; }); return "Group " + g.data.key + ": " + g.data.text + "\n" + mems + " members including " + links + " links"; } // Groups consist of a title in the color given by the group node data // above a translucent gray rectangle surrounding the member parts myDiagram.groupTemplate = $(go.Group, "Vertical", { selectionObjectName: "PANEL", // selection handle goes around shape, not label ungroupable: true // enable Ctrl-Shift-G to ungroup a selected Group }, $(go.TextBlock, { //alignment: go.Spot.Right, font: "bold 19px sans-serif", isMultiline: false, // don't allow newlines in text editable: true // allow in-place editing by user }, new go.Binding("text", "text").makeTwoWay(), new go.Binding("stroke", "color")), $(go.Panel, "Auto", { name: "PANEL" }, $(go.Shape, "Rectangle", // the rectangular shape around the members { fill: "rgba(128,128,128,0.2)", stroke: "gray", strokeWidth: 3, portId: "", cursor: "pointer", // the Shape is the port, not the whole Node // allow all kinds of links from and to this port fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true, toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true }), $(go.Placeholder, { margin: 10, background: "transparent" }) // represents where the members are ), { // this tooltip Adornment is shared by all groups toolTip: $("ToolTip", $(go.TextBlock, { margin: 4 }, // bind to tooltip, not to Group.data, to allow access to Group properties new go.Binding("text", "", groupInfo).ofObject()) ), // the same context menu Adornment is shared by all groups contextMenu: partContextMenu } ); // Define the behavior for the Diagram background: function diagramInfo(model) { // Tooltip info for the diagram's model return "Model:\n" + model.nodeDataArray.length + " nodes, " + model.linkDataArray.length + " links"; } // provide a tooltip for the background of the Diagram, when not over any Part myDiagram.toolTip = $("ToolTip", $(go.TextBlock, { margin: 4 }, new go.Binding("text", "", diagramInfo)) ); // provide a context menu for the background of the Diagram, when not over any Part myDiagram.contextMenu = $("ContextMenu", makeButton("Paste", (e, obj) => e.diagram.commandHandler.pasteSelection(e.diagram.toolManager.contextMenuTool.mouseDownPoint), o => o.diagram.commandHandler.canPasteSelection(o.diagram.toolManager.contextMenuTool.mouseDownPoint)), makeButton("Undo", (e, obj) => e.diagram.commandHandler.undo(), o => o.diagram.commandHandler.canUndo()), makeButton("Redo", (e, obj) => e.diagram.commandHandler.redo(), o => o.diagram.commandHandler.canRedo()) ); // Create the Diagram's Model: var nodeDataArray = [ { key: 1, text: "Alpha", color: "lightblue" }, { key: 2, text: "Beta", color: "orange" }, { key: 3, text: "Gamma", color: "lightgreen", group: 5 }, { key: 4, text: "Delta", color: "pink", group: 5 }, { key: 5, text: "Epsilon", color: "green", isGroup: true } ]; var linkDataArray = [ { from: 1, to: 2, color: "blue" }, { from: 2, to: 2 }, { from: 3, to: 4, color: "green" }, { from: 3, to: 1, color: "purple" } ]; myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray); } window.addEventListener('DOMContentLoaded', init); </script> <div id="sample"> <div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:400px"></div> <p> This sample demonstrates tooltips and context menus for all parts and for the diagram background, as well as several other powerful diagram editing abilities. </p> <p> Unlike the <a href="minimal.html">Minimal</a> sample, this sample has templates for Links and for Groups, plus tooltips and context menus for Nodes, for Links, for Groups, and for the Diagram. </p> <p>This sample has all of the functionality of the Minimal sample, but additionally allows the user to:</p> <ul> <li>create new nodes: double-click in the background of the diagram</li> <li>edit text: select the node and then click on the text, or select the node and press F2</li> <li>draw new links: drag from the inner edge of the node's or the group's shape</li> <li>reconnect existing links: select the link and then drag the diamond-shaped handle at either end of the link</li> <li>group nodes and links: select some nodes and links and then type Ctrl-G (or invoke via context menu)</li> <li>ungroup an existing group: select a group and then type Ctrl-Shift-G (or invoke via context menu)</li> </ul> <p> GoJS contains many other possible commands, which can be invoked by either mouse/keyboard/touch or programmatically. <a href="../intro/commands.html">See an overview of possible commands here.</a> On a Mac, use CMD instead of Ctrl. </p> <p> On touch devices, hold your finger stationary to bring up a context menu. The default context menu supports most of the standard commands that are enabled at that time for that object. </p> </div> </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>