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;
98 * Zero-delay timeout set when mouse events are fired, and canceled when
99 * touch events are detected, in order to prevent touch events registering
100 * as mouse events (some browsers will do this).
102 var deferred_mouse_event = null;
105 * Flag which, when set to true, will cause all mouse events to be
106 * ignored. Used to temporarily ignore events when generated by
107 * touch events, and not by a mouse.
109 var ignore_mouse = false;
112 * Forces all mouse events to be ignored until the event queue is flushed.
114 function ignorePendingMouseEvents() {
116 // Cancel deferred event
117 if (deferred_mouse_event) {
118 window.clearTimeout(deferred_mouse_event);
119 deferred_mouse_event = null;
122 // Ignore all other events until end of event loop
124 window.setTimeout(function() {
125 ignore_mouse = false;
130 function cancelEvent(e) {
132 if (e.preventDefault) e.preventDefault();
133 e.returnValue = false;
136 // Block context menu so right-click gets sent properly
137 element.addEventListener("contextmenu", function(e) {
141 element.addEventListener("mousemove", function(e) {
145 // If artificial event detected, ignore currently pending events
146 if (deferred_mouse_event)
147 ignorePendingMouseEvents();
152 guac_mouse.currentState.fromClientPosition(element, e.clientX, e.clientY);
154 if (guac_mouse.onmousemove)
155 deferred_mouse_event = window.setTimeout(function() {
156 guac_mouse.onmousemove(guac_mouse.currentState);
157 deferred_mouse_event = null;
162 element.addEventListener("mousedown", function(e) {
166 // If artificial event detected, ignore currently pending events
167 if (deferred_mouse_event)
168 ignorePendingMouseEvents();
175 guac_mouse.currentState.left = true;
178 guac_mouse.currentState.middle = true;
181 guac_mouse.currentState.right = true;
185 if (guac_mouse.onmousedown)
186 deferred_mouse_event = window.setTimeout(function() {
187 guac_mouse.onmousedown(guac_mouse.currentState);
188 deferred_mouse_event = null;
193 element.addEventListener("mouseup", function(e) {
197 // If artificial event detected, ignore currently pending events
198 if (deferred_mouse_event)
199 ignorePendingMouseEvents();
206 guac_mouse.currentState.left = false;
209 guac_mouse.currentState.middle = false;
212 guac_mouse.currentState.right = false;
216 if (guac_mouse.onmouseup)
217 deferred_mouse_event = window.setTimeout(function() {
218 guac_mouse.onmouseup(guac_mouse.currentState);
219 deferred_mouse_event = null;
224 element.addEventListener("mouseout", function(e) {
226 // Get parent of the element the mouse pointer is leaving
227 if (!e) e = window.event;
229 // Check that mouseout is due to actually LEAVING the element
230 var target = e.relatedTarget || e.toElement;
231 while (target != null) {
232 if (target === element)
234 target = target.parentNode;
239 // Release all buttons
240 if (guac_mouse.currentState.left
241 || guac_mouse.currentState.middle
242 || guac_mouse.currentState.right) {
244 guac_mouse.currentState.left = false;
245 guac_mouse.currentState.middle = false;
246 guac_mouse.currentState.right = false;
248 if (guac_mouse.onmouseup)
249 guac_mouse.onmouseup(guac_mouse.currentState);
254 // Override selection on mouse event element.
255 element.addEventListener("selectstart", function(e) {
260 // Ignore all pending mouse events when touch events are the apparent source
261 element.addEventListener("touchmove", ignorePendingMouseEvents, false);
262 element.addEventListener("touchstart", ignorePendingMouseEvents, false);
263 element.addEventListener("touchend", ignorePendingMouseEvents, false);
265 // Scroll wheel support
266 function mousewheel_handler(e) {
271 else if (e.wheelDelta)
272 delta = -event.wheelDelta;
276 if (guac_mouse.onmousedown) {
277 guac_mouse.currentState.up = true;
278 guac_mouse.onmousedown(guac_mouse.currentState);
281 if (guac_mouse.onmouseup) {
282 guac_mouse.currentState.up = false;
283 guac_mouse.onmouseup(guac_mouse.currentState);
289 if (guac_mouse.onmousedown) {
290 guac_mouse.currentState.down = true;
291 guac_mouse.onmousedown(guac_mouse.currentState);
294 if (guac_mouse.onmouseup) {
295 guac_mouse.currentState.down = false;
296 guac_mouse.onmouseup(guac_mouse.currentState);
303 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
304 element.addEventListener('mousewheel', mousewheel_handler, false);
310 * Provides cross-browser relative touch event translation for a given element.
312 * Touch events are translated into mouse events as if the touches occurred
313 * on a touchpad (drag to push the mouse pointer, tap to click).
316 * @param {Element} element The Element to use to provide touch events.
318 Guacamole.Mouse.Touchpad = function(element) {
321 * Reference to this Guacamole.Mouse.Touchpad.
324 var guac_touchpad = this;
327 * The distance a two-finger touch must move per scrollwheel event, in
330 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
333 * The maximum number of milliseconds to wait for a touch to end for the
334 * gesture to be considered a click.
336 this.clickTimingThreshold = 250;
339 * The maximum number of pixels to allow a touch to move for the gesture to
340 * be considered a click.
342 this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
345 * The current mouse state. The properties of this state are updated when
346 * mouse events fire. This state object is also passed in as a parameter to
347 * the handler of any mouse events.
349 * @type Guacamole.Mouse.State
351 this.currentState = new Guacamole.Mouse.State(
353 false, false, false, false, false
357 * Fired whenever a mouse button is effectively pressed. This can happen
358 * as part of a "click" gesture initiated by the user by tapping one
359 * or more fingers over the touchpad element, as part of a "scroll"
360 * gesture initiated by dragging two fingers up or down, etc.
363 * @param {Guacamole.Mouse.State} state The current mouse state.
365 this.onmousedown = null;
368 * Fired whenever a mouse button is effectively released. This can happen
369 * as part of a "click" gesture initiated by the user by tapping one
370 * or more fingers over the touchpad element, as part of a "scroll"
371 * gesture initiated by dragging two fingers up or down, etc.
374 * @param {Guacamole.Mouse.State} state The current mouse state.
376 this.onmouseup = null;
379 * Fired whenever the user moves the mouse by dragging their finger over
380 * the touchpad element.
383 * @param {Guacamole.Mouse.State} state The current mouse state.
385 this.onmousemove = null;
388 var last_touch_x = 0;
389 var last_touch_y = 0;
390 var last_touch_time = 0;
391 var pixels_moved = 0;
393 var touch_buttons = {
399 var gesture_in_progress = false;
400 var click_release_timeout = null;
402 element.addEventListener("touchend", function(e) {
407 // If we're handling a gesture AND this is the last touch
408 if (gesture_in_progress && e.touches.length == 0) {
410 var time = new Date().getTime();
412 // Get corresponding mouse button
413 var button = touch_buttons[touch_count];
415 // If mouse already down, release anad clear timeout
416 if (guac_touchpad.currentState[button]) {
418 // Fire button up event
419 guac_touchpad.currentState[button] = false;
420 if (guac_touchpad.onmouseup)
421 guac_touchpad.onmouseup(guac_touchpad.currentState);
423 // Clear timeout, if set
424 if (click_release_timeout) {
425 window.clearTimeout(click_release_timeout);
426 click_release_timeout = null;
431 // If single tap detected (based on time and distance)
432 if (time - last_touch_time <= guac_touchpad.clickTimingThreshold
433 && pixels_moved < guac_touchpad.clickMoveThreshold) {
435 // Fire button down event
436 guac_touchpad.currentState[button] = true;
437 if (guac_touchpad.onmousedown)
438 guac_touchpad.onmousedown(guac_touchpad.currentState);
440 // Delay mouse up - mouse up should be canceled if
441 // touchstart within timeout.
442 click_release_timeout = window.setTimeout(function() {
444 // Fire button up event
445 guac_touchpad.currentState[button] = false;
446 if (guac_touchpad.onmouseup)
447 guac_touchpad.onmouseup(guac_touchpad.currentState);
450 gesture_in_progress = false;
452 }, guac_touchpad.clickTimingThreshold);
456 // If we're not waiting to see if this is a click, stop gesture
457 if (!click_release_timeout)
458 gesture_in_progress = false;
464 element.addEventListener("touchstart", function(e) {
469 // Track number of touches, but no more than three
470 touch_count = Math.min(e.touches.length, 3);
472 // Clear timeout, if set
473 if (click_release_timeout) {
474 window.clearTimeout(click_release_timeout);
475 click_release_timeout = null;
478 // Record initial touch location and time for touch movement
480 if (!gesture_in_progress) {
482 // Stop mouse events while touching
483 gesture_in_progress = true;
485 // Record touch location and time
486 var starting_touch = e.touches[0];
487 last_touch_x = starting_touch.clientX;
488 last_touch_y = starting_touch.clientY;
489 last_touch_time = new Date().getTime();
496 element.addEventListener("touchmove", function(e) {
501 // Get change in touch location
502 var touch = e.touches[0];
503 var delta_x = touch.clientX - last_touch_x;
504 var delta_y = touch.clientY - last_touch_y;
506 // Track pixels moved
507 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
509 // If only one touch involved, this is mouse move
510 if (touch_count == 1) {
512 // Calculate average velocity in Manhatten pixels per millisecond
513 var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
515 // Scale mouse movement relative to velocity
516 var scale = 1 + velocity;
518 // Update mouse location
519 guac_touchpad.currentState.x += delta_x*scale;
520 guac_touchpad.currentState.y += delta_y*scale;
522 // Prevent mouse from leaving screen
524 if (guac_touchpad.currentState.x < 0)
525 guac_touchpad.currentState.x = 0;
526 else if (guac_touchpad.currentState.x >= element.offsetWidth)
527 guac_touchpad.currentState.x = element.offsetWidth - 1;
529 if (guac_touchpad.currentState.y < 0)
530 guac_touchpad.currentState.y = 0;
531 else if (guac_touchpad.currentState.y >= element.offsetHeight)
532 guac_touchpad.currentState.y = element.offsetHeight - 1;
534 // Fire movement event, if defined
535 if (guac_touchpad.onmousemove)
536 guac_touchpad.onmousemove(guac_touchpad.currentState);
538 // Update touch location
539 last_touch_x = touch.clientX;
540 last_touch_y = touch.clientY;
544 // Interpret two-finger swipe as scrollwheel
545 else if (touch_count == 2) {
547 // If change in location passes threshold for scroll
548 if (Math.abs(delta_y) >= guac_touchpad.scrollThreshold) {
550 // Decide button based on Y movement direction
552 if (delta_y > 0) button = "down";
555 // Fire button down event
556 guac_touchpad.currentState[button] = true;
557 if (guac_touchpad.onmousedown)
558 guac_touchpad.onmousedown(guac_touchpad.currentState);
560 // Fire button up event
561 guac_touchpad.currentState[button] = false;
562 if (guac_touchpad.onmouseup)
563 guac_touchpad.onmouseup(guac_touchpad.currentState);
565 // Only update touch location after a scroll has been
567 last_touch_x = touch.clientX;
568 last_touch_y = touch.clientY;
579 * Provides cross-browser absolute touch event translation for a given element.
581 * Touch events are translated into mouse events as if the touches occurred
582 * on a touchscreen (tapping anywhere on the screen clicks at that point,
583 * long-press to right-click).
586 * @param {Element} element The Element to use to provide touch events.
588 Guacamole.Mouse.Touchscreen = function(element) {
591 * Reference to this Guacamole.Mouse.Touchscreen.
594 var guac_touchscreen = this;
597 * The distance a two-finger touch must move per scrollwheel event, in
600 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
603 * The current mouse state. The properties of this state are updated when
604 * mouse events fire. This state object is also passed in as a parameter to
605 * the handler of any mouse events.
607 * @type Guacamole.Mouse.State
609 this.currentState = new Guacamole.Mouse.State(
611 false, false, false, false, false
615 * Fired whenever a mouse button is effectively pressed. This can happen
616 * as part of a "mousedown" gesture initiated by the user by pressing one
617 * finger over the touchscreen element, as part of a "scroll" gesture
618 * initiated by dragging two fingers up or down, etc.
621 * @param {Guacamole.Mouse.State} state The current mouse state.
623 this.onmousedown = null;
626 * Fired whenever a mouse button is effectively released. This can happen
627 * as part of a "mouseup" gesture initiated by the user by removing the
628 * finger pressed against the touchscreen element, or as part of a "scroll"
629 * gesture initiated by dragging two fingers up or down, etc.
632 * @param {Guacamole.Mouse.State} state The current mouse state.
634 this.onmouseup = null;
637 * Fired whenever the user moves the mouse by dragging their finger over
638 * the touchscreen element. Note that unlike Guacamole.Mouse.Touchpad,
639 * dragging a finger over the touchscreen element will always cause
640 * the mouse button to be effectively down, as if clicking-and-dragging.
643 * @param {Guacamole.Mouse.State} state The current mouse state.
645 this.onmousemove = null;
647 element.addEventListener("touchend", function(e) {
649 // Ignore if more than one touch
650 if (e.touches.length + e.changedTouches.length != 1)
657 guac_touchscreen.currentState.left = false;
659 // Fire release event when the last touch is released, if event defined
660 if (e.touches.length == 0 && guac_touchscreen.onmouseup)
661 guac_touchscreen.onmouseup(guac_touchscreen.currentState);
665 element.addEventListener("touchstart", function(e) {
667 // Ignore if more than one touch
668 if (e.touches.length != 1)
675 var touch = e.touches[0];
678 guac_touchscreen.currentState.left = true;
679 guac_touchscreen.currentState.fromClientPosition(element, touch.clientX, touch.clientY);
681 // Fire press event, if defined
682 if (guac_touchscreen.onmousedown)
683 guac_touchscreen.onmousedown(guac_touchscreen.currentState);
688 element.addEventListener("touchmove", function(e) {
690 // Ignore if more than one touch
691 if (e.touches.length != 1)
698 var touch = e.touches[0];
701 guac_touchscreen.currentState.fromClientPosition(element, touch.clientX, touch.clientY);
703 // Fire movement event, if defined
704 if (guac_touchscreen.onmousemove)
705 guac_touchscreen.onmousemove(guac_touchscreen.currentState);
712 * Simple container for properties describing the state of a mouse.
715 * @param {Number} x The X position of the mouse pointer in pixels.
716 * @param {Number} y The Y position of the mouse pointer in pixels.
717 * @param {Boolean} left Whether the left mouse button is pressed.
718 * @param {Boolean} middle Whether the middle mouse button is pressed.
719 * @param {Boolean} right Whether the right mouse button is pressed.
720 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
721 * button, usually part of a scroll wheel).
722 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
723 * button, usually part of a scroll wheel).
725 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
728 * Reference to this Guacamole.Mouse.State.
731 var guac_state = this;
734 * The current X position of the mouse pointer.
740 * The current Y position of the mouse pointer.
746 * Whether the left mouse button is currently pressed.
752 * Whether the middle mouse button is currently pressed.
758 * Whether the right mouse button is currently pressed.
764 * Whether the up mouse button is currently pressed. This is the fourth
765 * mouse button, associated with upward scrolling of the mouse scroll
772 * Whether the down mouse button is currently pressed. This is the fifth
773 * mouse button, associated with downward scrolling of the mouse scroll
780 * Updates the position represented within this state object by the given
781 * element and clientX/clientY coordinates (commonly available within event
782 * objects). Position is translated from clientX/clientY (relative to
783 * viewport) to element-relative coordinates.
785 * @param {Element} element The element the coordinates should be relative
787 * @param {Number} clientX The X coordinate to translate, viewport-relative.
788 * @param {Number} clientY The Y coordinate to translate, viewport-relative.
790 this.fromClientPosition = function(element, clientX, clientY) {
792 guac_state.x = clientX - element.offsetLeft;
793 guac_state.y = clientY - element.offsetTop;
795 // This is all JUST so we can get the mouse position within the element
796 var parent = element.offsetParent;
797 while (parent && !(parent === document.body)) {
798 guac_state.x -= parent.offsetLeft - parent.scrollLeft;
799 guac_state.y -= parent.offsetTop - parent.scrollTop;
801 parent = parent.offsetParent;
804 // Offset by document scroll amount
805 var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
806 var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
808 guac_state.x -= parent.offsetLeft - documentScrollLeft;
809 guac_state.y -= parent.offsetTop - documentScrollTop;