UNPKG

gojs

Version:

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

115 lines (105 loc) 4.84 kB
<!DOCTYPE html> <html> <head> <title>GoJS Class Hierarchy Tree</title> <meta name="description" content="The JavaScript class hierarchy defined by the GoJS library." /> <!-- Copyright 1998-2016 by Northwoods Software Corporation. --> <meta charset="UTF-8"> <script src="go.js"></script> <link href="../assets/css/goSamples.css" rel="stylesheet" type="text/css" /> <!-- you don't need to use this --> <script src="goSamples.js"></script> <!-- this is only for the GoJS Samples framework --> <script id="code"> function init() { if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this var $ = go.GraphObject.make; // for conciseness in defining templates var diagram = $(go.Diagram, "myDiagramDiv", // id of DIV { // Automatically lay out the diagram as a tree; // separate trees are arranged vertically above each other. layout: $(go.TreeLayout, { nodeSpacing: 3 }) }); // Define a node template showing class names. // Double-clicking opens up the documentation for that class. diagram.nodeTemplate = $(go.Node, "Auto", { doubleClick: nodeDoubleClick, // this function is defined below toolTip: $(go.Adornment, "Auto", $(go.Shape, { fill: "lightyellow" }), $(go.TextBlock, "double-click\nfor documentation", { margin: 5 }) ) }, $(go.Shape, { fill: "#1F4963", stroke: null }), $(go.TextBlock, { font: "bold 13px Helvetica, bold Arial, sans-serif", stroke: "white", margin: 3 }, new go.Binding("text", "key"))); // Define a trivial link template with no arrowhead diagram.linkTemplate = $(go.Link, { curve: go.Link.Bezier, toEndSegmentLength: 30, fromEndSegmentLength: 30 }, $(go.Shape, { strokeWidth: 1.5 }) // the link shape, with the default black stroke ); // Collect all of the data for the model of the class hierarchy var nodeDataArray = []; // Iterate over all of the classes in "go" for (k in go) { var cls = go[k]; if (!cls) continue; var proto = cls.prototype; if (!proto) continue; proto.constructor.className = k; // remember name // find base class constructor var base = Object.getPrototypeOf(proto).constructor; if (base === Object) { // "root" node? nodeDataArray.push({ key: k }); } else { // add a node for this class and a tree-parent reference to the base class name nodeDataArray.push({ key: k, parent: base.className }); } } // Create the model for the hierarchy diagram diagram.model = new go.TreeModel(nodeDataArray); // Now collect all node data that are singletons var singlesArray = []; // for classes that don't inherit from another class diagram.nodes.each(function(node) { if (node.linksConnected.count === 0) { singlesArray.push(node.data); } }); // Remove the unconnected class nodes from the main Diagram diagram.model.removeNodeDataCollection(singlesArray); // Display the unconnected classes in a separate Diagram var singletons = $(go.Diagram, "mySingletons", { nodeTemplate: diagram.nodeTemplate, // share the node template with the main Diagram layout: $(go.GridLayout, { wrappingColumn: 1, // put the unconnected nodes in a column spacing: new go.Size(3, 3) }), model: new go.Model(singlesArray) // use a separate model }); } // When a Node is double clicked, open the documentation for the corresponding class in a new window function nodeDoubleClick(event, node) { window.open("../api/symbols/" + node.data.key + ".html"); } </script> </head> <body onload="init()"> <div id="sample"> <h3>GoJS Class Hierarchy Tree</h3> <!-- The DIV needs an explicit size or else we won't see anything. --> <div id="myDiagramDiv" style="border:1px solid blue; width:79%; height:725px; float: left"></div> <div id="mySingletons" style="border:1px solid blue; width:19%; height:725px; float: right"></div> <p>A <a>TreeLayout</a> of the JavaScript class hierarchy defined by the GoJS library. Classes that do not have any inheritance relationship are shown at the right.</p> <p>Double-clicking on a node will open the API reference for that class in a new window.</p> <p>For more uses of the Tree Layout, see the <a href="DOMTree.html">DOM Tree</a> and <a href="visualTree.html">Visual Tree</a> samples.</p> </div> </body> </html>