gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
96 lines (88 loc) • 3.07 kB
JavaScript
function WireDragTool() {
go.Tool.call(this);
this.name = "WireDragTool";
/** @type {number} */
this._delay = 175;
/** @type {Node} */
this.moveNode = null;
}
go.Diagram.inherit(WireDragTool, go.Tool);
/**
* This tool can run when there has been a mouse-drag, far enough away not to be a click,
* and there has been delay of at least {@link #delay} milliseconds
* after the mouse-down before a mouse-move.
* <p/>
* This method may be overridden.
* @this {WireDragTool}
* @return {boolean}
*/
WireDragTool.prototype.canStart = function() {
if (!this.isEnabled) return false;
var diagram = this.diagram;
if (diagram === null) return false;
// heed IsReadOnly & AllowInsert
if (diagram.isReadOnly || diagram.isModelReadOnly) return false;
if (!diagram.allowInsert) return false;
var e = diagram.lastInput;
// require left button & that it has moved far enough away from the mouse down point, so it isn't a click
if (!e.left) return false;
// don't include the following checks when this tool is running modally
if (diagram.currentTool !== this) {
if (!this.isBeyondDragSize()) return false;
// must wait for "delay" milliseconds before that tool can run
if (e.timestamp - diagram.firstInput.timestamp < this.delay) return false;
}
return true;
};
/**
* Capture the mouse and creates the wire endpoints, then creates a link between them
* @this {WireDragTool}
*/
WireDragTool.prototype.doActivate = function() {
var diagram = this.diagram;
if (diagram === null) return;
this.isActive = true;
diagram.isMouseCaptured = true;
var label1 = { category: "junction" }; // Create empty NodeData
var label2 = { category: "junction" };
diagram.model.addNodeData(label1);
diagram.model.addNodeData(label2);
var node1 = diagram.findNodeForData(label1);
var node2 = diagram.findNodeForData(label2);
node1.location = diagram.firstInput.documentPoint;
node2.location = diagram.lastInput.documentPoint;
this.moveNode = node2; // remembers node2 for moving
diagram.model.addLinkData({
from: node1.key,
to: node2.key,
fromPort: "inout",
toPort: "inout"
});
this.doMouseMove();
};
/**
* Update the second junction's position according to the mouse location
* @this {WireDragTool}
*/
WireDragTool.prototype.doMouseMove = function() {
var diagram = this.diagram;
if (diagram === null) return;
if (this.isActive) {
this.moveNode.location = diagram.lastInput.documentPoint;
}
};
// Public properties
/**
* Gets or sets the time in milliseconds for which the mouse must be stationary
* before this tool can be started.
* The default value is 175 milliseconds.
* A value of zero will allow this tool to run without any wait after the mouse down.
* Setting this property does not raise any events.
* @name WireDragTool#delay
* @function.
* @return {number}
*/
Object.defineProperty(WireDragTool.prototype, "delay", {
get: function() { return this._delay; },
set: function(val) { this._delay = val; }
});