547f51722c103672f648f6de02f9960f6f0aa641
[guacamole-common-js.git] / src / main / resources / mouse.js
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is guacamole-common-js.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2010
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
40
41 /**
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.
46  * 
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).
49  * 
50  * @constructor
51  * @param {Element} element The Element to use to provide mouse events.
52  */
53 Guacamole.Mouse = function(element) {
54
55     /**
56      * Reference to this Guacamole.Mouse.
57      * @private
58      */
59     var guac_mouse = this;
60
61     /**
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.
65      * 
66      * @type Guacamole.Mouse.State
67      */
68     this.currentState = new Guacamole.Mouse.State(
69         0, 0, 
70         false, false, false, false, false
71     );
72
73     /**
74      * Fired whenever the user presses a mouse button down over the element
75      * associated with this Guacamole.Mouse.
76      * 
77      * @event
78      * @param {Guacamole.Mouse.State} state The current mouse state.
79      */
80         this.onmousedown = null;
81
82     /**
83      * Fired whenever the user releases a mouse button down over the element
84      * associated with this Guacamole.Mouse.
85      * 
86      * @event
87      * @param {Guacamole.Mouse.State} state The current mouse state.
88      */
89         this.onmouseup = null;
90
91     /**
92      * Fired whenever the user moves the mouse over the element associated with
93      * this Guacamole.Mouse.
94      * 
95      * @event
96      * @param {Guacamole.Mouse.State} state The current mouse state.
97      */
98         this.onmousemove = null;
99
100     function cancelEvent(e) {
101         e.stopPropagation();
102         if (e.preventDefault) e.preventDefault();
103         e.returnValue = false;
104     }
105
106     function moveMouse(pageX, pageY) {
107
108         guac_mouse.currentState.x = pageX - element.offsetLeft;
109         guac_mouse.currentState.y = pageY - element.offsetTop;
110
111         // This is all JUST so we can get the mouse position within the element
112         var parent = element.offsetParent;
113         while (parent) {
114
115             if (parent.offsetLeft)
116                 guac_mouse.currentState.x -= parent.offsetLeft;
117
118             if (parent.offsetTop)
119                 guac_mouse.currentState.y -= parent.offsetTop;
120
121             parent = parent.offsetParent;
122         }
123
124         if (guac_mouse.onmousemove)
125             guac_mouse.onmousemove(guac_mouse.currentState);
126
127     }
128
129
130     // Block context menu so right-click gets sent properly
131     element.addEventListener("contextmenu", function(e) {
132         cancelEvent(e);
133     }, false);
134
135     element.addEventListener("mousemove", function(e) {
136
137         // Don't handle if we aren't supposed to
138         if (gesture_in_progress) return;
139
140         cancelEvent(e);
141
142         moveMouse(e.pageX, e.pageY);
143
144     }, false);
145
146     var last_touch_x = 0;
147     var last_touch_y = 0;
148     var last_touch_time = 0;
149     var pixels_moved = 0;
150
151     var gesture_in_progress = false;
152     var click_release_timeout = null;
153
154     element.addEventListener("touchend", function(e) {
155         
156         // If we're handling a gesture
157         if (gesture_in_progress) {
158             
159             cancelEvent(e);
160             
161             var time = new Date().getTime();
162
163             // If mouse already down, release anad clear timeout
164             if (guac_mouse.currentState.left) {
165
166                 // Fire left button up event
167                 guac_mouse.currentState.left = false;
168                 if (guac_mouse.onmouseup)
169                     guac_mouse.onmouseup(guac_mouse.currentState);
170
171                 // Clear timeout, if set
172                 if (click_release_timeout) {
173                     window.clearTimeout(click_release_timeout);
174                     click_release_timeout = null;
175                 }
176
177             }
178
179             // If single tap detected (based on time and distance)
180             if (time - last_touch_time <= 250 && pixels_moved < 10) {
181
182                 // Fire left button down event
183                 guac_mouse.currentState.left = true;
184                 if (guac_mouse.onmousedown)
185                     guac_mouse.onmousedown(guac_mouse.currentState);
186
187                 // Delay mouse up - mouse up should be canceled if
188                 // touchstart within timeout.
189                 click_release_timeout = window.setTimeout(function() {
190                     
191                     // Fire left button up event
192                     guac_mouse.currentState.left = false;
193                     if (guac_mouse.onmouseup)
194                         guac_mouse.onmouseup(guac_mouse.currentState);
195                     
196                     // Allow mouse events now that touching is over
197                     gesture_in_progress = false;
198             
199                 }, 250);
200
201             }
202
203         }
204
205     }, false);
206
207     element.addEventListener("touchstart", function(e) {
208
209         // Record initial touch location and time for single-touch movement
210         // and tap gestures
211         if (e.touches.length == 1) {
212
213             cancelEvent(e);
214
215             // Stop mouse events while touching
216             gesture_in_progress = true;
217
218             // Clear timeout, if set
219             if (click_release_timeout) {
220                 window.clearTimeout(click_release_timeout);
221                 click_release_timeout = null;
222             }
223
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();
229             pixels_moved = 0;
230
231             // TODO: Handle different buttons
232
233         }
234
235     }, false);
236
237     element.addEventListener("touchmove", function(e) {
238
239         // Handle single-touch movement gesture (touchpad mouse move)
240         if (e.touches.length == 1) {
241
242             cancelEvent(e);
243
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;
248
249             // Track pixels moved
250             pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
251
252             // Update mouse location
253             guac_mouse.currentState.x += delta_x;
254             guac_mouse.currentState.y += delta_y;
255
256             // Prevent mouse from leaving screen
257
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;
262
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;
267
268             // Fire movement event, if defined
269             if (guac_mouse.onmousemove)
270                 guac_mouse.onmousemove(guac_mouse.currentState);
271
272             // Update touch location
273             last_touch_x = touch.screenX;
274             last_touch_y = touch.screenY;
275
276         }
277
278     }, false);
279
280
281     element.addEventListener("mousedown", function(e) {
282
283         // Don't handle if we aren't supposed to
284         if (gesture_in_progress) return;
285
286         cancelEvent(e);
287
288         switch (e.button) {
289             case 0:
290                 guac_mouse.currentState.left = true;
291                 break;
292             case 1:
293                 guac_mouse.currentState.middle = true;
294                 break;
295             case 2:
296                 guac_mouse.currentState.right = true;
297                 break;
298         }
299
300         if (guac_mouse.onmousedown)
301             guac_mouse.onmousedown(guac_mouse.currentState);
302
303     }, false);
304
305
306     element.addEventListener("mouseup", function(e) {
307
308         // Don't handle if we aren't supposed to
309         if (gesture_in_progress) return;
310
311         cancelEvent(e);
312
313         switch (e.button) {
314             case 0:
315                 guac_mouse.currentState.left = false;
316                 break;
317             case 1:
318                 guac_mouse.currentState.middle = false;
319                 break;
320             case 2:
321                 guac_mouse.currentState.right = false;
322                 break;
323         }
324
325         if (guac_mouse.onmouseup)
326             guac_mouse.onmouseup(guac_mouse.currentState);
327
328     }, false);
329
330     element.addEventListener("mouseout", function(e) {
331
332         // Don't handle if we aren't supposed to
333         if (gesture_in_progress) return;
334
335         // Get parent of the element the mouse pointer is leaving
336         if (!e) e = window.event;
337
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)
342                 return;
343             target = target.parentNode;
344         }
345
346         cancelEvent(e);
347
348         // Release all buttons
349         if (guac_mouse.currentState.left
350             || guac_mouse.currentState.middle
351             || guac_mouse.currentState.right) {
352
353             guac_mouse.currentState.left = false;
354             guac_mouse.currentState.middle = false;
355             guac_mouse.currentState.right = false;
356
357             if (guac_mouse.onmouseup)
358                 guac_mouse.onmouseup(guac_mouse.currentState);
359         }
360
361     }, false);
362
363     // Override selection on mouse event element.
364     element.addEventListener("selectstart", function(e) {
365         cancelEvent(e);
366     }, false);
367
368     // Scroll wheel support
369     function mousewheel_handler(e) {
370
371         // Don't handle if we aren't supposed to
372         if (gesture_in_progress) return;
373
374         var delta = 0;
375         if (e.detail)
376             delta = e.detail;
377         else if (e.wheelDelta)
378             delta = -event.wheelDelta;
379
380         // Up
381         if (delta < 0) {
382             if (guac_mouse.onmousedown) {
383                 guac_mouse.currentState.up = true;
384                 guac_mouse.onmousedown(guac_mouse.currentState);
385             }
386
387             if (guac_mouse.onmouseup) {
388                 guac_mouse.currentState.up = false;
389                 guac_mouse.onmouseup(guac_mouse.currentState);
390             }
391         }
392
393         // Down
394         if (delta > 0) {
395             if (guac_mouse.onmousedown) {
396                 guac_mouse.currentState.down = true;
397                 guac_mouse.onmousedown(guac_mouse.currentState);
398             }
399
400             if (guac_mouse.onmouseup) {
401                 guac_mouse.currentState.down = false;
402                 guac_mouse.onmouseup(guac_mouse.currentState);
403             }
404         }
405
406         cancelEvent(e);
407
408     }
409     element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
410     element.addEventListener('mousewheel',     mousewheel_handler, false);
411
412 };
413
414 /**
415  * Simple container for properties describing the state of a mouse.
416  * 
417  * @constructor
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). 
427  */
428 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
429
430     /**
431      * The current X position of the mouse pointer.
432      * @type Number
433      */
434     this.x = x;
435
436     /**
437      * The current Y position of the mouse pointer.
438      * @type Number
439      */
440     this.y = y;
441
442     /**
443      * Whether the left mouse button is currently pressed.
444      * @type Boolean
445      */
446     this.left = left;
447
448     /**
449      * Whether the middle mouse button is currently pressed.
450      * @type Boolean
451      */
452     this.middle = middle
453
454     /**
455      * Whether the right mouse button is currently pressed.
456      * @type Boolean
457      */
458     this.right = right;
459
460     /**
461      * Whether the up mouse button is currently pressed. This is the fourth
462      * mouse button, associated with upward scrolling of the mouse scroll
463      * wheel.
464      * @type Boolean
465      */
466     this.up = up;
467
468     /**
469      * Whether the down mouse button is currently pressed. This is the fifth 
470      * mouse button, associated with downward scrolling of the mouse scroll
471      * wheel.
472      * @type Boolean
473      */
474     this.down = down;
475     
476 };
477