UNPKG

5.49 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 * The NodeLabelDraggingTool class lets the user move a label on a Node.
17 *
18 * This tool only works when the Node has a label (any GraphObject) marked with
19 * { _isNodeLabel: true } that is positioned in a Spot Panel.
20 * It works by modifying that label's {@link GraphObject#alignment} property to have an
21 * offset from the center of the panel.
22 *
23 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/NodeLabelDragging.html">Node Label Dragging</a> sample.
24 * @category Tool Extension
25 */
26export class NodeLabelDraggingTool extends go.Tool {
27 /**
28 * The label being dragged.
29 */
30 public label: go.GraphObject | null = null;
31 private _offset: go.Point = new go.Point(); // of the mouse relative to the center of the label object
32 private _originalAlignment: go.Spot = go.Spot.Default;
33 private _originalCenter: go.Point = new go.Point();
34
35 /**
36 * Constructs a NodeLabelDraggingTool and sets the name for the tool.
37 */
38 constructor() {
39 super();
40 this.name = 'NodeLabelDragging';
41 }
42
43 /**
44 * From the GraphObject at the mouse point, search up the visual tree until we get to
45 * an object that has the "_isNodeLabel" property set to true, that is in a Spot Panel,
46 * and that is not the first element of that Panel (i.e. not the main element of the panel).
47 * @return {GraphObject} This returns null if no such label is at the mouse down point.
48 */
49 public findLabel(): go.GraphObject | null {
50 const diagram = this.diagram;
51 const e = diagram.firstInput;
52 let elt = diagram.findObjectAt(e.documentPoint, null, null);
53
54 if (elt === null || !(elt.part instanceof go.Node)) return null;
55 while (elt.panel !== null) {
56 if ((elt as any)['_isNodeLabel'] && elt.panel.type === go.Panel.Spot && elt.panel.findMainElement() !== elt) return elt;
57 elt = elt.panel;
58 }
59 return null;
60 }
61
62 /**
63 * This tool can only start if the mouse has moved enough so that it is not a click,
64 * and if the mouse down point is on a GraphObject "label" in a Spot Panel,
65 * as determined by findLabel().
66 */
67 public canStart(): boolean {
68 if (!super.canStart()) return false;
69 const diagram = this.diagram;
70 // require left button & that it has moved far enough away from the mouse down point, so it isn't a click
71 const e = diagram.lastInput;
72 if (!e.left) return false;
73 if (!this.isBeyondDragSize()) return false;
74
75 return this.findLabel() !== null;
76 }
77
78 /**
79 * Start a transaction, call {@link findLabel} and remember it as the "label" property,
80 * and remember the original value for the label's {@link GraphObject#alignment} property.
81 */
82 public doActivate(): void {
83 this.startTransaction('Shifted Label');
84 this.label = this.findLabel();
85 if (this.label !== null) {
86 // compute the offset of the mouse-down point relative to the center of the label
87 this._offset = this.diagram.firstInput.documentPoint.copy().subtract(this.label.getDocumentPoint(go.Spot.Center));
88 this._originalAlignment = this.label.alignment.copy();
89 const panel = this.label.panel;
90 if (panel !== null) {
91 const main = panel.findMainElement();
92 if (main !== null) this._originalCenter = main.getDocumentPoint(go.Spot.Center);
93 }
94 }
95 super.doActivate();
96 }
97
98 /**
99 * Stop any ongoing transaction.
100 */
101 public doDeactivate(): void {
102 super.doDeactivate();
103 this.stopTransaction();
104 }
105
106 /**
107 * Clear any reference to a label element.
108 */
109 public doStop(): void {
110 this.label = null;
111 super.doStop();
112 }
113
114 /**
115 * Restore the label's original value for GraphObject.alignment.
116 */
117 public doCancel(): void {
118 if (this.label !== null) {
119 this.label.alignment = this._originalAlignment;
120 }
121 super.doCancel();
122 }
123
124 /**
125 * During the drag, call updateAlignment in order to set the {@link GraphObject#alignment} of the label.
126 */
127 public doMouseMove(): void {
128 if (!this.isActive) return;
129 this.updateAlignment();
130 }
131
132 /**
133 * At the end of the drag, update the alignment of the label and finish the tool,
134 * completing a transaction.
135 */
136 public doMouseUp(): void {
137 if (!this.isActive) return;
138 this.updateAlignment();
139 this.transactionResult = 'Shifted Label';
140 this.stopTool();
141 }
142
143 /**
144 * Save the label's {@link GraphObject#alignment} as an absolute offset from the center of the Spot Panel
145 * that the label is in.
146 */
147 public updateAlignment(): void {
148 if (this.label === null) return;
149 const last = this.diagram.lastInput.documentPoint;
150 const cntr = this._originalCenter;
151 this.label.alignment = new go.Spot(0.5, 0.5, last.x - this._offset.x - cntr.x, last.y - this._offset.y - cntr.y);
152 }
153}