UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 6.42 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../../../core/Accessor.js";import{isSome as i}from"../../../core/arrayUtils.js";import{abortHandle as s}from"../../../core/handleUtils.js";import{debounce as a}from"../../../core/promiseUtils.js";import r from"../../../core/ReactiveMap.js";import{whenOnce as o,watch as l}from"../../../core/reactiveUtils.js";import{property as n,subclass as p}from"../../../core/accessorSupport/decorators.js";import{UpdatingHandles as d}from"../../../core/support/UpdatingHandles.js";import c from"../../../layers/GraphicsLayer.js";import{isSceneLayer as h}from"../../../layers/support/layerUtils.js";import u from"../../../views/interactive/sketch/SketchOptions.js";import{getVisualVariablesForLayer as y,updateGraphicSymbolWhenRequired as m,setVisualVariablesAndElevationInfoForUpdate as v,swapForEditingSession as g,visualVariableInteractiveUpdate as f,getSizeVariableAttribute as _,getRotationVariableAttribute as b}from"../workflowUtils.js";import w from"../../Sketch/SketchViewModel.js";const k=Symbol();let U=class extends t{constructor(e){super(e),this.features=[],this.onGeometriesUpdated=()=>{},this.onVisualVariableAttributesUpdated=()=>{},this.sketchOptions=new u,this.snappingManager=null,this.sourceLayer=null,this.view=null,this.viewModel=null,this.useLegacyCreateTools=!1,this._nextUpdateSubset=[],this._sketchGraphicsMap=new r,this._sketchLayer=null,this._updatingHandles=new d,this._visualVariableAttributes=new Map,this._visualVariablesForLayer=null,this._webStyleCache=new Map}async initialize(){this._visualVariablesForLayer=y(this.sourceLayer),this._nextUpdateSubset=this.features,this._initializeSketchGraphics(),this._initializeSketchLayer(),await this._updatingHandles.addPromise(this._initializeSketchViewModel())}destroy(){const{_sketchLayer:e,view:t}=this;t?.destroyed||t?.map.remove(e),this._updatingHandles.destroy(),e.destroy(),this.viewModel.destroy()}get defaultUpdateOptions(){const e=this.sourceLayer;return{multipleSelectionEnabled:!1,reshapeOptions:{enableCreateCurveFromStraightEdge:!h(e)&&e.capabilities?.data.supportsTrueCurve}}}set defaultUpdateOptions(e){this._set("defaultUpdateOptions",e)}get sketchGraphics(){return Array.from(this._sketchGraphicsMap.values())}get updating(){return this._updatingHandles.updating}async enter(){await this.startUpdatingFeatures(this._nextUpdateSubset)}async exit(){this._completeCurrentOperation()}notifyAttributesChanged({features:e,fieldName:t}){const i=this._webStyleCache,s=this.view.scale,a=this._updatingHandles;for(const r of e){const e=this._sketchGraphicsMap.get(r),o=r.getAttribute(t);if(!e||e.getAttribute(t)===o)continue;e.setAttribute(t,o);const l=this._visualVariableAttributes.get(r);null==l?.size||l.size.isUpdatingInteractively||l.size.field!==t||l.size.setInitialValue(o),null==l?.rotation||l.rotation.isUpdatingInteractively||l.rotation.field!==t||l.rotation.setInitialValue(o),a.addPromise(m(e,i,s))}}async startUpdatingFeatures(e){const{onGeometriesUpdated:t,onVisualVariableAttributesUpdated:a,_sketchGraphicsMap:r,_sketchLayer:l,sourceLayer:n,view:p,viewModel:d,_webStyleCache:c}=this,h=e.map(e=>r.get(e)).filter(i),u={...this.defaultUpdateOptions};this._completeCurrentOperation();let y=null;if(1===e.length){const t=e[0],i=this._visualVariableAttributes.get(t);!(!i?.rotation&&!i?.size)&&(await v({graphic:h[0],sketchLayer:l,sketchViewModel:d,sourceLayer:n,visualVariables:i,webStyleCache:c}),"point"===n.geometryType&&(u.enableRotation=null!=i.rotation,u.enableScaling=null!=i.size,y=async e=>{const s=e.graphics[0];f(p,s,e,i)&&(a?.([{feature:t,attributes:s.attributes}],e),this._updatingHandles.addPromise(m(s,c,"2d"===p.type?p.scale:null)))}))}else u.tool="move";const g=()=>d.update(h,u),_=d.on("update",a=>{if("complete"===a.state){if(null===p.activeTool)return void g();const e=new AbortController,t=s(e);return this.addHandles(t,k),void this._updatingHandles.addPromise(o(()=>null===p.activeTool,e.signal).then(async()=>{e.signal.aborted||(e.abort(),await g())}))}const l=e.map(e=>{const t=r.get(e);return t?{feature:e,geometry:t.geometry}:null}).filter(i);t(l,a),y?.(a)});await g(),this._nextUpdateSubset=this.features,this.addHandles(_,k),d.addHandles(_)}_completeCurrentOperation(){this.removeHandles(k),this.viewModel.complete()}_initializeSketchGraphics(){const{features:e,_sketchGraphicsMap:t,sourceLayer:i,_visualVariableAttributes:s}=this,a=this._visualVariablesForLayer,r=a.rotation?.field,o=a.size?.field,l={rotation:r?i.getField(r):null,size:o?i.getField(o):null},n=!(!l.rotation&&!l.size);for(const p of e)t.set(p,p.clone()),n&&s.set(p,L(a,l))}_initializeSketchLayer(){const{view:e}=this,t=new c({elevationInfo:this.sourceLayer.elevationInfo,internal:!0,listMode:"hide",title:"SketchController layer"});this._sketchLayer=t,e.map.add(t),this._updatingHandles.addPromise(e.whenLayerView(t))}async _initializeSketchViewModel(){const{defaultUpdateOptions:e,sketchOptions:t,snappingManager:i,useLegacyCreateTools:s,view:r,_sketchGraphicsMap:o,_sketchLayer:n,_webStyleCache:p}=this,d=Array.from(o.entries()),c=new w({allowDeleteKey:!1,defaultUpdateOptions:e,layer:n,sketchOptions:t,snappingManager:i,useLegacyCreateTools:s,updateOnGraphicClick:!1,view:r});this.viewModel=c;const h=a(e=>Promise.all(d.map(([,t])=>m(t,p,e))));await h("2d"===r.type?r.scale:null),"2d"===r.type&&this.addHandles(l(()=>r.scale,e=>h(e))),this.addHandles(await Promise.all(d.map(([e,t])=>g(n,r,e,t))))}};function L({rotation:e,size:t},i){return{rotation:e?b(e,i.rotation):null,size:t?_(t,i.size):null}}e([n()],U.prototype,"defaultUpdateOptions",null),e([n()],U.prototype,"features",void 0),e([n()],U.prototype,"onGeometriesUpdated",void 0),e([n()],U.prototype,"onVisualVariableAttributesUpdated",void 0),e([n()],U.prototype,"sketchGraphics",null),e([n()],U.prototype,"sketchOptions",void 0),e([n()],U.prototype,"snappingManager",void 0),e([n()],U.prototype,"sourceLayer",void 0),e([n()],U.prototype,"updating",null),e([n()],U.prototype,"view",void 0),e([n()],U.prototype,"viewModel",void 0),e([n()],U.prototype,"useLegacyCreateTools",void 0),e([n()],U.prototype,"_nextUpdateSubset",void 0),e([n()],U.prototype,"_sketchGraphicsMap",void 0),e([n()],U.prototype,"_sketchLayer",void 0),e([n()],U.prototype,"_visualVariablesForLayer",void 0),U=e([p("esri.widgets.Editor.support.SketchController")],U);export{U as SketchController};