UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 10.9 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as t}from"tslib";import{getAssetUrl as e}from"../../assets.js";import o from"../../Graphic.js";import{onLocaleChange as i,getLocale as s,fetchMessageBundle as r}from"../../intl.js";import n from"../../core/Collection.js";import a from"../../core/Error.js";import{EventedAccessor as h}from"../../core/Evented.js";import c from"../../core/Logger.js";import{watch as d,initial as l,when as m,whenOnce as v}from"../../core/reactiveUtils.js";import{property as p,subclass as u}from"../../core/accessorSupport/decorators.js";import{ensureType as f}from"../../core/accessorSupport/ensureType.js";import g from"../../geometry/Point.js";import{load as w}from"../../geometry/projectionUtils.js";import _ from"../../symbols/PictureMarkerSymbol.js";import{pointSymbolTypes as y}from"../../symbols/support/typeUtils.js";import C from"./support/Conversion.js";import{isValidPoint as b,isSupportedNotation as L}from"./support/coordinateConversionUtils.js";import M from"./support/Format.js";import{generateDefaultFormats as P,setDefaultPatterns as F}from"./support/formatUtils.js";import{GoTo as S}from"../support/GoTo.js";const j=new g([0,0,500]),H="xy",G=["mgrs","utm","usng","dd","dms","ddm"],x="esri__coordinateConversionWidgetState",E={conversions:"conversions",formats:"formats",view:"view",viewChange:"view-change",cursor:"cursor"},W="esri/images/search/search-symbol-32.png";let U=class extends(S(h)){constructor(t){super(t),this._conversionPromise=null,this._locationGraphic=null,this._pointerCount=0,this.formats=new n,this.formatterAvailable=!1,this.formatterLoading=!0,this.messages=null,this.filteredFormats=new n,this.locationSymbol=new _({url:e(W),width:24,height:24}),this.storageEnabled=!0,this.storageType="session",this.view=null,this._saveWidgetState=this._saveWidgetState.bind(this),this._handleFormatChange=this._handleFormatChange.bind(this),this._handleConversionChange=this._handleConversionChange.bind(this),this._handleViewChange=this._handleViewChange.bind(this),this._onClick=this._onClick.bind(this),this._onPointerMove=this._onPointerMove.bind(this),this._onPointerDown=this._onPointerDown.bind(this),this._onPointerUp=this._onPointerUp.bind(this),this.conversions=new n}initialize(){const t=async()=>this.messages=await r("esri/widgets/CoordinateConversion/t9n/CoordinateConversion");this.formats.addMany(P()),t().then(()=>{this.destroyed||(F(this.messages,this.formats),this.storageEnabled&&this._loadWidgetState(),this.formats.forEach(t=>{t.viewModel=this,this.addHandles(d(()=>t.currentPattern,this._saveWidgetState),t.name??"unnamed-format")}),this.addHandles(this.conversions.on("change",this._handleConversionChange),E.conversions),this.addHandles(this.formats.on("change",this._handleFormatChange),E.formats),this.addHandles(i(()=>{t().then(()=>{F(this.messages,this.formats)})})),w().then(()=>{this.formatterAvailable=!0}).catch(t=>{c.getLogger(this).error(new a("coordinate-conversion:projection-load-failed","Failed to load the projection module.",{error:t})),this.formatterAvailable=!1,this._filterFormatsAndConversions()}).then(()=>this.addHandles(d(()=>this.view,this._handleViewChange,l),E.viewChange)).finally(()=>this.formatterLoading=!1),0===this.conversions.length&&this._addDefaultConversion())})}destroy(){this.removeHandles(),this._cleanUpView(this.view),this.view=null}get conversions(){return this._get("conversions")}set conversions(t){const e=new n;t.forEach(t=>{let o=null;if("string"==typeof t){const e=this.formats.find(e=>e.name===t);e&&(o=new C({format:e}))}else o=f(C,t);o&&e.add(o)}),this._set("conversions",e)}set currentLocation(t){this._set("currentLocation",t),this._updateConversions()}get currentLocation(){return this._get("currentLocation")||null}set mode(t){switch(t){case"capture":this.currentLocation=null,this._startCaptureMode(),this._set("mode",t);break;case"live":this._startLiveMode(),this._set("mode",t)}}get mode(){return this._get("mode")||"live"}get state(){const{messages:t,view:e}=this,o=e?.ready;return t?o?"ready":e?"loading":"disabled":"disabled"}get storage(){return"session"===this.storageType?sessionStorage:localStorage}get waitingForConversions(){return null!=this._conversionPromise}setLocation(t){if(this._locationGraphic&&this.view?.graphics.remove(this._locationGraphic),!t)return;const e=t.clone();e.hasZ&&(e.z=void 0),this._locationGraphic=new o({geometry:e,symbol:this.locationSymbol}),this.view?.graphics.add(this._locationGraphic)}async convert(t,e){if(!b(e))throw new a("coordinate-conversion:invalid-point","Invalid point cannot be converted.",{point:e});return await this._waitForFormatter(),Promise.resolve().then(()=>t.convert(e))}async goToLocation(t){const{view:e}=this;if(!e)throw new a("coordinate-conversion:go-to-failed","no view");const o="3d"===e.type?e.clippingArea:null,i=e.map?.basemap?.baseLayers;if(o||(i?.length??0)>0){const e=o??i?.at(0)?.fullExtent;if(null!=e&&!e.contains(t))throw new a("coordinate-conversion:go-to-failed","Point outside basemap extent.",{point:t})}return this.callGoTo({target:t})}pause(){this.currentLocation=null,this.removeHandles(E.view),this.view&&(this.removeHandles(E.cursor),this._locationGraphic&&this.view.graphics.remove(this._locationGraphic))}async previewConversion(t,e=this.currentLocation||j){return(await A(t,e)).displayCoordinate}resume(){"capture"===this.mode?this._startCaptureMode():this._startLiveMode()}async reverseConvert(t,e){return await this._waitForFormatter(),e.reverseConvert(t)}async updateConversions(t,e){if(await this._waitForFormatter(),"point"!==e?.type)throw this._clearConversions(this.conversions),new a("coordinate-conversion:invalid-input-point","Point is invalid, conversions cannot be updated.",{point:e});return this._convertMany(t,e)}_addDefaultConversion(){const t=this.formats.find(t=>t.name===H)||this.formats.at(0);t&&this.conversions.add(new C({format:t}))}_cleanUpView(t){t&&(this._locationGraphic&&t.graphics.remove(this._locationGraphic),this.removeHandles(E.view),this.removeHandles(E.cursor))}_clearConversions(t){t.forEach(t=>{t.position={location:null,coordinate:null}})}async _convertMany(t,e){return Promise.all(t.map(t=>A(t,e)))}_handleConversionChange(t){for(const e of t.added){const{format:t}=e;t&&(t.viewModel=this,this.currentLocation&&(this._set("waitingForConversions",!0),this.convert(t,this.currentLocation).then(t=>{e.position=t,this._set("waitingForConversions",!1)})))}this._saveWidgetState()}_handleFormatChange(t){t.added.forEach(t=>{this.addHandles(d(()=>t.currentPattern,this._saveWidgetState),t.name??"unnamed-format"),t.viewModel=this}),t.removed.forEach(t=>{t.viewModel=null;const e=this.conversions.filter(e=>e.format===t);this.conversions.removeMany(e),t.name&&this.removeHandles(t.name)}),0===this.conversions.length&&this._addDefaultConversion()}_loadWidgetState(){try{const t=JSON.parse(this.storage.getItem(x));t&&this._setWidgetState(t)}catch(t){c.getLogger(this).error(new a("coordinate-conversion:invalid-session-storage-json","Could not read from storage.",{error:t}))}}_startCaptureMode(){this.removeHandles(E.view),this.view&&(this.currentLocation&&this.setLocation(this.currentLocation),this.removeHandles(E.cursor),this.addHandles(this.view.acquireCursor("crosshair","high"),E.cursor),this.addHandles(this.view.on("click",this._onClick),E.view))}_startLiveMode(){this._pointerCount=0,this.removeHandles(E.view),this.view&&(this.removeHandles(E.cursor),this._locationGraphic&&this.view.graphics.remove(this._locationGraphic),this.addHandles([this.view.on("pointer-down",this._onPointerDown),this.view.on("pointer-up",this._onPointerUp),this.view.on("pointer-move",this._onPointerMove)],E.view))}_handleViewChange(t,e){e&&e!==t&&this._cleanUpView(e),t&&("capture"===this.mode?this._startCaptureMode():this._startLiveMode(),e&&this._filterFormatsAndConversions())}_onClick(t){if(0===t.button){const e=this.view?.toMap(t),o=e?.normalize();this.setLocation(o),this.currentLocation=o}}_onPointerDown(t){const{pointerType:e}=t;if(this._pointerCount++,("touch"===e||"pen"===e)&&1===this._pointerCount){const e=this.view?.toMap(t);this.currentLocation=e?.normalize()}}_onPointerMove(t){const{pointerType:e}=t;if("mouse"===e||1===this._pointerCount){const e=this.view?.toMap(t);this.currentLocation=e?.normalize()}}_onPointerUp(){this._pointerCount--}_setWidgetState(t){try{t.formats.forEach(e=>{const o=this.formats.find(t=>t.name===e.name);o&&t.locale===s()&&e.currentPattern&&(o.currentPattern=e.currentPattern),o&&e.index>=0&&this.conversions.add(new C({format:o}))})}catch(e){c.getLogger(this).warn(new a("coordinate-conversion:session-storage-read-error","Could not get state from stored JSON.",{error:e}))}}_saveWidgetState(){if(!this.storageEnabled)return;const t=this._toJSON();try{this.storage.setItem(x,JSON.stringify({formats:t,locale:s()}))}catch(e){c.getLogger(this).error(new a("coordinate-conversion:local-storage-write-error","Could not write to localStorage.",{error:e}))}}async _updateConversions(){try{await this.updateConversions(this.conversions.toArray(),this.currentLocation)}catch{}}_toJSON(){return this.formats.filter(t=>{const e=t.name;return"xy"===e||"basemap"===e||L(e)}).map(t=>({name:t.name,currentPattern:t.currentPattern,defaultPattern:t.defaultPattern,index:this.conversions.findIndex(e=>e.format===t)})).sort((t,e)=>t.index-e.index).toArray()}_filterFormatsAndConversions(){const{formatterAvailable:t,conversions:e,formats:o}=this;t||this.addHandles(m(()=>this.view?.spatialReference,t=>{const i=t.isWebMercator||t.isWGS84,s=o.filter(t=>{const{name:e}=t;return!!T(e)||"xy"===e&&!i}),r=this.filteredFormats.filter(t=>"xy"===t.name&&i&&!this.formats.includes(t)),n=e.filter(t=>s.includes(t.format));o.removeMany(s),e.removeMany(n),this.filteredFormats.addMany(s.filter(t=>!this.filteredFormats.includes(t))),o.addMany(r)},{once:!0,initial:!0}),E.view)}async _waitForFormatter(){this.formatterLoading&&await v(()=>!1===this.formatterLoading)}};async function A(t,e){try{t.position=await(t.format?.convert(e))}catch(o){t.position=null}return t}function T(t){return G.includes(t)}t([p()],U.prototype,"conversions",null),t([p({type:g})],U.prototype,"currentLocation",null),t([p({type:n.ofType(M)})],U.prototype,"formats",void 0),t([p()],U.prototype,"formatterLoading",void 0),t([p()],U.prototype,"messages",void 0),t([p()],U.prototype,"mode",null),t([p()],U.prototype,"filteredFormats",void 0),t([p({readOnly:!0})],U.prototype,"state",null),t([p({types:y})],U.prototype,"locationSymbol",void 0),t([p({readOnly:!0})],U.prototype,"storage",null),t([p()],U.prototype,"storageEnabled",void 0),t([p()],U.prototype,"storageType",void 0),t([p({readOnly:!0})],U.prototype,"waitingForConversions",null),t([p()],U.prototype,"view",void 0),U=t([u("esri.widgets.CoordinateConversion.CoordinateConversionViewModel")],U);const O=U;export{O as default};