6f1edf0c5dceae9b51fcd0ac011cd3b101b73d60
[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 event support is planned, but currently only in testing (translate
48  * touch events into mouse events).
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             // FIXME: Prevent mouse from leaving screen
251
252             // Fire movement event, if defined
253             if (guac_mouse.onmousemove)
254                 guac_mouse.onmousemove(guac_mouse.currentState);
255
256             // Update touch location
257             last_touch_x = touch.pageX;
258             last_touch_y = touch.pageY;
259
260         }
261
262     };
263
264
265     element.onmousedown = function(e) {
266
267         // Don't handle if we aren't supposed to
268         if (gesture_in_progress) return;
269
270         e.stopPropagation();
271
272         switch (e.button) {
273             case 0:
274                 guac_mouse.currentState.left = true;
275                 break;
276             case 1:
277                 guac_mouse.currentState.middle = true;
278                 break;
279             case 2:
280                 guac_mouse.currentState.right = true;
281                 break;
282         }
283
284         if (guac_mouse.onmousedown)
285             guac_mouse.onmousedown(guac_mouse.currentState);
286
287     };
288
289
290     element.onmouseup = function(e) {
291
292         // Don't handle if we aren't supposed to
293         if (gesture_in_progress) return;
294
295         e.stopPropagation();
296
297         switch (e.button) {
298             case 0:
299                 guac_mouse.currentState.left = false;
300                 break;
301             case 1:
302                 guac_mouse.currentState.middle = false;
303                 break;
304             case 2:
305                 guac_mouse.currentState.right = false;
306                 break;
307         }
308
309         if (guac_mouse.onmouseup)
310             guac_mouse.onmouseup(guac_mouse.currentState);
311
312     };
313
314     element.onmouseout = function(e) {
315
316         // Don't handle if we aren't supposed to
317         if (gesture_in_progress) return;
318
319         e.stopPropagation();
320
321         // Release all buttons
322         if (guac_mouse.currentState.left
323             || guac_mouse.currentState.middle
324             || guac_mouse.currentState.right) {
325
326             guac_mouse.currentState.left = false;
327             guac_mouse.currentState.middle = false;
328             guac_mouse.currentState.right = false;
329
330             if (guac_mouse.onmouseup)
331                 guac_mouse.onmouseup(guac_mouse.currentState);
332         }
333
334     };
335
336     // Override selection on mouse event element.
337     element.onselectstart = function() {
338         return false;
339     };
340
341     // Scroll wheel support
342     element.onmousewheel = function(e) {
343
344         // Don't handle if we aren't supposed to
345         if (gesture_in_progress) return;
346
347         var delta = 0;
348         if (e.detail)
349             delta = e.detail;
350         else if (e.wheelDelta)
351             delta = -event.wheelDelta;
352
353         // Up
354         if (delta < 0) {
355             if (guac_mouse.onmousedown) {
356                 guac_mouse.currentState.up = true;
357                 guac_mouse.onmousedown(guac_mouse.currentState);
358             }
359
360             if (guac_mouse.onmouseup) {
361                 guac_mouse.currentState.up = false;
362                 guac_mouse.onmouseup(guac_mouse.currentState);
363             }
364         }
365
366         // Down
367         if (delta > 0) {
368             if (guac_mouse.onmousedown) {
369                 guac_mouse.currentState.down = true;
370                 guac_mouse.onmousedown(guac_mouse.currentState);
371             }
372
373             if (guac_mouse.onmouseup) {
374                 guac_mouse.currentState.down = false;
375                 guac_mouse.onmouseup(guac_mouse.currentState);
376             }
377         }
378
379         if (e.preventDefault)
380             e.preventDefault();
381
382         e.returnValue = false;
383
384     };
385
386     element.addEventListener('DOMMouseScroll', element.onmousewheel, false);
387
388 };
389
390 /**
391  * Simple container for properties describing the state of a mouse.
392  * 
393  * @constructor
394  * @param {Number} x The X position of the mouse pointer in pixels.
395  * @param {Number} y The Y position of the mouse pointer in pixels.
396  * @param {Boolean} left Whether the left mouse button is pressed. 
397  * @param {Boolean} middle Whether the middle mouse button is pressed. 
398  * @param {Boolean} right Whether the right mouse button is pressed. 
399  * @param {Boolean} up Whether the up mouse button is pressed (the fourth
400  *                     button, usually part of a scroll wheel). 
401  * @param {Boolean} down Whether the down mouse button is pressed (the fifth
402  *                       button, usually part of a scroll wheel). 
403  */
404 Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
405
406     /**
407      * The current X position of the mouse pointer.
408      * @type Number
409      */
410     this.x = x;
411
412     /**
413      * The current Y position of the mouse pointer.
414      * @type Number
415      */
416     this.y = y;
417
418     /**
419      * Whether the left mouse button is currently pressed.
420      * @type Boolean
421      */
422     this.left = left;
423
424     /**
425      * Whether the middle mouse button is currently pressed.
426      * @type Boolean
427      */
428     this.middle = middle
429
430     /**
431      * Whether the right mouse button is currently pressed.
432      * @type Boolean
433      */
434     this.right = right;
435
436     /**
437      * Whether the up mouse button is currently pressed. This is the fourth
438      * mouse button, associated with upward scrolling of the mouse scroll
439      * wheel.
440      * @type Boolean
441      */
442     this.up = up;
443
444     /**
445      * Whether the down mouse button is currently pressed. This is the fifth 
446      * mouse button, associated with downward scrolling of the mouse scroll
447      * wheel.
448      * @type Boolean
449      */
450     this.down = down;
451     
452 };
453