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 moveMouse(pageX, pageY) {
102 guac_mouse.currentState.x = pageX - element.offsetLeft;
103 guac_mouse.currentState.y = pageY - element.offsetTop;
105 // This is all JUST so we can get the mouse position within the element
106 var parent = element.offsetParent;
108 if (parent.offsetLeft && parent.offsetTop) {
109 guac_mouse.currentState.x -= parent.offsetLeft;
110 guac_mouse.currentState.y -= parent.offsetTop;
112 parent = parent.offsetParent;
115 if (guac_mouse.onmousemove)
116 guac_mouse.onmousemove(guac_mouse.currentState);
121 // Block context menu so right-click gets sent properly
122 element.oncontextmenu = function(e) {
126 element.onmousemove = function(e) {
128 // Don't handle if we aren't supposed to
129 if (gesture_in_progress) return;
133 moveMouse(e.pageX, e.pageY);
137 var last_touch_x = 0;
138 var last_touch_y = 0;
139 var last_touch_time = 0;
140 var pixels_moved = 0;
142 var gesture_in_progress = false;
143 var click_release_timeout = null;
145 element.ontouchend = function(e) {
147 // If we're handling a gesture
148 if (gesture_in_progress) {
153 var time = new Date().getTime();
155 // If mouse already down, release anad clear timeout
156 if (guac_mouse.currentState.left) {
158 // Fire left button up event
159 guac_mouse.currentState.left = false;
160 if (guac_mouse.onmouseup)
161 guac_mouse.onmouseup(guac_mouse.currentState);
163 // Clear timeout, if set
164 if (click_release_timeout) {
165 window.clearTimeout(click_release_timeout);
166 click_release_timeout = null;
171 // If single tap detected (based on time and distance)
172 if (time - last_touch_time <= 250 && pixels_moved < 10) {
174 // Fire left button down event
175 guac_mouse.currentState.left = true;
176 if (guac_mouse.onmousedown)
177 guac_mouse.onmousedown(guac_mouse.currentState);
179 // Delay mouse up - mouse up should be canceled if
180 // touchstart within timeout.
181 click_release_timeout = window.setTimeout(function() {
183 // Fire left button up event
184 guac_mouse.currentState.left = false;
185 if (guac_mouse.onmouseup)
186 guac_mouse.onmouseup(guac_mouse.currentState);
188 // Allow mouse events now that touching is over
189 gesture_in_progress = false;
199 element.ontouchstart = function(e) {
201 // Record initial touch location and time for single-touch movement
203 if (e.touches.length == 1) {
208 // Stop mouse events while touching
209 gesture_in_progress = true;
211 // Clear timeout, if set
212 if (click_release_timeout) {
213 window.clearTimeout(click_release_timeout);
214 click_release_timeout = null;
217 // Record touch location and time
218 var starting_touch = e.touches[0];
219 last_touch_x = starting_touch.pageX;
220 last_touch_y = starting_touch.pageY;
221 last_touch_time = new Date().getTime();
224 // TODO: Handle different buttons
230 element.ontouchmove = function(e) {
232 // Handle single-touch movement gesture (touchpad mouse move)
233 if (e.touches.length == 1) {
238 // Get change in touch location
239 var touch = e.touches[0];
240 var delta_x = touch.pageX - last_touch_x;
241 var delta_y = touch.pageY - last_touch_y;
243 // Track pixels moved
244 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
246 // Update mouse location
247 guac_mouse.currentState.x += delta_x;
248 guac_mouse.currentState.y += delta_y;
250 // Prevent mouse from leaving screen
252 if (guac_mouse.currentState.x < 0)
253 guac_mouse.currentState.x = 0;
254 else if (guac_mouse.currentState.x >= element.offsetWidth)
255 guac_mouse.currentState.x = element.offsetWidth - 1;
257 if (guac_mouse.currentState.y < 0)
258 guac_mouse.currentState.y = 0;
259 else if (guac_mouse.currentState.y >= element.offsetHeight)
260 guac_mouse.currentState.y = element.offsetHeight - 1;
262 // Fire movement event, if defined
263 if (guac_mouse.onmousemove)
264 guac_mouse.onmousemove(guac_mouse.currentState);
266 // Update touch location
267 last_touch_x = touch.pageX;
268 last_touch_y = touch.pageY;
275 element.onmousedown = function(e) {
277 // Don't handle if we aren't supposed to
278 if (gesture_in_progress) return;
284 guac_mouse.currentState.left = true;
287 guac_mouse.currentState.middle = true;
290 guac_mouse.currentState.right = true;
294 if (guac_mouse.onmousedown)
295 guac_mouse.onmousedown(guac_mouse.currentState);
300 element.onmouseup = function(e) {
302 // Don't handle if we aren't supposed to
303 if (gesture_in_progress) return;
309 guac_mouse.currentState.left = false;
312 guac_mouse.currentState.middle = false;
315 guac_mouse.currentState.right = false;
319 if (guac_mouse.onmouseup)
320 guac_mouse.onmouseup(guac_mouse.currentState);
324 element.onmouseout = function(e) {
326 // Don't handle if we aren't supposed to
327 if (gesture_in_progress) return;
331 // Release all buttons
332 if (guac_mouse.currentState.left
333 || guac_mouse.currentState.middle
334 || guac_mouse.currentState.right) {
336 guac_mouse.currentState.left = false;
337 guac_mouse.currentState.middle = false;
338 guac_mouse.currentState.right = false;
340 if (guac_mouse.onmouseup)
341 guac_mouse.onmouseup(guac_mouse.currentState);
346 // Override selection on mouse event element.
347 element.onselectstart = function() {
351 // Scroll wheel support
352 element.onmousewheel = function(e) {
354 // Don't handle if we aren't supposed to
355 if (gesture_in_progress) return;
360 else if (e.wheelDelta)
361 delta = -event.wheelDelta;
365 if (guac_mouse.onmousedown) {
366 guac_mouse.currentState.up = true;
367 guac_mouse.onmousedown(guac_mouse.currentState);
370 if (guac_mouse.onmouseup) {
371 guac_mouse.currentState.up = false;
372 guac_mouse.onmouseup(guac_mouse.currentState);
378 if (guac_mouse.onmousedown) {
379 guac_mouse.currentState.down = true;
380 guac_mouse.onmousedown(guac_mouse.currentState);
383 if (guac_mouse.onmouseup) {
384 guac_mouse.currentState.down = false;
385 guac_mouse.onmouseup(guac_mouse.currentState);
389 if (e.preventDefault)
392 e.returnValue = false;
396 element.addEventListener('DOMMouseScroll', element.onmousewheel, false);
401 * Simple container for properties describing the state of a mouse.
404 * @param {Number} x The X position of the mouse pointer in pixels.
405 * @param {Number} y The Y position of the mouse pointer in pixels.
406 * @param {Boolean} left Whether the left mouse button is pressed.
407 * @param {Boolean} middle Whether the middle mouse button is pressed.
408 * @param {Boolean} right Whether the right mouse button is pressed.
409 * @param {Boolean} up Whether the up mouse button is pressed (the fourth
410 * button, usually part of a scroll wheel).
411 * @param {Boolean} down Whether the down mouse button is pressed (the fifth
412 * button, usually part of a scroll wheel).
414 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
417 * The current X position of the mouse pointer.
423 * The current Y position of the mouse pointer.
429 * Whether the left mouse button is currently pressed.
435 * Whether the middle mouse button is currently pressed.
441 * Whether the right mouse button is currently pressed.
447 * Whether the up mouse button is currently pressed. This is the fourth
448 * mouse button, associated with upward scrolling of the mouse scroll
455 * Whether the down mouse button is currently pressed. This is the fifth
456 * mouse button, associated with downward scrolling of the mouse scroll