GUACAMOLE-1204: Provide convenience function for cancelling DOM events.

This commit is contained in:
Michael Jumper
2021-05-24 19:26:35 -07:00
parent 6644955fff
commit 568355140d
2 changed files with 21 additions and 13 deletions

View File

@@ -125,6 +125,20 @@ Guacamole.Event.DOMEvent = function DOMEvent(type, events) {
}; };
/**
* Convenience function for cancelling all further processing of a given DOM
* event. Invoking this function prevents the default behavior of the event and
* stops any further propagation.
*
* @param {Event} event
* The DOM event to cancel.
*/
Guacamole.Event.DOMEvent.cancelEvent = function cancelEvent(event) {
event.stopPropagation();
if (event.preventDefault) event.preventDefault();
event.returnValue = false;
};
/** /**
* An object which can dispatch {@link Guacamole.Event} objects. Listeners * An object which can dispatch {@link Guacamole.Event} objects. Listeners
* registered with {@link Guacamole.Event.Target#on on()} will automatically * registered with {@link Guacamole.Event.Target#on on()} will automatically

View File

@@ -118,20 +118,14 @@ Guacamole.Mouse = function(element) {
*/ */
var scroll_delta = 0; var scroll_delta = 0;
function cancelEvent(e) {
e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
}
// Block context menu so right-click gets sent properly // Block context menu so right-click gets sent properly
element.addEventListener("contextmenu", function(e) { element.addEventListener("contextmenu", function(e) {
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
}, false); }, false);
element.addEventListener("mousemove", function(e) { element.addEventListener("mousemove", function(e) {
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
// If ignoring events, decrement counter // If ignoring events, decrement counter
if (ignore_mouse) { if (ignore_mouse) {
@@ -148,7 +142,7 @@ Guacamole.Mouse = function(element) {
element.addEventListener("mousedown", function(e) { element.addEventListener("mousedown", function(e) {
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
// Do not handle if ignoring events // Do not handle if ignoring events
if (ignore_mouse) if (ignore_mouse)
@@ -173,7 +167,7 @@ Guacamole.Mouse = function(element) {
element.addEventListener("mouseup", function(e) { element.addEventListener("mouseup", function(e) {
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
// Do not handle if ignoring events // Do not handle if ignoring events
if (ignore_mouse) if (ignore_mouse)
@@ -209,7 +203,7 @@ Guacamole.Mouse = function(element) {
target = target.parentNode; target = target.parentNode;
} }
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
// Release all buttons // Release all buttons
if (guac_mouse.currentState.left if (guac_mouse.currentState.left
@@ -232,7 +226,7 @@ Guacamole.Mouse = function(element) {
// Override selection on mouse event element. // Override selection on mouse event element.
element.addEventListener("selectstart", function(e) { element.addEventListener("selectstart", function(e) {
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
}, false); }, false);
// Ignore all pending mouse events when touch events are the apparent source // Ignore all pending mouse events when touch events are the apparent source
@@ -319,7 +313,7 @@ Guacamole.Mouse = function(element) {
} }
cancelEvent(e); Guacamole.Event.DOMEvent.cancelEvent(e);
} }