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;
133 guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
134 guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
136 parent = parent.offsetParent;
139 if (guac_mouse.onmousemove)
140 guac_mouse.onmousemove(guac_mouse.currentState);
145 // Block context menu so right-click gets sent properly
146 element.addEventListener("contextmenu", function(e) {
150 element.addEventListener("mousemove", function(e) {
152 // Don't handle if we aren't supposed to
153 if (gesture_in_progress) return;
157 moveMouse(e.clientX, e.clientY);
162 var last_touch_x = 0;
163 var last_touch_y = 0;
164 var last_touch_time = 0;
165 var pixels_moved = 0;
166 var touch_distance = 0;
168 var touch_buttons = {
174 var gesture_in_progress = false;
175 var click_release_timeout = null;
177 element.addEventListener("touchend", function(e) {
179 // If we're handling a gesture AND this is the last touch
180 if (gesture_in_progress && e.touches.length == 0) {
184 var time = new Date().getTime();
186 // Get corresponding mouse button
187 var button = touch_buttons[touch_count];
189 // If mouse already down, release anad clear timeout
190 if (guac_mouse.currentState[button]) {
192 // Fire button up event
193 guac_mouse.currentState[button] = false;
194 if (guac_mouse.onmouseup)
195 guac_mouse.onmouseup(guac_mouse.currentState);
197 // Clear timeout, if set
198 if (click_release_timeout) {
199 window.clearTimeout(click_release_timeout);
200 click_release_timeout = null;
205 // If single tap detected (based on time and distance)
206 if (time - last_touch_time <= guac_mouse.clickTimingThreshold
207 && pixels_moved < guac_mouse.clickMoveThreshold) {
209 // Fire button down event
210 guac_mouse.currentState[button] = true;
211 if (guac_mouse.onmousedown)
212 guac_mouse.onmousedown(guac_mouse.currentState);
214 // Delay mouse up - mouse up should be canceled if
215 // touchstart within timeout.
216 click_release_timeout = window.setTimeout(function() {
218 // Fire button up event
219 guac_mouse.currentState[button] = false;
220 if (guac_mouse.onmouseup)
221 guac_mouse.onmouseup(guac_mouse.currentState);
223 // Allow mouse events now that touching is over
224 gesture_in_progress = false;
226 }, guac_mouse.clickTimingThreshold);
234 element.addEventListener("touchstart", function(e) {
236 // Track number of touches, but no more than three
237 touch_count = Math.min(e.touches.length, 3);
239 // Record initial touch location and time for touch movement
241 if (e.touches.length == 1) {
245 // Stop mouse events while touching
246 gesture_in_progress = true;
248 // Clear timeout, if set
249 if (click_release_timeout) {
250 window.clearTimeout(click_release_timeout);
251 click_release_timeout = null;
254 // Record touch location and time
255 var starting_touch = e.touches[0];
256 last_touch_x = starting_touch.clientX;
257 last_touch_y = starting_touch.clientY;
258 last_touch_time = new Date().getTime();
265 element.addEventListener("touchmove", function(e) {
269 // Get change in touch location
270 var touch = e.touches[0];
271 var delta_x = touch.clientX - last_touch_x;
272 var delta_y = touch.clientY - last_touch_y;
274 // Track pixels moved
275 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
277 // If only one touch involved, this is mouse move
278 if (touch_count == 1) {
280 // Update mouse location
281 guac_mouse.currentState.x += delta_x;
282 guac_mouse.currentState.y += delta_y;
284 // Prevent mouse from leaving screen
286 if (guac_mouse.currentState.x < 0)
287 guac_mouse.currentState.x = 0;
288 else if (guac_mouse.currentState.x >= element.offsetWidth)
289 guac_mouse.currentState.x = element.offsetWidth - 1;
291 if (guac_mouse.currentState.y < 0)
292 guac_mouse.currentState.y = 0;
293 else if (guac_mouse.currentState.y >= element.offsetHeight)
294 guac_mouse.currentState.y = element.offsetHeight - 1;
296 // Fire movement event, if defined
297 if (guac_mouse.onmousemove)
298 guac_mouse.onmousemove(guac_mouse.currentState);
300 // Update touch location
301 last_touch_x = touch.clientX;
302 last_touch_y = touch.clientY;
306 // Interpret two-finger swipe as scrollwheel
307 else if (touch_count == 2) {
309 // If change in location passes threshold for scroll
310 if (Math.abs(delta_y) >= guac_mouse.scrollThreshold) {
312 // Decide button based on Y movement direction
314 if (delta_y > 0) button = "down";
317 // Fire button down event
318 guac_mouse.currentState[button] = true;
319 if (guac_mouse.onmousedown)
320 guac_mouse.onmousedown(guac_mouse.currentState);
322 // Fire button up event
323 guac_mouse.currentState[button] = false;
324 if (guac_mouse.onmouseup)
325 guac_mouse.onmouseup(guac_mouse.currentState);
327 // Only update touch location after a scroll has been
329 last_touch_x = touch.clientX;
330 last_touch_y = touch.clientY;
339 element.addEventListener("mousedown", function(e) {
341 // Don't handle if we aren't supposed to
342 if (gesture_in_progress) return;
348 guac_mouse.currentState.left = true;
351 guac_mouse.currentState.middle = true;
354 guac_mouse.currentState.right = true;
358 if (guac_mouse.onmousedown)
359 guac_mouse.onmousedown(guac_mouse.currentState);
364 element.addEventListener("mouseup", function(e) {
366 // Don't handle if we aren't supposed to
367 if (gesture_in_progress) return;
373 guac_mouse.currentState.left = false;
376 guac_mouse.currentState.middle = false;
379 guac_mouse.currentState.right = false;
383 if (guac_mouse.onmouseup)
384 guac_mouse.onmouseup(guac_mouse.currentState);
388 element.addEventListener("mouseout", function(e) {
390 // Don't handle if we aren't supposed to
391 if (gesture_in_progress) return;
393 // Get parent of the element the mouse pointer is leaving
394 if (!e) e = window.event;
396 // Check that mouseout is due to actually LEAVING the element
397 var target = e.relatedTarget || e.toElement;
398 while (target != null) {
399 if (target === element)
401 target = target.parentNode;
406 // Release all buttons
407 if (guac_mouse.currentState.left
408 || guac_mouse.currentState.middle
409 || guac_mouse.currentState.right) {
411 guac_mouse.currentState.left = false;
412 guac_mouse.currentState.middle = false;
413 guac_mouse.currentState.right = false;
415 if (guac_mouse.onmouseup)
416 guac_mouse.onmouseup(guac_mouse.currentState);
421 // Override selection on mouse event element.
422 element.addEventListener("selectstart", function(e) {
426 // Scroll wheel support
427 function mousewheel_handler(e) {
429 // Don't handle if we aren't supposed to
430 if (gesture_in_progress) return;
435 else if (e.wheelDelta)
436 delta = -event.wheelDelta;
440 if (guac_mouse.onmousedown) {
441 guac_mouse.currentState.up = true;
442 guac_mouse.onmousedown(guac_mouse.currentState);
445 if (guac_mouse.onmouseup) {
446 guac_mouse.currentState.up = false;
447 guac_mouse.onmouseup(guac_mouse.currentState);
453 if (guac_mouse.onmousedown) {
454 guac_mouse.currentState.down = true;
455 guac_mouse.onmousedown(guac_mouse.currentState);
458 if (guac_mouse.onmouseup) {
459 guac_mouse.currentState.down = false;
460 guac_mouse.onmouseup(guac_mouse.currentState);
467 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
468 element.addEventListener('mousewheel', mousewheel_handler, false);
473 * Simple container for properties describing the state of a mouse.
476 * @param {Number} x The X position of the mouse pointer in pixels.
477 * @param {Number} y The Y position of the mouse pointer in pixels.
478 * @param {Boolean} left Whether the left mouse button is pressed.
479 * @param {Boolean} middle Whether the middle mouse button is pressed.
480 * @param {Boolean} right Whether the right mouse button is pressed.
481 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
482 * button, usually part of a scroll wheel).
483 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
484 * button, usually part of a scroll wheel).
486 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
489 * The current X position of the mouse pointer.
495 * The current Y position of the mouse pointer.
501 * Whether the left mouse button is currently pressed.
507 * Whether the middle mouse button is currently pressed.
513 * Whether the right mouse button is currently pressed.
519 * Whether the up mouse button is currently pressed. This is the fourth
520 * mouse button, associated with upward scrolling of the mouse scroll
527 * Whether the down mouse button is currently pressed. This is the fifth
528 * mouse button, associated with downward scrolling of the mouse scroll