@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 3.75 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import{_ as e}from"../../../chunks/tslib.es6.js";import{createTask as o}from"../../../core/asyncUtils.js";import t from"../../../core/Collection.js";import s from"../../../core/Evented.js";import{makeHandle as r}from"../../../core/handleUtils.js";import{debounce as i,ignoreAbortErrors as n,onAbort as c}from"../../../core/promiseUtils.js";import{createScreenPoint as l}from"../../../core/screenUtils.js";import{property as a}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/has.js";import"../../../core/Logger.js";import"../../../core/RandomLCG.js";import{subclass as p}from"../../../core/accessorSupport/decorators/subclass.js";import m from"../../../geometry/Point.js";import d from"../../../geometry/Polygon.js";import{isClockwise as h}from"../../../geometry/support/coordsUtils.js";import{DrawScreenTool as u}from"../../../views/draw/DrawScreenTool.js";import{sketchKeys as y}from"../../../views/interactive/keybindings.js";import{getSelectionFromGeometry as g}from"./selectorUtils.js";const f=100;let v=class extends s.EventedAccessor{constructor(e){super(e),this._processTask=null,this.options=null,this.selection=new t,this.sources=null,this._process=i((async(e,t)=>{const{callback:s,selector:r,completed:i}=e,n=o((async e=>{const{options:o,sources:t,selection:n,view:c}=this;if(!(!t?.length&&!c?.selectionManager.sources.length)&&c){if(r&&null!=t){const s=await g({currentSelection:n,selector:r,signal:e,sources:t,view:c});i&&!1!==o?.persistSelection&&c.selectionManager?.updateSelection({current:n.toArray(),...s})}s&&s()}else n.removeAll()}));return c(t,(()=>n.abort())),this._processTask=n,n.promise}),f)}initialize(){this._setup()}cancel(){this.removeAllHandles(),this._processTask?.abort()}async _setup(){const{view:e}=this;if(await e.whenReady(),this.destroyed)return;const o=this.options,t=o?.createTool??"rectangle",s=o?.mode??("polygon"===t?"click":"hybrid"),i=Symbol();this._tool=new u({view:e,mode:s,geometryType:t}),o?.selectOnComplete||this.addHandles(this._tool.on(["cursor-update","vertex-add","vertex-remove"],(()=>n(this._process({selector:this._selectionArea})))),i),this.addHandles([e.on("key-down",(e=>{if(!e.repeat)switch(e.key){case y.constraint:this._tool.uniformSizeToggled=!0,e.stopPropagation();break;case y.center:this._tool.centeredToggled=!0,e.stopPropagation()}})),e.on("key-up",(e=>{switch(e.key){case y.constraint:this._tool.uniformSizeToggled=!1,e.stopPropagation();break;case y.center:this._tool.centeredToggled=!1,e.stopPropagation()}})),this._tool.on("complete",(async e=>{this.removeHandles(i);const o=()=>{this.removeAllHandles(),this.emit("complete",{aborted:e.aborted,selection:this.selection.toArray()})};e.aborted?(this.cancel(),this.selection.removeAll(),o()):await this._process({selector:this._selectionArea,callback:o,completed:!0})}))],i),this.addHandles(r((()=>e.tools.remove(this._tool)))),e.addAndActivateTool(this._tool)}get _selectionArea(){const e=l(),o=this._tool.coordinates,t=this.view.spatialReference,s=o=>{e.x=o[0],e.y=o[1];const{x:t,y:s}=this.view.toMap(e);return[t,s]};if(0===o.length)return;if(1===o.length||o.every((([e,t])=>e===o[0][0]&&t===o[0][1]))){const[e,r]=s(o[0]);return new m({x:e,y:r,spatialReference:t})}const r=o.map(s);if(0===r.length)return;h(r)||r.reverse();return new d({spatialReference:t,rings:[r]})}};e([a()],v.prototype,"options",void 0),e([a({readOnly:!0})],v.prototype,"selection",void 0),e([a()],v.prototype,"sources",void 0),e([a({constructOnly:!0})],v.prototype,"view",void 0),v=e([p("esri.widgets.support.Selector2D.SelectionOperation")],v);const w=v;export{w as default};