forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeLabelDraggingTool.js
More file actions
145 lines (131 loc) · 4.44 KB
/
Copy pathNodeLabelDraggingTool.js
File metadata and controls
145 lines (131 loc) · 4.44 KB
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
"use strict";
/*
* Copyright (C) 1998-2015 by Northwoods Software Corporation. All Rights Reserved.
*/
// A custom Tool for moving a label on a Node
/**
* @constructor
* @extends Tool
* @class
* This tool only works when the Node has a label (any GraphObject) marked with
* { _isNodeLabel: true } that is positioned in a Spot Panel.
* It works by modifying that label's GraphObject.alignment property to have an
* offset from the center of the panel.
*/
function NodeLabelDraggingTool() {
go.Tool.call(this);
this.name = "NodeLabelDragging";
/** @type {GraphObject} */
this.label = null;
/** @type {Point} */
this._originalAlignment = null;
/** @type {Point} */
this._originalCenter = null;
}
go.Diagram.inherit(NodeLabelDraggingTool, go.Tool);
/**
* This tool can only start if the mouse has moved enough so that it is not a click,
* and if the mouse down point is on a GraphObject "label" in a Spot Panel,
* as determined by findLabel().
* @this {NodeLabelDraggingTool}
* @return {boolean}
*/
NodeLabelDraggingTool.prototype.canStart = function() {
if (!go.Tool.prototype.canStart.call(this)) return false;
var diagram = this.diagram;
if (diagram === null) return false;
// require left button & that it has moved far enough away from the mouse down point, so it isn't a click
var e = diagram.lastInput;
if (!e.left) return false;
if (!this.isBeyondDragSize()) return false;
return this.findLabel() !== null;
}
/**
* From the GraphObject at the mouse point, search up the visual tree until we get to
* an object that has the "_isNodeLabel" property set to true, that is in a Spot Panel,
* and that is not the first element of that Panel (i.e. not the main element of the panel).
* @this {NodeLabelDraggingTool}
* @return {GraphObject} This returns null if no such label is at the mouse down point.
*/
NodeLabelDraggingTool.prototype.findLabel = function() {
var diagram = this.diagram;
var e = diagram.firstInput;
var elt = diagram.findObjectAt(e.documentPoint, null, null);
if (elt === null || !(elt.part instanceof go.Node)) return null;
while (elt.panel !== null && elt.panel.type === go.Panel.Spot && elt.panel.elt(0) !== elt) {
if (elt._isNodeLabel) return elt;
elt = elt.panel;
}
return null;
};
/**
* Start a transaction, call findLabel and remember it as the "label" property,
* and remember the original value for the label's alignment property.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.doActivate = function() {
this.startTransaction("Shifted Label");
this.label = this.findLabel();
if (this.label !== null) {
this._originalAlignment = this.label.alignment.copy();
var main = this.label.panel.findMainElement();
this._originalCenter = main.getDocumentPoint(go.Spot.Center);
}
go.Tool.prototype.doActivate.call(this);
}
/**
* Stop any ongoing transaction.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.doDeactivate = function() {
go.Tool.prototype.doDeactivate.call(this);
this.stopTransaction();
}
/**
* Clear any reference to a label element.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.doStop = function() {
this.label = null;
go.Tool.prototype.doStop.call(this);
}
/**
* Restore the label's original value for GraphObject.alignment.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.doCancel = function() {
if (this.label !== null) {
this.label.alignment = this._originalAlignment;
}
go.Tool.prototype.doCancel.call(this);
}
/**
* During the drag, call updateAlignment in order to set the GraphObject.alignment of the label.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.doMouseMove = function() {
if (!this.isActive) return;
this.updateAlignment();
}
/**
* At the end of the drag, update the alignment of the label and finish the tool,
* completing a transaction.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.doMouseUp = function() {
if (!this.isActive) return;
this.updateAlignment();
this.transactionResult = "Shifted Label";
this.stopTool();
}
/**
* Save the label's GraphObject.alignment as an absolute offset from the center of the Spot Panel
* that the label is in.
* @this {NodeLabelDraggingTool}
*/
NodeLabelDraggingTool.prototype.updateAlignment = function() {
if (this.label === null) return;
var last = this.diagram.lastInput.documentPoint;
var cntr = this._originalCenter;
this.label.alignment = new go.Spot(0.5, 0.5, last.x - cntr.x, last.y - cntr.y);
}