@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 8.81 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../Graphic.js";import i from"../../core/Accessor.js";import{createTask as s}from"../../core/asyncUtils.js";import{makeHandle as o}from"../../core/handleUtils.js";import"../../core/has.js";import{destroyMaybe as n,abortMaybe as a}from"../../core/maybe.js";import{ignoreAbortErrors as r,throwIfAborted as l}from"../../core/promiseUtils.js";import{watch as h,syncAndInitial as c,when as p}from"../../core/reactiveUtils.js";import{property as d,subclass as u}from"../../core/accessorSupport/decorators.js";import _ from"../../layers/GraphicsLayer.js";import m from"../../symbols/CIMSymbol.js";import v from"../../symbols/SimpleLineSymbol.js";import{ViewEventPriorities as y}from"../../views/input/InputManager.js";import{isPolyline as g}from"./support/geometryUtils.js";import f from"../Sketch/SketchViewModel.js";function b(t){return"2d"===t?new m({data:{type:"CIMSymbolReference",symbol:{type:"CIMLineSymbol",symbolLayers:[{type:"CIMSolidStroke",effects:[{type:"CIMGeometricEffectDashes",dashTemplate:[5,4],lineDashEnding:"FullGap",controlPointEnding:"NoConstraint"}],enable:!0,capStyle:"Butt",joinStyle:"Round",width:1.5,color:[0,0,0,255]},{type:"CIMSolidStroke",enable:!0,capStyle:"Butt",joinStyle:"Round",width:1.5,color:[255,255,255,255]}]}}}):new v({color:[0,0,0,0]})}let I=class extends i{constructor(t){super(t),this.state="ready",this._pendingStartOptions=null,this._previousInputInfo=null,this._shouldRemoveLastPoint=!1,this._sketchedGraphics=new WeakSet,this._creationToolPromise=null,this._updateToolPromise=null,this._updateDisabled=!1}initialize(){this.addHandles(h(()=>({view:this.tool.viewModel.view,visible:this.tool.visible}),({view:t,visible:e})=>{null!=t&&e?this._attach(t):this._detach()},c),"view")}destroy(){this._detach()}get canStopCreating(){const t=this._geometry,e=this._shouldRemoveLastPoint?3:2;return g(t)&&t.paths.length>0&&t.paths[0].length>=e}get _input(){return this.tool.viewModel.input}set _input(t){this.tool.viewModel.input=t}get _geometry(){return this._input?.geometry}get _visibleAndEditable(){return this.tool.visible&&this.tool.editable}get _view(){return this.tool.viewModel.view}get test(){}start(t={mode:"sketch"}){if(!this.tool.editable)return;const e=this._view;if(null!=e&&e.ready)switch(this._pendingStartOptions=null,this._stopInteraction(),null==this._previousInputInfo&&this._storePreviousInput(this._input),this._setSketchedGraphic(null),t.mode){case"sketch":this._set("state","creating"),this._startCreationInteraction();break;case"select":this._set("state","selecting"),this._startSelectionInteraction()}else this._pendingStartOptions=t}stop(){this._pendingStartOptions=null,this._stopInteractionAndUpdate(),this._clearPreviousInput()}cancel(){this._pendingStartOptions=null,this._stopInteractionAndUpdate(),this._restorePreviousInput()}clear(){this._stopInteractionAndUpdate(),this._set("state","ready"),this._clearPreviousInput(),this._input=null,this._pendingStartOptions=null}isSketchedGraphic(t){return null!=t&&this._sketchedGraphics.has(t)}_attach(t){this._detach();const e={mode:"3d"===t.type?"relative-to-ground":"on-the-ground",offset:null};this._graphicsLayer=new _({listMode:"hide",internal:!0,elevationInfo:e,title:"Elevation info"});const i=b(t.type);this._sketchVM=new f({layer:this._graphicsLayer,view:t,defaultCreateOptions:{mode:"click",hasZ:!1},updateOnGraphicClick:!1,defaultUpdateOptions:{reshapeOptions:{shapeOperation:"none"},enableRotation:!1,enableScaling:!1,enableMoveAllGraphics:!1,enableZ:!1,multipleSelectionEnabled:!1,toggleToolOnClick:!1,tool:"reshape"},polylineSymbol:i,activeLineSymbol:i}),this.addHandles([p(()=>t.ready,()=>{const t=this._pendingStartOptions;t&&this.start(t)},c),h(()=>[this._input,this._visibleAndEditable],()=>this._update(),c),h(()=>({map:t.map,graphicsLayer:this._graphicsLayer}),({map:t,graphicsLayer:e})=>{null!=t&&null!=e&&t.add(e),this._update()},c)],"main")}_detach(){this.removeHandles("main"),this._sketchVM=n(this._sketchVM),this._creationToolPromise=null,this._updateToolPromise=null;const t=this._view?.map,e=this._graphicsLayer;null!=t&&null!=e&&t.remove(e),this._graphicsLayer=n(this._graphicsLayer),this._shouldRemoveLastPoint=!1,this._set("state","ready")}_startCreationInteraction(){this._stopInteractionAndUpdate();const t=this._view,i=this._sketchVM;if(null==t||null==i)return;this._shouldRemoveLastPoint=!1;const s=i.on("create",t=>{const e=t.graphic;switch(t.state){case"complete":this._shouldRemoveLastPoint=!1,this._setSketchedGraphic(e),this._stopInteractionAndUpdate(),this._clearPreviousInput();break;case"cancel":this.cancel();break;case"active":this._setSketchedGraphic(e),"cursor-update"===t.toolEventInfo?.type&&(this._shouldRemoveLastPoint=!0);break;case"start":this._setSketchedGraphic(e)}}),n=()=>{s.remove();const t=this.canStopCreating,o=this._geometry?.clone();i.cancel(),this._creationToolPromise=null,null!=o&&t?this._shouldRemoveLastPoint&&this._setSketchedGraphic(new e({geometry:S(o)})):this._input=null};this.removeHandles("interaction"),this.addHandles(o(n),"interaction"),this._creationToolPromise=r(i.create("polyline"))}_startReshapeInteraction(){this._stopInteraction();const t=this._view,e=this._sketchVM;if(null==t||null==e)return;const i=e.on("update",t=>{const e=t.graphics[0];switch(t.state){case"complete":this._setSketchedGraphic(e),this._stopInteractionAndUpdate(),this._clearPreviousInput();break;case"active":case"start":this._setSketchedGraphic(e)}}),s=()=>{i.remove(),e.cancel(),this._updateToolPromise=null};this.removeHandles("interaction"),this.addHandles(o(s),"interaction");const n=this._input;n&&(n.visible=!0,this._updateToolPromise=r(e.update(n,{tool:"reshape"})))}_startSelectionInteraction(){this._stopInteraction();const t=this._view;if(null==t)return;const e=t.acquireCursor("crosshair","high");t.closePopup();let i=null;const n=o(()=>a(i)),r=t.on("immediate-click",e=>{e.defer(async()=>{a(i),i=s(async i=>{const{results:s}=await t.hitTest(e);l(i);const o=s.filter(t=>"graphic"===t.type).map(t=>t.graphic).find(t=>null!=t.geometry&&"polyline"===t.geometry.type);o&&(e.preventDefault(),e.stopPropagation(),this._input=o,this._clearPreviousInput(),this._stopInteractionAndUpdate())}),await i.promise})},y.TOOL),h=t.on("key-down",t=>{"Escape"===t.key&&this.cancel()});this.removeHandles("interaction"),this.addHandles([r,h,n,e],"interaction"),t.ready&&t.focus()}_stopInteraction(){this.removeHandles("interaction")}_stopInteractionAndUpdate(){this.hasHandles("interaction")&&(this._updateDisabled=!0,this._stopInteraction(),this._updateDisabled=!1,this._triggerUpdate())}_triggerUpdate(){this._set("state","ready"),this._update()}_update(){if(this._updateDisabled)return;const t=this.state;if("selecting"!==t){if(this._visibleAndEditable){if("creating"===t||"reshaping"===t&&this.isSketchedGraphic(this._input))return}else this.cancel();this._set("state",this._getNextState()),this._updateVisuals()}else this.stop()}_getNextState(){return null==this._input?"ready":this.isSketchedGraphic(this._input)?"creating"===this.state?"creating":this._visibleAndEditable?"reshaping":"reshaping-disabled":"selected"}_updateVisuals(){switch(this.state){case"creating":break;case"reshaping":this._startReshapeInteraction();break;case"reshaping-disabled":{this._stopInteractionAndUpdate();const t=this._input;null!=t&&this.isSketchedGraphic(t)&&(t.visible=!1);break}case"ready":case"selected":this._stopInteractionAndUpdate()}this._updateSketchedGraphic()}_storePreviousInput(t){this._previousInputInfo={graphic:t}}_restorePreviousInput(){const t=this._previousInputInfo;null!=t&&(this._clearPreviousInput(),this._input=t.graphic,this._triggerUpdate())}_clearPreviousInput(){this._previousInputInfo=null}_updateSketchedGraphic(){const t=this._graphicsLayer;if(null==t)return;const e=t.graphics,i=this._input;if(null==i||!this.isSketchedGraphic(i))return void e.removeAll();if(-1===e.indexOf(i))e.removeAll(),e.add(i);else if(1!==e.length){const t=e.filter(t=>t!==i);e.removeMany(t)}}_setSketchedGraphic(t){null!=t&&this._sketchedGraphics.add(t),this._input=t,this._updateSketchedGraphic()}};function S(t){if(g(t)){const e=t.clone();return e.paths=[e.paths[0].slice(0,-1)],e}return t}t([d({nonNullable:!0})],I.prototype,"state",void 0),t([d({nonNullable:!0})],I.prototype,"tool",void 0),t([d()],I.prototype,"canStopCreating",null),t([d()],I.prototype,"_graphicsLayer",void 0),t([d()],I.prototype,"_sketchVM",void 0),t([d()],I.prototype,"_input",null),t([d()],I.prototype,"_geometry",null),t([d()],I.prototype,"_visibleAndEditable",null),t([d()],I.prototype,"_view",null),t([d()],I.prototype,"_shouldRemoveLastPoint",void 0),I=t([u("esri.widgets.ElevationProfile.ElevationProfileInteraction")],I);export{I as ElevationProfileInteraction};