2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacamole-common-js.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
42 * Provides cross-browser mouse events for a given element. The events of
43 * the given element are automatically populated with handlers that translate
44 * mouse events into a non-browser-specific event provided by the
45 * Guacamole.Mouse instance.
47 * Touch events are translated into mouse events as if the touches occurred
48 * on a touchpad (drag to push the mouse pointer, tap to click).
51 * @param {Element} element The Element to use to provide mouse events.
53 Guacamole.Mouse = function(element) {
56 * Reference to this Guacamole.Mouse.
59 var guac_mouse = this;
62 * The distance a two-finger touch must move per scrollwheel event, in
65 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
68 * The maximum number of milliseconds to wait for a touch to end for the
69 * gesture to be considered a click.
71 this.clickTimingThreshold = 250;
74 * The maximum number of pixels to allow a touch to move for the gesture to
75 * be considered a click.
77 this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
80 * The current mouse state. The properties of this state are updated when
81 * mouse events fire. This state object is also passed in as a parameter to
82 * the handler of any mouse events.
84 * @type Guacamole.Mouse.State
86 this.currentState = new Guacamole.Mouse.State(
88 false, false, false, false, false
92 * Fired whenever the user presses a mouse button down over the element
93 * associated with this Guacamole.Mouse.
96 * @param {Guacamole.Mouse.State} state The current mouse state.
98 this.onmousedown = null;
101 * Fired whenever the user releases a mouse button down over the element
102 * associated with this Guacamole.Mouse.
105 * @param {Guacamole.Mouse.State} state The current mouse state.
107 this.onmouseup = null;
110 * Fired whenever the user moves the mouse over the element associated with
111 * this Guacamole.Mouse.
114 * @param {Guacamole.Mouse.State} state The current mouse state.
116 this.onmousemove = null;
118 function cancelEvent(e) {
120 if (e.preventDefault) e.preventDefault();
121 e.returnValue = false;
124 function moveMouse(clientX, clientY) {
126 guac_mouse.currentState.x = clientX - element.offsetLeft;
127 guac_mouse.currentState.y = clientY - element.offsetTop;
129 // This is all JUST so we can get the mouse position within the element
130 var parent = element.offsetParent;
131 while (parent && !(parent === document.body)) {
132 guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
133 guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
135 parent = parent.offsetParent;
138 // Offset by document scroll amount
139 var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
140 var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
142 guac_mouse.currentState.x -= parent.offsetLeft - documentScrollLeft;
143 guac_mouse.currentState.y -= parent.offsetTop - documentScrollTop;
145 if (guac_mouse.onmousemove)
146 guac_mouse.onmousemove(guac_mouse.currentState);
151 // Block context menu so right-click gets sent properly
152 element.addEventListener("contextmenu", function(e) {
156 element.addEventListener("mousemove", function(e) {
158 // Don't handle if we aren't supposed to
159 if (gesture_in_progress) return;
163 moveMouse(e.clientX, e.clientY);
168 var last_touch_x = 0;
169 var last_touch_y = 0;
170 var last_touch_time = 0;
171 var pixels_moved = 0;
173 var touch_buttons = {
179 var gesture_in_progress = false;
180 var click_release_timeout = null;
182 element.addEventListener("touchend", function(e) {
186 // If we're handling a gesture AND this is the last touch
187 if (gesture_in_progress && e.touches.length == 0) {
189 var time = new Date().getTime();
191 // Get corresponding mouse button
192 var button = touch_buttons[touch_count];
194 // If mouse already down, release anad clear timeout
195 if (guac_mouse.currentState[button]) {
197 // Fire button up event
198 guac_mouse.currentState[button] = false;
199 if (guac_mouse.onmouseup)
200 guac_mouse.onmouseup(guac_mouse.currentState);
202 // Clear timeout, if set
203 if (click_release_timeout) {
204 window.clearTimeout(click_release_timeout);
205 click_release_timeout = null;
210 // If single tap detected (based on time and distance)
211 if (time - last_touch_time <= guac_mouse.clickTimingThreshold
212 && pixels_moved < guac_mouse.clickMoveThreshold) {
214 // Fire button down event
215 guac_mouse.currentState[button] = true;
216 if (guac_mouse.onmousedown)
217 guac_mouse.onmousedown(guac_mouse.currentState);
219 // Delay mouse up - mouse up should be canceled if
220 // touchstart within timeout.
221 click_release_timeout = window.setTimeout(function() {
223 // Fire button up event
224 guac_mouse.currentState[button] = false;
225 if (guac_mouse.onmouseup)
226 guac_mouse.onmouseup(guac_mouse.currentState);
229 gesture_in_progress = false;
231 }, guac_mouse.clickTimingThreshold);
235 // If we're not waiting to see if this is a click, stop gesture
236 if (!click_release_timeout)
237 gesture_in_progress = false;
243 element.addEventListener("touchstart", function(e) {
247 // Track number of touches, but no more than three
248 touch_count = Math.min(e.touches.length, 3);
250 // Clear timeout, if set
251 if (click_release_timeout) {
252 window.clearTimeout(click_release_timeout);
253 click_release_timeout = null;
256 // Record initial touch location and time for touch movement
258 if (!gesture_in_progress) {
260 // Stop mouse events while touching
261 gesture_in_progress = true;
263 // Record touch location and time
264 var starting_touch = e.touches[0];
265 last_touch_x = starting_touch.clientX;
266 last_touch_y = starting_touch.clientY;
267 last_touch_time = new Date().getTime();
274 element.addEventListener("touchmove", function(e) {
278 // Get change in touch location
279 var touch = e.touches[0];
280 var delta_x = touch.clientX - last_touch_x;
281 var delta_y = touch.clientY - last_touch_y;
283 // Track pixels moved
284 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
286 // If only one touch involved, this is mouse move
287 if (touch_count == 1) {
289 // Calculate average velocity in Manhatten pixels per millisecond
290 var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
292 // Scale mouse movement relative to velocity
293 var scale = 1 + velocity;
295 // Update mouse location
296 guac_mouse.currentState.x += delta_x*scale;
297 guac_mouse.currentState.y += delta_y*scale;
299 // Prevent mouse from leaving screen
301 if (guac_mouse.currentState.x < 0)
302 guac_mouse.currentState.x = 0;
303 else if (guac_mouse.currentState.x >= element.offsetWidth)
304 guac_mouse.currentState.x = element.offsetWidth - 1;
306 if (guac_mouse.currentState.y < 0)
307 guac_mouse.currentState.y = 0;
308 else if (guac_mouse.currentState.y >= element.offsetHeight)
309 guac_mouse.currentState.y = element.offsetHeight - 1;
311 // Fire movement event, if defined
312 if (guac_mouse.onmousemove)
313 guac_mouse.onmousemove(guac_mouse.currentState);
315 // Update touch location
316 last_touch_x = touch.clientX;
317 last_touch_y = touch.clientY;
321 // Interpret two-finger swipe as scrollwheel
322 else if (touch_count == 2) {
324 // If change in location passes threshold for scroll
325 if (Math.abs(delta_y) >= guac_mouse.scrollThreshold) {
327 // Decide button based on Y movement direction
329 if (delta_y > 0) button = "down";
332 // Fire button down event
333 guac_mouse.currentState[button] = true;
334 if (guac_mouse.onmousedown)
335 guac_mouse.onmousedown(guac_mouse.currentState);
337 // Fire button up event
338 guac_mouse.currentState[button] = false;
339 if (guac_mouse.onmouseup)
340 guac_mouse.onmouseup(guac_mouse.currentState);
342 // Only update touch location after a scroll has been
344 last_touch_x = touch.clientX;
345 last_touch_y = touch.clientY;
354 element.addEventListener("mousedown", function(e) {
356 // Don't handle if we aren't supposed to
357 if (gesture_in_progress) return;
363 guac_mouse.currentState.left = true;
366 guac_mouse.currentState.middle = true;
369 guac_mouse.currentState.right = true;
373 if (guac_mouse.onmousedown)
374 guac_mouse.onmousedown(guac_mouse.currentState);
379 element.addEventListener("mouseup", function(e) {
381 // Don't handle if we aren't supposed to
382 if (gesture_in_progress) return;
388 guac_mouse.currentState.left = false;
391 guac_mouse.currentState.middle = false;
394 guac_mouse.currentState.right = false;
398 if (guac_mouse.onmouseup)
399 guac_mouse.onmouseup(guac_mouse.currentState);
403 element.addEventListener("mouseout", function(e) {
405 // Don't handle if we aren't supposed to
406 if (gesture_in_progress) return;
408 // Get parent of the element the mouse pointer is leaving
409 if (!e) e = window.event;
411 // Check that mouseout is due to actually LEAVING the element
412 var target = e.relatedTarget || e.toElement;
413 while (target != null) {
414 if (target === element)
416 target = target.parentNode;
421 // Release all buttons
422 if (guac_mouse.currentState.left
423 || guac_mouse.currentState.middle
424 || guac_mouse.currentState.right) {
426 guac_mouse.currentState.left = false;
427 guac_mouse.currentState.middle = false;
428 guac_mouse.currentState.right = false;
430 if (guac_mouse.onmouseup)
431 guac_mouse.onmouseup(guac_mouse.currentState);
436 // Override selection on mouse event element.
437 element.addEventListener("selectstart", function(e) {
441 // Scroll wheel support
442 function mousewheel_handler(e) {
444 // Don't handle if we aren't supposed to
445 if (gesture_in_progress) return;
450 else if (e.wheelDelta)
451 delta = -event.wheelDelta;
455 if (guac_mouse.onmousedown) {
456 guac_mouse.currentState.up = true;
457 guac_mouse.onmousedown(guac_mouse.currentState);
460 if (guac_mouse.onmouseup) {
461 guac_mouse.currentState.up = false;
462 guac_mouse.onmouseup(guac_mouse.currentState);
468 if (guac_mouse.onmousedown) {
469 guac_mouse.currentState.down = true;
470 guac_mouse.onmousedown(guac_mouse.currentState);
473 if (guac_mouse.onmouseup) {
474 guac_mouse.currentState.down = false;
475 guac_mouse.onmouseup(guac_mouse.currentState);
482 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
483 element.addEventListener('mousewheel', mousewheel_handler, false);
488 * Simple container for properties describing the state of a mouse.
491 * @param {Number} x The X position of the mouse pointer in pixels.
492 * @param {Number} y The Y position of the mouse pointer in pixels.
493 * @param {Boolean} left Whether the left mouse button is pressed.
494 * @param {Boolean} middle Whether the middle mouse button is pressed.
495 * @param {Boolean} right Whether the right mouse button is pressed.
496 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
497 * button, usually part of a scroll wheel).
498 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
499 * button, usually part of a scroll wheel).
501 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
504 * The current X position of the mouse pointer.
510 * The current Y position of the mouse pointer.
516 * Whether the left mouse button is currently pressed.
522 * Whether the middle mouse button is currently pressed.
528 * Whether the right mouse button is currently pressed.
534 * Whether the up mouse button is currently pressed. This is the fourth
535 * mouse button, associated with upward scrolling of the mouse scroll
542 * Whether the down mouse button is currently pressed. This is the fifth
543 * mouse button, associated with downward scrolling of the mouse scroll