-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathSerpentineLayout.js
254 lines (253 loc) · 9.26 KB
/
SerpentineLayout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright 1998-2025 by Northwoods Software Corporation. All Rights Reserved.
*/
/*
* This is an extension and not part of the main GoJS library.
* The source code for this is at extensionsJSM/SerpentineLayout.ts.
* Note that the API for this class may change with any version, even point releases.
* If you intend to use an extension in production, you should copy the code to your own source directory.
* Extensions can be found in the GoJS kit under the extensions or extensionsJSM folders.
* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
*/
/**
* A custom {@link go.Layout} that lays out a chain of nodes in a snake-like fashion.
*
* This layout assumes the graph is a chain of Nodes,
* positioning nodes in horizontal rows back and forth, alternating between left-to-right
* and right-to-left within the {@link wrap} limit.
* {@link spacing} controls the distance between nodes.
* {@link leftSpot} and {@link rightSpot} determine the Spots to use for the {@link go.Link.fromSpot} and {@link go.Link.toSpot}.
*
* When this layout is the Diagram.layout, it is automatically invalidated when the viewport changes size.
*
* If you want to experiment with this extension, try the <a href="../../samples/Serpentine.html">Serpentine Layout</a> sample.
* @category Layout Extension
*/
class SerpentineLayout extends go.Layout {
/**
* Constructs a SerpentineLayout and sets the {@link isViewportSized} property to true.
*/
constructor(init) {
super();
this.isViewportSized = true;
this._spacing = new go.Size(30, 30);
this._wrap = NaN;
this._root = null;
this._leftSpot = go.Spot.Left;
this._rightSpot = go.Spot.Right;
if (init)
Object.assign(this, init);
}
/**
* Gets or sets the {@link go.Size} whose width specifies the horizontal space between nodes
* and whose height specifies the minimum vertical space between nodes.
*
* The default value is 30x30.
*/
get spacing() {
return this._spacing;
}
set spacing(val) {
if (!this._spacing.equals(val)) {
if (!(val instanceof go.Size))
throw new Error('new value for SerpentineLayout.spacing must be a Size, not: ' + val);
this._spacing = val;
this.invalidateLayout();
}
}
/**
* Gets or sets the total width of the layout.
*
* The default value is NaN, which for {@link go.Diagram.layout}s means that it uses
* the {@link go.Diagram.viewportBounds}.
*/
get wrap() {
return this._wrap;
}
set wrap(val) {
if (this._wrap !== val) {
if (typeof val !== 'number')
throw new Error('SerpentineLayout.wrap must be a number');
this._wrap = val;
this.invalidateLayout();
}
}
/**
* Gets or sets the starting node of the sequence.
*
* The default value is null, which causes the layout to look for a node without any incoming links.
*/
get root() {
return this._root;
}
set root(val) {
if (this._root !== val) {
if (val !== null && !(val instanceof go.Node))
throw new Error('SerpentinelLayout.root must be a go.Node');
this._root = val;
this.invalidateLayout();
}
}
/**
* Gets or sets the Spot to use on the left side of a Node.
*
* The default value is {@link go.Spot.Left}.
*/
get leftSpot() {
return this._leftSpot;
}
set leftSpot(val) {
if (!this._leftSpot.equals(val)) {
if (!(val instanceof go.Spot))
throw new Error('SerpentinelLayout.leftSpot must be a Spot');
this._leftSpot = val;
this.invalidateLayout();
}
}
/**
* Gets or sets the Spot to use on the right side of a Node.
*
* The default value is {@link go.Spot.Right}.
*/
get rightSpot() {
return this._rightSpot;
}
set rightSpot(val) {
if (!this._rightSpot.equals(val)) {
if (!(val instanceof go.Spot))
throw new Error('SerpentinelLayout.rightSpot must be a Spot');
this._rightSpot = val;
this.invalidateLayout();
}
}
/**
* Copies properties to a cloned Layout.
*/
cloneProtected(copy) {
super.cloneProtected(copy);
copy._spacing = this._spacing;
copy._wrap = this._wrap;
// don't copy _root
copy._leftSpot = this._leftSpot;
copy._rightSpot = this._rightSpot;
}
/**
* This method actually positions all of the Nodes, assuming that the ordering of the nodes
* is given by a single link from one node to the next.
* This respects the {@link spacing} and {@link wrap} properties to affect the layout.
* @param collection - A collection of {@link go.Part}s.
*/
doLayout(collection) {
const diagram = this.diagram;
const coll = this.collectParts(collection);
let root = this.root;
if (root === null) {
// find a root node -- one without any incoming links
const it = coll.iterator;
while (it.next()) {
const n = it.value;
if (!(n instanceof go.Node))
continue;
if (root === null)
root = n;
if (n.findLinksInto().count === 0) {
root = n;
break;
}
}
}
// couldn't find a root node
if (root === null)
return;
const spacing = this.spacing;
// calculate the width at which we should start a new row
let wrap = this.wrap;
if (diagram !== null && isNaN(wrap)) {
if (this.group === null) {
// for a top-level layout, use the Diagram.viewportBounds
const pad = diagram.padding;
wrap = Math.max(spacing.width * 2, diagram.viewportBounds.width - 24 - pad.left - pad.right);
}
else {
wrap = 1000; // provide a better default value?
}
}
// implementations of doLayout that do not make use of a LayoutNetwork
// need to perform their own transactions
if (diagram !== null)
diagram.startTransaction('Serpentine Layout');
// start on the left, at Layout.arrangementOrigin
this.arrangementOrigin = this.initialOrigin(this.arrangementOrigin);
let x = this.arrangementOrigin.x;
let rowh = 0;
let y = this.arrangementOrigin.y;
let increasing = true;
let node = root;
while (node !== null) {
const orignode = node;
if (node.containingGroup !== null)
node = node.containingGroup;
const b = this.getLayoutBounds(node);
// get the next node, if any
let nextlink = null;
for (const it = orignode.findLinksOutOf().iterator; it.next();) {
if (coll.has(it.value)) {
nextlink = it.value;
break;
}
}
let nextnode = nextlink !== null ? nextlink.toNode : null;
const orignextnode = nextnode;
if (nextnode !== null && nextnode.containingGroup !== null)
nextnode = nextnode.containingGroup;
const nb = nextnode !== null ? this.getLayoutBounds(nextnode) : new go.Rect();
if (increasing) {
node.move(new go.Point(x, y));
x += b.width;
rowh = Math.max(rowh, b.height);
if (x + spacing.width + nb.width > wrap) {
y += rowh + spacing.height;
x = wrap - spacing.width;
rowh = 0;
increasing = false;
if (nextlink !== null) {
nextlink.fromSpot = go.Spot.Right;
nextlink.toSpot = go.Spot.Right;
}
}
else {
x += spacing.width;
if (nextlink !== null) {
nextlink.fromSpot = go.Spot.Right;
nextlink.toSpot = go.Spot.Left;
}
}
}
else {
x -= b.width;
node.move(new go.Point(x, y));
rowh = Math.max(rowh, b.height);
if (x - spacing.width - nb.width < 0) {
y += rowh + spacing.height;
x = 0;
rowh = 0;
increasing = true;
if (nextlink !== null) {
nextlink.fromSpot = go.Spot.Left;
nextlink.toSpot = go.Spot.Left;
}
}
else {
x -= spacing.width;
if (nextlink !== null) {
nextlink.fromSpot = go.Spot.Left;
nextlink.toSpot = go.Spot.Right;
}
}
}
node = orignextnode;
}
if (diagram !== null)
diagram.commitTransaction('Serpentine Layout');
}
}