UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 9.29 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */ import{makeHandle as e}from"../../core/handleUtils.js";import"../../core/has.js";import{rad2deg as t}from"../../core/mathUtils.js";import{createScreenPoint as n}from"../../core/screenUtils.js";import{InputHandler as a}from"./InputHandler.js";import{ViewEventPriorities as r}from"./InputManager.js";const i=["click","double-click","immediate-click","immediate-double-click","hold","drag","key-down","key-up","pointer-down","pointer-move","pointer-up","pointer-drag","mouse-wheel","pointer-enter","pointer-leave","gamepad","focus","blur","double-tap-drag","vertical-two-finger-drag"],o={};function s(e){return!!o[e]}function p(e){for(const t of e)if(!s(t))return!1;return!0}i.forEach(e=>{o[e]=!0});class c{constructor(e){this._handlers=new Map,this._counter=0,this._handlerCounts=new Map,this.view=e,this.inputManager=null}connect(e){e&&this.disconnect(),this.inputManager=e,this._handlers.forEach(({handler:e,priority:t},n)=>this.inputManager?.installHandlers(n,[e],t))}disconnect(){this.inputManager&&this._handlers.forEach((e,t)=>this.inputManager?.uninstallHandlers(t)),this.inputManager=null}destroy(){this.disconnect(),this._handlers.clear(),this.view=null}on(t,n,a,i){const o=Array.isArray(t)?t:t.split(",");if(!p(o))return o.some(s)&&console.error("Error: registering input events and other events on the view at the same time is not supported."),null;let c,u;Array.isArray(n)?u=n:(c=n,u=[]),"function"==typeof a?c=a:i=a,i=null!=i?i:r.DEFAULT;const d=this._createUniqueGroupName(),m=new l(this.view,o,u,c);this._handlers.set(d,{handler:m,priority:i});for(const e of o){const t=this._handlerCounts.get(e)||0;this._handlerCounts.set(e,t+1)}return this.inputManager&&this.inputManager.installHandlers(d,[m],i),e(()=>this._removeHandler(d,o))}hasHandler(e){return!!this._handlerCounts.get(e)}_removeHandler(e,t){if(this._handlers.has(e)){this._handlers.delete(e);for(const e of t){const t=this._handlerCounts.get(e);void 0===t||(1===t?this._handlerCounts.delete(e):this._handlerCounts.set(e,t-1))}}this.inputManager&&this.inputManager.uninstallHandlers(e)}_createUniqueGroupName(){return this._counter+=1,`viewEvents_${this._counter}`}}class l extends a{constructor(e,t,n,a){super(!0),this._latestDragStart=void 0,this.view=e;for(const r of t)switch(r){case"click":this.registerIncoming("click",n,t=>a(m(e,t)));break;case"double-click":this.registerIncoming("double-click",n,t=>a(g(e,t)));break;case"immediate-click":this.registerIncoming("immediate-click",n,t=>a(f(e,t)));break;case"immediate-double-click":this.registerIncoming("immediate-double-click",n,t=>a(v(e,t)));break;case"hold":this.registerIncoming("hold",n,t=>a(b(e,t)));break;case"drag":this.registerIncoming("drag",n,e=>{const t=this._wrapDrag(e);t&&a(t)});break;case"key-down":this.registerIncoming("key-down",n,e=>a(y(e)));break;case"key-up":this.registerIncoming("key-up",n,e=>a(k(e)));break;case"pointer-down":this.registerIncoming("pointer-down",n,e=>a(I(e,"pointer-down")));break;case"pointer-move":this.registerIncoming("pointer-move",n,e=>a(I(e,"pointer-move")));break;case"pointer-up":this.registerIncoming("pointer-up",n,e=>a(I(e,"pointer-up")));break;case"pointer-drag":this.registerIncoming("pointer-drag",n,e=>a(D(e)));break;case"mouse-wheel":this.registerIncoming("mouse-wheel",n,e=>a(P(e)));break;case"pointer-enter":this.registerIncoming("pointer-enter",n,e=>a(I(e,"pointer-enter")));break;case"pointer-leave":this.registerIncoming("pointer-leave",n,e=>a(I(e,"pointer-leave")));break;case"gamepad":this.registerIncoming("gamepad",n,e=>{a(_(e))});break;case"focus":this.registerIncoming("focus",n,e=>{a(u(e))});break;case"blur":this.registerIncoming("blur",n,e=>{a(d(e))});break;case"double-tap-drag":this.registerIncoming("double-tap-drag",n,e=>a(x(e)));break;case"vertical-two-finger-drag":this.registerIncoming("vertical-two-finger-drag",n,e=>a(w(e)))}}_wrapDrag(e){const n=e.data,{x:a,y:r}=n.center,{action:i,pointerType:o,button:s}=n;if("start"===i&&(this._latestDragStart=n),!this._latestDragStart)return;const p=n.pointer.native,c=n.buttons,{cancelable:l,timestamp:u}=e,d={x:this._latestDragStart.center.x,y:this._latestDragStart.center.y};return"end"===i&&(this._latestDragStart=void 0),{type:"drag",action:i,x:a,y:r,origin:d,pointerType:o,button:s,buttons:c,radius:n.radius,angle:t(n.angle),native:p,timestamp:u,cancelable:l,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}}function u(e){return{type:"focus",timestamp:e.timestamp,native:e.data.native,cancelable:e.cancelable,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function d(e){return{type:"blur",timestamp:e.timestamp,native:e.data.native,cancelable:e.cancelable,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function m(e,t){const{pointerType:a,button:r,buttons:i,x:o,y:s,native:p,eventId:c}=t.data,{cancelable:l,timestamp:u}=t;return{type:"click",pointerType:a,button:r,buttons:i,x:o,y:s,native:p,timestamp:u,screenPoint:n(o,s),mapPoint:h(e,o,s),eventId:c,cancelable:l,stopPropagation:()=>t.stopPropagation(),defer:e=>t.defer(e),preventDefault:()=>t.preventDefault()}}function g(e,t){const{pointerType:n,button:a,buttons:r,x:i,y:o,native:s,eventId:p}=t.data,{cancelable:c,timestamp:l}=t;return{type:"double-click",pointerType:n,button:a,buttons:r,x:i,y:o,native:s,timestamp:l,mapPoint:h(e,i,o),eventId:p,cancelable:c,stopPropagation:()=>t.stopPropagation(),defer:e=>t.defer(e),preventDefault:()=>t.preventDefault()}}function f(e,t){const{pointerType:n,button:a,buttons:r,x:i,y:o,native:s,eventId:p}=t.data,c=s.pointerId,{cancelable:l,timestamp:u}=t;return{type:"immediate-click",pointerId:c,pointerType:n,button:a,buttons:r,x:i,y:o,native:s,timestamp:u,mapPoint:h(e,i,o),eventId:p,cancelable:l,stopPropagation:()=>t.stopPropagation(),defer:e=>t.defer(e),preventDefault:()=>t.preventDefault()}}function v(e,t){const{pointerType:n,button:a,buttons:r,x:i,y:o,native:s,eventId:p}=t.data,c=s.pointerId,{cancelable:l,timestamp:u}=t;return{type:"immediate-double-click",pointerId:c,pointerType:n,button:a,buttons:r,x:i,y:o,native:s,timestamp:u,mapPoint:h(e,i,o),eventId:p,cancelable:l,stopPropagation:()=>t.stopPropagation(),defer:e=>t.defer(e),preventDefault:()=>t.preventDefault()}}function b(e,t){const{pointerType:n,button:a,buttons:r,x:i,y:o,native:s}=t.data,{cancelable:p,timestamp:c}=t;return{type:"hold",pointerType:n,button:a,buttons:r,x:i,y:o,native:s,timestamp:c,mapPoint:h(e,i,o),cancelable:p,stopPropagation:()=>t.stopPropagation(),defer:e=>t.defer(e),preventDefault:()=>t.preventDefault()}}function h(e,t,a){return e.toMap(n(t,a),{exclude:[]})}function y(e){const{key:t,repeat:n,native:a}=e.data,{cancelable:r,timestamp:i}=e;return{type:"key-down",key:t,repeat:n,native:a,timestamp:i,cancelable:r,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function k(e){const{key:t,native:n}=e.data,{cancelable:a,timestamp:r}=e;return{type:"key-up",key:t,native:n,timestamp:r,cancelable:a,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function I(e,t){const{x:n,y:a,button:r,buttons:i,native:o,eventId:s}=e.data,p=o.pointerId,c=o.pointerType,{cancelable:l,timestamp:u}=e;return{type:t,x:n,y:a,pointerId:p,pointerType:c,button:r,buttons:i,native:o,timestamp:u,eventId:s,cancelable:l,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function D(e){const{x:t,y:n,buttons:a,native:r,eventId:i}=e.data.currentEvent,{button:o}=e.data.startEvent,s=e.data.startEvent.native.pointerId,p=e.data.startEvent.native.pointerType,c=e.data.action,l={x:e.data.startEvent.x,y:e.data.startEvent.y},{cancelable:u,timestamp:d}=e;return{type:"pointer-drag",x:t,y:n,pointerId:s,pointerType:p,button:o,buttons:a,action:c,origin:l,native:r,timestamp:d,eventId:i,cancelable:u,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function P(e){const{cancelable:t,data:n,timestamp:a}=e,{x:r,y:i,deltaY:o,native:s}=n;return{type:"mouse-wheel",x:r,y:i,deltaY:o,native:s,timestamp:a,cancelable:t,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function _(e){const{action:t,state:n,device:a}=e.data,{cancelable:r,timestamp:i}=e,{buttons:o,axes:s}=n;return{type:"gamepad",device:a,timestamp:i,action:t,buttons:o,axes:s,cancelable:r,stopPropagation:()=>e.stopPropagation(),defer:t=>e.defer(t),preventDefault:()=>e.preventDefault()}}function x(e){const{cancelable:t,data:n,timestamp:a}=e,{action:r,button:i,buttons:o,pointer:s,pointerType:p,delta:c}=n,{native:l}=s,{x:u,y:d}=c;return{type:"double-tap-drag",action:r,button:i,buttons:o,cancelable:t,native:l,pointerType:p,timestamp:a,x:u,y:d,defer:t=>e.defer(t),preventDefault:()=>e.preventDefault(),stopPropagation:()=>e.stopPropagation()}}function w(e){const{cancelable:t,data:n,timestamp:a}=e,{action:r,delta:i,pointer:o}=n,{native:s}=o;return{type:"vertical-two-finger-drag",action:r,cancelable:t,native:s,timestamp:a,y:i,defer:t=>e.defer(t),preventDefault:()=>e.preventDefault(),stopPropagation:()=>e.stopPropagation()}}export{c as ViewEvents,i as eventTypes};