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 relative 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.Touchpad.
279 var guac_touchpad = 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 a mouse button is effectively pressed. This can happen
313 * as part of a "click" gesture initiated by the user by tapping one
314 * or more fingers over the touchpad element, as part of a "scroll"
315 * gesture initiated by dragging two fingers up or down, etc.
318 * @param {Guacamole.Mouse.State} state The current mouse state.
320 this.onmousedown = null;
323 * Fired whenever a mouse button is effectively released. This can happen
324 * as part of a "click" gesture initiated by the user by tapping one
325 * or more fingers over the touchpad element, as part of a "scroll"
326 * gesture initiated by dragging two fingers up or down, etc.
329 * @param {Guacamole.Mouse.State} state The current mouse state.
331 this.onmouseup = null;
334 * Fired whenever the user moves the mouse by dragging their finger over
335 * the touchpad element.
338 * @param {Guacamole.Mouse.State} state The current mouse state.
340 this.onmousemove = null;
343 var last_touch_x = 0;
344 var last_touch_y = 0;
345 var last_touch_time = 0;
346 var pixels_moved = 0;
348 var touch_buttons = {
354 var gesture_in_progress = false;
355 var click_release_timeout = null;
357 element.addEventListener("touchend", function(e) {
362 // If we're handling a gesture AND this is the last touch
363 if (gesture_in_progress && e.touches.length == 0) {
365 var time = new Date().getTime();
367 // Get corresponding mouse button
368 var button = touch_buttons[touch_count];
370 // If mouse already down, release anad clear timeout
371 if (guac_touchpad.currentState[button]) {
373 // Fire button up event
374 guac_touchpad.currentState[button] = false;
375 if (guac_touchpad.onmouseup)
376 guac_touchpad.onmouseup(guac_touchpad.currentState);
378 // Clear timeout, if set
379 if (click_release_timeout) {
380 window.clearTimeout(click_release_timeout);
381 click_release_timeout = null;
386 // If single tap detected (based on time and distance)
387 if (time - last_touch_time <= guac_touchpad.clickTimingThreshold
388 && pixels_moved < guac_touchpad.clickMoveThreshold) {
390 // Fire button down event
391 guac_touchpad.currentState[button] = true;
392 if (guac_touchpad.onmousedown)
393 guac_touchpad.onmousedown(guac_touchpad.currentState);
395 // Delay mouse up - mouse up should be canceled if
396 // touchstart within timeout.
397 click_release_timeout = window.setTimeout(function() {
399 // Fire button up event
400 guac_touchpad.currentState[button] = false;
401 if (guac_touchpad.onmouseup)
402 guac_touchpad.onmouseup(guac_touchpad.currentState);
405 gesture_in_progress = false;
407 }, guac_touchpad.clickTimingThreshold);
411 // If we're not waiting to see if this is a click, stop gesture
412 if (!click_release_timeout)
413 gesture_in_progress = false;
419 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) {
456 // Get change in touch location
457 var touch = e.touches[0];
458 var delta_x = touch.clientX - last_touch_x;
459 var delta_y = touch.clientY - last_touch_y;
461 // Track pixels moved
462 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
464 // If only one touch involved, this is mouse move
465 if (touch_count == 1) {
467 // Calculate average velocity in Manhatten pixels per millisecond
468 var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
470 // Scale mouse movement relative to velocity
471 var scale = 1 + velocity;
473 // Update mouse location
474 guac_touchpad.currentState.x += delta_x*scale;
475 guac_touchpad.currentState.y += delta_y*scale;
477 // Prevent mouse from leaving screen
479 if (guac_touchpad.currentState.x < 0)
480 guac_touchpad.currentState.x = 0;
481 else if (guac_touchpad.currentState.x >= element.offsetWidth)
482 guac_touchpad.currentState.x = element.offsetWidth - 1;
484 if (guac_touchpad.currentState.y < 0)
485 guac_touchpad.currentState.y = 0;
486 else if (guac_touchpad.currentState.y >= element.offsetHeight)
487 guac_touchpad.currentState.y = element.offsetHeight - 1;
489 // Fire movement event, if defined
490 if (guac_touchpad.onmousemove)
491 guac_touchpad.onmousemove(guac_touchpad.currentState);
493 // Update touch location
494 last_touch_x = touch.clientX;
495 last_touch_y = touch.clientY;
499 // Interpret two-finger swipe as scrollwheel
500 else if (touch_count == 2) {
502 // If change in location passes threshold for scroll
503 if (Math.abs(delta_y) >= guac_touchpad.scrollThreshold) {
505 // Decide button based on Y movement direction
507 if (delta_y > 0) button = "down";
510 // Fire button down event
511 guac_touchpad.currentState[button] = true;
512 if (guac_touchpad.onmousedown)
513 guac_touchpad.onmousedown(guac_touchpad.currentState);
515 // Fire button up event
516 guac_touchpad.currentState[button] = false;
517 if (guac_touchpad.onmouseup)
518 guac_touchpad.onmouseup(guac_touchpad.currentState);
520 // Only update touch location after a scroll has been
522 last_touch_x = touch.clientX;
523 last_touch_y = touch.clientY;
534 * Provides cross-browser absolute touch event translation for a given element.
536 * Touch events are translated into mouse events as if the touches occurred
537 * on a touchscreen (tapping anywhere on the screen clicks at that point,
538 * long-press to right-click).
541 * @param {Element} element The Element to use to provide touch events.
543 Guacamole.Mouse.Touchscreen = function(element) {
546 * Reference to this Guacamole.Mouse.Touchscreen.
549 var guac_touchscreen = this;
552 * The distance a two-finger touch must move per scrollwheel event, in
555 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
558 * The current mouse state. The properties of this state are updated when
559 * mouse events fire. This state object is also passed in as a parameter to
560 * the handler of any mouse events.
562 * @type Guacamole.Mouse.State
564 this.currentState = new Guacamole.Mouse.State(
566 false, false, false, false, false
570 * Fired whenever a mouse button is effectively pressed. This can happen
571 * as part of a "mousedown" gesture initiated by the user by pressing one
572 * finger over the touchscreen element, as part of a "scroll" gesture
573 * initiated by dragging two fingers up or down, etc.
576 * @param {Guacamole.Mouse.State} state The current mouse state.
578 this.onmousedown = null;
581 * Fired whenever a mouse button is effectively released. This can happen
582 * as part of a "mouseup" gesture initiated by the user by removing the
583 * finger pressed against the touchscreen element, or as part of a "scroll"
584 * gesture initiated by dragging two fingers up or down, etc.
587 * @param {Guacamole.Mouse.State} state The current mouse state.
589 this.onmouseup = null;
592 * Fired whenever the user moves the mouse by dragging their finger over
593 * the touchscreen element. Note that unlike Guacamole.Mouse.Touchpad,
594 * dragging a finger over the touchscreen element will always cause
595 * the mouse button to be effectively down, as if clicking-and-dragging.
598 * @param {Guacamole.Mouse.State} state The current mouse state.
600 this.onmousemove = null;
602 element.addEventListener("touchend", function(e) {
607 // Fire release event when the last touch is released, if event defined
608 if (e.touches.length == 0 && guac_touchscreen.onmouseup)
609 guac_touchscreen.onmouseup(guac_touchscreen.currentState);
613 element.addEventListener("touchstart", function(e) {
619 var touch = e.touches[0];
622 guac_touchscreen.currentstate.x = touch.clientX;
623 guac_touchscreen.currentstate.y = touch.clientY;
625 // Fire press event, if defined
626 if (guac_touchscreen.onmousedown)
627 guac_touchscreen.onmousedown(guac_touchscreen.currentState);
632 element.addEventListener("touchmove", function(e) {
638 var touch = e.touches[0];
641 guac_touchscreen.currentstate.x = touch.clientX;
642 guac_touchscreen.currentstate.y = touch.clientY;
644 // Fire movement event, if defined
645 if (guac_touchscreen.onmousemove)
646 guac_touchscreen.onmousemove(guac_touchscreen.currentState);
653 * Simple container for properties describing the state of a mouse.
656 * @param {Number} x The X position of the mouse pointer in pixels.
657 * @param {Number} y The Y position of the mouse pointer in pixels.
658 * @param {Boolean} left Whether the left mouse button is pressed.
659 * @param {Boolean} middle Whether the middle mouse button is pressed.
660 * @param {Boolean} right Whether the right mouse button is pressed.
661 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
662 * button, usually part of a scroll wheel).
663 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
664 * button, usually part of a scroll wheel).
666 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
669 * The current X position of the mouse pointer.
675 * The current Y position of the mouse pointer.
681 * Whether the left mouse button is currently pressed.
687 * Whether the middle mouse button is currently pressed.
693 * Whether the right mouse button is currently pressed.
699 * Whether the up mouse button is currently pressed. This is the fourth
700 * mouse button, associated with upward scrolling of the mouse scroll
707 * Whether the down mouse button is currently pressed. This is the fifth
708 * mouse button, associated with downward scrolling of the mouse scroll