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 |
|
13 | import * as go from '../release/go-module.js';
|
14 |
|
15 | /**
|
16 | * The LinkLabelDraggingTool class lets the user move a label on a {@link Link}.
|
17 | *
|
18 | * This tool only works when the Link has a label
|
19 | * that is positioned at the {@link Link#midPoint} plus some offset.
|
20 | * It does not work for labels that have a particular {@link GraphObject#segmentIndex}.
|
21 | *
|
22 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/LinkLabelDragging.html">Link Label Dragging</a> sample.
|
23 | * @category Tool Extension
|
24 | */
|
25 | export class LinkLabelDraggingTool extends go.Tool {
|
26 | /**
|
27 | * The label being dragged.
|
28 | */
|
29 | public label: go.GraphObject | null = null;
|
30 | private _offset: go.Point = new go.Point(); // of the mouse relative to the center of the label object
|
31 | private _originalOffset: go.Point | null = null;
|
32 |
|
33 | /**
|
34 | * Constructs a LinkLabelDraggingTool and sets the name for the tool.
|
35 | */
|
36 | constructor() {
|
37 | super();
|
38 | this.name = 'LinkLabelDragging';
|
39 | }
|
40 |
|
41 | /**
|
42 | * From the GraphObject at the mouse point, search up the visual tree until we get to
|
43 | * an object that is a label of a Link.
|
44 | * @return {GraphObject} This returns null if no such label is at the mouse down point.
|
45 | */
|
46 | public findLabel(): go.GraphObject | null {
|
47 | const diagram = this.diagram;
|
48 | const e = diagram.lastInput;
|
49 | let elt = diagram.findObjectAt(e.documentPoint, null, null);
|
50 |
|
51 | if (elt === null || !(elt.part instanceof go.Link)) return null;
|
52 | while (elt !== null && elt.panel !== elt.part) {
|
53 | elt = elt.panel;
|
54 | }
|
55 | // If it's at an arrowhead segment index, don't consider it a label:
|
56 | if (elt !== null && (elt.segmentIndex === 0 || elt.segmentIndex === -1)) return null;
|
57 | return elt;
|
58 | }
|
59 |
|
60 | /**
|
61 | * This tool can only start if the mouse has moved enough so that it is not a click,
|
62 | * and if the mouse down point is on a GraphObject "label" in a Link Panel,
|
63 | * as determined by {@link #findLabel}.
|
64 | */
|
65 | public canStart(): boolean {
|
66 | if (!super.canStart()) return false;
|
67 | const diagram = this.diagram;
|
68 | // require left button & that it has moved far enough away from the mouse down point, so it isn't a click
|
69 | const e = diagram.lastInput;
|
70 | if (!e.left) return false;
|
71 | if (!this.isBeyondDragSize()) return false;
|
72 |
|
73 | return this.findLabel() !== null;
|
74 | }
|
75 |
|
76 | /**
|
77 | * Start a transaction, call {@link #findLabel} and remember it as the "label" property,
|
78 | * and remember the original value for the label's {@link GraphObject#segmentOffset} property.
|
79 | */
|
80 | public doActivate(): void {
|
81 | this.startTransaction('Shifted Label');
|
82 | this.label = this.findLabel();
|
83 | if (this.label !== null) {
|
84 | // compute the offset of the mouse-down point relative to the center of the label
|
85 | this._offset = this.diagram.firstInput.documentPoint.copy().subtract(this.label.getDocumentPoint(go.Spot.Center));
|
86 | this._originalOffset = this.label.segmentOffset.copy();
|
87 | }
|
88 | super.doActivate();
|
89 | }
|
90 |
|
91 | /**
|
92 | * Stop any ongoing transaction.
|
93 | */
|
94 | public doDeactivate(): void {
|
95 | super.doDeactivate();
|
96 | this.stopTransaction();
|
97 | }
|
98 |
|
99 | /**
|
100 | * Clear any reference to a label element.
|
101 | */
|
102 | public doStop(): void {
|
103 | this.label = null;
|
104 | super.doStop();
|
105 | }
|
106 |
|
107 | /**
|
108 | * Restore the label's original value for {@link GraphObject#segmentOffset}.
|
109 | */
|
110 | public doCancel(): void {
|
111 | if (this.label !== null && this._originalOffset !== null) {
|
112 | this.label.segmentOffset = this._originalOffset;
|
113 | }
|
114 | super.doCancel();
|
115 | }
|
116 |
|
117 | /**
|
118 | * During the drag, call {@link #updateSegmentOffset} in order to set
|
119 | * the {@link GraphObject#segmentOffset} of the label.
|
120 | */
|
121 | public doMouseMove(): void {
|
122 | if (!this.isActive) return;
|
123 | this.updateSegmentOffset();
|
124 | }
|
125 |
|
126 | /**
|
127 | * At the end of the drag, update the segment offset of the label and finish the tool,
|
128 | * completing a transaction.
|
129 | */
|
130 | public doMouseUp(): void {
|
131 | if (!this.isActive) return;
|
132 | this.updateSegmentOffset();
|
133 | this.transactionResult = 'Shifted Label';
|
134 | this.stopTool();
|
135 | }
|
136 |
|
137 | /**
|
138 | * Save the label's {@link GraphObject#segmentOffset} as a rotated offset from the midpoint of the
|
139 | * Link that the label is in.
|
140 | */
|
141 | public updateSegmentOffset(): void {
|
142 | const lab = this.label;
|
143 | if (lab === null) return;
|
144 | const link = lab.part;
|
145 | if (!(link instanceof go.Link)) return;
|
146 |
|
147 | const last = this.diagram.lastInput.documentPoint;
|
148 | const idx = lab.segmentIndex;
|
149 | const numpts = link.pointsCount;
|
150 | // if the label is a "mid" label, assume it is positioned differently from a label at a particular segment
|
151 | if (idx < -numpts || idx >= numpts) {
|
152 | const mid = link.midPoint;
|
153 | // need to rotate this point to account for the angle of the link segment at the mid-point
|
154 | const p = new go.Point(last.x - this._offset.x - mid.x, last.y - this._offset.y - mid.y);
|
155 | lab.segmentOffset = p.rotate(-link.midAngle);
|
156 | } else { // handle the label point being on a partiular segment with a given fraction
|
157 | const frac = lab.segmentFraction;
|
158 | let a: go.Point;
|
159 | let b: go.Point;
|
160 | if (idx >= 0) { // indexing forwards
|
161 | a = link.getPoint(idx);
|
162 | b = (idx < numpts - 1) ? link.getPoint(idx + 1) : a;
|
163 | } else { // or backwards if segmentIndex is negative
|
164 | const i = numpts + idx;
|
165 | a = link.getPoint(i);
|
166 | b = (i > 0) ? link.getPoint(i - 1) : a;
|
167 | }
|
168 | const labx = a.x + (b.x - a.x) * frac;
|
169 | const laby = a.y + (b.y - a.y) * frac;
|
170 | const p = new go.Point(last.x - this._offset.x - labx, last.y - this._offset.y - laby);
|
171 | const segangle = (idx >= 0) ? a.directionPoint(b) : b.directionPoint(a);
|
172 | lab.segmentOffset = p.rotate(-segangle);
|
173 | }
|
174 | }
|
175 | }
|