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 current mouse state. The properties of this state are updated when
63 * mouse events fire. This state object is also passed in as a parameter to
64 * the handler of any mouse events.
66 * @type Guacamole.Mouse.State
68 this.currentState = new Guacamole.Mouse.State(
70 false, false, false, false, false
74 * Fired whenever the user presses a mouse button down over the element
75 * associated with this Guacamole.Mouse.
78 * @param {Guacamole.Mouse.State} state The current mouse state.
80 this.onmousedown = null;
83 * Fired whenever the user releases a mouse button down over the element
84 * associated with this Guacamole.Mouse.
87 * @param {Guacamole.Mouse.State} state The current mouse state.
89 this.onmouseup = null;
92 * Fired whenever the user moves the mouse over the element associated with
93 * this Guacamole.Mouse.
96 * @param {Guacamole.Mouse.State} state The current mouse state.
98 this.onmousemove = null;
100 function cancelEvent(e) {
102 if (e.preventDefault) e.preventDefault();
103 e.returnValue = false;
106 function moveMouse(pageX, pageY) {
108 guac_mouse.currentState.x = pageX - element.offsetLeft;
109 guac_mouse.currentState.y = pageY - element.offsetTop;
111 // This is all JUST so we can get the mouse position within the element
112 var parent = element.offsetParent;
114 if (parent.offsetLeft && parent.offsetTop) {
115 guac_mouse.currentState.x -= parent.offsetLeft;
116 guac_mouse.currentState.y -= parent.offsetTop;
118 parent = parent.offsetParent;
121 if (guac_mouse.onmousemove)
122 guac_mouse.onmousemove(guac_mouse.currentState);
127 // Block context menu so right-click gets sent properly
128 element.addEventListener("contextmenu", function(e) {
132 element.addEventListener("mousemove", function(e) {
134 // Don't handle if we aren't supposed to
135 if (gesture_in_progress) return;
139 moveMouse(e.pageX, e.pageY);
143 var last_touch_x = 0;
144 var last_touch_y = 0;
145 var last_touch_time = 0;
146 var pixels_moved = 0;
148 var gesture_in_progress = false;
149 var click_release_timeout = null;
151 element.addEventListener("touchend", function(e) {
153 // If we're handling a gesture
154 if (gesture_in_progress) {
158 var time = new Date().getTime();
160 // If mouse already down, release anad clear timeout
161 if (guac_mouse.currentState.left) {
163 // Fire left button up event
164 guac_mouse.currentState.left = false;
165 if (guac_mouse.onmouseup)
166 guac_mouse.onmouseup(guac_mouse.currentState);
168 // Clear timeout, if set
169 if (click_release_timeout) {
170 window.clearTimeout(click_release_timeout);
171 click_release_timeout = null;
176 // If single tap detected (based on time and distance)
177 if (time - last_touch_time <= 250 && pixels_moved < 10) {
179 // Fire left button down event
180 guac_mouse.currentState.left = true;
181 if (guac_mouse.onmousedown)
182 guac_mouse.onmousedown(guac_mouse.currentState);
184 // Delay mouse up - mouse up should be canceled if
185 // touchstart within timeout.
186 click_release_timeout = window.setTimeout(function() {
188 // Fire left button up event
189 guac_mouse.currentState.left = false;
190 if (guac_mouse.onmouseup)
191 guac_mouse.onmouseup(guac_mouse.currentState);
193 // Allow mouse events now that touching is over
194 gesture_in_progress = false;
204 element.addEventListener("touchstart", function(e) {
206 // Record initial touch location and time for single-touch movement
208 if (e.touches.length == 1) {
212 // Stop mouse events while touching
213 gesture_in_progress = true;
215 // Clear timeout, if set
216 if (click_release_timeout) {
217 window.clearTimeout(click_release_timeout);
218 click_release_timeout = null;
221 // Record touch location and time
222 var starting_touch = e.touches[0];
223 last_touch_x = starting_touch.pageX;
224 last_touch_y = starting_touch.pageY;
225 last_touch_time = new Date().getTime();
228 // TODO: Handle different buttons
234 element.addEventListener("touchmove", function(e) {
236 // Handle single-touch movement gesture (touchpad mouse move)
237 if (e.touches.length == 1) {
241 // Get change in touch location
242 var touch = e.touches[0];
243 var delta_x = touch.pageX - last_touch_x;
244 var delta_y = touch.pageY - last_touch_y;
246 // Track pixels moved
247 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
249 // Update mouse location
250 guac_mouse.currentState.x += delta_x;
251 guac_mouse.currentState.y += delta_y;
253 // Prevent mouse from leaving screen
255 if (guac_mouse.currentState.x < 0)
256 guac_mouse.currentState.x = 0;
257 else if (guac_mouse.currentState.x >= element.offsetWidth)
258 guac_mouse.currentState.x = element.offsetWidth - 1;
260 if (guac_mouse.currentState.y < 0)
261 guac_mouse.currentState.y = 0;
262 else if (guac_mouse.currentState.y >= element.offsetHeight)
263 guac_mouse.currentState.y = element.offsetHeight - 1;
265 // Fire movement event, if defined
266 if (guac_mouse.onmousemove)
267 guac_mouse.onmousemove(guac_mouse.currentState);
269 // Update touch location
270 last_touch_x = touch.pageX;
271 last_touch_y = touch.pageY;
278 element.addEventListener("mousedown", function(e) {
280 // Don't handle if we aren't supposed to
281 if (gesture_in_progress) return;
287 guac_mouse.currentState.left = true;
290 guac_mouse.currentState.middle = true;
293 guac_mouse.currentState.right = true;
297 if (guac_mouse.onmousedown)
298 guac_mouse.onmousedown(guac_mouse.currentState);
303 element.addEventListener("mouseup", function(e) {
305 // Don't handle if we aren't supposed to
306 if (gesture_in_progress) return;
312 guac_mouse.currentState.left = false;
315 guac_mouse.currentState.middle = false;
318 guac_mouse.currentState.right = false;
322 if (guac_mouse.onmouseup)
323 guac_mouse.onmouseup(guac_mouse.currentState);
327 element.addEventListener("mouseout", function(e) {
329 // Don't handle if we aren't supposed to
330 if (gesture_in_progress) return;
332 // Get parent of the element the mouse pointer is leaving
333 if (!e) e = window.event;
335 // Check that mouseout is due to actually LEAVING the element
336 var target = e.relatedTarget || e.toElement;
337 while (target != null) {
338 if (target === element)
340 target = target.parentNode;
345 // Release all buttons
346 if (guac_mouse.currentState.left
347 || guac_mouse.currentState.middle
348 || guac_mouse.currentState.right) {
350 guac_mouse.currentState.left = false;
351 guac_mouse.currentState.middle = false;
352 guac_mouse.currentState.right = false;
354 if (guac_mouse.onmouseup)
355 guac_mouse.onmouseup(guac_mouse.currentState);
360 // Override selection on mouse event element.
361 element.addEventListener("selectstart", function(e) {
365 // Scroll wheel support
366 function mousewheel_handler(e) {
368 // Don't handle if we aren't supposed to
369 if (gesture_in_progress) return;
374 else if (e.wheelDelta)
375 delta = -event.wheelDelta;
379 if (guac_mouse.onmousedown) {
380 guac_mouse.currentState.up = true;
381 guac_mouse.onmousedown(guac_mouse.currentState);
384 if (guac_mouse.onmouseup) {
385 guac_mouse.currentState.up = false;
386 guac_mouse.onmouseup(guac_mouse.currentState);
392 if (guac_mouse.onmousedown) {
393 guac_mouse.currentState.down = true;
394 guac_mouse.onmousedown(guac_mouse.currentState);
397 if (guac_mouse.onmouseup) {
398 guac_mouse.currentState.down = false;
399 guac_mouse.onmouseup(guac_mouse.currentState);
406 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
407 element.addEventListener('mousewheel', mousewheel_handler, false);
412 * Simple container for properties describing the state of a mouse.
415 * @param {Number} x The X position of the mouse pointer in pixels.
416 * @param {Number} y The Y position of the mouse pointer in pixels.
417 * @param {Boolean} left Whether the left mouse button is pressed.
418 * @param {Boolean} middle Whether the middle mouse button is pressed.
419 * @param {Boolean} right Whether the right mouse button is pressed.
420 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
421 * button, usually part of a scroll wheel).
422 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
423 * button, usually part of a scroll wheel).
425 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
428 * The current X position of the mouse pointer.
434 * The current Y position of the mouse pointer.
440 * Whether the left mouse button is currently pressed.
446 * Whether the middle mouse button is currently pressed.
452 * Whether the right mouse button is currently pressed.
458 * Whether the up mouse button is currently pressed. This is the fourth
459 * mouse button, associated with upward scrolling of the mouse scroll
466 * Whether the down mouse button is currently pressed. This is the fifth
467 * mouse button, associated with downward scrolling of the mouse scroll