gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
218 lines (201 loc) • 10.7 kB
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="Links can connect with Links by using label Nodes on Links."/>
<link rel="stylesheet" href="../assets/css/style.css"/>
<!-- Copyright 1998-2023 by Northwoods Software Corporation. -->
<title>Links to Links</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;
myDiagram =
new go.Diagram("myDiagramDiv", // create a Diagram for the DIV HTML element
{
"LinkDrawn": maybeChangeLinkCategory, // these two DiagramEvents call a
"LinkRelinked": maybeChangeLinkCategory, // function that is defined below
"undoManager.isEnabled": true
});
// when the document is modified, add a "*" to the title and enable the "Save" button
myDiagram.addDiagramListener("Modified", e => {
var button = document.getElementById("SaveButton");
if (button) button.disabled = !myDiagram.isModified;
var idx = document.title.indexOf("*");
if (myDiagram.isModified) {
if (idx < 0) document.title += "*";
} else {
if (idx >= 0) document.title = document.title.slice(0, idx);
}
});
// the regular node template, which supports user-drawn links from the main Shape
myDiagram.nodeTemplate =
$("Node", "Auto",
{
locationSpot: go.Spot.Center,
layerName: "Background"
}, // always have regular nodes behind Links
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
$("Shape", "RoundedRectangle",
{
fill: "white", stroke: null,
portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer"
},
new go.Binding("fill", "color")),
$("TextBlock",
{ margin: 8 }, // make some extra space for the shape around the text
new go.Binding("text", "key")) // the label shows the node data's key
);
// This is the template for a label node on a link: just an Ellipse.
// This node supports user-drawn links to and from the label node.
myDiagram.nodeTemplateMap.add("LinkLabel",
$("Node",
{
selectable: false, avoidable: false,
layerName: "Foreground"
}, // always have link label nodes in front of Links
$("Shape", "Ellipse",
{
width: 5, height: 5, stroke: null,
portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer"
})
));
// the regular link template, a straight blue arrow
myDiagram.linkTemplate =
$("Link",
{ relinkableFrom: true, relinkableTo: true, toShortLength: 2 },
$("Shape", { stroke: "#2E68CC", strokeWidth: 2 }),
$("Shape", { fill: "#2E68CC", stroke: null, toArrow: "Standard" })
);
// This template shows links connecting with label nodes as green and arrow-less.
myDiagram.linkTemplateMap.add("linkToLink",
$("Link",
{ relinkableFrom: true, relinkableTo: true },
$("Shape", { stroke: "#2D9945", strokeWidth: 2 })
));
// GraphLinksModel support for link label nodes requires specifying two properties.
myDiagram.model =
new go.GraphLinksModel(
{ linkLabelKeysProperty: "labelKeys" });
// Whenever a new Link is drawn by the LinkingTool, it also adds a node data object
// that acts as the label node for the link, to allow links to be drawn to/from the link.
myDiagram.toolManager.linkingTool.archetypeLabelNodeData =
{ category: "LinkLabel" };
// this DiagramEvent handler is called during the linking or relinking transactions
function maybeChangeLinkCategory(e) {
var link = e.subject;
var linktolink = (link.fromNode.isLinkLabel || link.toNode.isLinkLabel);
e.diagram.model.setCategoryForLinkData(link.data, (linktolink ? "linkToLink" : ""));
}
load();
}
// Show the diagram's model in JSON format
function save() {
document.getElementById("mySavedModel").value = myDiagram.model.toJson();
myDiagram.isModified = false;
}
function load() {
myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:400px;"></div>
<p>
This demonstrates the ability for a <a>Link</a> to appear to connect with another Link.
Regular links are blue. Link-connecting links are green.
Try moving a node around to see how the links adapt.
Initially the "Alpha" node connects with the link between Gamma and Delta.
There is also a link between the two horizontal links.
</p>
<p>
This effect is achieved by using "label nodes" that belong to links.
Such "label nodes" are real <a>Node</a>s that are referenced from their owning <a>Link</a>.
This sample customizes the "Link Label" Node template to allow the user to draw new links to/from such label nodes.
</p>
<p>
Newly drawn links automatically get a label node by the <a>LinkingTool</a> because this sample initializes
the <a>LinkingTool.archetypeLabelNodeData</a> property of the <a>ToolManager.linkingTool</a>.
The category (i.e. template) of each link is determined by what kinds of nodes the link is connected with.
</p>
<div>
<div>
<button id="SaveButton" onclick="save()">Save</button>
<button onclick="load()">Load</button>
Diagram.model saved in JSON format:<br />
</div>
<textarea id="mySavedModel" style="width:100%;height:400px">
{ "class": "go.GraphLinksModel",
"linkLabelKeysProperty": "labelKeys",
"nodeDataArray": [
{"key":"Alpha", "color":"lightblue", "loc":"29 14"},
{"key":"Beta", "color":"orange", "loc":"129 14"},
{"key":"Gamma", "color":"lightgreen", "loc":"29 106"},
{"key":"Delta", "color":"pink", "loc":"129 106"},
{"key":"A-B", "category":"LinkLabel" },
{"key":"G-D", "category":"LinkLabel" },
{"key":"A-G", "category":"LinkLabel" },
{"key":"A-G-D", "category":"LinkLabel" },
{"key":"A-B-G-D", "category":"LinkLabel" }
],
"linkDataArray": [
{"from":"Alpha", "to":"Beta", "labelKeys":[ "A-B" ]},
{"from":"Gamma", "to":"Delta", "labelKeys":[ "G-D" ]},
{"from":"Alpha", "to":"Gamma", "labelKeys":[ "A-G" ]},
{"from":"Alpha", "to":"G-D", "labelKeys":[ "A-G-D" ], "category":"linkToLink"},
{"from":"A-B", "to":"G-D", "labelKeys":[ "A-B-G-D" ], "category":"linkToLink"}
]}
</textarea>
</div>
</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>