@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 5.5 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../../../../Graphic.js";import r from"../../../../../core/Accessor.js";import i from"../../../../../core/Collection.js";import o from"../../../../../core/Error.js";import{watch as s}from"../../../../../core/reactiveUtils.js";import{property as a}from"../../../../../core/accessorSupport/decorators/property.js";import{subclass as c}from"../../../../../core/accessorSupport/decorators/subclass.js";import{SymbolDictionary as h}from"./SymbolDictionary.js";import{intersectionDistance2D as p}from"../../../../interactive/support/utils.js";const n="__drawTool_zIndex",l="__drawTool_seq",m={outputGeometry:0,outline:10,constructionLine:20,guide:30,vertex:40,controlPoint:90,cursor:100},d={idle:0,focused:5,active:60};let y=class extends r{constructor(e){super(e),this.symbolOverrides={},this._active=!1,this._nextSequence=0,this._viewGraphics=new i,this._graphicPropsStore=new Map,this._visualPropsStore=new Map}initialize(){this._symbolDictionary=new h({accentColor:this.view.effectiveTheme.accentColor,initialOverrides:this.symbolOverrides}),this.addHandles(s(()=>this.view.effectiveTheme.accentColor,e=>{this._symbolDictionary.updateAccentColor(e),this._refreshSymbols()}))}destroy(){this.deactivate(),this._viewGraphics.destroyAll(),this._symbolDictionary.destroy()}activate(){this._active||(this.view.graphics.addMany(this._viewGraphics),this._active=!0)}deactivate(){this.view.graphics.removeMany(this._viewGraphics),this._active=!1}overrideSymbol(e){const{symbol:t,geometryType:r,role:i,state:o}=e;this._symbolDictionary.addOverride(t,r,i,o),this._refreshSymbols()}removeGraphic(e){const t=this._graphicPropsStore.get(e);t&&(this._removeViewGraphic(t.graphic),this._graphicPropsStore.delete(e))}removeVisual(e){const t=this._visualPropsStore.get(e);t&&(this._removeViewGraphic(t.graphic),t.graphic.destroy(),this._visualPropsStore.delete(e))}addOrUpdateGraphic(e,t){this._graphicPropsStore.has(e)?this._updateGraphicProps(e,t):this._createGraphic(e,t)}addOrUpdateVisual(e,t){this._visualPropsStore.has(e)?this._updateVisual(e,t):this._createVisual(e,t)}hitTestVisualDistance(e,t){const r=this._visualPropsStore.get(t);if(!r?.graphic?.geometry)return null;const{result:i,cache:o}=p(this.view,e,r.graphic.geometry,r.graphic.symbol,r.intersectionCache)??{};return r.intersectionCache=o,i}_createGraphic(e,r){const i=this._nextSequence++,o=w(r),s=new t({attributes:{[n]:o.zIndex,[l]:i},geometry:o.geometry,symbol:o.symbol});this._graphicPropsStore.set(e,{graphic:s,props:o,sequence:i}),o.geometry&&this._addViewGraphic(s,o.zIndex,i)}_createVisual(e,r){const i=this._nextSequence++,o=b(r),s=g(o.role,o.state),a=new t({attributes:{[n]:s,[l]:i},geometry:o.geometry});this._visualPropsStore.set(e,{graphic:a,props:o,sequence:i,zIndex:s}),o.geometry&&(a.symbol=this._symbolDictionary.get(o.geometry.type,o.role,o.state),this._addViewGraphic(a,s,i))}_addViewGraphic(e,t,r){const i=u(this._viewGraphics,t,r);if(this._viewGraphics.add(e,i),!this._active)return;const o=this._viewGraphics.getItemAt(i+1),s=o?this.view.graphics.indexOf(o):void 0;this.view.graphics.add(e,s)}_removeViewGraphic(e){this.view.graphics.remove(e),this._viewGraphics.remove(e)}_updateGraphicProps(e,t){const r=this._graphicPropsStore.get(e);if(!r)throw new o("draw:graphic-not-found","Graphic with the given id does not exist.");const i=r.props,s=w(t,i);r.props=s,r.graphic.symbol!==s.symbol&&(r.graphic.symbol=s.symbol),r.graphic.geometry!==s.geometry&&(r.graphic.geometry=s.geometry),i.zIndex!==s.zIndex&&r.graphic.setAttribute(n,s.zIndex),null==i.geometry||null!=s.geometry?null!=i.geometry||null==s.geometry?i.zIndex!==s.zIndex&&(this._removeViewGraphic(r.graphic),this._addViewGraphic(r.graphic,s.zIndex,r.sequence)):this._addViewGraphic(r.graphic,s.zIndex,r.sequence):this._removeViewGraphic(r.graphic)}_refreshSymbols(){for(const{graphic:e,props:t}of this._visualPropsStore.values()){const{geometry:r,role:i,state:o}=t;e.symbol=this._symbolDictionary.get(r?.type,i,o)}}_updateVisual(e,t){const r=this._visualPropsStore.get(e);if(!r)throw new o("draw:visual-not-found","Visual with the given id does not exist.");const i=r.props,s=r.zIndex,a=b(t,i);r.props=a;let c=!1;i.geometry!==a.geometry&&(r.graphic.geometry=a.geometry,c=i.geometry?.type!==a.geometry?.type),null==i.geometry||null!=a.geometry?(i.role===a.role&&i.state===a.state||(r.zIndex=g(a.role,a.state),r.graphic.setAttribute(n,r.zIndex),c=!0),c&&a.geometry&&(r.graphic.symbol=this._symbolDictionary.get(a.geometry.type,a.role,a.state)),null!=i.geometry||null==a.geometry?s!==r.zIndex&&(this._removeViewGraphic(r.graphic),this._addViewGraphic(r.graphic,r.zIndex,r.sequence)):this._addViewGraphic(r.graphic,r.zIndex,r.sequence)):this._removeViewGraphic(r.graphic)}get test(){return{active:this._active,symbolDictionary:this._symbolDictionary}}};function g(e,t){return m[e]+d[t]}function u(e,t,r){let i=0,o=e.length;for(;i<o;){const s=i+o>>>1,a=e.getItemAt(s),c=_(a),h=v(a);c<t||c===t&&h<r?i=s+1:o=s}return i}function _(e){return e.getAttribute(n)??0}function v(e){return e.getAttribute(l)??0}function w(e,t={geometry:null,symbol:null,zIndex:0}){return{...t,...e}}function b(e,t={geometry:null,role:"outputGeometry",state:"idle"}){return{...t,...e}}e([a()],y.prototype,"symbolOverrides",void 0),e([a()],y.prototype,"view",void 0),y=e([c("esri.views.2d.interactive.editingTools.draw.DrawToolRenderer2D")],y);export{y as DrawToolRenderer2D,g as toZIndex};