UNPKG

gojs

Version:

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

274 lines (251 loc) 13.9 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="When a diagram node is selected show a selection Adornment holding buttons on which a click invokes a command or a drag starts a tool"/> <link rel="stylesheet" href="../assets/css/style.css"/> <!-- Copyright 1998-2023 by Northwoods Software Corporation. --> <title>Buttons in a Circular Adornment</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"> // Produce a "Button" that is shaped as the intersection of a circular sector and an annular ring. // The first argument is the angle at which the button will be placed relative to the center. // The second argument is the sweep angle over which the button will extend; // by default this is 90 degrees, as if there are exactly four buttons around the whole circle. go.GraphObject.defineBuilder("SectorButton", args => { var angle = go.GraphObject.takeBuilderArgument(args, 0.0, a => typeof a === "number"); var sweep = go.GraphObject.takeBuilderArgument(args, 90.0, a => typeof a === "number"); // default brushes for "Button" shape var buttonFillNormal = "whitesmoke"; var buttonStrokeNormal = "gray"; var buttonFillOver = "dodgerblue"; var buttonStrokeOver = "blue"; var buttonFillDisabled = "darkgray"; function makeAnnularWedge(angle, sweep, outer, inner) { // the Geometry will be centered about (0,0) var p = new go.Point(outer, 0).rotate(angle - sweep / 2); var q = new go.Point(inner, 0).rotate(angle + sweep / 2); var geo = new go.Geometry() .add(new go.PathFigure(-outer, -outer)) // always make sure the Geometry includes the top left corner .add(new go.PathFigure(outer, outer)) // and the bottom right corner of the whole circular area .add(new go.PathFigure(p.x, p.y) // start at outer corner, go clockwise .add(new go.PathSegment(go.PathSegment.Arc, angle - sweep / 2, sweep, 0, 0, outer, outer)) .add(new go.PathSegment(go.PathSegment.Line, q.x, q.y)) // to opposite inner corner, then anticlockwise .add(new go.PathSegment(go.PathSegment.Arc, angle + sweep / 2, -sweep, 0, 0, inner, inner).close())); return geo; } var geo = makeAnnularWedge(angle, sweep, 80, 40); var pt = new go.Point(60, 0).rotate(angle); var align = new go.Spot(0.5, 0.5, pt.x, pt.y); args.forEach(obj => { if (obj instanceof go.GraphObject) obj.alignment = align; }); var button = go.GraphObject.make(go.Panel, "Spot", { isActionable: true, // needed so that the ActionTool intercepts mouse events enabledChanged: (button, enabled) => { const shape = button.findObject("ButtonBorder"); if (shape !== null) { shape.fill = enabled ? button["_buttonFillNormal"] : button["_buttonFillDisabled"]; } }, // save these values for the mouseEnter and mouseLeave event handlers "_buttonFillNormal": buttonFillNormal, "_buttonStrokeNormal": buttonStrokeNormal, "_buttonFillOver": buttonFillOver, "_buttonStrokeOver": buttonStrokeOver, "_buttonFillDisabled": buttonFillDisabled }, go.GraphObject.make(go.Shape, // the border { name: "ButtonBorder", fill: buttonFillNormal, stroke: buttonStrokeNormal, geometry: geo }) ); // There's no GraphObject inside the button shape -- it must be added as part of the button definition. // This way the object could be a TextBlock or a Shape or a Picture or arbitrarily complex Panel. // mouse-over behavior button.mouseEnter = (e, button, prev) => { var shape = button.findObject("ButtonBorder"); // the border Shape if (shape instanceof go.Shape) { var brush = button["_buttonFillOver"]; button["_buttonFillNormal"] = shape.fill; shape.fill = brush; brush = button["_buttonStrokeOver"]; button["_buttonStrokeNormal"] = shape.stroke; shape.stroke = brush; } }; button.mouseLeave = (e, button, prev) => { var shape = button.findObject("ButtonBorder"); // the border Shape if (shape instanceof go.Shape) { shape.fill = button["_buttonFillNormal"]; shape.stroke = button["_buttonStrokeNormal"]; } }; return button; }); function init() { if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this // 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; myDiagram = new go.Diagram("myDiagramDiv", { // show the selection Adornment only on the first selection if it is a Node "ChangedSelection": e => { var node = e.diagram.selection.first(); if (node === null) { var oldnode = selectionAdornment.adornedPart; if (oldnode) oldnode.removeAdornment("Radial"); selectionAdornment.adornedObject = null; } else if (node instanceof go.Node) { selectionAdornment.adornedObject = node; node.addAdornment("Radial", selectionAdornment); } }, "InitialLayoutCompleted": e => { // show the adornment on the "Beta" node e.diagram.select(e.diagram.findNodeForKey(2)); }, "undoManager.isEnabled": true }); myDiagram.nodeTemplate = $(go.Node, "Auto", { resizable: true }, new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify), $(go.Shape, { portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer" }, new go.Binding("fill", "color")), $(go.TextBlock, { margin: 8, editable: true }, new go.Binding("text").makeTwoWay()) ); // Show a radial Adornment holding buttons when a node is selected. // This is not a template, so there is only one instance of it that can be shown at a time. // If you want every selected node to have their own copy of this adornment, // set Node.selectionAdornmentTemplate to this and remove the "ChangedSelection" DiagramEvent listener. var selectionAdornment = $(go.Adornment, "Spot", { layerName: "Tool" }, // so that it's in front of other Adornments $(go.Panel, "Auto", $(go.Shape, { fill: null, stroke: "dodgerblue", strokeWidth: 4, pickable: false }), $(go.Placeholder, { margin: 2 }) ), $(go.Panel, $("SectorButton", 0, 90, // start angle, sweep angle, $(go.TextBlock, "New\nLink"), { // start drawing a new link from this node click: (e, button) => { var node = button.part.adornedPart; // this Node e.diagram.clearSelection(); // hide all Adornments for clarity var tool = e.diagram.toolManager.linkingTool; // the LinkingTool tool.startObject = node.port; // the default port of the Node e.diagram.currentTool = tool; // start the LinkingTool tool.doActivate(); // and activate it } }), $("SectorButton", 90, 90, $(go.Shape, { geometryString: "F1 M29.181 19.070c-1.679-2.908-0.669-6.634 2.255-8.328l-3.145-5.447c-0.898 0.527-1.943 0.829-3.058 0.829-3.361 0-6.085-2.742-6.085-6.125h-6.289c0.008 1.044-0.252 2.103-0.811 3.070-1.679 2.908-5.411 3.897-8.339 2.211l-3.144 5.447c0.905 0.515 1.689 1.268 2.246 2.234 1.676 2.903 0.672 6.623-2.241 8.319l3.145 5.447c0.895-0.522 1.935-0.82 3.044-0.82 3.35 0 6.067 2.725 6.084 6.092h6.289c-0.003-1.034 0.259-2.080 0.811-3.038 1.676-2.903 5.399-3.894 8.325-2.219l3.145-5.447c-0.899-0.515-1.678-1.266-2.232-2.226zM16 22.479c-3.578 0-6.479-2.901-6.479-6.479s2.901-6.479 6.479-6.479c3.578 0 6.479 2.901 6.479 6.479s-2.901 6.479-6.479 6.479z" }), { click: (e, button) => { // maybe use a DataInspector? alert("Show Settings for " + button.part.adornedPart.data.text); } }), $("SectorButton", 180, 90, $(go.TextBlock, "?", { font: "bold 14pt sans-serif" }), { click: (e, button) => { // maybe show some info in HTML? alert("Show Information about " + button.part.adornedPart.data.text); } }), $("SectorButton", 270, 90, $(go.Shape, "XLine", { stroke: "red", strokeWidth: 4, width: 16, height: 16 }), { click: (e, button) => { // this is different from CommandHandler.deleteSelection because this // only deletes the one node, not all selected parts e.diagram.commit(d => d.remove(button.part.adornedPart), "deleted node"); } }) ) ); myDiagram.model = new go.GraphLinksModel( [ { key: 1, text: "Alpha", color: "lightblue" }, { key: 2, text: "Beta", color: "orange" }, { key: 3, text: "Gamma", color: "lightgreen" }, { key: 4, text: "Delta", color: "pink" }, ], [ { from: 1, to: 2 }, { from: 1, to: 3 }, { from: 2, to: 2 }, { from: 3, to: 4 }, { from: 4, to: 1 } ]); } window.addEventListener('DOMContentLoaded', init); </script> <div id="sample"> <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:500px"></div> <p> Selecting a Node shows not only a dodgerblue rectangle around the selected node, but also a circular arrangement of Buttons. The red "X" deletes the node. The "?" and cog/gear Shape currently just call `alert`, but they could do anything that you want. The "New Link" starts the <a>LinkingTool</a> drawing a new Link from that Node. </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>