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.
48 * @param {Element} element The Element to use to provide mouse events.
50 Guacamole.Mouse = function(element) {
53 * Reference to this Guacamole.Mouse.
56 var guac_mouse = this;
59 * The current mouse state. The properties of this state are updated when
60 * mouse events fire. This state object is also passed in as a parameter to
61 * the handler of any mouse events.
63 * @type Guacamole.Mouse.State
65 this.currentState = new Guacamole.Mouse.State(
67 false, false, false, false, false
71 * Fired whenever the user presses a mouse button down over the element
72 * associated with this Guacamole.Mouse.
75 * @param {Guacamole.Mouse.State} state The current mouse state.
77 this.onmousedown = null;
80 * Fired whenever the user releases a mouse button down over the element
81 * associated with this Guacamole.Mouse.
84 * @param {Guacamole.Mouse.State} state The current mouse state.
86 this.onmouseup = null;
89 * Fired whenever the user moves the mouse over the element associated with
90 * this Guacamole.Mouse.
93 * @param {Guacamole.Mouse.State} state The current mouse state.
95 this.onmousemove = null;
97 function cancelEvent(e) {
99 if (e.preventDefault) e.preventDefault();
100 e.returnValue = false;
103 function moveMouse(clientX, clientY) {
105 guac_mouse.currentState.x = clientX - element.offsetLeft;
106 guac_mouse.currentState.y = clientY - element.offsetTop;
108 // This is all JUST so we can get the mouse position within the element
109 var parent = element.offsetParent;
110 while (parent && !(parent === document.body)) {
111 guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
112 guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
114 parent = parent.offsetParent;
117 // Offset by document scroll amount
118 var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
119 var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
121 guac_mouse.currentState.x -= parent.offsetLeft - documentScrollLeft;
122 guac_mouse.currentState.y -= parent.offsetTop - documentScrollTop;
124 if (guac_mouse.onmousemove)
125 guac_mouse.onmousemove(guac_mouse.currentState);
130 // Block context menu so right-click gets sent properly
131 element.addEventListener("contextmenu", function(e) {
135 element.addEventListener("mousemove", function(e) {
139 moveMouse(e.clientX, e.clientY);
143 element.addEventListener("mousedown", function(e) {
149 guac_mouse.currentState.left = true;
152 guac_mouse.currentState.middle = true;
155 guac_mouse.currentState.right = true;
159 if (guac_mouse.onmousedown)
160 guac_mouse.onmousedown(guac_mouse.currentState);
164 element.addEventListener("mouseup", function(e) {
170 guac_mouse.currentState.left = false;
173 guac_mouse.currentState.middle = false;
176 guac_mouse.currentState.right = false;
180 if (guac_mouse.onmouseup)
181 guac_mouse.onmouseup(guac_mouse.currentState);
185 element.addEventListener("mouseout", function(e) {
187 // Get parent of the element the mouse pointer is leaving
188 if (!e) e = window.event;
190 // Check that mouseout is due to actually LEAVING the element
191 var target = e.relatedTarget || e.toElement;
192 while (target != null) {
193 if (target === element)
195 target = target.parentNode;
200 // Release all buttons
201 if (guac_mouse.currentState.left
202 || guac_mouse.currentState.middle
203 || guac_mouse.currentState.right) {
205 guac_mouse.currentState.left = false;
206 guac_mouse.currentState.middle = false;
207 guac_mouse.currentState.right = false;
209 if (guac_mouse.onmouseup)
210 guac_mouse.onmouseup(guac_mouse.currentState);
215 // Override selection on mouse event element.
216 element.addEventListener("selectstart", function(e) {
220 // Scroll wheel support
221 function mousewheel_handler(e) {
226 else if (e.wheelDelta)
227 delta = -event.wheelDelta;
231 if (guac_mouse.onmousedown) {
232 guac_mouse.currentState.up = true;
233 guac_mouse.onmousedown(guac_mouse.currentState);
236 if (guac_mouse.onmouseup) {
237 guac_mouse.currentState.up = false;
238 guac_mouse.onmouseup(guac_mouse.currentState);
244 if (guac_mouse.onmousedown) {
245 guac_mouse.currentState.down = true;
246 guac_mouse.onmousedown(guac_mouse.currentState);
249 if (guac_mouse.onmouseup) {
250 guac_mouse.currentState.down = false;
251 guac_mouse.onmouseup(guac_mouse.currentState);
258 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
259 element.addEventListener('mousewheel', mousewheel_handler, false);
265 * Provides cross-browser touch event translation for a given element.
267 * Touch events are translated into mouse events as if the touches occurred
268 * on a touchpad (drag to push the mouse pointer, tap to click).
271 * @param {Element} element The Element to use to provide touch events.
273 Guacamole.Mouse.Touchpad = function(element) {
276 * Reference to this Guacamole.Mouse.
279 var guac_mouse = this;
282 * The distance a two-finger touch must move per scrollwheel event, in
285 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
288 * The maximum number of milliseconds to wait for a touch to end for the
289 * gesture to be considered a click.
291 this.clickTimingThreshold = 250;
294 * The maximum number of pixels to allow a touch to move for the gesture to
295 * be considered a click.
297 this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
300 * The current mouse state. The properties of this state are updated when
301 * mouse events fire. This state object is also passed in as a parameter to
302 * the handler of any mouse events.
304 * @type Guacamole.Mouse.State
306 this.currentState = new Guacamole.Mouse.State(
308 false, false, false, false, false
312 * Fired whenever the user presses a mouse button down over the element
313 * associated with this Guacamole.Mouse.
316 * @param {Guacamole.Mouse.State} state The current mouse state.
318 this.onmousedown = null;
321 * Fired whenever the user releases a mouse button down over the element
322 * associated with this Guacamole.Mouse.
325 * @param {Guacamole.Mouse.State} state The current mouse state.
327 this.onmouseup = null;
330 * Fired whenever the user moves the mouse over the element associated with
331 * this Guacamole.Mouse.
334 * @param {Guacamole.Mouse.State} state The current mouse state.
336 this.onmousemove = null;
338 function cancelEvent(e) {
340 if (e.preventDefault) e.preventDefault();
341 e.returnValue = false;
345 var last_touch_x = 0;
346 var last_touch_y = 0;
347 var last_touch_time = 0;
348 var pixels_moved = 0;
350 var touch_buttons = {
356 var gesture_in_progress = false;
357 var click_release_timeout = null;
359 element.addEventListener("touchend", function(e) {
363 // If we're handling a gesture AND this is the last touch
364 if (gesture_in_progress && e.touches.length == 0) {
366 var time = new Date().getTime();
368 // Get corresponding mouse button
369 var button = touch_buttons[touch_count];
371 // If mouse already down, release anad clear timeout
372 if (guac_mouse.currentState[button]) {
374 // Fire button up event
375 guac_mouse.currentState[button] = false;
376 if (guac_mouse.onmouseup)
377 guac_mouse.onmouseup(guac_mouse.currentState);
379 // Clear timeout, if set
380 if (click_release_timeout) {
381 window.clearTimeout(click_release_timeout);
382 click_release_timeout = null;
387 // If single tap detected (based on time and distance)
388 if (time - last_touch_time <= guac_mouse.clickTimingThreshold
389 && pixels_moved < guac_mouse.clickMoveThreshold) {
391 // Fire button down event
392 guac_mouse.currentState[button] = true;
393 if (guac_mouse.onmousedown)
394 guac_mouse.onmousedown(guac_mouse.currentState);
396 // Delay mouse up - mouse up should be canceled if
397 // touchstart within timeout.
398 click_release_timeout = window.setTimeout(function() {
400 // Fire button up event
401 guac_mouse.currentState[button] = false;
402 if (guac_mouse.onmouseup)
403 guac_mouse.onmouseup(guac_mouse.currentState);
406 gesture_in_progress = false;
408 }, guac_mouse.clickTimingThreshold);
412 // If we're not waiting to see if this is a click, stop gesture
413 if (!click_release_timeout)
414 gesture_in_progress = false;
420 element.addEventListener("touchstart", function(e) {
424 // Track number of touches, but no more than three
425 touch_count = Math.min(e.touches.length, 3);
427 // Clear timeout, if set
428 if (click_release_timeout) {
429 window.clearTimeout(click_release_timeout);
430 click_release_timeout = null;
433 // Record initial touch location and time for touch movement
435 if (!gesture_in_progress) {
437 // Stop mouse events while touching
438 gesture_in_progress = true;
440 // Record touch location and time
441 var starting_touch = e.touches[0];
442 last_touch_x = starting_touch.clientX;
443 last_touch_y = starting_touch.clientY;
444 last_touch_time = new Date().getTime();
451 element.addEventListener("touchmove", function(e) {
455 // Get change in touch location
456 var touch = e.touches[0];
457 var delta_x = touch.clientX - last_touch_x;
458 var delta_y = touch.clientY - last_touch_y;
460 // Track pixels moved
461 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
463 // If only one touch involved, this is mouse move
464 if (touch_count == 1) {
466 // Calculate average velocity in Manhatten pixels per millisecond
467 var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
469 // Scale mouse movement relative to velocity
470 var scale = 1 + velocity;
472 // Update mouse location
473 guac_mouse.currentState.x += delta_x*scale;
474 guac_mouse.currentState.y += delta_y*scale;
476 // Prevent mouse from leaving screen
478 if (guac_mouse.currentState.x < 0)
479 guac_mouse.currentState.x = 0;
480 else if (guac_mouse.currentState.x >= element.offsetWidth)
481 guac_mouse.currentState.x = element.offsetWidth - 1;
483 if (guac_mouse.currentState.y < 0)
484 guac_mouse.currentState.y = 0;
485 else if (guac_mouse.currentState.y >= element.offsetHeight)
486 guac_mouse.currentState.y = element.offsetHeight - 1;
488 // Fire movement event, if defined
489 if (guac_mouse.onmousemove)
490 guac_mouse.onmousemove(guac_mouse.currentState);
492 // Update touch location
493 last_touch_x = touch.clientX;
494 last_touch_y = touch.clientY;
498 // Interpret two-finger swipe as scrollwheel
499 else if (touch_count == 2) {
501 // If change in location passes threshold for scroll
502 if (Math.abs(delta_y) >= guac_mouse.scrollThreshold) {
504 // Decide button based on Y movement direction
506 if (delta_y > 0) button = "down";
509 // Fire button down event
510 guac_mouse.currentState[button] = true;
511 if (guac_mouse.onmousedown)
512 guac_mouse.onmousedown(guac_mouse.currentState);
514 // Fire button up event
515 guac_mouse.currentState[button] = false;
516 if (guac_mouse.onmouseup)
517 guac_mouse.onmouseup(guac_mouse.currentState);
519 // Only update touch location after a scroll has been
521 last_touch_x = touch.clientX;
522 last_touch_y = touch.clientY;
533 * Simple container for properties describing the state of a mouse.
536 * @param {Number} x The X position of the mouse pointer in pixels.
537 * @param {Number} y The Y position of the mouse pointer in pixels.
538 * @param {Boolean} left Whether the left mouse button is pressed.
539 * @param {Boolean} middle Whether the middle mouse button is pressed.
540 * @param {Boolean} right Whether the right mouse button is pressed.
541 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
542 * button, usually part of a scroll wheel).
543 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
544 * button, usually part of a scroll wheel).
546 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
549 * The current X position of the mouse pointer.
555 * The current Y position of the mouse pointer.
561 * Whether the left mouse button is currently pressed.
567 * Whether the middle mouse button is currently pressed.
573 * Whether the right mouse button is currently pressed.
579 * Whether the up mouse button is currently pressed. This is the fourth
580 * mouse button, associated with upward scrolling of the mouse scroll
587 * Whether the down mouse button is currently pressed. This is the fifth
588 * mouse button, associated with downward scrolling of the mouse scroll