1 | /*
|
2 | * Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
|
3 | */
|
4 | /*
|
5 | * This is an extension and not part of the main GoJS library.
|
6 | * Note that the API for this class may change with any version, even point releases.
|
7 | * If you intend to use an extension in production, you should copy the code to your own source directory.
|
8 | * Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
|
9 | * See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
|
10 | */
|
11 | import * as go from '../release/go-module.js';
|
12 | /**
|
13 | * A custom {@link Layout} that lays out a chain of nodes in a spiral.
|
14 | *
|
15 | * This layout assumes the graph is a chain of {@link Node}s,
|
16 | * {@link #spacing} controls the spacing between nodes.
|
17 | *
|
18 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/Spiral.html">Spiral Layout</a> sample.
|
19 | * @category Layout Extension
|
20 | */
|
21 | export class SpiralLayout extends go.Layout {
|
22 | constructor() {
|
23 | super(...arguments);
|
24 | this._radius = NaN;
|
25 | this._spacing = 10;
|
26 | this._clockwise = true;
|
27 | }
|
28 | /**
|
29 | * Gets or sets the radius distance.
|
30 | *
|
31 | * The default value is NaN.
|
32 | */
|
33 | get radius() { return this._radius; }
|
34 | set radius(val) {
|
35 | if (typeof val !== 'number')
|
36 | throw new Error('new value ofr SpiralLayout.radius must be a number, not ' + val);
|
37 | if (this._radius !== val) {
|
38 | this._radius = val;
|
39 | this.invalidateLayout();
|
40 | }
|
41 | }
|
42 | /**
|
43 | * Gets or sets the spacing between nodes.
|
44 | *
|
45 | * The default value is 100.
|
46 | */
|
47 | get spacing() { return this._spacing; }
|
48 | set spacing(val) {
|
49 | if (typeof val !== 'number')
|
50 | throw new Error('new value for SpiralLayout.spacing must be a number, not: ' + val);
|
51 | if (this._spacing !== val) {
|
52 | this._spacing = val;
|
53 | this.invalidateLayout();
|
54 | }
|
55 | }
|
56 | /**
|
57 | * Gets or sets whether the spiral should go clockwise or counter-clockwise.
|
58 | *
|
59 | * The default value is true.
|
60 | */
|
61 | get clockwise() { return this._clockwise; }
|
62 | set clockwise(val) {
|
63 | if (typeof val !== 'boolean')
|
64 | throw new Error('new value for SpiralLayout.clockwise must be a boolean, not: ' + val);
|
65 | if (this._clockwise !== val) {
|
66 | this._clockwise = val;
|
67 | this.invalidateLayout();
|
68 | }
|
69 | }
|
70 | /**
|
71 | * Copies properties to a cloned Layout.
|
72 | */
|
73 | cloneProtected(copy) {
|
74 | super.cloneProtected(copy);
|
75 | copy._radius = this._radius;
|
76 | copy._spacing = this._spacing;
|
77 | copy._clockwise = this._clockwise;
|
78 | }
|
79 | /**
|
80 | * This method actually positions all of the Nodes, assuming that the ordering of the nodes
|
81 | * is given by a single link from one node to the next.
|
82 | * This respects the {@link #spacing} property to affect the layout.
|
83 | * @param {Diagram|Group|Iterable.<Part>} coll A {@link Diagram} or a {@link Group} or a collection of {@link Part}s.
|
84 | */
|
85 | doLayout(coll) {
|
86 | if (this.network === null) {
|
87 | this.network = this.makeNetwork(coll);
|
88 | }
|
89 | this.arrangementOrigin = this.initialOrigin(this.arrangementOrigin);
|
90 | const originx = this.arrangementOrigin.x;
|
91 | const originy = this.arrangementOrigin.y;
|
92 | let root = null;
|
93 | // find a root vertex -- one without any incoming edges
|
94 | const it = this.network.vertexes.iterator;
|
95 | while (it.next()) {
|
96 | const v = it.value;
|
97 | if (root === null)
|
98 | root = v; // in case there are only circles
|
99 | if (v.sourceEdges.count === 0) {
|
100 | root = v;
|
101 | break;
|
102 | }
|
103 | }
|
104 | // couldn't find a root vertex
|
105 | if (root === null) {
|
106 | this.network = null;
|
107 | return;
|
108 | }
|
109 | const space = this.spacing;
|
110 | const cw = (this.clockwise ? 1 : -1);
|
111 | let rad = this.radius;
|
112 | if (rad <= 0 || isNaN(rad) || !isFinite(rad))
|
113 | rad = this.diameter(root) / 4;
|
114 | // treat the root node specially: it goes in the center
|
115 | let angle = cw * Math.PI;
|
116 | root.centerX = originx;
|
117 | root.centerY = originy;
|
118 | let edge = root.destinationEdges.first();
|
119 | // if (edge === null || edge.link === null) return;
|
120 | const link = (edge !== null ? edge.link : null);
|
121 | if (link !== null)
|
122 | link.curviness = cw * rad;
|
123 | // now locate each of the following nodes, in order, along a spiral
|
124 | let vert = (edge !== null ? edge.toVertex : null);
|
125 | while (vert !== null) {
|
126 | // involute spiral
|
127 | const cos = Math.cos(angle);
|
128 | const sin = Math.sin(angle);
|
129 | let x = rad * (cos + angle * sin);
|
130 | let y = rad * (sin - angle * cos);
|
131 | // the link might connect to a member node of a group
|
132 | if (link !== null && vert.node instanceof go.Group && link.toNode !== null && link.toNode !== vert.node) {
|
133 | const offset = link.toNode.location.copy().subtract(vert.node.location);
|
134 | x -= offset.x;
|
135 | y -= offset.y;
|
136 | }
|
137 | vert.centerX = x + originx;
|
138 | vert.centerY = y + originy;
|
139 | const nextedge = vert.destinationEdges.first();
|
140 | const nextvert = (nextedge !== null ? nextedge.toVertex : null);
|
141 | if (nextvert !== null) {
|
142 | // clockwise curves want positive Link.curviness
|
143 | if (this.isRouting && nextedge !== null && nextedge.link !== null) {
|
144 | if (!isNaN(nextedge.link.curviness)) {
|
145 | const c = nextedge.link.curviness;
|
146 | nextedge.link.curviness = cw * Math.abs(c);
|
147 | }
|
148 | }
|
149 | // determine next node's angle
|
150 | const dia = this.diameter(vert) / 2 + this.diameter(nextvert) / 2;
|
151 | angle += cw * Math.atan((dia + space) / Math.sqrt(x * x + y * y));
|
152 | }
|
153 | edge = nextedge;
|
154 | vert = nextvert;
|
155 | }
|
156 | this.updateParts();
|
157 | this.network = null;
|
158 | }
|
159 | /**
|
160 | * Compute the effective diameter of a Node.
|
161 | */
|
162 | diameter(v) {
|
163 | if (!v)
|
164 | return 0;
|
165 | const b = v.bounds;
|
166 | return Math.sqrt(b.width * b.width + b.height * b.height);
|
167 | }
|
168 | }
|