UNPKG

8.1 kBJavaScriptView Raw
1"use strict";
2/*
3* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
4*/
5
6// A class for simulating mouse and keyboard input.
7
8// As a special hack, this supports limited simulation of drag-and-drop between Diagrams,
9// by setting on the EVENTPROPS argument of the mouseDown/mouseMove/mouseUp methods
10// both the "sourceDiagram" and "targetDiagram" properties.
11// Although InputEvent.targetDiagram is a real property,
12// the "sourceDiagram" property is only used by these Robot methods.
13
14var isMac = (this.navigator !== undefined && this.navigator.platform !== undefined && this.navigator.platform.toUpperCase().indexOf('MAC') >= 0);
15
16/*
17* This is an extension and not part of the main GoJS library.
18* Note that the API for this class may change with any version, even point releases.
19* If you intend to use an extension in production, you should copy the code to your own source directory.
20* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
21* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
22*/
23
24/**
25* @constructor
26* @class
27* @param {Diagram=} dia the Diagram on which the Robot simulates input events
28*/
29function Robot(dia) {
30 if (dia === undefined) dia = null;
31 this.diagram = dia;
32}
33
34/**
35* @ignore
36* Transfer property settings from a JavaScript Object to an InputEvent.
37* @this {Robot}
38* @param {InputEvent} e
39* @param {Object} props
40*/
41Robot.prototype.initializeEvent = function(e, props) {
42 if (!props) return;
43 for (var p in props) {
44 if (p !== "sourceDiagram") e[p] = props[p];
45 // If people write control: true, switch to alt: true on macs, for ctrl+copy
46 if (p === "control") {
47 if (isMac) {
48 e['alt'] = props[p];
49 } else {
50 e[p] = props[p];
51 }
52 }
53 }
54};
55
56/**
57* Simulate a mouse down event.
58* @this {Robot}
59* @param {number} x the X-coordinate of the mouse point in document coordinates.
60* @param {number} y the Y-coordinate of the mouse point in document coordinates.
61* @param {number=} time the timestamp of the simulated event, in milliseconds; default zero
62* @param {object=} eventprops an optional argument providing properties for the InputEvent.
63*/
64Robot.prototype.mouseDown = function(x, y, time, eventprops) {
65 if (typeof x !== "number" || typeof y !== "number") throw new Error("Robot.mouseDown first two args must be X,Y numbers");
66 if (time === undefined) time = 0;
67
68 var diagram = this.diagram;
69 if (eventprops && eventprops.sourceDiagram) diagram = eventprops.sourceDiagram;
70 if (!diagram.isEnabled) return;
71
72 var n = new go.InputEvent();
73 n.diagram = diagram;
74 n.documentPoint = new go.Point(x, y);
75 n.viewPoint = diagram.transformDocToView(n.documentPoint);
76 n.timestamp = time;
77 n.down = true;
78 this.initializeEvent(n, eventprops);
79 diagram.lastInput = n;
80 diagram.firstInput = n.copy();
81 diagram.currentTool.doMouseDown();
82};
83
84/**
85* Simulate a mouse move event.
86* @this {Robot}
87* @param {number} x the X-coordinate of the mouse point in document coordinates.
88* @param {number} y the Y-coordinate of the mouse point in document coordinates.
89* @param {number=} time the timestamp of the simulated event, in milliseconds; default zero
90* @param {object=} eventprops an optional argument providing properties for the InputEvent.
91*/
92Robot.prototype.mouseMove = function(x, y, time, eventprops) {
93 if (typeof x !== "number" || typeof y !== "number") throw new Error("Robot.mouseMove first two args must be X,Y numbers");
94 if (time === undefined) time = 0;
95
96 var diagram = this.diagram;
97 if (eventprops && eventprops.sourceDiagram) diagram = eventprops.sourceDiagram;
98 if (!diagram.isEnabled) return;
99
100 var n = new go.InputEvent();
101 n.diagram = diagram;
102 n.documentPoint = new go.Point(x, y);
103 n.viewPoint = diagram.transformDocToView(n.documentPoint);
104 n.timestamp = time;
105 this.initializeEvent(n, eventprops);
106 diagram.lastInput = n;
107 diagram.currentTool.doMouseMove();
108};
109
110/**
111* Simulate a mouse up event.
112* @this {Robot}
113* @param {number} x the X-coordinate of the mouse point in document coordinates.
114* @param {number} y the Y-coordinate of the mouse point in document coordinates.
115* @param {number=} time the timestamp of the simulated event, in milliseconds; default zero
116* @param {object=} eventprops an optional argument providing properties for the InputEvent.
117*/
118Robot.prototype.mouseUp = function(x, y, time, eventprops) {
119 if (typeof x !== "number" || typeof y !== "number") throw new Error("Robot.mouseUp first two args must be X,Y numbers");
120 if (time === undefined) time = 0;
121
122 var diagram = this.diagram;
123 if (eventprops && eventprops.sourceDiagram) diagram = eventprops.sourceDiagram;
124 if (!diagram.isEnabled) return;
125
126 var n = new go.InputEvent();
127 n.diagram = diagram;
128 n.documentPoint = new go.Point(x, y);
129 n.viewPoint = diagram.transformDocToView(n.documentPoint);
130 n.timestamp = time;
131 n.up = true;
132 if (diagram.firstInput.documentPoint.equals(n.documentPoint)) n.clickCount = 1; // at least??
133 this.initializeEvent(n, eventprops);
134 diagram.lastInput = n;
135 // if (diagram.simulatedMouseUp(null, (n as any).sourceDiagram, n.documentPoint, n.targetDiagram)) return;
136 diagram.currentTool.doMouseUp();
137};
138
139/**
140* Simulate a mouse wheel event.
141* @this {Robot}
142* @param {number} delta non-zero turn
143* @param {number=} time the timestamp of the simulated event, in milliseconds; default zero
144* @param {object=} eventprops an optional argument providing properties for the InputEvent.
145*/
146Robot.prototype.mouseWheel = function(delta, time, eventprops) {
147 if (typeof delta !== "number") throw new Error("Robot.mouseWheel first arg must be DELTA number");
148 if (time === undefined) time = 0;
149
150 var diagram = this.diagram;
151 if (!diagram.isEnabled) return;
152
153 var n = diagram.lastInput.copy();
154 n.diagram = diagram;
155 n.delta = delta;
156 n.timestamp = time;
157 this.initializeEvent(n, eventprops);
158 diagram.lastInput = n;
159 diagram.currentTool.doMouseWheel();
160};
161
162/**
163* Simulate a key down event.
164* @this {Robot}
165* @param {string|number} keyorcode
166* @param {number=} time the timestamp of the simulated event, in milliseconds; default zero
167* @param {object=} eventprops an optional argument providing properties for the InputEvent.
168*/
169Robot.prototype.keyDown = function(keyorcode, time, eventprops) {
170 if (typeof keyorcode !== "string" && typeof keyorcode !== "number") throw new Error("Robot.keyDown first arg must be a string or a number");
171 if (time === undefined) time = 0;
172
173 var diagram = this.diagram;
174 if (!diagram.isEnabled) return;
175
176 var n = diagram.lastInput.copy();
177 n.diagram = diagram;
178 if (typeof (keyorcode) === 'string') {
179 n.key = keyorcode;
180 } else if (typeof (keyorcode) === 'number') {
181 n.key = String.fromCharCode(keyorcode);
182 }
183 n.timestamp = time;
184 n.down = true;
185 this.initializeEvent(n, eventprops);
186 diagram.lastInput = n;
187 diagram.currentTool.doKeyDown();
188};
189
190/**
191* Simulate a key up event.
192* @this {Robot}
193* @param {string|number} keyorcode
194* @param {number=} time the timestamp of the simulated event, in milliseconds; default zero
195* @param {object=} eventprops an optional argument providing properties for the InputEvent.
196*/
197Robot.prototype.keyUp = function(keyorcode, time, eventprops) {
198 if (typeof keyorcode !== "string" && typeof keyorcode !== "number") throw new Error("Robot.keyUp first arg must be a string or a number");
199 if (time === undefined) time = 0;
200
201 var diagram = this.diagram;
202 if (!diagram.isEnabled) return;
203
204 var n = diagram.lastInput.copy();
205 n.diagram = diagram;
206 if (typeof (keyorcode) === 'string') {
207 n.key = keyorcode;
208 } else if (typeof (keyorcode) === 'number') {
209 n.key = String.fromCharCode(keyorcode);
210 }
211 n.timestamp = time;
212 n.up = true;
213 this.initializeEvent(n, eventprops);
214 diagram.lastInput = n;
215 diagram.currentTool.doKeyUp();
216};