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 ***** */
39 * Namespace for all Guacamole JavaScript objects.
42 var Guacamole = Guacamole || {};
45 * Provides cross-browser mouse events for a given element. The events of
46 * the given element are automatically populated with handlers that translate
47 * mouse events into a non-browser-specific event provided by the
48 * Guacamole.Mouse instance.
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 number of mousemove events to require before re-enabling mouse
63 * event handling after receiving a touch event.
65 this.touchMouseThreshold = 3;
68 * The current mouse state. The properties of this state are updated when
69 * mouse events fire. This state object is also passed in as a parameter to
70 * the handler of any mouse events.
72 * @type Guacamole.Mouse.State
74 this.currentState = new Guacamole.Mouse.State(
76 false, false, false, false, false
80 * Fired whenever the user presses 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.onmousedown = null;
89 * Fired whenever the user releases a mouse button down over the element
90 * associated with this Guacamole.Mouse.
93 * @param {Guacamole.Mouse.State} state The current mouse state.
95 this.onmouseup = null;
98 * Fired whenever the user moves the mouse over the element associated with
99 * this Guacamole.Mouse.
102 * @param {Guacamole.Mouse.State} state The current mouse state.
104 this.onmousemove = null;
107 * Counter of mouse events to ignore. This decremented by mousemove, and
108 * while non-zero, mouse events will have no effect.
111 var ignore_mouse = 0;
113 function cancelEvent(e) {
115 if (e.preventDefault) e.preventDefault();
116 e.returnValue = false;
119 // Block context menu so right-click gets sent properly
120 element.addEventListener("contextmenu", function(e) {
124 element.addEventListener("mousemove", function(e) {
128 // If ignoring events, decrement counter
134 guac_mouse.currentState.fromClientPosition(element, e.clientX, e.clientY);
136 if (guac_mouse.onmousemove)
137 guac_mouse.onmousemove(guac_mouse.currentState);
141 element.addEventListener("mousedown", function(e) {
145 // Do not handle if ignoring events
151 guac_mouse.currentState.left = true;
154 guac_mouse.currentState.middle = true;
157 guac_mouse.currentState.right = true;
161 if (guac_mouse.onmousedown)
162 guac_mouse.onmousedown(guac_mouse.currentState);
166 element.addEventListener("mouseup", function(e) {
170 // Do not handle if ignoring events
176 guac_mouse.currentState.left = false;
179 guac_mouse.currentState.middle = false;
182 guac_mouse.currentState.right = false;
186 if (guac_mouse.onmouseup)
187 guac_mouse.onmouseup(guac_mouse.currentState);
191 element.addEventListener("mouseout", function(e) {
193 // Get parent of the element the mouse pointer is leaving
194 if (!e) e = window.event;
196 // Check that mouseout is due to actually LEAVING the element
197 var target = e.relatedTarget || e.toElement;
198 while (target != null) {
199 if (target === element)
201 target = target.parentNode;
206 // Release all buttons
207 if (guac_mouse.currentState.left
208 || guac_mouse.currentState.middle
209 || guac_mouse.currentState.right) {
211 guac_mouse.currentState.left = false;
212 guac_mouse.currentState.middle = false;
213 guac_mouse.currentState.right = false;
215 if (guac_mouse.onmouseup)
216 guac_mouse.onmouseup(guac_mouse.currentState);
221 // Override selection on mouse event element.
222 element.addEventListener("selectstart", function(e) {
226 // Ignore all pending mouse events when touch events are the apparent source
227 function ignorePendingMouseEvents() { ignore_mouse = guac_mouse.touchMouseThreshold; }
229 element.addEventListener("touchmove", ignorePendingMouseEvents, false);
230 element.addEventListener("touchstart", ignorePendingMouseEvents, false);
231 element.addEventListener("touchend", ignorePendingMouseEvents, false);
233 // Scroll wheel support
234 function mousewheel_handler(e) {
239 else if (e.wheelDelta)
240 delta = -event.wheelDelta;
244 if (guac_mouse.onmousedown) {
245 guac_mouse.currentState.up = true;
246 guac_mouse.onmousedown(guac_mouse.currentState);
249 if (guac_mouse.onmouseup) {
250 guac_mouse.currentState.up = false;
251 guac_mouse.onmouseup(guac_mouse.currentState);
257 if (guac_mouse.onmousedown) {
258 guac_mouse.currentState.down = true;
259 guac_mouse.onmousedown(guac_mouse.currentState);
262 if (guac_mouse.onmouseup) {
263 guac_mouse.currentState.down = false;
264 guac_mouse.onmouseup(guac_mouse.currentState);
271 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
272 element.addEventListener('mousewheel', mousewheel_handler, false);
278 * Provides cross-browser relative touch event translation for a given element.
280 * Touch events are translated into mouse events as if the touches occurred
281 * on a touchpad (drag to push the mouse pointer, tap to click).
284 * @param {Element} element The Element to use to provide touch events.
286 Guacamole.Mouse.Touchpad = function(element) {
289 * Reference to this Guacamole.Mouse.Touchpad.
292 var guac_touchpad = this;
295 * The distance a two-finger touch must move per scrollwheel event, in
298 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
301 * The maximum number of milliseconds to wait for a touch to end for the
302 * gesture to be considered a click.
304 this.clickTimingThreshold = 250;
307 * The maximum number of pixels to allow a touch to move for the gesture to
308 * be considered a click.
310 this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
313 * The current mouse state. The properties of this state are updated when
314 * mouse events fire. This state object is also passed in as a parameter to
315 * the handler of any mouse events.
317 * @type Guacamole.Mouse.State
319 this.currentState = new Guacamole.Mouse.State(
321 false, false, false, false, false
325 * Fired whenever a mouse button is effectively pressed. This can happen
326 * as part of a "click" gesture initiated by the user by tapping one
327 * or more fingers over the touchpad element, as part of a "scroll"
328 * gesture initiated by dragging two fingers up or down, etc.
331 * @param {Guacamole.Mouse.State} state The current mouse state.
333 this.onmousedown = null;
336 * Fired whenever a mouse button is effectively released. This can happen
337 * as part of a "click" gesture initiated by the user by tapping one
338 * or more fingers over the touchpad element, as part of a "scroll"
339 * gesture initiated by dragging two fingers up or down, etc.
342 * @param {Guacamole.Mouse.State} state The current mouse state.
344 this.onmouseup = null;
347 * Fired whenever the user moves the mouse by dragging their finger over
348 * the touchpad element.
351 * @param {Guacamole.Mouse.State} state The current mouse state.
353 this.onmousemove = null;
356 var last_touch_x = 0;
357 var last_touch_y = 0;
358 var last_touch_time = 0;
359 var pixels_moved = 0;
361 var touch_buttons = {
367 var gesture_in_progress = false;
368 var click_release_timeout = null;
370 element.addEventListener("touchend", function(e) {
375 // If we're handling a gesture AND this is the last touch
376 if (gesture_in_progress && e.touches.length == 0) {
378 var time = new Date().getTime();
380 // Get corresponding mouse button
381 var button = touch_buttons[touch_count];
383 // If mouse already down, release anad clear timeout
384 if (guac_touchpad.currentState[button]) {
386 // Fire button up event
387 guac_touchpad.currentState[button] = false;
388 if (guac_touchpad.onmouseup)
389 guac_touchpad.onmouseup(guac_touchpad.currentState);
391 // Clear timeout, if set
392 if (click_release_timeout) {
393 window.clearTimeout(click_release_timeout);
394 click_release_timeout = null;
399 // If single tap detected (based on time and distance)
400 if (time - last_touch_time <= guac_touchpad.clickTimingThreshold
401 && pixels_moved < guac_touchpad.clickMoveThreshold) {
403 // Fire button down event
404 guac_touchpad.currentState[button] = true;
405 if (guac_touchpad.onmousedown)
406 guac_touchpad.onmousedown(guac_touchpad.currentState);
408 // Delay mouse up - mouse up should be canceled if
409 // touchstart within timeout.
410 click_release_timeout = window.setTimeout(function() {
412 // Fire button up event
413 guac_touchpad.currentState[button] = false;
414 if (guac_touchpad.onmouseup)
415 guac_touchpad.onmouseup(guac_touchpad.currentState);
418 gesture_in_progress = false;
420 }, guac_touchpad.clickTimingThreshold);
424 // If we're not waiting to see if this is a click, stop gesture
425 if (!click_release_timeout)
426 gesture_in_progress = false;
432 element.addEventListener("touchstart", function(e) {
437 // Track number of touches, but no more than three
438 touch_count = Math.min(e.touches.length, 3);
440 // Clear timeout, if set
441 if (click_release_timeout) {
442 window.clearTimeout(click_release_timeout);
443 click_release_timeout = null;
446 // Record initial touch location and time for touch movement
448 if (!gesture_in_progress) {
450 // Stop mouse events while touching
451 gesture_in_progress = true;
453 // Record touch location and time
454 var starting_touch = e.touches[0];
455 last_touch_x = starting_touch.clientX;
456 last_touch_y = starting_touch.clientY;
457 last_touch_time = new Date().getTime();
464 element.addEventListener("touchmove", function(e) {
469 // Get change in touch location
470 var touch = e.touches[0];
471 var delta_x = touch.clientX - last_touch_x;
472 var delta_y = touch.clientY - last_touch_y;
474 // Track pixels moved
475 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
477 // If only one touch involved, this is mouse move
478 if (touch_count == 1) {
480 // Calculate average velocity in Manhatten pixels per millisecond
481 var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
483 // Scale mouse movement relative to velocity
484 var scale = 1 + velocity;
486 // Update mouse location
487 guac_touchpad.currentState.x += delta_x*scale;
488 guac_touchpad.currentState.y += delta_y*scale;
490 // Prevent mouse from leaving screen
492 if (guac_touchpad.currentState.x < 0)
493 guac_touchpad.currentState.x = 0;
494 else if (guac_touchpad.currentState.x >= element.offsetWidth)
495 guac_touchpad.currentState.x = element.offsetWidth - 1;
497 if (guac_touchpad.currentState.y < 0)
498 guac_touchpad.currentState.y = 0;
499 else if (guac_touchpad.currentState.y >= element.offsetHeight)
500 guac_touchpad.currentState.y = element.offsetHeight - 1;
502 // Fire movement event, if defined
503 if (guac_touchpad.onmousemove)
504 guac_touchpad.onmousemove(guac_touchpad.currentState);
506 // Update touch location
507 last_touch_x = touch.clientX;
508 last_touch_y = touch.clientY;
512 // Interpret two-finger swipe as scrollwheel
513 else if (touch_count == 2) {
515 // If change in location passes threshold for scroll
516 if (Math.abs(delta_y) >= guac_touchpad.scrollThreshold) {
518 // Decide button based on Y movement direction
520 if (delta_y > 0) button = "down";
523 // Fire button down event
524 guac_touchpad.currentState[button] = true;
525 if (guac_touchpad.onmousedown)
526 guac_touchpad.onmousedown(guac_touchpad.currentState);
528 // Fire button up event
529 guac_touchpad.currentState[button] = false;
530 if (guac_touchpad.onmouseup)
531 guac_touchpad.onmouseup(guac_touchpad.currentState);
533 // Only update touch location after a scroll has been
535 last_touch_x = touch.clientX;
536 last_touch_y = touch.clientY;
547 * Provides cross-browser absolute touch event translation for a given element.
549 * Touch events are translated into mouse events as if the touches occurred
550 * on a touchscreen (tapping anywhere on the screen clicks at that point,
551 * long-press to right-click).
554 * @param {Element} element The Element to use to provide touch events.
556 Guacamole.Mouse.Touchscreen = function(element) {
559 * Reference to this Guacamole.Mouse.Touchscreen.
562 var guac_touchscreen = this;
565 * The distance a two-finger touch must move per scrollwheel event, in
568 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
571 * The current mouse state. The properties of this state are updated when
572 * mouse events fire. This state object is also passed in as a parameter to
573 * the handler of any mouse events.
575 * @type Guacamole.Mouse.State
577 this.currentState = new Guacamole.Mouse.State(
579 false, false, false, false, false
583 * Fired whenever a mouse button is effectively pressed. This can happen
584 * as part of a "mousedown" gesture initiated by the user by pressing one
585 * finger over the touchscreen element, as part of a "scroll" gesture
586 * initiated by dragging two fingers up or down, etc.
589 * @param {Guacamole.Mouse.State} state The current mouse state.
591 this.onmousedown = null;
594 * Fired whenever a mouse button is effectively released. This can happen
595 * as part of a "mouseup" gesture initiated by the user by removing the
596 * finger pressed against the touchscreen element, or as part of a "scroll"
597 * gesture initiated by dragging two fingers up or down, etc.
600 * @param {Guacamole.Mouse.State} state The current mouse state.
602 this.onmouseup = null;
605 * Fired whenever the user moves the mouse by dragging their finger over
606 * the touchscreen element. Note that unlike Guacamole.Mouse.Touchpad,
607 * dragging a finger over the touchscreen element will always cause
608 * the mouse button to be effectively down, as if clicking-and-dragging.
611 * @param {Guacamole.Mouse.State} state The current mouse state.
613 this.onmousemove = null;
615 element.addEventListener("touchend", function(e) {
617 // Ignore if more than one touch
618 if (e.touches.length + e.changedTouches.length != 1)
625 guac_touchscreen.currentState.left = false;
627 // Fire release event when the last touch is released, if event defined
628 if (e.touches.length == 0 && guac_touchscreen.onmouseup)
629 guac_touchscreen.onmouseup(guac_touchscreen.currentState);
633 element.addEventListener("touchstart", function(e) {
635 // Ignore if more than one touch
636 if (e.touches.length != 1)
643 var touch = e.touches[0];
646 guac_touchscreen.currentState.left = true;
647 guac_touchscreen.currentState.fromClientPosition(element, touch.clientX, touch.clientY);
649 // Fire press event, if defined
650 if (guac_touchscreen.onmousedown)
651 guac_touchscreen.onmousedown(guac_touchscreen.currentState);
656 element.addEventListener("touchmove", function(e) {
658 // Ignore if more than one touch
659 if (e.touches.length != 1)
666 var touch = e.touches[0];
669 guac_touchscreen.currentState.fromClientPosition(element, touch.clientX, touch.clientY);
671 // Fire movement event, if defined
672 if (guac_touchscreen.onmousemove)
673 guac_touchscreen.onmousemove(guac_touchscreen.currentState);
680 * Simple container for properties describing the state of a mouse.
683 * @param {Number} x The X position of the mouse pointer in pixels.
684 * @param {Number} y The Y position of the mouse pointer in pixels.
685 * @param {Boolean} left Whether the left mouse button is pressed.
686 * @param {Boolean} middle Whether the middle mouse button is pressed.
687 * @param {Boolean} right Whether the right mouse button is pressed.
688 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
689 * button, usually part of a scroll wheel).
690 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
691 * button, usually part of a scroll wheel).
693 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
696 * Reference to this Guacamole.Mouse.State.
699 var guac_state = this;
702 * The current X position of the mouse pointer.
708 * The current Y position of the mouse pointer.
714 * Whether the left mouse button is currently pressed.
720 * Whether the middle mouse button is currently pressed.
726 * Whether the right mouse button is currently pressed.
732 * Whether the up mouse button is currently pressed. This is the fourth
733 * mouse button, associated with upward scrolling of the mouse scroll
740 * Whether the down mouse button is currently pressed. This is the fifth
741 * mouse button, associated with downward scrolling of the mouse scroll
748 * Updates the position represented within this state object by the given
749 * element and clientX/clientY coordinates (commonly available within event
750 * objects). Position is translated from clientX/clientY (relative to
751 * viewport) to element-relative coordinates.
753 * @param {Element} element The element the coordinates should be relative
755 * @param {Number} clientX The X coordinate to translate, viewport-relative.
756 * @param {Number} clientY The Y coordinate to translate, viewport-relative.
758 this.fromClientPosition = function(element, clientX, clientY) {
760 guac_state.x = clientX - element.offsetLeft;
761 guac_state.y = clientY - element.offsetTop;
763 // This is all JUST so we can get the mouse position within the element
764 var parent = element.offsetParent;
765 while (parent && !(parent === document.body)) {
766 guac_state.x -= parent.offsetLeft - parent.scrollLeft;
767 guac_state.y -= parent.offsetTop - parent.scrollTop;
769 parent = parent.offsetParent;
772 // Element ultimately depends on positioning within document body,
773 // take document scroll into account.
775 var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
776 var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
778 guac_state.x -= parent.offsetLeft - documentScrollLeft;
779 guac_state.y -= parent.offsetTop - documentScrollTop;