JavaScript 65.5%
Python 17.8%
CSS 13%
HTML 3.7%
1"use strict";23Object.defineProperty(exports, "__esModule", {4 value: true5});6exports["default"] = void 0;7var _browser = require("./browser.js");8function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }9function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }10function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }11function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }12function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }13function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*14 * noVNC: HTML5 VNC client15 * Copyright (C) 2019 The noVNC authors16 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)17 */18var useFallback = !_browser.supportsCursorURIs || _browser.isTouchDevice;19var Cursor = exports["default"] = /*#__PURE__*/function () {20 function Cursor() {21 _classCallCheck(this, Cursor);22 this._target = null;23 this._canvas = document.createElement('canvas');24 if (useFallback) {25 this._canvas.style.position = 'fixed';26 this._canvas.style.zIndex = '65535';27 this._canvas.style.pointerEvents = 'none';28 // Safari on iOS can select the cursor image29 // https://bugs.webkit.org/show_bug.cgi?id=24922330 this._canvas.style.userSelect = 'none';31 this._canvas.style.WebkitUserSelect = 'none';32 // Can't use "display" because of Firefox bug #144599733 this._canvas.style.visibility = 'hidden';34 }35 this._position = {36 x: 0,37 y: 038 };39 this._hotSpot = {40 x: 0,41 y: 042 };43 this._eventHandlers = {44 'mouseover': this._handleMouseOver.bind(this),45 'mouseleave': this._handleMouseLeave.bind(this),46 'mousemove': this._handleMouseMove.bind(this),47 'mouseup': this._handleMouseUp.bind(this)48 };49 }50 return _createClass(Cursor, [{51 key: "attach",52 value: function attach(target) {53 if (this._target) {54 this.detach();55 }56 this._target = target;57 if (useFallback) {58 document.body.appendChild(this._canvas);59 var options = {60 capture: true,61 passive: true62 };63 this._target.addEventListener('mouseover', this._eventHandlers.mouseover, options);64 this._target.addEventListener('mouseleave', this._eventHandlers.mouseleave, options);65 this._target.addEventListener('mousemove', this._eventHandlers.mousemove, options);66 this._target.addEventListener('mouseup', this._eventHandlers.mouseup, options);67 }68 this.clear();69 }70 }, {71 key: "detach",72 value: function detach() {73 if (!this._target) {74 return;75 }76 if (useFallback) {77 var options = {78 capture: true,79 passive: true80 };81 this._target.removeEventListener('mouseover', this._eventHandlers.mouseover, options);82 this._target.removeEventListener('mouseleave', this._eventHandlers.mouseleave, options);83 this._target.removeEventListener('mousemove', this._eventHandlers.mousemove, options);84 this._target.removeEventListener('mouseup', this._eventHandlers.mouseup, options);85 if (document.contains(this._canvas)) {86 document.body.removeChild(this._canvas);87 }88 }89 this._target = null;90 }91 }, {92 key: "change",93 value: function change(rgba, hotx, hoty, w, h) {94 if (w === 0 || h === 0) {95 this.clear();96 return;97 }98 this._position.x = this._position.x + this._hotSpot.x - hotx;99 this._position.y = this._position.y + this._hotSpot.y - hoty;100 this._hotSpot.x = hotx;101 this._hotSpot.y = hoty;102 var ctx = this._canvas.getContext('2d');103 this._canvas.width = w;104 this._canvas.height = h;105 var img = new ImageData(new Uint8ClampedArray(rgba), w, h);106 ctx.clearRect(0, 0, w, h);107 ctx.putImageData(img, 0, 0);108 if (useFallback) {109 this._updatePosition();110 } else {111 var url = this._canvas.toDataURL();112 this._target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';113 }114 }115 }, {116 key: "clear",117 value: function clear() {118 this._target.style.cursor = 'none';119 this._canvas.width = 0;120 this._canvas.height = 0;121 this._position.x = this._position.x + this._hotSpot.x;122 this._position.y = this._position.y + this._hotSpot.y;123 this._hotSpot.x = 0;124 this._hotSpot.y = 0;125 }126127 // Mouse events might be emulated, this allows128 // moving the cursor in such cases129 }, {130 key: "move",131 value: function move(clientX, clientY) {132 if (!useFallback) {133 return;134 }135 // clientX/clientY are relative the _visual viewport_,136 // but our position is relative the _layout viewport_,137 // so try to compensate when we can138 if (window.visualViewport) {139 this._position.x = clientX + window.visualViewport.offsetLeft;140 this._position.y = clientY + window.visualViewport.offsetTop;141 } else {142 this._position.x = clientX;143 this._position.y = clientY;144 }145 this._updatePosition();146 var target = document.elementFromPoint(clientX, clientY);147 this._updateVisibility(target);148 }149 }, {150 key: "_handleMouseOver",151 value: function _handleMouseOver(event) {152 // This event could be because we're entering the target, or153 // moving around amongst its sub elements. Let the move handler154 // sort things out.155 this._handleMouseMove(event);156 }157 }, {158 key: "_handleMouseLeave",159 value: function _handleMouseLeave(event) {160 // Check if we should show the cursor on the element we are leaving to161 this._updateVisibility(event.relatedTarget);162 }163 }, {164 key: "_handleMouseMove",165 value: function _handleMouseMove(event) {166 this._updateVisibility(event.target);167 this._position.x = event.clientX - this._hotSpot.x;168 this._position.y = event.clientY - this._hotSpot.y;169 this._updatePosition();170 }171 }, {172 key: "_handleMouseUp",173 value: function _handleMouseUp(event) {174 var _this = this;175 // We might get this event because of a drag operation that176 // moved outside of the target. Check what's under the cursor177 // now and adjust visibility based on that.178 var target = document.elementFromPoint(event.clientX, event.clientY);179 this._updateVisibility(target);180181 // Captures end with a mouseup but we can't know the event order of182 // mouseup vs releaseCapture.183 //184 // In the cases when releaseCapture comes first, the code above is185 // enough.186 //187 // In the cases when the mouseup comes first, we need wait for the188 // browser to flush all events and then check again if the cursor189 // should be visible.190 if (this._captureIsActive()) {191 window.setTimeout(function () {192 // We might have detached at this point193 if (!_this._target) {194 return;195 }196 // Refresh the target from elementFromPoint since queued events197 // might have altered the DOM198 target = document.elementFromPoint(event.clientX, event.clientY);199 _this._updateVisibility(target);200 }, 0);201 }202 }203 }, {204 key: "_showCursor",205 value: function _showCursor() {206 if (this._canvas.style.visibility === 'hidden') {207 this._canvas.style.visibility = '';208 }209 }210 }, {211 key: "_hideCursor",212 value: function _hideCursor() {213 if (this._canvas.style.visibility !== 'hidden') {214 this._canvas.style.visibility = 'hidden';215 }216 }217218 // Should we currently display the cursor?219 // (i.e. are we over the target, or a child of the target without a220 // different cursor set)221 }, {222 key: "_shouldShowCursor",223 value: function _shouldShowCursor(target) {224 if (!target) {225 return false;226 }227 // Easy case228 if (target === this._target) {229 return true;230 }231 // Other part of the DOM?232 if (!this._target.contains(target)) {233 return false;234 }235 // Has the child its own cursor?236 // FIXME: How can we tell that a sub element has an237 // explicit "cursor: none;"?238 if (window.getComputedStyle(target).cursor !== 'none') {239 return false;240 }241 return true;242 }243 }, {244 key: "_updateVisibility",245 value: function _updateVisibility(target) {246 // When the cursor target has capture we want to show the cursor.247 // So, if a capture is active - look at the captured element instead.248 if (this._captureIsActive()) {249 target = document.captureElement;250 }251 if (this._shouldShowCursor(target)) {252 this._showCursor();253 } else {254 this._hideCursor();255 }256 }257 }, {258 key: "_updatePosition",259 value: function _updatePosition() {260 this._canvas.style.left = this._position.x + "px";261 this._canvas.style.top = this._position.y + "px";262 }263 }, {264 key: "_captureIsActive",265 value: function _captureIsActive() {266 return document.captureElement && document.documentElement.contains(document.captureElement);267 }268 }]);269}();