Use addEventListener() for mouse, fix handling of mouseout.

This commit is contained in:
Michael Jumper
2012-02-22 15:14:27 -08:00
parent 5c92d65234
commit 3f2a79fd0f

View File

@@ -97,6 +97,12 @@ Guacamole.Mouse = function(element) {
*/ */
this.onmousemove = null; this.onmousemove = null;
function cancelEvent(e) {
e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
}
function moveMouse(pageX, pageY) { function moveMouse(pageX, pageY) {
guac_mouse.currentState.x = pageX - element.offsetLeft; guac_mouse.currentState.x = pageX - element.offsetLeft;
@@ -119,20 +125,20 @@ Guacamole.Mouse = function(element) {
// Block context menu so right-click gets sent properly // Block context menu so right-click gets sent properly
element.oncontextmenu = function(e) { element.addEventListener("contextmenu", function(e) {
return false; cancelEvent(e);
}; }, false);
element.onmousemove = function(e) { element.addEventListener("mousemove", function(e) {
// Don't handle if we aren't supposed to // Don't handle if we aren't supposed to
if (gesture_in_progress) return; if (gesture_in_progress) return;
e.stopPropagation(); cancelEvent(e);
moveMouse(e.pageX, e.pageY); moveMouse(e.pageX, e.pageY);
}; }, false);
var last_touch_x = 0; var last_touch_x = 0;
var last_touch_y = 0; var last_touch_y = 0;
@@ -142,13 +148,12 @@ Guacamole.Mouse = function(element) {
var gesture_in_progress = false; var gesture_in_progress = false;
var click_release_timeout = null; var click_release_timeout = null;
element.ontouchend = function(e) { element.addEventListener("touchend", function(e) {
// If we're handling a gesture // If we're handling a gesture
if (gesture_in_progress) { if (gesture_in_progress) {
e.stopPropagation(); cancelEvent(e);
e.preventDefault();
var time = new Date().getTime(); var time = new Date().getTime();
@@ -194,16 +199,15 @@ Guacamole.Mouse = function(element) {
} }
}; }, false);
element.ontouchstart = function(e) { element.addEventListener("touchstart", function(e) {
// Record initial touch location and time for single-touch movement // Record initial touch location and time for single-touch movement
// and tap gestures // and tap gestures
if (e.touches.length == 1) { if (e.touches.length == 1) {
e.stopPropagation(); cancelEvent(e);
e.preventDefault();
// Stop mouse events while touching // Stop mouse events while touching
gesture_in_progress = true; gesture_in_progress = true;
@@ -225,15 +229,14 @@ Guacamole.Mouse = function(element) {
} }
}; }, false);
element.ontouchmove = function(e) { element.addEventListener("touchmove", function(e) {
// Handle single-touch movement gesture (touchpad mouse move) // Handle single-touch movement gesture (touchpad mouse move)
if (e.touches.length == 1) { if (e.touches.length == 1) {
e.stopPropagation(); cancelEvent(e);
e.preventDefault();
// Get change in touch location // Get change in touch location
var touch = e.touches[0]; var touch = e.touches[0];
@@ -269,15 +272,15 @@ Guacamole.Mouse = function(element) {
} }
}; }, false);
element.onmousedown = function(e) { element.addEventListener("mousedown", function(e) {
// Don't handle if we aren't supposed to // Don't handle if we aren't supposed to
if (gesture_in_progress) return; if (gesture_in_progress) return;
e.stopPropagation(); cancelEvent(e);
switch (e.button) { switch (e.button) {
case 0: case 0:
@@ -294,15 +297,15 @@ Guacamole.Mouse = function(element) {
if (guac_mouse.onmousedown) if (guac_mouse.onmousedown)
guac_mouse.onmousedown(guac_mouse.currentState); guac_mouse.onmousedown(guac_mouse.currentState);
}; }, false);
element.onmouseup = function(e) { element.addEventListener("mouseup", function(e) {
// Don't handle if we aren't supposed to // Don't handle if we aren't supposed to
if (gesture_in_progress) return; if (gesture_in_progress) return;
e.stopPropagation(); cancelEvent(e);
switch (e.button) { switch (e.button) {
case 0: case 0:
@@ -319,14 +322,25 @@ Guacamole.Mouse = function(element) {
if (guac_mouse.onmouseup) if (guac_mouse.onmouseup)
guac_mouse.onmouseup(guac_mouse.currentState); guac_mouse.onmouseup(guac_mouse.currentState);
}; }, false);
element.onmouseout = function(e) { element.addEventListener("mouseout", function(e) {
// Don't handle if we aren't supposed to // Don't handle if we aren't supposed to
if (gesture_in_progress) return; if (gesture_in_progress) return;
e.stopPropagation(); // Get parent of the element the mouse pointer is leaving
if (!e) e = window.event;
// Check that mouseout is due to actually LEAVING the element
var target = e.relatedTarget || e.toElement;
while (target != null) {
if (target === element)
return;
target = target.parentNode;
}
cancelEvent(e);
// Release all buttons // Release all buttons
if (guac_mouse.currentState.left if (guac_mouse.currentState.left
@@ -341,15 +355,15 @@ Guacamole.Mouse = function(element) {
guac_mouse.onmouseup(guac_mouse.currentState); guac_mouse.onmouseup(guac_mouse.currentState);
} }
}; }, true);
// Override selection on mouse event element. // Override selection on mouse event element.
element.onselectstart = function() { element.addEventListener("selectstart", function(e) {
return false; cancelEvent(e);
}; }, false);
// Scroll wheel support // Scroll wheel support
element.onmousewheel = function(e) { element.addEventListener('DOMMouseScroll', function(e) {
// Don't handle if we aren't supposed to // Don't handle if we aren't supposed to
if (gesture_in_progress) return; if (gesture_in_progress) return;
@@ -386,14 +400,9 @@ Guacamole.Mouse = function(element) {
} }
} }
if (e.preventDefault) cancelEvent(e);
e.preventDefault();
e.returnValue = false; }, false);
};
element.addEventListener('DOMMouseScroll', element.onmousewheel, false);
}; };