f648602ba64ee6d9661651b74e9b7e434bc20aec
[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             if (parent.offsetLeft && parent.offsetTop) {
115                 guac_mouse.currentState.x -= parent.offsetLeft;
116                 guac_mouse.currentState.y -= parent.offsetTop;
117             }
118             parent = parent.offsetParent;
119         }
120
121         if (guac_mouse.onmousemove)
122             guac_mouse.onmousemove(guac_mouse.currentState);
123
124     }
125
126
127     // Block context menu so right-click gets sent properly
128     element.addEventListener("contextmenu", function(e) {
129         cancelEvent(e);
130     }, false);
131
132     element.addEventListener("mousemove", function(e) {
133
134         // Don't handle if we aren't supposed to
135         if (gesture_in_progress) return;
136
137         cancelEvent(e);
138
139         moveMouse(e.pageX, e.pageY);
140
141     }, false);
142
143     var last_touch_x = 0;
144     var last_touch_y = 0;
145     var last_touch_time = 0;
146     var pixels_moved = 0;
147
148     var gesture_in_progress = false;
149     var click_release_timeout = null;
150
151     element.addEventListener("touchend", function(e) {
152         
153         // If we're handling a gesture
154         if (gesture_in_progress) {
155             
156             cancelEvent(e);
157             
158             var time = new Date().getTime();
159
160             // If mouse already down, release anad clear timeout
161             if (guac_mouse.currentState.left) {
162
163                 // Fire left button up event
164                 guac_mouse.currentState.left = false;
165                 if (guac_mouse.onmouseup)
166                     guac_mouse.onmouseup(guac_mouse.currentState);
167
168                 // Clear timeout, if set
169                 if (click_release_timeout) {
170                     window.clearTimeout(click_release_timeout);
171                     click_release_timeout = null;
172                 }
173
174             }
175
176             // If single tap detected (based on time and distance)
177             if (time - last_touch_time <= 250 && pixels_moved < 10) {
178
179                 // Fire left button down event
180                 guac_mouse.currentState.left = true;
181                 if (guac_mouse.onmousedown)
182                     guac_mouse.onmousedown(guac_mouse.currentState);
183
184                 // Delay mouse up - mouse up should be canceled if
185                 // touchstart within timeout.
186                 click_release_timeout = window.setTimeout(function() {
187                     
188                     // Fire left button up event
189                     guac_mouse.currentState.left = false;
190                     if (guac_mouse.onmouseup)
191                         guac_mouse.onmouseup(guac_mouse.currentState);
192                     
193                     // Allow mouse events now that touching is over
194                     gesture_in_progress = false;
195             
196                 }, 250);
197
198             }
199
200         }
201
202     }, false);
203
204     element.addEventListener("touchstart", function(e) {
205
206         // Record initial touch location and time for single-touch movement
207         // and tap gestures
208         if (e.touches.length == 1) {
209
210             cancelEvent(e);
211
212             // Stop mouse events while touching
213             gesture_in_progress = true;
214
215             // Clear timeout, if set
216             if (click_release_timeout) {
217                 window.clearTimeout(click_release_timeout);
218                 click_release_timeout = null;
219             }
220
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();
226             pixels_moved = 0;
227
228             // TODO: Handle different buttons
229
230         }
231
232     }, false);
233
234     element.addEventListener("touchmove", function(e) {
235
236         // Handle single-touch movement gesture (touchpad mouse move)
237         if (e.touches.length == 1) {
238
239             cancelEvent(e);
240
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;
245
246             // Track pixels moved
247             pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
248
249             // Update mouse location
250             guac_mouse.currentState.x += delta_x;
251             guac_mouse.currentState.y += delta_y;
252
253             // Prevent mouse from leaving screen
254
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;
259
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;
264
265             // Fire movement event, if defined
266             if (guac_mouse.onmousemove)
267                 guac_mouse.onmousemove(guac_mouse.currentState);
268
269             // Update touch location
270             last_touch_x = touch.pageX;
271             last_touch_y = touch.pageY;
272
273         }
274
275     }, false);
276
277
278     element.addEventListener("mousedown", function(e) {
279
280         // Don't handle if we aren't supposed to
281         if (gesture_in_progress) return;
282
283         cancelEvent(e);
284
285         switch (e.button) {
286             case 0:
287                 guac_mouse.currentState.left = true;
288                 break;
289             case 1:
290                 guac_mouse.currentState.middle = true;
291                 break;
292             case 2:
293                 guac_mouse.currentState.right = true;
294                 break;
295         }
296
297         if (guac_mouse.onmousedown)
298             guac_mouse.onmousedown(guac_mouse.currentState);
299
300     }, false);
301
302
303     element.addEventListener("mouseup", function(e) {
304
305         // Don't handle if we aren't supposed to
306         if (gesture_in_progress) return;
307
308         cancelEvent(e);
309
310         switch (e.button) {
311             case 0:
312                 guac_mouse.currentState.left = false;
313                 break;
314             case 1:
315                 guac_mouse.currentState.middle = false;
316                 break;
317             case 2:
318                 guac_mouse.currentState.right = false;
319                 break;
320         }
321
322         if (guac_mouse.onmouseup)
323             guac_mouse.onmouseup(guac_mouse.currentState);
324
325     }, false);
326
327     element.addEventListener("mouseout", function(e) {
328
329         // Don't handle if we aren't supposed to
330         if (gesture_in_progress) return;
331
332         // Get parent of the element the mouse pointer is leaving
333         if (!e) e = window.event;
334
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)
339                 return;
340             target = target.parentNode;
341         }
342
343         cancelEvent(e);
344
345         // Release all buttons
346         if (guac_mouse.currentState.left
347             || guac_mouse.currentState.middle
348             || guac_mouse.currentState.right) {
349
350             guac_mouse.currentState.left = false;
351             guac_mouse.currentState.middle = false;
352             guac_mouse.currentState.right = false;
353
354             if (guac_mouse.onmouseup)
355                 guac_mouse.onmouseup(guac_mouse.currentState);
356         }
357
358     }, false);
359
360     // Override selection on mouse event element.
361     element.addEventListener("selectstart", function(e) {
362         cancelEvent(e);
363     }, false);
364
365     // Scroll wheel support
366     function mousewheel_handler(e) {
367
368         // Don't handle if we aren't supposed to
369         if (gesture_in_progress) return;
370
371         var delta = 0;
372         if (e.detail)
373             delta = e.detail;
374         else if (e.wheelDelta)
375             delta = -event.wheelDelta;
376
377         // Up
378         if (delta < 0) {
379             if (guac_mouse.onmousedown) {
380                 guac_mouse.currentState.up = true;
381                 guac_mouse.onmousedown(guac_mouse.currentState);
382             }
383
384             if (guac_mouse.onmouseup) {
385                 guac_mouse.currentState.up = false;
386                 guac_mouse.onmouseup(guac_mouse.currentState);
387             }
388         }
389
390         // Down
391         if (delta > 0) {
392             if (guac_mouse.onmousedown) {
393                 guac_mouse.currentState.down = true;
394                 guac_mouse.onmousedown(guac_mouse.currentState);
395             }
396
397             if (guac_mouse.onmouseup) {
398                 guac_mouse.currentState.down = false;
399                 guac_mouse.onmouseup(guac_mouse.currentState);
400             }
401         }
402
403         cancelEvent(e);
404
405     }
406     element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
407     element.addEventListener('mousewheel',     mousewheel_handler, false);
408
409 };
410
411 /**
412  * Simple container for properties describing the state of a mouse.
413  * 
414  * @constructor
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). 
424  */
425 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
426
427     /**
428      * The current X position of the mouse pointer.
429      * @type Number
430      */
431     this.x = x;
432
433     /**
434      * The current Y position of the mouse pointer.
435      * @type Number
436      */
437     this.y = y;
438
439     /**
440      * Whether the left mouse button is currently pressed.
441      * @type Boolean
442      */
443     this.left = left;
444
445     /**
446      * Whether the middle mouse button is currently pressed.
447      * @type Boolean
448      */
449     this.middle = middle
450
451     /**
452      * Whether the right mouse button is currently pressed.
453      * @type Boolean
454      */
455     this.right = right;
456
457     /**
458      * Whether the up mouse button is currently pressed. This is the fourth
459      * mouse button, associated with upward scrolling of the mouse scroll
460      * wheel.
461      * @type Boolean
462      */
463     this.up = up;
464
465     /**
466      * Whether the down mouse button is currently pressed. This is the fifth 
467      * mouse button, associated with downward scrolling of the mouse scroll
468      * wheel.
469      * @type Boolean
470      */
471     this.down = down;
472     
473 };
474