clockwerk-guacamole – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 distance a two-finger touch must move per scrollwheel event, in
63 * pixels.
64 */
65 this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
66  
67 /**
68 * The maximum number of milliseconds to wait for a touch to end for the
69 * gesture to be considered a click.
70 */
71 this.clickTimingThreshold = 250;
72  
73 /**
74 * The maximum number of pixels to allow a touch to move for the gesture to
75 * be considered a click.
76 */
77 this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
78  
79 /**
80 * The current mouse state. The properties of this state are updated when
81 * mouse events fire. This state object is also passed in as a parameter to
82 * the handler of any mouse events.
83 *
84 * @type Guacamole.Mouse.State
85 */
86 this.currentState = new Guacamole.Mouse.State(
87 0, 0,
88 false, false, false, false, false
89 );
90  
91 /**
92 * Fired whenever the user presses a mouse button down over the element
93 * associated with this Guacamole.Mouse.
94 *
95 * @event
96 * @param {Guacamole.Mouse.State} state The current mouse state.
97 */
98 this.onmousedown = null;
99  
100 /**
101 * Fired whenever the user releases a mouse button down over the element
102 * associated with this Guacamole.Mouse.
103 *
104 * @event
105 * @param {Guacamole.Mouse.State} state The current mouse state.
106 */
107 this.onmouseup = null;
108  
109 /**
110 * Fired whenever the user moves the mouse over the element associated with
111 * this Guacamole.Mouse.
112 *
113 * @event
114 * @param {Guacamole.Mouse.State} state The current mouse state.
115 */
116 this.onmousemove = null;
117  
118 function cancelEvent(e) {
119 e.stopPropagation();
120 if (e.preventDefault) e.preventDefault();
121 e.returnValue = false;
122 }
123  
124 function moveMouse(clientX, clientY) {
125  
126 guac_mouse.currentState.x = clientX - element.offsetLeft;
127 guac_mouse.currentState.y = clientY - element.offsetTop;
128  
129 // This is all JUST so we can get the mouse position within the element
130 var parent = element.offsetParent;
131 while (parent && !(parent === document.body)) {
132 guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
133 guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
134  
135 parent = parent.offsetParent;
136 }
137  
138 // Offset by document scroll amount
139 var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
140 var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
141  
142 guac_mouse.currentState.x -= parent.offsetLeft - documentScrollLeft;
143 guac_mouse.currentState.y -= parent.offsetTop - documentScrollTop;
144  
145 if (guac_mouse.onmousemove)
146 guac_mouse.onmousemove(guac_mouse.currentState);
147  
148 }
149  
150  
151 // Block context menu so right-click gets sent properly
152 element.addEventListener("contextmenu", function(e) {
153 cancelEvent(e);
154 }, false);
155  
156 element.addEventListener("mousemove", function(e) {
157  
158 // Don't handle if we aren't supposed to
159 if (gesture_in_progress) return;
160  
161 cancelEvent(e);
162  
163 moveMouse(e.clientX, e.clientY);
164  
165 }, false);
166  
167 var touch_count = 0;
168 var last_touch_x = 0;
169 var last_touch_y = 0;
170 var last_touch_time = 0;
171 var pixels_moved = 0;
172  
173 var touch_buttons = {
174 1: "left",
175 2: "right",
176 3: "middle"
177 };
178  
179 var gesture_in_progress = false;
180 var click_release_timeout = null;
181  
182 element.addEventListener("touchend", function(e) {
183  
184 cancelEvent(e);
185  
186 // If we're handling a gesture AND this is the last touch
187 if (gesture_in_progress && e.touches.length == 0) {
188  
189 var time = new Date().getTime();
190  
191 // Get corresponding mouse button
192 var button = touch_buttons[touch_count];
193  
194 // If mouse already down, release anad clear timeout
195 if (guac_mouse.currentState[button]) {
196  
197 // Fire button up event
198 guac_mouse.currentState[button] = false;
199 if (guac_mouse.onmouseup)
200 guac_mouse.onmouseup(guac_mouse.currentState);
201  
202 // Clear timeout, if set
203 if (click_release_timeout) {
204 window.clearTimeout(click_release_timeout);
205 click_release_timeout = null;
206 }
207  
208 }
209  
210 // If single tap detected (based on time and distance)
211 if (time - last_touch_time <= guac_mouse.clickTimingThreshold
212 && pixels_moved < guac_mouse.clickMoveThreshold) {
213  
214 // Fire button down event
215 guac_mouse.currentState[button] = true;
216 if (guac_mouse.onmousedown)
217 guac_mouse.onmousedown(guac_mouse.currentState);
218  
219 // Delay mouse up - mouse up should be canceled if
220 // touchstart within timeout.
221 click_release_timeout = window.setTimeout(function() {
222  
223 // Fire button up event
224 guac_mouse.currentState[button] = false;
225 if (guac_mouse.onmouseup)
226 guac_mouse.onmouseup(guac_mouse.currentState);
227  
228 // Gesture now over
229 gesture_in_progress = false;
230  
231 }, guac_mouse.clickTimingThreshold);
232  
233 }
234  
235 // If we're not waiting to see if this is a click, stop gesture
236 if (!click_release_timeout)
237 gesture_in_progress = false;
238  
239 }
240  
241 }, false);
242  
243 element.addEventListener("touchstart", function(e) {
244  
245 cancelEvent(e);
246  
247 // Track number of touches, but no more than three
248 touch_count = Math.min(e.touches.length, 3);
249  
250 // Clear timeout, if set
251 if (click_release_timeout) {
252 window.clearTimeout(click_release_timeout);
253 click_release_timeout = null;
254 }
255  
256 // Record initial touch location and time for touch movement
257 // and tap gestures
258 if (!gesture_in_progress) {
259  
260 // Stop mouse events while touching
261 gesture_in_progress = true;
262  
263 // Record touch location and time
264 var starting_touch = e.touches[0];
265 last_touch_x = starting_touch.clientX;
266 last_touch_y = starting_touch.clientY;
267 last_touch_time = new Date().getTime();
268 pixels_moved = 0;
269  
270 }
271  
272 }, false);
273  
274 element.addEventListener("touchmove", function(e) {
275  
276 cancelEvent(e);
277  
278 // Get change in touch location
279 var touch = e.touches[0];
280 var delta_x = touch.clientX - last_touch_x;
281 var delta_y = touch.clientY - last_touch_y;
282  
283 // Track pixels moved
284 pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
285  
286 // If only one touch involved, this is mouse move
287 if (touch_count == 1) {
288  
289 // Calculate average velocity in Manhatten pixels per millisecond
290 var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
291  
292 // Scale mouse movement relative to velocity
293 var scale = 1 + velocity;
294  
295 // Update mouse location
296 guac_mouse.currentState.x += delta_x*scale;
297 guac_mouse.currentState.y += delta_y*scale;
298  
299 // Prevent mouse from leaving screen
300  
301 if (guac_mouse.currentState.x < 0)
302 < 0) guac_mouse.currentState.x = 0;
303 < 0) else if (guac_mouse.currentState.x >= element.offsetWidth)
304 < 0) guac_mouse.currentState.x = element.offsetWidth - 1;
305  
306 < 0) if (guac_mouse.currentState.y < 0)
307 < 0)< 0) guac_mouse.currentState.y = 0;
308 < 0)< 0) else if (guac_mouse.currentState.y >= element.offsetHeight)
309 < 0)< 0) guac_mouse.currentState.y = element.offsetHeight - 1;
310  
311 < 0)< 0) // Fire movement event, if defined
312 < 0)< 0) if (guac_mouse.onmousemove)
313 < 0)< 0) guac_mouse.onmousemove(guac_mouse.currentState);
314  
315 < 0)< 0) // Update touch location
316 < 0)< 0) last_touch_x = touch.clientX;
317 < 0)< 0) last_touch_y = touch.clientY;
318  
319 < 0)< 0) }
320  
321 < 0)< 0) // Interpret two-finger swipe as scrollwheel
322 < 0)< 0) else if (touch_count == 2) {
323  
324 < 0)< 0) // If change in location passes threshold for scroll
325 < 0)< 0) if (Math.abs(delta_y) >= guac_mouse.scrollThreshold) {
326  
327 < 0)< 0) // Decide button based on Y movement direction
328 < 0)< 0) var button;
329 < 0)< 0) if (delta_y > 0) button = "down";
330 < 0)< 0) else button = "up";
331  
332 < 0)< 0) // Fire button down event
333 < 0)< 0) guac_mouse.currentState[button] = true;
334 < 0)< 0) if (guac_mouse.onmousedown)
335 < 0)< 0) guac_mouse.onmousedown(guac_mouse.currentState);
336  
337 < 0)< 0) // Fire button up event
338 < 0)< 0) guac_mouse.currentState[button] = false;
339 < 0)< 0) if (guac_mouse.onmouseup)
340 < 0)< 0) guac_mouse.onmouseup(guac_mouse.currentState);
341  
342 < 0)< 0) // Only update touch location after a scroll has been
343 < 0)< 0) // detected
344 < 0)< 0) last_touch_x = touch.clientX;
345 < 0)< 0) last_touch_y = touch.clientY;
346  
347 < 0)< 0) }
348  
349 < 0)< 0) }
350  
351 < 0)< 0) }, false);
352  
353  
354 < 0)< 0) element.addEventListener("mousedown", function(e) {
355  
356 < 0)< 0) // Don't handle if we aren't supposed to
357 < 0)< 0) if (gesture_in_progress) return;
358  
359 < 0)< 0) cancelEvent(e);
360  
361 < 0)< 0) switch (e.button) {
362 < 0)< 0) case 0:
363 < 0)< 0) guac_mouse.currentState.left = true;
364 < 0)< 0) break;
365 < 0)< 0) case 1:
366 < 0)< 0) guac_mouse.currentState.middle = true;
367 < 0)< 0) break;
368 < 0)< 0) case 2:
369 < 0)< 0) guac_mouse.currentState.right = true;
370 < 0)< 0) break;
371 < 0)< 0) }
372  
373 < 0)< 0) if (guac_mouse.onmousedown)
374 < 0)< 0) guac_mouse.onmousedown(guac_mouse.currentState);
375  
376 < 0)< 0) }, false);
377  
378  
379 < 0)< 0) element.addEventListener("mouseup", function(e) {
380  
381 < 0)< 0) // Don't handle if we aren't supposed to
382 < 0)< 0) if (gesture_in_progress) return;
383  
384 < 0)< 0) cancelEvent(e);
385  
386 < 0)< 0) switch (e.button) {
387 < 0)< 0) case 0:
388 < 0)< 0) guac_mouse.currentState.left = false;
389 < 0)< 0) break;
390 < 0)< 0) case 1:
391 < 0)< 0) guac_mouse.currentState.middle = false;
392 < 0)< 0) break;
393 < 0)< 0) case 2:
394 < 0)< 0) guac_mouse.currentState.right = false;
395 < 0)< 0) break;
396 < 0)< 0) }
397  
398 < 0)< 0) if (guac_mouse.onmouseup)
399 < 0)< 0) guac_mouse.onmouseup(guac_mouse.currentState);
400  
401 < 0)< 0) }, false);
402  
403 < 0)< 0) element.addEventListener("mouseout", function(e) {
404  
405 < 0)< 0) // Don't handle if we aren't supposed to
406 < 0)< 0) if (gesture_in_progress) return;
407  
408 < 0)< 0) // Get parent of the element the mouse pointer is leaving
409 < 0)< 0) if (!e) e = window.event;
410  
411 < 0)< 0) // Check that mouseout is due to actually LEAVING the element
412 < 0)< 0) var target = e.relatedTarget || e.toElement;
413 < 0)< 0) while (target != null) {
414 < 0)< 0) if (target === element)
415 < 0)< 0) return;
416 < 0)< 0) target = target.parentNode;
417 < 0)< 0) }
418  
419 < 0)< 0) cancelEvent(e);
420  
421 < 0)< 0) // Release all buttons
422 < 0)< 0) if (guac_mouse.currentState.left
423 < 0)< 0) || guac_mouse.currentState.middle
424 < 0)< 0) || guac_mouse.currentState.right) {
425  
426 < 0)< 0) guac_mouse.currentState.left = false;
427 < 0)< 0) guac_mouse.currentState.middle = false;
428 < 0)< 0) guac_mouse.currentState.right = false;
429  
430 < 0)< 0) if (guac_mouse.onmouseup)
431 < 0)< 0) guac_mouse.onmouseup(guac_mouse.currentState);
432 < 0)< 0) }
433  
434 < 0)< 0) }, false);
435  
436 < 0)< 0) // Override selection on mouse event element.
437 < 0)< 0) element.addEventListener("selectstart", function(e) {
438 < 0)< 0) cancelEvent(e);
439 < 0)< 0) }, false);
440  
441 < 0)< 0) // Scroll wheel support
442 < 0)< 0) function mousewheel_handler(e) {
443  
444 < 0)< 0) // Don't handle if we aren't supposed to
445 < 0)< 0) if (gesture_in_progress) return;
446  
447 < 0)< 0) var delta = 0;
448 < 0)< 0) if (e.detail)
449 < 0)< 0) delta = e.detail;
450 < 0)< 0) else if (e.wheelDelta)
451 < 0)< 0) delta = -event.wheelDelta;
452  
453 < 0)< 0) // Up
454 < 0)< 0) if (delta < 0) {
455 < 0)< 0)< 0) { if (guac_mouse.onmousedown) {
456 < 0)< 0)< 0) { guac_mouse.currentState.up = true;
457 < 0)< 0)< 0) { guac_mouse.onmousedown(guac_mouse.currentState);
458 < 0)< 0)< 0) { }
459  
460 < 0)< 0)< 0) { if (guac_mouse.onmouseup) {
461 < 0)< 0)< 0) { guac_mouse.currentState.up = false;
462 < 0)< 0)< 0) { guac_mouse.onmouseup(guac_mouse.currentState);
463 < 0)< 0)< 0) { }
464 < 0)< 0)< 0) { }
465  
466 < 0)< 0)< 0) { // Down
467 < 0)< 0)< 0) { if (delta > 0) {
468 < 0)< 0)< 0) { if (guac_mouse.onmousedown) {
469 < 0)< 0)< 0) { guac_mouse.currentState.down = true;
470 < 0)< 0)< 0) { guac_mouse.onmousedown(guac_mouse.currentState);
471 < 0)< 0)< 0) { }
472  
473 < 0)< 0)< 0) { if (guac_mouse.onmouseup) {
474 < 0)< 0)< 0) { guac_mouse.currentState.down = false;
475 < 0)< 0)< 0) { guac_mouse.onmouseup(guac_mouse.currentState);
476 < 0)< 0)< 0) { }
477 < 0)< 0)< 0) { }
478  
479 < 0)< 0)< 0) { cancelEvent(e);
480  
481 < 0)< 0)< 0) { }
482 < 0)< 0)< 0) { element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
483 < 0)< 0)< 0) { element.addEventListener('mousewheel', mousewheel_handler, false);
484  
485 < 0)< 0)< 0) {};
486  
487 < 0)< 0)< 0) {/**
488 < 0)< 0)< 0) { * Simple container for properties describing the state of a mouse.
489 < 0)< 0)< 0) { *
490 < 0)< 0)< 0) { * @constructor
491 < 0)< 0)< 0) { * @param {Number} x The X position of the mouse pointer in pixels.
492 < 0)< 0)< 0) { * @param {Number} y The Y position of the mouse pointer in pixels.
493 < 0)< 0)< 0) { * @param {Boolean} left Whether the left mouse button is pressed.
494 < 0)< 0)< 0) { * @param {Boolean} middle Whether the middle mouse button is pressed.
495 < 0)< 0)< 0) { * @param {Boolean} right Whether the right mouse button is pressed.
496 < 0)< 0)< 0) { * @param {Boolean} up Whether the up mouse button is pressed (the fourth
497 < 0)< 0)< 0) { * button, usually part of a scroll wheel).
498 < 0)< 0)< 0) { * @param {Boolean} down Whether the down mouse button is pressed (the fifth
499 < 0)< 0)< 0) { * button, usually part of a scroll wheel).
500 < 0)< 0)< 0) { */
501 < 0)< 0)< 0) {Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
502  
503 < 0)< 0)< 0) { /**
504 < 0)< 0)< 0) { * The current X position of the mouse pointer.
505 < 0)< 0)< 0) { * @type Number
506 < 0)< 0)< 0) { */
507 < 0)< 0)< 0) { this.x = x;
508  
509 < 0)< 0)< 0) { /**
510 < 0)< 0)< 0) { * The current Y position of the mouse pointer.
511 < 0)< 0)< 0) { * @type Number
512 < 0)< 0)< 0) { */
513 < 0)< 0)< 0) { this.y = y;
514  
515 < 0)< 0)< 0) { /**
516 < 0)< 0)< 0) { * Whether the left mouse button is currently pressed.
517 < 0)< 0)< 0) { * @type Boolean
518 < 0)< 0)< 0) { */
519 < 0)< 0)< 0) { this.left = left;
520  
521 < 0)< 0)< 0) { /**
522 < 0)< 0)< 0) { * Whether the middle mouse button is currently pressed.
523 < 0)< 0)< 0) { * @type Boolean
524 < 0)< 0)< 0) { */
525 < 0)< 0)< 0) { this.middle = middle
526  
527 < 0)< 0)< 0) { /**
528 < 0)< 0)< 0) { * Whether the right mouse button is currently pressed.
529 < 0)< 0)< 0) { * @type Boolean
530 < 0)< 0)< 0) { */
531 < 0)< 0)< 0) { this.right = right;
532  
533 < 0)< 0)< 0) { /**
534 < 0)< 0)< 0) { * Whether the up mouse button is currently pressed. This is the fourth
535 < 0)< 0)< 0) { * mouse button, associated with upward scrolling of the mouse scroll
536 < 0)< 0)< 0) { * wheel.
537 < 0)< 0)< 0) { * @type Boolean
538 < 0)< 0)< 0) { */
539 < 0)< 0)< 0) { this.up = up;
540  
541 < 0)< 0)< 0) { /**
542 < 0)< 0)< 0) { * Whether the down mouse button is currently pressed. This is the fifth
543 < 0)< 0)< 0) { * mouse button, associated with downward scrolling of the mouse scroll
544 < 0)< 0)< 0) { * wheel.
545 < 0)< 0)< 0) { * @type Boolean
546 < 0)< 0)< 0) { */
547 < 0)< 0)< 0) { this.down = down;
548  
549 < 0)< 0)< 0) {};
550