UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 6.51 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import{createTask as t}from"../../../core/asyncUtils.js";import o from"../../../core/Collection.js";import{EventedAccessor as r}from"../../../core/Evented.js";import{makeHandle as s}from"../../../core/handleUtils.js";import i from"../../../core/Logger.js";import{abortMaybe as n}from"../../../core/maybe.js";import{debounce as l,onAbort as a,ignoreAbortErrors as c}from"../../../core/promiseUtils.js";import{createScreenPoint as p}from"../../../core/screenUtils.js";import{property as d,subclass as h}from"../../../core/accessorSupport/decorators.js";import m from"../../../geometry/Point.js";import y from"../../../geometry/Polygon.js";import{isClockwise as u}from"../../../geometry/support/coordsUtils.js";import{sketchKeys as v,selectionKeys as f}from"../../../views/interactive/keybindings.js";import{loadAutomaticLengthMeasurementUtils as g}from"../../../views/support/automaticLengthMeasurementUtils.js";import{getSelectionFromGeometry as _}from"./selectorUtils.js";const w=100;let T=class extends r{constructor(e){super(e),this._completed=!1,this._isConstraintKeyDown=!1,this._processTask=null,this.createTool="rectangle",this.mode=null,this.persistSelection=!0,this.returnFeatureTitleFields=!1,this.returnGeometry=!0,this.selection=new o,this.selectionManager=null,this.selectOnComplete=!0,this.sources=null,this.toolName="",this.type="add",this._process=l(async(e,o)=>{const{callback:r,selector:s,completed:i}=e,n=t(async e=>{const{effectiveSelectionManager:t,returnFeatureTitleFields:o,returnGeometry:n,selection:l,sources:a,view:c}=this;if(!(!a?.length&&!t.sources.length)&&c){if(s&&null!=a){const r=await _({returnFeatureTitleFields:o,returnGeometry:n,selector:s,signal:e,sources:a,view:c});if(l.removeAll(),l.addMany(r),i&&this.persistSelection)switch(this.effectiveType){case"remove":t.updateSelection({current:[],added:[],removed:r});break;case"replace":t.clear(),t.updateSelection({current:r,added:[],removed:[]});break;default:t.updateSelection({current:r,added:[],removed:[]})}}r&&r()}else l.removeAll()});return a(o,()=>n.abort()),this._processTask=n,n.promise},w),this._onOperationFinish=this._onOperationFinish.bind(this)}initialize(){this._setup()}get _defaultMode(){return"polygon"===this.createTool?"click":"hybrid"}get _selectionArea(){const e=this._tool.coordinates;if(0===e.length)return;const t=p(),o=this.view.spatialReference,r=e=>{t.x=e[0],t.y=e[1];const o=this.view.toMap(t);return[o?.x??0,o?.y??0]};if(1===e.length||e.every(([t,o])=>t===e[0][0]&&o===e[0][1])){const[t,s]=r(e[0]);return new m({x:t,y:s,spatialReference:o})}const s=e.map(r);if(0===s.length)return;u(s)||s.reverse();return new y({spatialReference:o,rings:[s]})}get completed(){return this._completed}get effectiveSelectionManager(){return this.selectionManager??this.view.selectionManager}get effectiveType(){const e=this.type??"add";if(this._isConstraintKeyDown)switch(e){case"add":return"remove";case"remove":case"replace":return"add";default:return e}return e}get processingFinalSelection(){return!(!this._processTask||!this._tool?.drawOperation?.isCompleted)}cancel(){this.selection.removeAll(),this._onOperationFinish(!0)}destroy(){this._toolImportController=n(this._toolImportController)}async _setup(){const{createTool:e,view:t}=this;if(await t.whenReady(),this.destroyed||this._toolImportController)return;const o=new AbortController;this._toolImportController=o;const[r,n]=await Promise.all([import("../../../views/draw/DrawScreenTool.js"),g()]);if(o.signal.aborted)return;const l=Symbol();try{this._tool=new r.DrawScreenTool({view:t,mode:this.mode??this._defaultMode,geometryType:e,cursor:"point"===e?"default":"crosshair",automaticLengthMeasurementUtils:n})}finally{this._toolImportController=null}this._tool?(this.selectOnComplete||this.addHandles(this._tool.on(["cursor-update","vertex-add","vertex-remove"],()=>{this._syncConstraintKeyDown(),c(this._process({selector:this._selectionArea}))}),l),this._syncConstraintKeyDown(),this.addHandles([t.on("key-down",e=>{if(!e.repeat)switch(this._syncConstraintKeyDown(),e.key){case v.constraint:this._tool.uniformSizeToggled=!0,e.stopPropagation();break;case v.center:this._tool.centeredToggled=!0,e.stopPropagation()}}),t.on("key-up",e=>{switch(this._syncConstraintKeyDown(),e.key){case v.constraint:this._tool.uniformSizeToggled=!1,e.stopPropagation();break;case v.center:this._tool.centeredToggled=!1,e.stopPropagation()}}),this.selection.on("change",({added:e,removed:t})=>this.emit("selection-change",{operationType:this.effectiveType,added:e,removed:t})),this._tool.on("complete",async e=>{this._syncConstraintKeyDown(),this.removeHandles(l),e.aborted?this.cancel():(this._tool.cursor="progress",await this._process({selector:this._selectionArea,callback:this._onOperationFinish,completed:!0}))})],l),this.addHandles(s(()=>t.tools.remove(this._tool))),t.addAndActivateTool(this._tool)):i.getLogger(this).warn("Unable to import Draw module. SelectionOperation failed to load.")}_syncConstraintKeyDown(){const{inputManager:e}=this.view;this._isConstraintKeyDown=f.invertType.some(t=>e.isModifierKeyDown(t))}_onOperationFinish(e=!1){this.removeAllHandles(),this._processTask?.abort(),this._completed=!0,this.emit("complete",{aborted:e,operationType:this.effectiveType,selection:this.selection.toArray(),toolName:this.toolName})}};e([d()],T.prototype,"_completed",void 0),e([d()],T.prototype,"_defaultMode",null),e([d()],T.prototype,"_isConstraintKeyDown",void 0),e([d()],T.prototype,"_processTask",void 0),e([d()],T.prototype,"_selectionArea",null),e([d()],T.prototype,"completed",null),e([d({constructOnly:!0})],T.prototype,"createTool",void 0),e([d()],T.prototype,"effectiveSelectionManager",null),e([d()],T.prototype,"effectiveType",null),e([d({constructOnly:!0})],T.prototype,"mode",void 0),e([d()],T.prototype,"persistSelection",void 0),e([d()],T.prototype,"processingFinalSelection",null),e([d()],T.prototype,"returnFeatureTitleFields",void 0),e([d()],T.prototype,"returnGeometry",void 0),e([d({readOnly:!0})],T.prototype,"selection",void 0),e([d({constructOnly:!0})],T.prototype,"selectionManager",void 0),e([d({constructOnly:!0})],T.prototype,"selectOnComplete",void 0),e([d()],T.prototype,"sources",void 0),e([d({constructOnly:!0})],T.prototype,"toolName",void 0),e([d()],T.prototype,"type",void 0),e([d({constructOnly:!0})],T.prototype,"view",void 0),T=e([h("esri.widgets.support.Selector2D.SelectionOperation")],T);const C=T;export{C as default};