SPB Git forge

spb/admin-ka

Public
41commits 1branches 0releases
172.9 MBsize
maindefault branch
20 days agolast push
JavaScript 65.5% Python 17.8% CSS 13% HTML 3.7%
4.2 KB · 139 lines javascript
Raw Blame History
1/*2 * noVNC: HTML5 VNC client3 * Copyright (C) 2018 The noVNC authors4 * Licensed under MPL 2.0 (see LICENSE.txt)5 *6 * See README.md for usage and integration instructions.7 */89/*10 * Cross-browser event and position routines11 */1213export function getPointerEvent(e) {14    return e.changedTouches ? e.changedTouches[0] : e.touches ? e.touches[0] : e;15}1617export function stopEvent(e) {18    e.stopPropagation();19    e.preventDefault();20}2122// Emulate Element.setCapture() when not supported23let _captureRecursion = false;24let _elementForUnflushedEvents = null;25document.captureElement = null;26function _captureProxy(e) {27    // Recursion protection as we'll see our own event28    if (_captureRecursion) return;2930    // Clone the event as we cannot dispatch an already dispatched event31    const newEv = new e.constructor(e.type, e);3233    _captureRecursion = true;34    if (document.captureElement) {35        document.captureElement.dispatchEvent(newEv);36    } else {37        _elementForUnflushedEvents.dispatchEvent(newEv);38    }39    _captureRecursion = false;4041    // Avoid double events42    e.stopPropagation();4344    // Respect the wishes of the redirected event handlers45    if (newEv.defaultPrevented) {46        e.preventDefault();47    }4849    // Implicitly release the capture on button release50    if (e.type === "mouseup") {51        releaseCapture();52    }53}5455// Follow cursor style of target element56function _capturedElemChanged() {57    const proxyElem = document.getElementById("noVNC_mouse_capture_elem");58    proxyElem.style.cursor = window.getComputedStyle(document.captureElement).cursor;59}6061const _captureObserver = new MutationObserver(_capturedElemChanged);6263export function setCapture(target) {64    if (target.setCapture) {6566        target.setCapture();67        document.captureElement = target;68    } else {69        // Release any existing capture in case this method is70        // called multiple times without coordination71        releaseCapture();7273        let proxyElem = document.getElementById("noVNC_mouse_capture_elem");7475        if (proxyElem === null) {76            proxyElem = document.createElement("div");77            proxyElem.id = "noVNC_mouse_capture_elem";78            proxyElem.style.position = "fixed";79            proxyElem.style.top = "0px";80            proxyElem.style.left = "0px";81            proxyElem.style.width = "100%";82            proxyElem.style.height = "100%";83            proxyElem.style.zIndex = 10000;84            proxyElem.style.display = "none";85            document.body.appendChild(proxyElem);8687            // This is to make sure callers don't get confused by having88            // our blocking element as the target89            proxyElem.addEventListener('contextmenu', _captureProxy);9091            proxyElem.addEventListener('mousemove', _captureProxy);92            proxyElem.addEventListener('mouseup', _captureProxy);93        }9495        document.captureElement = target;9697        // Track cursor and get initial cursor98        _captureObserver.observe(target, {attributes: true});99        _capturedElemChanged();100101        proxyElem.style.display = "";102103        // We listen to events on window in order to keep tracking if it104        // happens to leave the viewport105        window.addEventListener('mousemove', _captureProxy);106        window.addEventListener('mouseup', _captureProxy);107    }108}109110export function releaseCapture() {111    if (document.releaseCapture) {112113        document.releaseCapture();114        document.captureElement = null;115116    } else {117        if (!document.captureElement) {118            return;119        }120121        // There might be events already queued. The event proxy needs122        // access to the captured element for these queued events.123        // E.g. contextmenu (right-click) in Microsoft Edge124        //125        // Before removing the capturedElem pointer we save it to a126        // temporary variable that the unflushed events can use.127        _elementForUnflushedEvents = document.captureElement;128        document.captureElement = null;129130        _captureObserver.disconnect();131132        const proxyElem = document.getElementById("noVNC_mouse_capture_elem");133        proxyElem.style.display = "none";134135        window.removeEventListener('mousemove', _captureProxy);136        window.removeEventListener('mouseup', _captureProxy);137    }138}139