-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathLinkShiftingTool.js
303 lines (302 loc) · 11.4 KB
/
LinkShiftingTool.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* 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/LinkShiftingTool.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.
*/
/**
* The LinkShiftingTool class lets the user shift the end of a link to be anywhere along the edges of the port;
* use it in a diagram.toolManager.mouseDownTools list:
* ```js
* myDiagram.toolManager.mouseDownTools.add(new LinkShiftingTool());
* ```
*
* If you want to experiment with this extension, try the <a href="../../samples/LinkShifting.html">Link Shifting</a> sample.
* @category Tool Extension
*/
class LinkShiftingTool extends go.Tool {
/**
* Constructs a LinkShiftingTool and sets the handles and name of the tool.
*/
constructor(init) {
super();
this.name = 'LinkShifting';
this._fromHandleArchetype =
new go.Shape({
geometryString: 'F1 M0 0 L8 0 M8 4 L0 4',
fill: null,
stroke: 'dodgerblue',
background: 'lightblue',
cursor: 'pointer',
segmentIndex: 0,
segmentFraction: 1,
segmentOrientation: go.Orientation.Along
});
this._fromAdornmentTemplate =
new go.Adornment(go.Panel.Link)
.add(this._fromHandleArchetype)
.freezeBindings();
this._toHandleArchetype =
new go.Shape({
geometryString: 'F1 M0 0 L8 0 M8 4 L0 4',
fill: null,
stroke: 'dodgerblue',
background: 'lightblue',
cursor: 'pointer',
segmentIndex: -1,
segmentFraction: 1,
segmentOrientation: go.Orientation.Along
});
this._toAdornmentTemplate =
new go.Adornment(go.Panel.Link)
.add(this._toHandleArchetype)
.freezeBindings();
this._originalPoints = null;
this._handle = null;
this._originalPoints = null;
if (init)
Object.assign(this, init);
}
/**
* A small GraphObject used as a shifting handle.
*/
get fromHandleArchetype() {
return this._fromHandleArchetype;
}
set fromHandleArchetype(value) {
if (value !== null && !(value instanceof go.GraphObject))
throw new Error('LinkShiftingTool.fromHandleArchetype must be a GraphObject');
this._fromHandleArchetype = value;
if (value !== null)
this._fromAdornmentTemplate = new go.Adornment(go.Panel.Link).add(value).freezeBindings();
else
this._fromAdornmentTemplate = null;
}
/**
* A small GraphObject used as a shifting handle.
*/
get toHandleArchetype() {
return this._toHandleArchetype;
}
set toHandleArchetype(value) {
if (value !== null && !(value instanceof go.GraphObject))
throw new Error('LinkShiftingTool.toHandleArchetype must be a GraphObject');
this._toHandleArchetype = value;
if (value !== null)
this._toAdornmentTemplate = new go.Adornment(go.Panel.Link).add(value).freezeBindings();
else
this._toAdornmentTemplate = null;
}
/**
* Show an {@link go.Adornment} with a reshape handle at each end of the link which allows for shifting of the end points.
*/
updateAdornments(part) {
if (part === null || !(part instanceof go.Link))
return; // this tool only applies to Links
const link = part;
// show handles if link is selected, remove them if no longer selected
let category = 'LinkShiftingFrom';
let adornment = null;
if (link.isSelected && !this.diagram.isReadOnly && link.fromPort) {
const selelt = link.selectionObject;
if (selelt !== null &&
link.actualBounds.isReal() &&
link.isVisible() &&
selelt.actualBounds.isReal() &&
selelt.isVisibleObject()) {
const spot = link.computeSpot(true);
if (spot.isSide() || spot.isSpot()) {
adornment = link.findAdornment(category);
if (adornment === null) {
adornment = this.makeAdornment(selelt, false);
if (adornment) {
adornment.category = category;
link.addAdornment(category, adornment);
}
}
else {
// This is just to invalidate the measure, so it recomputes itself based on the adorned link
adornment.segmentFraction = Math.random();
}
}
}
}
if (adornment === null)
link.removeAdornment(category);
category = 'LinkShiftingTo';
adornment = null;
if (link.isSelected && !this.diagram.isReadOnly && link.toPort) {
const selelt = link.selectionObject;
if (selelt !== null &&
link.actualBounds.isReal() &&
link.isVisible() &&
selelt.actualBounds.isReal() &&
selelt.isVisibleObject()) {
const spot = link.computeSpot(false);
if (spot.isSide() || spot.isSpot()) {
adornment = link.findAdornment(category);
if (adornment === null) {
adornment = this.makeAdornment(selelt, true);
if (adornment) {
adornment.category = category;
link.addAdornment(category, adornment);
}
}
else {
// This is just to invalidate the measure, so it recomputes itself based on the adorned link
adornment.segmentFraction = Math.random();
}
}
}
}
if (adornment === null)
link.removeAdornment(category);
}
/**
* @hidden @internal
* @param selelt - the {@link go.GraphObject} of the {@link go.Link} being shifted.
* @param toend
*/
makeAdornment(selelt, toend) {
let ad = toend ? this._toAdornmentTemplate : this._fromAdornmentTemplate;
if (ad) {
ad = ad.copy();
ad.adornedObject = selelt;
}
return ad;
}
/**
* This tool may run when there is a mouse-down event on a reshaping handle.
*/
canStart() {
if (!this.isEnabled)
return false;
const diagram = this.diagram;
if (diagram.isReadOnly || diagram.isModelReadOnly)
return false;
if (!diagram.lastInput.left)
return false;
let h = this.findToolHandleAt(diagram.firstInput.documentPoint, 'LinkShiftingFrom');
if (h === null)
h = this.findToolHandleAt(diagram.firstInput.documentPoint, 'LinkShiftingTo');
return h !== null;
}
/**
* Start shifting, if {@link findToolHandleAt} finds a reshaping handle at the mouse down point.
*
* If successful this sets the handle to be the reshape handle that it finds.
* It also remembers the original points in case this tool is cancelled.
* And it starts a transaction.
*/
doActivate() {
const diagram = this.diagram;
let h = this.findToolHandleAt(diagram.firstInput.documentPoint, 'LinkShiftingFrom');
if (h === null)
h = this.findToolHandleAt(diagram.firstInput.documentPoint, 'LinkShiftingTo');
if (h === null)
return;
const ad = h.part;
if (ad === null || ad.adornedObject === null)
return;
const link = ad.adornedObject.part;
if (!(link instanceof go.Link))
return;
this._handle = h;
this._originalPoints = link.points.copy();
this.startTransaction(this.name);
diagram.isMouseCaptured = true;
diagram.currentCursor = 'pointer';
this.isActive = true;
}
/**
* This stops the current shifting operation with the link as it is.
*/
doDeactivate() {
this.isActive = false;
const diagram = this.diagram;
diagram.isMouseCaptured = false;
diagram.currentCursor = '';
this.stopTransaction();
}
/**
* Perform cleanup of tool state.
*/
doStop() {
this._handle = null;
this._originalPoints = null;
}
/**
* Restore the link route to be the original points and stop this tool.
*/
doCancel() {
if (this._handle !== null) {
const ad = this._handle.part;
if (ad.adornedObject === null)
return;
const link = ad.adornedObject.part;
if (this._originalPoints !== null)
link.points = this._originalPoints;
}
this.stopTool();
}
/**
* Call {@link doReshape} with a new point determined by the mouse
* to change the end point of the link.
*/
doMouseMove() {
if (this.isActive) {
this.doReshape(this.diagram.lastInput.documentPoint);
}
}
/**
* Reshape the link's end with a point based on the most recent mouse point by calling {@link doReshape},
* and then stop this tool.
*/
doMouseUp() {
if (this.isActive) {
this.doReshape(this.diagram.lastInput.documentPoint);
this.transactionResult = this.name;
}
this.stopTool();
}
/**
* Find the closest point along the edge of the link's port and shift the end of the link to that point.
*/
doReshape(pt) {
if (this._handle === null)
return;
const ad = this._handle.part;
if (ad.adornedObject === null)
return;
const link = ad.adornedObject.part;
const fromend = ad.category === 'LinkShiftingFrom';
let port = null;
if (fromend) {
port = link.fromPort;
}
else {
port = link.toPort;
}
if (port === null)
return;
// support rotated ports
const portang = port.getDocumentAngle();
const center = port.getDocumentPoint(go.Spot.Center);
const farpt = pt.copy().offset((pt.x - center.x) * 1000, (pt.y - center.y) * 1000);
const portb = new go.Rect(port.getDocumentPoint(go.Spot.TopLeft).subtract(center).rotate(-portang).add(center), port.getDocumentPoint(go.Spot.BottomRight).subtract(center).rotate(-portang).add(center));
let lp = link.getLinkPointFromPoint(port.part, port, center, farpt, fromend);
lp = lp.copy().subtract(center).rotate(-portang).add(center);
const spot = new go.Spot(Math.max(0, Math.min(1, (lp.x - portb.x) / (portb.width || 1))), Math.max(0, Math.min(1, (lp.y - portb.y) / (portb.height || 1))));
if (fromend) {
link.fromSpot = spot;
}
else {
link.toSpot = spot;
}
}
}