UNPKG

5.33 kBPlain TextView Raw
1'use strict';
2/*
3* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
4*/
5
6import * as go from '../release/go.js';
7
8export function init() {
9 if ((window as any).goSamples) (window as any).goSamples(); // init for these samples -- you don't need to call this
10
11 const $ = go.GraphObject.make; // for conciseness in defining templates
12
13 const myDiagram =
14 $(go.Diagram, 'myDiagramDiv', // create a Diagram for the DIV HTML element
15 {
16 isReadOnly: true, // don't allow move or delete
17 layout: $(go.CircularLayout,
18 {
19 radius: 100, // minimum radius
20 spacing: 0, // circular nodes will touch each other
21 nodeDiameterFormula: go.CircularLayout.Circular, // assume nodes are circular
22 startAngle: 270 // first node will be at top
23 }),
24 // define a DiagramEvent listener
25 'LayoutCompleted': (e: go.DiagramEvent) => {
26 // now that the CircularLayout has finished, we know where its center is
27 const cntr = myDiagram.findNodeForKey('Center');
28 if (cntr !== null) cntr.location = (myDiagram.layout as go.CircularLayout).actualCenter;
29 }
30 });
31
32 // construct a shared radial gradient brush
33 const radBrush = $(go.Brush, 'Radial', { 0: '#550266', 1: '#80418C' });
34
35 // these are the nodes that are in a circle
36 myDiagram.nodeTemplate =
37 $(go.Node,
38 $(go.Shape, 'Circle',
39 {
40 desiredSize: new go.Size(28, 28),
41 fill: radBrush, strokeWidth: 0, stroke: null
42 }), // no outline
43 {
44 locationSpot: go.Spot.Center,
45 click: showArrowInfo, // defined below
46 toolTip: // define a tooltip for each link that displays its information
47 $<go.Adornment>('ToolTip',
48 $(go.TextBlock, { margin: 4 },
49 new go.Binding('text', '', infoString).ofObject())
50 )
51 }
52 );
53
54 // use a special template for the center node
55 myDiagram.nodeTemplateMap.add('Center',
56 $(go.Node, 'Spot',
57 {
58 selectable: false,
59 isLayoutPositioned: false, // the Diagram.layout will not position this node
60 locationSpot: go.Spot.Center
61 },
62 $(go.Shape, 'Circle',
63 { fill: radBrush, strokeWidth: 0, stroke: null, desiredSize: new go.Size(200, 200) }), // no outline
64 $(go.TextBlock, 'Arrowheads',
65 { margin: 1, stroke: 'white', font: 'bold 14px sans-serif' })
66 ));
67
68 // all Links have both "toArrow" and "fromArrow" Shapes,
69 // where both arrow properties are data bound
70 myDiagram.linkTemplate =
71 $(go.Link, // the whole link panel
72 { routing: go.Link.Normal },
73 $(go.Shape, // the link shape
74 // the first element is assumed to be main element: as if isPanelMain were true
75 { stroke: 'gray', strokeWidth: 2 }),
76 $(go.Shape, // the "from" arrowhead
77 new go.Binding('fromArrow', 'fromArrow'),
78 { scale: 2, fill: '#D4B52C' }),
79 $(go.Shape, // the "to" arrowhead
80 new go.Binding('toArrow', 'toArrow'),
81 { scale: 2, fill: '#D4B52C' }),
82 {
83 click: showArrowInfo,
84 toolTip: // define a tooltip for each link that displays its information
85 $<go.Adornment>('ToolTip',
86 $(go.TextBlock, { margin: 4 },
87 new go.Binding('text', '', infoString).ofObject())
88 )
89 }
90 );
91
92 // collect all of the predefined arrowhead names
93 const arrowheads = go.Shape.getArrowheadGeometries().toKeySet().toArray();
94 if (arrowheads.length % 2 === 1) arrowheads.push(''); // make sure there's an even number
95
96 // create all of the link data, two arrowheads per link
97 const linkdata = [];
98 let i = 0;
99 for (let j = 0; j < arrowheads.length; j = j + 2) {
100 linkdata.push({ from: 'Center', to: i++, toArrow: arrowheads[j], fromArrow: arrowheads[j + 1] });
101 }
102
103 myDiagram.model =
104 $(go.GraphLinksModel,
105 { // this gets copied automatically when there's a link data reference to a new node key
106 // and is then added to the nodeDataArray
107 archetypeNodeData: {},
108 // the node array starts with just the special Center node
109 nodeDataArray: [{ category: 'Center', key: 'Center' }],
110 // the link array was created above
111 linkDataArray: linkdata
112 });
113}
114
115// a conversion function used to get arrowhead information for a Part
116export function infoString(obj: go.GraphObject) {
117 let part = obj.part;
118 if (part instanceof go.Adornment) part = part.adornedPart;
119 let msg = '';
120 if (part instanceof go.Link) {
121 const link = part;
122 msg = 'toArrow: ' + link.data.toArrow + ';\nfromArrow: ' + link.data.fromArrow;
123 } else if (part instanceof go.Node) {
124 const node = part;
125 const link = node.linksConnected.first();
126 if (link) msg = 'toArrow: ' + link.data.toArrow + ';\nfromArrow: ' + link.data.fromArrow;
127 }
128 return msg;
129}
130
131// a GraphObject.click event handler to show arrowhead information
132export function showArrowInfo(e: go.InputEvent, obj: go.GraphObject) {
133 const msg = infoString(obj);
134 if (msg) {
135 const status = document.getElementById('myArrowheadInfo');
136 if (status) status.textContent = msg;
137 }
138}