UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 5.11 kB
import has from"../../core/has.js";import{destroyMaybe as e}from"../../core/maybe.js";import{eventKey as t}from"./keys.js";import{GamepadSource as n}from"./gamepad/GamepadSource.js";import{createScreenPointFromNativeEvent as i}from"../support/screenUtils.js";const o=has("edge"),a=has("chrome"),s=has("ff"),r=has("safari"),h="esri-view-surface",l={touchNone:`${h}--touch-none`,touchPan:`${h}--touch-pan`};class c{constructor(e,t){this._input=t,this._active={},this._callback=()=>{},this._activePointerCaptures=new Set,this._keyDownState=new Set,this._eventId=1,this._browserTouchPanningEnabled=!1,this._element=e,e.getAttribute("tabindex")||e.setAttribute("tabindex","0"),this._eventHandlers={"key-down":this._handleKey,"key-up":this._handleKey,"pointer-down":this._handlePointer,"pointer-move":this._handlePointerPreventDefault,"pointer-up":this._handlePointerPreventDefault,"pointer-enter":this._handlePointer,"pointer-leave":this._handlePointer,"pointer-cancel":this._handlePointer,"mouse-wheel":this._handleMouseWheel,"pointer-capture-lost":this._handlePointerCaptureLost},this._updateTouchAction(),this._element.addEventListener("keydown",this._preventAltKeyDefault),this._gamepadSource=new n(e,this._input),this._gamepadSource.onEvent=e=>this._callback("gamepad",e)}destroy(){this._callback=()=>{},this.activeEvents=null,this._activePointerCaptures.forEach((e=>{this._releasePointerCaptureSafe(e)})),this._gamepadSource=e(this._gamepadSource),this._activePointerCaptures=null,this._removeTouchAction(),this._element.removeEventListener("keydown",this._preventAltKeyDefault)}get browserTouchPanningEnabled(){return this._browserTouchPanningEnabled}set browserTouchPanningEnabled(e){this._browserTouchPanningEnabled=e,this._updateTouchAction(),this._updateTouchEventHandling()}set onEventReceived(e){this._callback=e}set activeEvents(e){for(const t in this._active)if(!e||!e.has(t)){const e=this._active[t];this._element.removeEventListener(u[t],e),delete this._active[t]}e&&e.forEach((e=>{if(!this._active[e]&&u[e]){const t=(this._eventHandlers[e]||this._handleDefault).bind(this,e);this._element.addEventListener(u[e],t),this._active[e]=t}})),this._gamepadSource.hasEventListeners=e?.has("gamepad")??!1}setPointerCapture(e,t){t?(this._element.setPointerCapture(e.pointerId),this._activePointerCaptures.add(e.pointerId)):(this._releasePointerCaptureSafe(e.pointerId),this._activePointerCaptures.delete(e.pointerId))}_updateTouchAction(){this._element.classList.remove(this._browserTouchPanningEnabled?l.touchNone:l.touchPan),this._element.classList.add(this._browserTouchPanningEnabled?l.touchPan:l.touchNone)}_updateTouchEventHandling(){this._browserTouchPanningEnabled?this._element.addEventListener("touchmove",this._preventMultiTouchPanning):this._element.removeEventListener("touchmove",this._preventMultiTouchPanning)}_removeTouchAction(){this._element.classList.remove(l.touchNone),this._element.classList.remove(l.touchPan),this._element.removeEventListener("touchmove",this._preventMultiTouchPanning)}_releasePointerCaptureSafe(e){try{if(this._element.hasPointerCapture&&!this._element.hasPointerCapture(e))return;this._element.releasePointerCapture(e)}catch(t){}}_updateNormalizedPointerLikeEvent(e,t){const n=i(this._element,e);return c.test.disableSubpixelCoordinates&&(n.x=Math.round(n.x),n.y=Math.round(n.y)),t.x=n.x,t.y=n.y,t}_handleKey(e,n){const i=t(n);i&&"key-up"===e&&this._keyDownState.delete(i);const o={native:n,key:i,repeat:!!i&&this._keyDownState.has(i)};i&&"key-down"===e&&this._keyDownState.add(o.key),this._callback(e,o)}_handlePointer(e,t){const n=this._updateNormalizedPointerLikeEvent(t,{native:t,x:0,y:0,pointerType:t.pointerType,button:t.button,buttons:t.buttons,eventId:this._eventId++});this._callback(e,n)}_handlePointerPreventDefault(e,t){const n=this._updateNormalizedPointerLikeEvent(t,{native:t,x:0,y:0,pointerType:t.pointerType,button:t.button,buttons:t.buttons,eventId:this._eventId++});t.preventDefault(),this._callback(e,n)}_handleMouseWheel(e,t){let n=t.deltaY;switch(t.deltaMode){case 0:o&&(n=n/document.documentElement.clientHeight*600);break;case 1:n*=30;break;case 2:n*=900}o?n*=.7:a||r?n*=.6:s&&(n*=1.375);const i=100,h=Math.abs(n);if(h>i){const e=.02;n=n/h*200/(1+Math.exp(-e*(h-i)))}const l=this._updateNormalizedPointerLikeEvent(t,{native:t,x:0,y:0,deltaY:n});this._callback(e,l)}_handlePointerCaptureLost(e,t){this._activePointerCaptures.delete(t.pointerId),this._handleDefault(e,t)}_handleDefault(e,t){const n={native:t};t.preventDefault(),this._callback(e,n)}_preventAltKeyDefault(e){"Alt"===e.key&&e.preventDefault()}_preventMultiTouchPanning(e){e.touches.length>1&&e.preventDefault()}}c.test={disableSubpixelCoordinates:!1};const u={"key-down":"keydown","key-up":"keyup","pointer-down":"pointerdown","pointer-up":"pointerup","pointer-move":"pointermove","mouse-wheel":"wheel","pointer-capture-got":"gotpointercapture","pointer-capture-lost":"lostpointercapture","context-menu":"contextmenu","pointer-enter":"pointerenter","pointer-leave":"pointerleave","pointer-cancel":"pointercancel",focus:"focus",blur:"blur"};export{c as BrowserEventSource};