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;
115 if (parent.offsetLeft)
116 guac_mouse.currentState.x -= parent.offsetLeft;
118 if (parent.offsetTop)
119 guac_mouse.currentState.y -= parent.offsetTop;
121 parent = parent.offsetParent;
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) {
137 // Don't handle if we aren't supposed to
138 if (gesture_in_progress) return;
142 moveMouse(e.pageX, e.pageY);
146 var last_touch_x = 0;
147 var last_touch_y = 0;
148 var last_touch_time = 0;
149 var pixels_moved = 0;
151 var gesture_in_progress = false;
152 var click_release_timeout = null;
154 element.addEventListener("touchend", function(e) {
156 // If we're handling a gesture
157 if (gesture_in_progress) {
161 var time = new Date().getTime();
163 // If mouse already down, release anad clear timeout
164 if (guac_mouse.currentState.left) {
166 // Fire left button up event
167 guac_mouse.currentState.left = false;
168 if (guac_mouse.onmouseup)
169 guac_mouse.onmouseup(guac_mouse.currentState);
171 // Clear timeout, if set
172 if (click_release_timeout) {
173 window.clearTimeout(click_release_timeout);
174 click_release_timeout = null;
179 // If single tap detected (based on time and distance)
180 if (time - last_touch_time <= 250 && pixels_moved < 10) {
182 // Fire left button down event
183 guac_mouse.currentState.left = true;
184 if (guac_mouse.onmousedown)
185 guac_mouse.onmousedown(guac_mouse.currentState);
187 // Delay mouse up - mouse up should be canceled if
188 // touchstart within timeout.
189 click_release_timeout = window.setTimeout(function() {
191 // Fire left button up event
192 guac_mouse.currentState.left = false;
193 if (guac_mouse.onmouseup)
194 guac_mouse.onmouseup(guac_mouse.currentState);
196 // Allow mouse events now that touching is over
197 gesture_in_progress = false;
207 element.addEventListener("touchstart", function(e) {
209 // Record initial touch location and time for single-touch movement
211 if (e.touches.length == 1) {
215 // Stop mouse events while touching
216 gesture_in_progress = true;
218 // Clear timeout, if set
219 if (click_release_timeout) {
220 window.clearTimeout(click_release_timeout);
221 click_release_timeout = null;
224 // Record touch location and time
225 var starting_touch = e.touches[0];
226 last_touch_x = starting_touch.screenX;
227 last_touch_y = starting_touch.screenY;
228 last_touch_time = new Date().getTime();
231 // TODO: Handle different buttons
237 element.addEventListener("touchmove", function(e) {
239 // Handle single-touch movement gesture (touchpad mouse move)
240 if (e.touches.length == 1) {
244 // Get change in touch location
245 var touch = e.touches[0];
246 var delta_x = touch.screenX - last_touch_x;
247 var delta_y = touch.screenY - last_touch_y;
249 // Track pixels moved
250 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
252 // Update mouse location
253 guac_mouse.currentState.x += delta_x;
254 guac_mouse.currentState.y += delta_y;
256 // Prevent mouse from leaving screen
258 if (guac_mouse.currentState.x < 0)
259 guac_mouse.currentState.x = 0;
260 else if (guac_mouse.currentState.x >= element.offsetWidth)
261 guac_mouse.currentState.x = element.offsetWidth - 1;
263 if (guac_mouse.currentState.y < 0)
264 guac_mouse.currentState.y = 0;
265 else if (guac_mouse.currentState.y >= element.offsetHeight)
266 guac_mouse.currentState.y = element.offsetHeight - 1;
268 // Fire movement event, if defined
269 if (guac_mouse.onmousemove)
270 guac_mouse.onmousemove(guac_mouse.currentState);
272 // Update touch location
273 last_touch_x = touch.screenX;
274 last_touch_y = touch.screenY;
281 element.addEventListener("mousedown", function(e) {
283 // Don't handle if we aren't supposed to
284 if (gesture_in_progress) return;
290 guac_mouse.currentState.left = true;
293 guac_mouse.currentState.middle = true;
296 guac_mouse.currentState.right = true;
300 if (guac_mouse.onmousedown)
301 guac_mouse.onmousedown(guac_mouse.currentState);
306 element.addEventListener("mouseup", function(e) {
308 // Don't handle if we aren't supposed to
309 if (gesture_in_progress) return;
315 guac_mouse.currentState.left = false;
318 guac_mouse.currentState.middle = false;
321 guac_mouse.currentState.right = false;
325 if (guac_mouse.onmouseup)
326 guac_mouse.onmouseup(guac_mouse.currentState);
330 element.addEventListener("mouseout", function(e) {
332 // Don't handle if we aren't supposed to
333 if (gesture_in_progress) return;
335 // Get parent of the element the mouse pointer is leaving
336 if (!e) e = window.event;
338 // Check that mouseout is due to actually LEAVING the element
339 var target = e.relatedTarget || e.toElement;
340 while (target != null) {
341 if (target === element)
343 target = target.parentNode;
348 // Release all buttons
349 if (guac_mouse.currentState.left
350 || guac_mouse.currentState.middle
351 || guac_mouse.currentState.right) {
353 guac_mouse.currentState.left = false;
354 guac_mouse.currentState.middle = false;
355 guac_mouse.currentState.right = false;
357 if (guac_mouse.onmouseup)
358 guac_mouse.onmouseup(guac_mouse.currentState);
363 // Override selection on mouse event element.
364 element.addEventListener("selectstart", function(e) {
368 // Scroll wheel support
369 function mousewheel_handler(e) {
371 // Don't handle if we aren't supposed to
372 if (gesture_in_progress) return;
377 else if (e.wheelDelta)
378 delta = -event.wheelDelta;
382 if (guac_mouse.onmousedown) {
383 guac_mouse.currentState.up = true;
384 guac_mouse.onmousedown(guac_mouse.currentState);
387 if (guac_mouse.onmouseup) {
388 guac_mouse.currentState.up = false;
389 guac_mouse.onmouseup(guac_mouse.currentState);
395 if (guac_mouse.onmousedown) {
396 guac_mouse.currentState.down = true;
397 guac_mouse.onmousedown(guac_mouse.currentState);
400 if (guac_mouse.onmouseup) {
401 guac_mouse.currentState.down = false;
402 guac_mouse.onmouseup(guac_mouse.currentState);
409 element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
410 element.addEventListener('mousewheel', mousewheel_handler, false);
415 * Simple container for properties describing the state of a mouse.
418 * @param {Number} x The X position of the mouse pointer in pixels.
419 * @param {Number} y The Y position of the mouse pointer in pixels.
420 * @param {Boolean} left Whether the left mouse button is pressed.
421 * @param {Boolean} middle Whether the middle mouse button is pressed.
422 * @param {Boolean} right Whether the right mouse button is pressed.
423 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
424 * button, usually part of a scroll wheel).
425 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
426 * button, usually part of a scroll wheel).
428 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
431 * The current X position of the mouse pointer.
437 * The current Y position of the mouse pointer.
443 * Whether the left mouse button is currently pressed.
449 * Whether the middle mouse button is currently pressed.
455 * Whether the right mouse button is currently pressed.
461 * Whether the up mouse button is currently pressed. This is the fourth
462 * mouse button, associated with upward scrolling of the mouse scroll
469 * Whether the down mouse button is currently pressed. This is the fifth
470 * mouse button, associated with downward scrolling of the mouse scroll