UNPKG

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