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