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