3 * Guacamole - Clientless Remote Desktop
4 * Copyright (C) 2010 Michael Jumper
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // Guacamole namespace
21 var Guacamole = Guacamole || {};
24 * Provides cross-browser mouse events for a given element. The events of
25 * the given element are automatically populated with handlers that translate
26 * mouse events into a non-browser-specific event provided by the
27 * Guacamole.Mouse instance.
29 * Touch event support is planned, but currently only in testing (translate
30 * touch events into mouse events).
33 * @param {Element} element The Element to use to provide mouse events.
35 Guacamole.Mouse = function(element) {
38 * Reference to this Guacamole.Mouse.
41 var guac_mouse = this;
44 * The current mouse state. The properties of this state are updated when
45 * mouse events fire. This state object is also passed in as a parameter to
46 * the handler of any mouse events.
48 * @type Guacamole.Mouse.State
50 this.currentState = new Guacamole.Mouse.State(
52 false, false, false, false, false
56 * Fired whenever the user presses a mouse button down over the element
57 * associated with this Guacamole.Mouse.
60 * @param {Guacamole.Mouse.State} state The current mouse state.
62 this.onmousedown = null;
65 * Fired whenever the user releases a mouse button down over the element
66 * associated with this Guacamole.Mouse.
69 * @param {Guacamole.Mouse.State} state The current mouse state.
71 this.onmouseup = null;
74 * Fired whenever the user moves the mouse over the element associated with
75 * this Guacamole.Mouse.
78 * @param {Guacamole.Mouse.State} state The current mouse state.
80 this.onmousemove = null;
82 function moveMouse(pageX, pageY) {
84 guac_mouse.currentState.x = pageX - element.offsetLeft;
85 guac_mouse.currentState.y = pageY - element.offsetTop;
87 // This is all JUST so we can get the mouse position within the element
88 var parent = element.offsetParent;
90 if (parent.offsetLeft && parent.offsetTop) {
91 guac_mouse.currentState.x -= parent.offsetLeft;
92 guac_mouse.currentState.y -= parent.offsetTop;
94 parent = parent.offsetParent;
97 if (guac_mouse.onmousemove)
98 guac_mouse.onmousemove(guac_mouse.currentState);
103 // Block context menu so right-click gets sent properly
104 element.oncontextmenu = function(e) {
108 element.onmousemove = function(e) {
112 moveMouse(e.pageX, e.pageY);
116 var last_touch_x = 0;
117 var last_touch_y = 0;
119 element.ontouchend = function(e) {
124 // TODO: Handle tap-to-click.
128 element.ontouchstart = function(e) {
133 // Record initial touch location and time for single-touch movement
135 if (e.touches.length == 1) {
137 var starting_touch = e.touches[0];
138 last_touch_x = starting_touch.pageX;
139 last_touch_y = starting_touch.pageY;
141 // TODO: Record time (for sake of tap-to-click)
147 element.ontouchmove = function(e) {
152 // Handle single-touch movement gesture (touchpad mouse move)
153 if (e.touches.length == 1) {
155 // Get change in touch location
156 var touch = e.touches[0];
157 var delta_x = touch.pageX - last_touch_x;
158 var delta_y = touch.pageY - last_touch_y;
160 // Update mouse location
161 guac_mouse.currentState.x += delta_x;
162 guac_mouse.currentState.y += delta_y;
164 // FIXME: Prevent mouse from leaving screen
166 // Fire movement event, if defined
167 if (guac_mouse.onmousemove)
168 guac_mouse.onmousemove(guac_mouse.currentState);
170 // Update touch location
171 last_touch_x = touch.pageX;
172 last_touch_y = touch.pageY;
179 element.onmousedown = function(e) {
185 guac_mouse.currentState.left = true;
188 guac_mouse.currentState.middle = true;
191 guac_mouse.currentState.right = true;
195 if (guac_mouse.onmousedown)
196 guac_mouse.onmousedown(guac_mouse.currentState);
201 element.onmouseup = function(e) {
207 guac_mouse.currentState.left = false;
210 guac_mouse.currentState.middle = false;
213 guac_mouse.currentState.right = false;
217 if (guac_mouse.onmouseup)
218 guac_mouse.onmouseup(guac_mouse.currentState);
222 element.onmouseout = function(e) {
226 // Release all buttons
227 if (guac_mouse.currentState.left
228 || guac_mouse.currentState.middle
229 || guac_mouse.currentState.right) {
231 guac_mouse.currentState.left = false;
232 guac_mouse.currentState.middle = false;
233 guac_mouse.currentState.right = false;
235 if (guac_mouse.onmouseup)
236 guac_mouse.onmouseup(guac_mouse.currentState);
241 // Override selection on mouse event element.
242 element.onselectstart = function() {
246 // Scroll wheel support
247 function handleScroll(e) {
252 else if (e.wheelDelta)
253 delta = -event.wheelDelta;
257 if (guac_mouse.onmousedown) {
258 guac_mouse.currentState.up = true;
259 guac_mouse.onmousedown(guac_mouse.currentState);
262 if (guac_mouse.onmouseup) {
263 guac_mouse.currentState.up = false;
264 guac_mouse.onmouseup(guac_mouse.currentState);
270 if (guac_mouse.onmousedown) {
271 guac_mouse.currentState.down = true;
272 guac_mouse.onmousedown(guac_mouse.currentState);
275 if (guac_mouse.onmouseup) {
276 guac_mouse.currentState.down = false;
277 guac_mouse.onmouseup(guac_mouse.currentState);
281 if (e.preventDefault)
284 e.returnValue = false;
287 element.addEventListener('DOMMouseScroll', handleScroll, false);
289 element.onmousewheel = function(e) {
296 * Simple container for properties describing the state of a mouse.
299 * @param {Number} x The X position of the mouse pointer in pixels.
300 * @param {Number} y The Y position of the mouse pointer in pixels.
301 * @param {Boolean} left Whether the left mouse button is pressed.
302 * @param {Boolean} middle Whether the middle mouse button is pressed.
303 * @param {Boolean} right Whether the right mouse button is pressed.
304 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
305 * button, usually part of a scroll wheel).
306 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
307 * button, usually part of a scroll wheel).
309 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
312 * The current X position of the mouse pointer.
318 * The current Y position of the mouse pointer.
324 * Whether the left mouse button is currently pressed.
330 * Whether the middle mouse button is currently pressed.
336 * Whether the right mouse button is currently pressed.
342 * Whether the up mouse button is currently pressed. This is the fourth
343 * mouse button, associated with upward scrolling of the mouse scroll
350 * Whether the down mouse button is currently pressed. This is the fifth
351 * mouse button, associated with downward scrolling of the mouse scroll