UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 13.6 kB
import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../Color.js";import r from"../../Graphic.js";import"../../symbols.js";import{getAccentColor as o}from"../../core/analysisThemeUtils.js";import i from"../../core/Collection.js";import{deprecatedProperty as s,deprecatedFunction as l,deprecated as a}from"../../core/deprecate.js";import n from"../../core/Error.js";import c from"../../core/Evented.js";import{HandleOwnerMixin as h}from"../../core/HandleOwner.js";import u from"../../core/Logger.js";import{isNone as d,unwrapOr as p,unwrap as m,isSome as y,applySome as g}from"../../core/maybe.js";import{isAbortError as v}from"../../core/promiseUtils.js";import{on as f,watch as w,syncAndInitial as _,when as b,initial as S}from"../../core/reactiveUtils.js";import{property as L}from"../../core/accessorSupport/decorators/property.js";import{cast as T}from"../../core/accessorSupport/decorators/cast.js";import"../../core/arrayUtils.js";import{subclass as C}from"../../core/accessorSupport/decorators/subclass.js";import{getLocale as M}from"../../intl/locale.js";import D from"../../layers/GraphicsLayer.js";import j from"../../layers/RouteLayer.js";import A from"../../layers/support/RouteStopSymbols.js";import O from"../../layers/support/RouteSymbols.js";import{fetchServiceDescription as z}from"../../rest/networkService.js";import R from"../../rest/support/RouteParameters.js";import P from"../../rest/support/Stop.js";import x from"../../rest/support/TravelMode.js";import{GoToMixin as U}from"../support/GoTo.js";import I from"../../symbols/SimpleLineSymbol.js";import E from"../../symbols/SimpleMarkerSymbol.js";import H from"../../symbols/PictureMarkerSymbol.js";function N(e){return"esri.Graphic"===e?.declaredClass}function k(e){return Array.isArray(e)&&e.length&&N(e[0])}function G(e){return i.isCollection(e)&&e.length&&N(e.getItemAt(0))}function F(e){return"esri.rest.support.Stop"===e?.declaredClass}var K;!function(e){e[e.Active=0]="Active",e[e.Complete=1]="Complete",e[e.Failed=2]="Failed",e[e.Idle=3]="Idle",e[e.Suspended=4]="Suspended"}(K||(K={}));const $="esri.widgets.Directions.DirectionsViewModel",B=u.getLogger($);let J=class extends(h(U(c.EventedAccessor))){constructor(e){super(e),this._highlightLayer=new D({listMode:"hide",internal:!0}),this._highlight=null,this._legacyLayer=null,this._loadPromise=null,this._loadController=null,this._routeController=null,this._serviceDescriptionStatus=K.Idle,this.apiKey=void 0,this.defaultTravelMode=null,this.departureTime="now",this.lastError=null,this.lastRoute=null,this.layer=null,this.maxStops=50,this.routeParameters=new R({directionsLengthUnits:"kilometers",findBestSequence:!1,returnZ:!0,startTime:null,startTimeIsUTC:!0,useTimeWindows:!1}),this.serviceDescription=null,this.view=null}initialize(){this.addHandles([f((()=>g(this.layer,(e=>e.stops))),"change",(()=>{this.clearResults()})),w((()=>this.layer),((e,t)=>{if(y(e)){for(;e.stops.length<2;)e.stops.add(new P);this._set("defaultTravelMode",null),this.addHandles(b((()=>y(this.serviceDescription)&&m(m(e.routeInfo)?.analysisSettings)?.travelMode),(e=>{this.defaultTravelMode=this._resolveDefaultTravelMode(e)}),{once:!0}))}d(e)&&(this.layer=this.legacyLayer),y(t)&&t===this.legacyLayer&&this.view?.map?.remove(t)}),_),w((()=>this.view?.map),((e,t)=>{this.layer===this.legacyLayer&&(t?.remove(this.layer),e?.add(this.layer)),t?.remove(this._highlightLayer),e?.add(this._highlightLayer)}),S)])}destroy(){this.view?.map?.remove(this._highlightLayer),this.layer===this.legacyLayer&&this.view?.map?.remove(this.layer)}get _directionsLanguage(){if(d(this.serviceDescription))return;const e=this.serviceDescription.directionsSupportedLanguages;if(!e)return;const t=p(this.routeParameters.directionsLanguage,M()).slice(0,2);return e.find((e=>e.toLowerCase().slice(0,2)===t))}get impedanceAttribute(){const e=m(this.routeParameters.travelMode)?.impedanceAttributeName??m(this.routeParameters.impedanceAttribute)??m(this.serviceDescription)?.impedance??null;return this.getCostAttribute(e)}get legacyLayer(){return d(this._legacyLayer)&&(this._legacyLayer=this._getLegacyLayer()),this._legacyLayer}set routeServiceUrl(e){s(B,"routeServiceUrl",{replacement:"layer.url",version:"4.24",warnOnce:!0}),this.legacyLayer.url=e}get routeServiceUrl(){return s(B,"routeServiceUrl",{replacement:"layer.url",version:"4.24",warnOnce:!0}),this.legacyLayer.url}set routeSymbol(e){s(B,"routeSymbol",{replacement:"layer.defaultSymbols.directionLines",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.directionLines=e}get routeSymbol(){return s(B,"routeSymbol",{replacement:"layer.defaultSymbols.directionLines",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.directionLines}get selectedTravelMode(){return d(this.serviceDescription)?null:this.defaultTravelMode??this.serviceDescription.defaultTravelMode??this.serviceDescription.supportedTravelModes?.[0]??null}set selectedTravelMode(e){this._override("selectedTravelMode",e)}get state(){if(y(this._routeController))return"routing";if(y(this.lastError))return"error";switch(this._serviceDescriptionStatus){case K.Suspended:return"unauthenticated";case K.Idle:return"disabled";case K.Active:return"initializing";case K.Failed:return"error";default:return"ready"}}set stops(e){s(B,"stops",{replacement:"layer.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.stops=e}get stops(){return s(B,"stops",{replacement:"layer.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.stops}castStops(e){return k(e)||G(e)?e.map((e=>P.fromGraphic(e))):e}set stopSymbols(e){s(B,"stopSymbols",{replacement:"layer.defaultSymbols.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.stops=e}get stopSymbols(){return s(B,"stopSymbols",{replacement:"layer.defaultSymbols.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.stops}get timeAttribute(){const e=m(this.routeParameters.travelMode)?.timeAttributeName??m(this.routeParameters.directionsTimeAttribute)??m(this.serviceDescription)?.directionsTimeAttribute??null;return this.getCostAttribute(e)}get travelModes(){const e=m(this.serviceDescription)?.supportedTravelModes?.slice()??[];return y(this.defaultTravelMode)&&!e.includes(this.defaultTravelMode)&&e.unshift(this.defaultTravelMode),e}async load(){if(y(this._loadPromise))return this._loadPromise;this._loadPromise=this._load(),await this._loadPromise,this._loadPromise=null}async highlight(e){this.clearHighlights();const t=await this.view.whenLayerView(this.layer);this._highlight=t.highlight(e)}highlightSegment(e){l(B,"highlightSegment",{replacement:"highlight",version:"4.24",warnOnce:!0});const{geometry:o,symbol:i}=e;if(d(this.view)||d(o)||d(i))return;this.clearHighlights();const s=i.clone();s.color=new t([0,0,0,.8]),s.width=Math.ceil(s.width/2);const a=new r({geometry:o,symbol:s});this._highlightLayer.add(a)}clearHighlights(){this._highlightLayer.removeAll(),y(this._highlight)&&(this._highlight.remove(),this._highlight=null)}centerAt(e){if(d(this.view))return;const t=F(e)||N(e)?e.geometry:e;d(t)||this.callGoTo({target:t})}clearResults(){this._set("lastRoute",null),this.layer.removeResult()}async getDirections(){const{apiKey:e,departureTime:t,layer:r,state:o}=this;if(d(r))throw new n("directions-view-model:missing-route-layer","A route layer must be associated with the view model.");if(r===this.legacyLayer&&a(B,"Using the Directions widget or view model without setting `layer` is deprecated.",{see:"https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Directions-DirectionsViewModel.html#layer",version:"4.24",warnOnce:!0}),"unauthenticated"===o||"initializing"===o||"disabled"===o||this._serviceDescriptionStatus===K.Failed)throw new n("directions-view-model:not-loaded","Cannot get directions until view model loads.");this._set("lastError",null),y(this._routeController)&&(this._routeController.abort(),this._routeController=null);const i="now"===t,s="now"===t?new Date:t;if(s&&!i){const e=60*s.getTimezoneOffset()*1e3;s.setTime(s.getTime()-e)}const l=y(this.view)?this.view.spatialReference:null,c=this.routeParameters.clone();c.set({startTime:s,startTimeIsUTC:i,directionsLanguage:this._directionsLanguage,apiKey:e,outSpatialReference:l}),y(this.selectedTravelMode)&&(c.travelMode=this.selectedTravelMode);if(r.stops.filter((({geometry:e})=>y(e))).length<2){const e=new n("directions-view-model:not-enough-stops","Not enough stops for routing");throw this._set("lastError",e),e}this._routeController=new AbortController;const{signal:h}=this._routeController;let u=null;try{u=await r.solve(c,{signal:h})}catch(p){if(!v(p)){const e=new n("directions-view-model:unable-to-route","Unable to route to these addresses",{error:p});throw this._set("lastError",e),this._set("lastRoute",null),e}}finally{this._routeController=null}for(const a of u.stops)d(a.geometry)&&(a.name=null);return r.update(u),this._set("lastRoute",u),this.zoomToRoute(),u}getCostAttribute(e){return(m(this.serviceDescription)?.networkDataset.networkAttributes??[]).find((({name:t,usageType:r})=>t===e&&"cost"===r))??null}reset(){this.clearHighlights(),this.clearResults(),y(this.layer)&&(this.layer.removeAll(),this.layer.stops=new i([new P,new P]))}save(){return this.layer.save()}saveAs(e,t={}){return this.layer.saveAs(e,t)}zoomToRoute(){const{view:e,layer:{routeInfo:t}}=this;if(d(e)||d(t))return;const{geometry:r}=t;if(d(r))return;const{extent:o}=r,i=o.width>o.height,s=o.clone().expand(i?2:1);this.callGoTo({target:s})}_getLegacyLayer(){const e=o();return new j({listMode:"hide",defaultSymbols:new O({directionLines:new I({color:e,width:7,cap:"round",join:"round"}),directionPoints:null,routeInfo:null,stops:new A({break:new E({color:[255,255,255],size:10,outline:{color:[20,89,127],width:2.5}}),first:new E({color:e,size:19,outline:{color:[51,51,51],width:3}}),last:new H({width:23,height:23,url:`data:image/svg+xml;base64,${btoa(`<svg width="30" height="30" viewBox="0 0 30 30" xmlns="http://www.w3.org/2000/svg"><g fill-rule="nonzero" fill="none"><path d="M15.15.3C6.9.3.3 6.9.3 15.15S6.9 29.7 15.15 29.7 29.7 23.1 29.7 15.15C29.7 6.9 23.25.3 15.15.3z" fill="#333"/><path d="M15 4.8C9.3 4.8 4.8 9.45 4.8 15c0 5.55 4.65 10.2 10.2 10.2 5.55 0 10.2-4.5 10.2-10.2 0-5.55-4.5-10.2-10.2-10.2z" fill="${e.toHex()}"/><path fill="#333" d="M10.5 10.5h9v9h-9z"/></g></svg>`)}`}),middle:new E({color:[51,51,51],size:12,outline:{color:e,width:3}}),unlocated:new H({height:18,width:18,url:`data:image/svg+xml;base64,${btoa(`<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M10.1.2C4.6.2.2 4.6.2 10.1s4.4 9.7 9.9 9.7 9.7-4.4 9.7-9.7c0-5.5-4.3-9.9-9.7-9.9z" fill="#333" fill-rule="nonzero"/><path d="M10 2.7c-4.08 0-7.3 3.328-7.3 7.3s3.328 7.3 7.3 7.3 7.3-3.22 7.3-7.3c0-3.972-3.22-7.3-7.3-7.3z" fill="${e.toHex()}" fill-rule="nonzero"/><path d="M11.414 10l5.304-5.303-1.415-1.415L10 8.586 4.697 3.282 3.282 4.697 8.586 10l-5.304 5.303 1.415 1.415L10 11.414l5.303 5.304 1.415-1.415L11.414 10z" fill="#333"/></g></svg>`)}`}),waypoint:new E({color:[255,255,255],size:10,outline:{color:[20,89,127],width:2.5}})})}),stops:[{},{}]})}async _load(){if(y(this.serviceDescription)||d(this.layer))return;y(this._loadController)&&(this._loadController.abort(),this._loadController=null),this._loadController=new AbortController;const{signal:e}=this._loadController;try{this._serviceDescriptionStatus=K.Active;const t=await z(this.layer.url,this.apiKey,{signal:e});this._set("serviceDescription",t),this._serviceDescriptionStatus=K.Complete}catch(t){if(v(t))return void(this._serviceDescriptionStatus=K.Idle);if("identity-manager:user-aborted"===t.name)return void(this._serviceDescriptionStatus=K.Suspended);const e=new n("directions-view-model:service-metadata-unavailable","Cannot load route service metadata",{error:t});throw this._serviceDescriptionStatus=K.Failed,this._set("lastError",e),e}finally{this._loadController=null}}_resolveDefaultTravelMode(e){if(d(this.serviceDescription))return null;const{defaultTravelMode:t,supportedTravelModes:r}=this.serviceDescription,o=/^<(?<name>.*)>$/i.exec(e.name)?.groups?.name;if(o){const i=r?.find((({name:e})=>e.toLocaleLowerCase()===o.trim().toLocaleLowerCase())),s=i??t;return x.fromJSON({...s.toJSON(),...e.toJSON()})}const i=r?.find((({name:t})=>t.toLocaleLowerCase()===e.name.toLocaleLowerCase()));return i??t}};e([L()],J.prototype,"_directionsLanguage",null),e([L()],J.prototype,"_routeController",void 0),e([L()],J.prototype,"_serviceDescriptionStatus",void 0),e([L()],J.prototype,"apiKey",void 0),e([L()],J.prototype,"defaultTravelMode",void 0),e([L()],J.prototype,"departureTime",void 0),e([L({readOnly:!0})],J.prototype,"impedanceAttribute",null),e([L()],J.prototype,"lastError",void 0),e([L({readOnly:!0})],J.prototype,"lastRoute",void 0),e([L()],J.prototype,"layer",void 0),e([L({readOnly:!0})],J.prototype,"legacyLayer",null),e([L({type:Number,range:{min:2,max:50},nonNullable:!0})],J.prototype,"maxStops",void 0),e([L({type:R,nonNullable:!0})],J.prototype,"routeParameters",void 0),e([L()],J.prototype,"routeServiceUrl",null),e([L()],J.prototype,"routeSymbol",null),e([L()],J.prototype,"selectedTravelMode",null),e([L({readOnly:!0})],J.prototype,"serviceDescription",void 0),e([L({readOnly:!0})],J.prototype,"state",null),e([L()],J.prototype,"stops",null),e([T("stops")],J.prototype,"castStops",null),e([L()],J.prototype,"stopSymbols",null),e([L({readOnly:!0})],J.prototype,"timeAttribute",null),e([L()],J.prototype,"travelModes",null),e([L()],J.prototype,"view",void 0),e([L()],J.prototype,"getDirections",null),e([L()],J.prototype,"zoomToRoute",null),J=e([C($)],J);const V=J;export{V as default};