UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 9.49 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../../Graphic.js";import{fetchMessageBundle as i,onLocaleChange as r}from"../../intl.js";import{getContrast as s,multiplyOpacity as n}from"../../core/colorUtils.js";import{handlesGroup as o}from"../../core/handleUtils.js";import{destroyMaybe as a}from"../../core/maybe.js";import{watch as l,initial as p}from"../../core/reactiveUtils.js";import{formatDecimal as m,formatImperialLength as c,formatMetricLength as h}from"../../core/unitFormatUtils.js";import{convertUnit as u}from"../../core/units.js";import{property as d,subclass as y}from"../../core/accessorSupport/decorators.js";import{UpdatingHandles as v}from"../../core/support/UpdatingHandles.js";import{isSupported as g,geodesicLengths as _,geodesicDensify as f}from"../../geometry/geodesicUtils.js";import w from"../../geometry/Point.js";import L from"../../geometry/Polyline.js";import{e as b,l as S}from"../../chunks/geodeticDensifyOperator.js";import{e as j,l as M}from"../../chunks/geodeticLengthOperator.js";import{e as x}from"../../chunks/lengthOperator.js";import C from"../../layers/GraphicsLayer.js";import A from"../../symbols/CIMSymbol.js";import D from"../../symbols/Font.js";import k from"../../symbols/SimpleMarkerSymbol.js";import G from"../../symbols/TextSymbol.js";import{SnappingVisualizer2D as T}from"../../views/2d/interactive/SnappingVisualizer2D.js";import O from"../../views/draw/Draw.js";import{createCoordinateHelper as H}from"../../views/interactive/coordinateHelper.js";import{createManipulatorDragEventPipeline as P,screenToMap as R,dragManipulatedObject as U}from"../../views/interactive/dragEventPipeline.js";import{GraphicManipulator as E}from"../../views/interactive/GraphicManipulator.js";import{InteractiveToolBase as I}from"../../views/interactive/InteractiveToolBase.js";import{EditGeometry as z}from"../../views/interactive/editGeometry/EditGeometry.js";import{EditGeometryOperations as B}from"../../views/interactive/editGeometry/EditGeometryOperations.js";import{SnappingContext as F}from"../../views/interactive/snapping/SnappingContext.js";import{createSnapDragEventPipelineStep as V}from"../../views/interactive/snapping/SnappingDragPipelineStep.js";import{setupSnappingToggleHandles as W}from"../../views/interactive/snapping/snappingUtils.js";const N=1e5;let q=class extends I{constructor(e){super(e),this._measurementLayer=new C({internal:!0,listMode:"hide",visible:!1,title:"DistanceMeasurement2DTool measurement layer"}),this._manipulatorLayer=new C({internal:!0,listMode:"hide",visible:!1,title:"DistanceMeasurement2DTool manipulator layer"}),this._snappingLayer=new C({internal:!0,listMode:"hide",visible:!1,title:"DistanceMeasurement2DTool snapping layer"}),this._updatingHandles=new v,this._vertices=[],this._drawActive=!1,this.measurement=null,this.measurementLabel=null}initialize(){i("esri/core/t9n/Units").then(e=>{this.messages=e});const e=this.view;this._draw=new O({view:e}),e.map.addMany([this._measurementLayer,this._manipulatorLayer,this._snappingLayer]),e.focus(),this.addHandles([r(async()=>{this.messages=await i("esri/core/t9n/Units")}),l(()=>[this.unit,this.messages],()=>{this._updatePolylines()},p),l(()=>[this.view.effectiveTheme.accentColor,this.view.effectiveTheme.textColor],()=>{if(!this._vertices.length)return;const e=this._vertices.map(({coord:e})=>e);for(const{handle:t}of this._vertices)t.remove();this._vertices=[],this._measurementLayer.removeAll(),this._manipulatorLayer.removeAll(),this._snappingLayer.removeAll(),this.manipulators.removeAll(),this._updateSketch(e)})]),W(this)}destroy(){const{map:e}=this.view;this._draw.view=null,this._draw=a(this._draw),e.removeMany([this._measurementLayer,this._manipulatorLayer,this._snappingLayer]),this._measurementLayer.removeAll(),this._manipulatorLayer.removeAll(),this._snappingLayer.removeAll(),this._set("measurement",null),this._set("measurementLabel",null),this._measurementLayer=a(this._measurementLayer),this._manipulatorLayer=a(this._manipulatorLayer),this._snappingLayer=a(this._snappingLayer),this._updatingHandles=a(this._updatingHandles),this._resetVertices()}get cursor(){return this._drawActive?"crosshair":null}set editable(e){this._set("editable",e),e||this._draw.reset()}get snappingOptions(){return this.snappingManager.options}get updating(){return this._updatingHandles.updating||this.snappingManager.updating}onActivate(){this._drawActive||0!==this._vertices.length||this._startSketch()}onShow(){this._measurementLayer.visible=!0,this._manipulatorLayer.visible=!0,this._snappingLayer.visible=!0}onHide(){this._measurementLayer.visible=!1,this._manipulatorLayer.visible=!1,this._snappingLayer.visible=!1}_getSnappingContext(e){return new F({elevationInfo:{mode:"on-the-ground",offset:0},pointer:e,editGeometryOperations:new B(new z("point",H(!1,!1,this.view.spatialReference)),2),visualizer:new T(this._snappingLayer)})}_resetVertices(){for(const{handle:e}of this._vertices)e.remove();this._vertices=[]}_startSketch(){this._drawActive=!0;const{snappingManager:e}=this;this._draw.create("polyline",{mode:"click",snappingManager:e}).on(["draw-complete","vertex-add","vertex-update","vertex-remove","cursor-update","undo","redo"],e=>{this._updateSketch(e.vertices),"draw-complete"===e.type&&this._stopSketch()})}_stopSketch(){this.manipulators.forEach(({manipulator:e})=>{e.interactive=!0}),this._drawActive=!1,this.finishToolCreation()}_updateSketch(e){const{spatialReference:t}=this.view;for(;this._vertices.length>e.length;){const{handle:e,manipulator:t}=this._vertices.pop();e.remove(),this.manipulators.remove(t)}for(let l=this._vertices.length;l<e.length;l++){const[i,r]=e[l],s=new w({x:i,y:r,spatialReference:t}),n=J(s,this.view,this._manipulatorLayer);this.manipulators.add(n);const a=B.fromGeometry(s,2),p=[a.on("vertex-update",e=>{const t=e.vertices.at(0);t&&(this._vertices[l].coord=[t.pos[0],t.pos[1]],this._updatePolylines())}),P(n,(e,t,i,r)=>{const s=this._getSnappingContext(r),{snappingManager:n}=this,o=this._updatingHandles,{snappingStep:l}=V({snappingContext:s,snappingManager:n,updatingHandles:o});t.next(R(this.view)).next(t=>{if("start"===t.action){const i=new w(t.mapStart);e.graphic.geometry=i,a.trySetGeometry(i)}return t}).next(...l).next(U({operations:a})).next(()=>{e.graphic.geometry=a.data.geometry})})];this._vertices.push({manipulator:n,coord:[i,r],handle:o(p)})}const i=this._vertices.length-1,r=this._vertices[i],[s,n]=e[i];r.coord[0]===s&&r.coord[1]===n||(r.coord=[s,n],r.manipulator.graphic.geometry=new w({x:s,y:n,spatialReference:t}));const a=this._drawActive?this._vertices[i].manipulator:null;this.manipulators.forEach(({manipulator:e})=>{e.interactive=null==a||e!==a}),this._updatePolylines()}_updatePolylines(){if(this._vertices.length<2)return void this._measurementLayer.removeAll();const e=K(this._vertices.map(({coord:e})=>e),this.view.spatialReference);if(!e)return;const{measurement:i,drawing:r,original:o}=e;this._set("measurement",i);const a=Y(this.messages,i,this.unit);let l,p;this._set("measurementLabel",a);const{graphics:m}=this._measurementLayer,c=2===m.length,{effectiveTheme:h}=this.view;c?(l=m.at(0),p=m.at(1)):(l=new t({symbol:new A({data:{type:"CIMSymbolReference",symbol:{type:"CIMLineSymbol",symbolLayers:[{type:"CIMSolidStroke",effects:[{type:"CIMGeometricEffectDashes",dashTemplate:[14,12],lineDashEnding:"FullGap",controlPointEnding:"NoConstraint"}],enable:!0,capStyle:"Butt",joinStyle:"Round",width:3.5,color:s(h.accentColor).toArray()},{type:"CIMSolidStroke",enable:!0,capStyle:"Butt",joinStyle:"Round",width:5,color:h.accentColor.toArray()}]}}})}),p=new t({symbol:new G({color:h.textColor,haloColor:n(s(h.textColor,160),.5),haloSize:2,font:new D({size:14,family:"sans-serif"})})}),m.removeAll(),m.addMany([l,p])),l.geometry=r,p.geometry=o.extent?.center,p.symbol.text=a}};function J(e,i,r){const s=n(i.effectiveTheme.accentColor,.5),o={type:"simple-line",width:0},a=8,l=new k({style:"circle",color:s,size:a,outline:o}),p=new k({style:"circle",color:s,size:1.5*a,outline:o}),m=new t({geometry:e,symbol:l});return new E({view:i,layer:r,graphic:m,focusedSymbol:p})}function K(e,t){const i=new L({paths:[e],spatialReference:t});let r,s;return t.isGeographic?g(t)?(r=_([i],"meters")[0],s=f(i,N)):(r=j(i,{unit:"meters"}),s=b(i,N,{unit:"meters"})):t.isWebMercator?(r=j(i,{unit:"meters"}),s=b(i,N,{unit:"meters"})):(r=x(i,{unit:"meters"}),s=i),{measurement:{geometry:s,length:r},original:i,drawing:s}}function Q(e){return null!=e}async function X(e){(e?.isGeographic&&!g(e)||e?.isWebMercator)&&await Promise.all([S(),M()])}function Y(e,t,i){if(!t||!e)return"";switch(i){case"metric":return h(e,t.length,"meters");case"imperial":return c(e,t.length,"meters");default:return m(e,u(t.length,"meters",i),i)}}e([d()],q.prototype,"_drawActive",void 0),e([d({readOnly:!0})],q.prototype,"cursor",null),e([d({value:!0})],q.prototype,"editable",null),e([d({readOnly:!0})],q.prototype,"measurement",void 0),e([d({readOnly:!0})],q.prototype,"measurementLabel",void 0),e([d()],q.prototype,"messages",void 0),e([d({constructOnly:!0})],q.prototype,"snappingManager",void 0),e([d()],q.prototype,"unit",void 0),e([d()],q.prototype,"updating",null),e([d({constructOnly:!0})],q.prototype,"view",void 0),q=e([y("esri.widgets.DistanceMeasurement2D.DistanceMeasurement2DTool")],q);export{q as DistanceMeasurement2DTool,K as createDistanceMeasurementInfo2D,Y as createDistanceMeasurementLabel,Q as isSupported,X as loadGeometryOperators};