]>
git.proxmox.com Git - mirror_novnc.git/blob - core/util/events.js
8fdf6aa6f69e4097f3a14c29a1c6714e9422024b
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * See README.md for usage and integration instructions.
10 * Cross-browser event and position routines
13 import * as Log
from './logging.js';
15 export function getPointerEvent (e
) {
16 return e
.changedTouches
? e
.changedTouches
[0] : e
.touches
? e
.touches
[0] : e
;
19 export function stopEvent (e
) {
24 // Emulate Element.setCapture() when not supported
25 var _captureRecursion
= false;
26 var _captureElem
= null;
27 const _captureProxy = function (e
) {
28 // Recursion protection as we'll see our own event
29 if (_captureRecursion
) return;
31 // Clone the event as we cannot dispatch an already dispatched event
32 var newEv
= new e
.constructor(e
.type
, e
);
34 _captureRecursion
= true;
35 _captureElem
.dispatchEvent(newEv
);
36 _captureRecursion
= false;
38 // Avoid double events
41 // Respect the wishes of the redirected event handlers
42 if (newEv
.defaultPrevented
) {
46 // Implicitly release the capture on button release
47 if ((e
.type
=== "mouseup") || (e
.type
=== "touchend")) {
52 // Follow cursor style of target element
53 const _captureElemChanged = function() {
54 var captureElem
= document
.getElementById("noVNC_mouse_capture_elem");
55 captureElem
.style
.cursor
= window
.getComputedStyle(_captureElem
).cursor
;
57 const _captureObserver
= new MutationObserver(_captureElemChanged
);
59 var _captureIndex
= 0;
61 export function setCapture (elem
) {
62 if (elem
.setCapture
) {
66 // IE releases capture on 'click' events which might not trigger
67 elem
.addEventListener('mouseup', releaseCapture
);
68 elem
.addEventListener('touchend', releaseCapture
);
71 // Release any existing capture in case this method is
72 // called multiple times without coordination
75 // Safari on iOS 9 has a broken constructor for TouchEvent.
76 // We are fine in this case however, since Safari seems to
77 // have some sort of implicit setCapture magic anyway.
78 if (window
.TouchEvent
!== undefined) {
80 new TouchEvent("touchstart");
86 var captureElem
= document
.getElementById("noVNC_mouse_capture_elem");
88 if (captureElem
=== null) {
89 captureElem
= document
.createElement("div");
90 captureElem
.id
= "noVNC_mouse_capture_elem";
91 captureElem
.style
.position
= "fixed";
92 captureElem
.style
.top
= "0px";
93 captureElem
.style
.left
= "0px";
94 captureElem
.style
.width
= "100%";
95 captureElem
.style
.height
= "100%";
96 captureElem
.style
.zIndex
= 10000;
97 captureElem
.style
.display
= "none";
98 document
.body
.appendChild(captureElem
);
100 // This is to make sure callers don't get confused by having
101 // our blocking element as the target
102 captureElem
.addEventListener('contextmenu', _captureProxy
);
104 captureElem
.addEventListener('mousemove', _captureProxy
);
105 captureElem
.addEventListener('mouseup', _captureProxy
);
107 captureElem
.addEventListener('touchmove', _captureProxy
);
108 captureElem
.addEventListener('touchend', _captureProxy
);
114 // Track cursor and get initial cursor
115 _captureObserver
.observe(elem
, {attributes
:true});
116 _captureElemChanged();
118 captureElem
.style
.display
= null;
120 // We listen to events on window in order to keep tracking if it
121 // happens to leave the viewport
122 window
.addEventListener('mousemove', _captureProxy
);
123 window
.addEventListener('mouseup', _captureProxy
);
125 window
.addEventListener('touchmove', _captureProxy
);
126 window
.addEventListener('touchend', _captureProxy
);
130 export function releaseCapture () {
131 if (document
.releaseCapture
) {
133 document
.releaseCapture();
140 // There might be events already queued, so we need to wait for
141 // them to flush. E.g. contextmenu in Microsoft Edge
142 window
.setTimeout(function(expected
) {
143 // Only clear it if it's the expected grab (i.e. no one
144 // else has initiated a new grab)
145 if (_captureIndex
=== expected
) {
148 }, 0, _captureIndex
);
150 _captureObserver
.disconnect();
152 var captureElem
= document
.getElementById("noVNC_mouse_capture_elem");
153 captureElem
.style
.display
= "none";
155 window
.removeEventListener('mousemove', _captureProxy
);
156 window
.removeEventListener('mouseup', _captureProxy
);
158 window
.removeEventListener('touchmove', _captureProxy
);
159 window
.removeEventListener('touchend', _captureProxy
);