2018-08-14 12:11:49 +00:00
|
|
|
/*
|
|
|
|
* noVNC: HTML5 VNC client
|
2019-06-24 11:26:25 +00:00
|
|
|
* Copyright (C) 2018 The noVNC Authors
|
2018-08-14 12:11:49 +00:00
|
|
|
* Licensed under MPL 2.0 (see LICENSE.txt)
|
|
|
|
*
|
|
|
|
* See README.md for usage and integration instructions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cross-browser event and position routines
|
|
|
|
*/
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function getPointerEvent(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
return e.changedTouches ? e.changedTouches[0] : e.touches ? e.touches[0] : e;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function stopEvent(e) {
|
2018-08-14 12:11:49 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Emulate Element.setCapture() when not supported
|
2019-06-24 11:26:25 +00:00
|
|
|
let _captureRecursion = false;
|
2020-08-11 12:05:09 +00:00
|
|
|
let _elementForUnflushedEvents = null;
|
|
|
|
document.captureElement = null;
|
2018-08-14 12:11:49 +00:00
|
|
|
function _captureProxy(e) {
|
|
|
|
// Recursion protection as we'll see our own event
|
|
|
|
if (_captureRecursion) return;
|
|
|
|
|
|
|
|
// Clone the event as we cannot dispatch an already dispatched event
|
2019-06-24 11:26:25 +00:00
|
|
|
const newEv = new e.constructor(e.type, e);
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
_captureRecursion = true;
|
2020-08-11 12:05:09 +00:00
|
|
|
if (document.captureElement) {
|
|
|
|
document.captureElement.dispatchEvent(newEv);
|
|
|
|
} else {
|
|
|
|
_elementForUnflushedEvents.dispatchEvent(newEv);
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
_captureRecursion = false;
|
|
|
|
|
|
|
|
// Avoid double events
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Respect the wishes of the redirected event handlers
|
|
|
|
if (newEv.defaultPrevented) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implicitly release the capture on button release
|
|
|
|
if (e.type === "mouseup") {
|
|
|
|
releaseCapture();
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Follow cursor style of target element
|
2020-08-11 12:05:09 +00:00
|
|
|
function _capturedElemChanged() {
|
|
|
|
const proxyElem = document.getElementById("noVNC_mouse_capture_elem");
|
|
|
|
proxyElem.style.cursor = window.getComputedStyle(document.captureElement).cursor;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
const _captureObserver = new MutationObserver(_capturedElemChanged);
|
2019-06-24 11:26:25 +00:00
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
export function setCapture(target) {
|
|
|
|
if (target.setCapture) {
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
target.setCapture();
|
|
|
|
document.captureElement = target;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// IE releases capture on 'click' events which might not trigger
|
2020-08-11 12:05:09 +00:00
|
|
|
target.addEventListener('mouseup', releaseCapture);
|
2018-08-27 12:30:50 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
} else {
|
|
|
|
// Release any existing capture in case this method is
|
|
|
|
// called multiple times without coordination
|
|
|
|
releaseCapture();
|
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
let proxyElem = document.getElementById("noVNC_mouse_capture_elem");
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
if (proxyElem === null) {
|
|
|
|
proxyElem = document.createElement("div");
|
|
|
|
proxyElem.id = "noVNC_mouse_capture_elem";
|
|
|
|
proxyElem.style.position = "fixed";
|
|
|
|
proxyElem.style.top = "0px";
|
|
|
|
proxyElem.style.left = "0px";
|
|
|
|
proxyElem.style.width = "100%";
|
|
|
|
proxyElem.style.height = "100%";
|
|
|
|
proxyElem.style.zIndex = 10000;
|
|
|
|
proxyElem.style.display = "none";
|
|
|
|
document.body.appendChild(proxyElem);
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// This is to make sure callers don't get confused by having
|
|
|
|
// our blocking element as the target
|
2020-08-11 12:05:09 +00:00
|
|
|
proxyElem.addEventListener('contextmenu', _captureProxy);
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
proxyElem.addEventListener('mousemove', _captureProxy);
|
|
|
|
proxyElem.addEventListener('mouseup', _captureProxy);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
document.captureElement = target;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Track cursor and get initial cursor
|
2020-08-11 12:05:09 +00:00
|
|
|
_captureObserver.observe(target, {attributes: true});
|
|
|
|
_capturedElemChanged();
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
proxyElem.style.display = "";
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// We listen to events on window in order to keep tracking if it
|
|
|
|
// happens to leave the viewport
|
|
|
|
window.addEventListener('mousemove', _captureProxy);
|
|
|
|
window.addEventListener('mouseup', _captureProxy);
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function releaseCapture() {
|
2018-08-14 12:11:49 +00:00
|
|
|
if (document.releaseCapture) {
|
|
|
|
|
|
|
|
document.releaseCapture();
|
2020-08-11 12:05:09 +00:00
|
|
|
document.captureElement = null;
|
2018-08-27 12:30:50 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
} else {
|
2020-08-11 12:05:09 +00:00
|
|
|
if (!document.captureElement) {
|
2018-08-14 12:11:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
// There might be events already queued. The event proxy needs
|
|
|
|
// access to the captured element for these queued events.
|
|
|
|
// E.g. contextmenu (right-click) in Microsoft Edge
|
|
|
|
//
|
|
|
|
// Before removing the capturedElem pointer we save it to a
|
|
|
|
// temporary variable that the unflushed events can use.
|
|
|
|
_elementForUnflushedEvents = document.captureElement;
|
|
|
|
document.captureElement = null;
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
_captureObserver.disconnect();
|
|
|
|
|
2020-08-11 12:05:09 +00:00
|
|
|
const proxyElem = document.getElementById("noVNC_mouse_capture_elem");
|
|
|
|
proxyElem.style.display = "none";
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
window.removeEventListener('mousemove', _captureProxy);
|
|
|
|
window.removeEventListener('mouseup', _captureProxy);
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|