@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 12.4 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../core/Collection.js";import r from"../../core/Error.js";import{EventedAccessor as i}from"../../core/Evented.js";import o from"../../core/Logger.js";import{clamp as s}from"../../core/mathUtils.js";import{debounce as a,throwIfNotAbortError as l,isAbortError as n}from"../../core/promiseUtils.js";import{watch as u,syncAndInitial as d,when as c,on as h}from"../../core/reactiveUtils.js";import{property as p,subclass as m}from"../../core/accessorSupport/decorators.js";import{resolveTimeZone as v,formatTimestamp as y,convertDateFormatToIntlOptions as g}from"../../intl/date.js";import{getLocale as f,getLocaleParts as w}from"../../intl/locale.js";import{fetchServiceDescription as _}from"../../rest/networkService.js";import T from"../../rest/support/RouteParameters.js";import S from"../../rest/support/Stop.js";import D from"../../rest/support/TravelMode.js";import{defaultDatePattern as b,defaultTimePattern as C}from"../../support/dateUtils.js";import{system as M}from"../../time/constants.js";import{shortTimeZoneName as I}from"../../time/timeZoneUtils.js";import{DepartureTimeOption as j}from"./support/directionsUtils.js";import{getDefaultLocaleOptions as A}from"../support/dateUtils.js";import{GoTo as P}from"../support/GoTo.js";import{DateTime as L,FixedOffsetZone as O,IANAZone as R}from"luxon";function E(e){return e.map(e=>({locale:e,parts:w(e)})).filter(({parts:e})=>!!e)}function U(e,t){return e.find(({parts:e})=>V(e.language,t.language)&&e.region===t.region)?.locale??e.find(({parts:e})=>V(e.language,t.language))?.locale}function N(e){return"esri.Graphic"===e?.declaredClass}function k(e){return"no"===e||"nb"===e}function V(e,t){return e===t||k(e)&&k(t)}function z(e){return"esri.rest.support.Stop"===e?.declaredClass}let F=class extends(P(i)){constructor(e){super(e),this._debouncedSolveHandler=a(async e=>{switch(e.type){case"create":switch(e.state){case"start":case"active":return}break;case"delete":break;case"update":switch(e.state){case"start":return void(this._networkFeatureUpdated=!1);case"active":if(this._networkFeatureUpdated=!0,this.autoSolveOn.includes("active"))break;return;case"complete":if(!this._networkFeatureUpdated)return;if(this.autoSolveOn.includes("complete"))break;return}}if((this.layer?.stops.filter(({geometry:e})=>!!e).length??0)<2||!this.autoSolve)return;const t="update"===e.type&&"active"===e.state;try{await this.getDirections({zoomToSolvedRoute:!1,ignoreSolvedStopGeometry:t,generalizeGeometry:t})}catch(r){o.getLogger(this).error(r)}}),this._highlight=null,this._loadController=null,this._loadPromise=null,this._networkFeatureUpdated=!1,this._layerView=null,this._routeController=null,this._serviceDescriptionStatus=3,this.apiKey=void 0,this.autoSolve=!0,this.autoSolveOn=["complete"],this.defaultTravelMode=null,this.departureIsoDate=void 0,this.departureIsoTime=void 0,this.departureOption=j.NOW,this.departureTime="now",this.lastError=null,this.lastRoute=null,this.layer=null,this.maxStops=50,this.routeParameters=new T({directionsLengthUnits:"kilometers",findBestSequence:!1,preserveFirstStop:!0,preserveLastStop:!0,returnZ:!0,useTimeWindows:!1}),this.serviceDescription=null,this.view=null}initialize(){this.addHandles([u(()=>this.layer,e=>{if(e){for(;e.stops.length<2;)e.stops.add(new S);this._set("defaultTravelMode",null),this.addHandles(c(()=>this.serviceDescription?e.routeInfo?.analysisSettings?.travelMode:null,e=>{this.defaultTravelMode=this._resolveDefaultTravelMode(e)},{once:!0}))}},d),u(()=>[this.layer,this.view],async()=>{this._layerView=await this._getRouteLayerView()},d),h(()=>this._layerView,["create","delete","update"],e=>{this._debouncedSolveHandler(e).catch(l)})]);const e=v(this.view?.timeZone??M),t=L.fromMillis(Date.now());this.departureIsoDate=t.toFormat(b,A(e)),this.departureIsoTime=t.toFormat(C,A(e))}get _directionsLanguage(){const e=this.serviceDescription?.directionsSupportedLanguages;if(!e)return;const t=this.routeParameters.directionsLanguage??f(),r=w(t),i=r?.language,o=r?.region;if(!i)return;return U(E(e),{language:i,region:o})}get formattedEta(){const e=this.layer?.routeInfo?.endTime,t=this.layer?.routeInfo?.endTimeOffset;if(this.departureOption===j.UNSPECIFIED||!e||!t)return null;const r=L.fromJSDate(e,{zone:O.instance(t)}),i=L.fromJSDate(e,{zone:R.create(v(this.view?.timeZone??M))}),o=L.fromJSDate(new Date,{zone:R.create(v(this.view?.timeZone??M))}),s=o.year===i.year&&o.month===i.month&&o.day===i.day,a=i.offset===t?void 0:I,l=s?void 0:g("short-date"),n=g("short-time");return y(r.toISO(),{...l,...n,timeZoneName:a})}get impedanceAttribute(){const e=this.routeParameters.travelMode?.impedanceAttributeName??this.routeParameters.impedanceAttribute??this.serviceDescription?.impedance??null;return this.getCostAttribute(e)}get selectedNetworkFeatures(){return this._layerView?.selectedNetworkFeatures??null}get selectedTravelMode(){return this.serviceDescription?this.defaultTravelMode??this.serviceDescription.defaultTravelMode??this.serviceDescription.supportedTravelModes?.[0]??null:null}set selectedTravelMode(e){this._override("selectedTravelMode",e)}get state(){if(this._routeController)return"routing";if(this.lastError)return"error";switch(this._serviceDescriptionStatus){case 4:return"unauthenticated";case 3:return"disabled";case 0:return"initializing";case 2:return"error";default:return"ready"}}get timeAttribute(){const e=this.routeParameters.travelMode?.timeAttributeName??this.routeParameters.directionsTimeAttribute??this.serviceDescription?.directionsTimeAttribute??null;return this.getCostAttribute(e)}get travelModes(){const e=this.serviceDescription?.supportedTravelModes?.slice()??[];return null==this.defaultTravelMode||e.includes(this.defaultTravelMode)||e.unshift(this.defaultTravelMode),e}async load(){if(this._loadPromise)return this._loadPromise;this._loadPromise=this._load(),await this._loadPromise,this._loadPromise=null}async highlight(e){this.clearHighlights();const t=await this._getRouteLayerView();this._highlight=t?.highlight(e)??null}clearHighlights(){this._highlight?.remove(),this._highlight=null}centerAt(e){if(!this.view)return;const t=z(e)||N(e)?e.geometry:e;t&&this.callGoTo({target:t})}clearResults(){this._set("lastRoute",null),this.layer?.removeResult()}async create(e){await(this._layerView?.create(e))}async getDirections(e={zoomToSolvedRoute:!0,ignoreSolvedStopGeometry:!1,generalizeGeometry:!1}){const{apiKey:t,layer:i,state:o}=this;if(!i)throw new r("directions-view-model:missing-route-layer","A route layer must be associated with the view model.");if("disabled"===o||"initializing"===o||"unauthenticated"===o||2===this._serviceDescriptionStatus)throw new r("directions-view-model:not-loaded","Cannot get directions until view model loads.");this._routeController?.abort(),this._routeController=null;const{startTime:s,startTimeIsUTC:a}=this._getStartTimeParameters(),l=this.view?.spatialReference??null,u=this.routeParameters.clone(),d=e.generalizeGeometry?this._getGeometryPrecision():null;u.set({apiKey:t,directionsLanguage:this._directionsLanguage,geometryPrecision:d,outSpatialReference:l,startTime:s,startTimeIsUTC:a}),this.selectedTravelMode&&(u.travelMode=this.selectedTravelMode);if(i.stops.filter(({geometry:e})=>!!e).length<2){const e=new r("directions-view-model:not-enough-stops","Not enough stops for routing");throw this._set("lastError",e),e}i.stops.forEach(e=>e.sequence=null),this._routeController=new AbortController;const{signal:c}=this._routeController;let h=null;try{h=await i.solve(u,{signal:c})}catch(p){if(!n(p)){const e=new r("directions-view-model:unable-to-route","Unable to route to these addresses",{error:p});throw this._set("lastError",e),this.clearResults(),e}}finally{this._routeController=null}this._set("lastError",null);for(const r of h.stops)r.geometry||(r.name=null);if(e.ignoreSolvedStopGeometry&&i.stops.every(({objectId:e})=>!!e)&&h.stops.every(({objectId:e})=>!!e))for(const r of h.stops){const e=i.stops.find(({objectId:e})=>e===r.objectId)?.geometry;e&&(r.geometry=e.clone())}return i.update(h),this._set("lastRoute",h),e.zoomToSolvedRoute&&this.zoomToRoute(),h}getCostAttribute(e){return(this.serviceDescription?.networkDataset?.networkAttributes??[]).find(({name:t,usageType:r})=>t===e&&"cost"===r)??null}remove(e){this._layerView?.remove(e)}reset(){this.clearHighlights(),this.clearResults(),null!=this.layer&&(this.layer.removeAll(),this.layer.stops=new t([new S,new S]))}save(){if(!this.layer)throw new r("directions-view-model:missing-layer","save() requires a layer");return this.layer.save()}saveAs(e,t={}){if(!this.layer)throw new r("directions-view-model:missing-layer","saveAs() requires a layer");return this.layer.saveAs(e,t)}startEditing(){this._layerView&&(this._layerView.interactive=!0,this._layerView.interactiveOptions.waypointAdditionEnabled=!this.routeParameters.findBestSequence)}stopEditing(){this._layerView&&(this._layerView.interactive=!1)}updateDepartureTime(){if(this.departureIsoDate&&this.departureIsoTime)switch(this.departureOption){case j.NOW:this.departureTime="now";break;case j.DEPART_AT:{const e=L.fromISO(`${this.departureIsoDate}T${this.departureIsoTime}`,{zone:O.instance(0)});this.departureTime=e.toJSDate();break}case j.UNSPECIFIED:this.departureTime=null}}zoomToRoute(){const{view:e,layer:t}=this,r=t?.routeInfo?.geometry?.extent;if(!e||!r)return;const i=r.width>r.height,o=r.clone().expand(i?2:1);this.callGoTo({target:o})}_getGeometryPrecision(){const e=this.view?.resolution;return e?s(Math.ceil(-Math.log10(e)),0,4):null}async _getRouteLayerView(){return this.view&&this.layer?this.view.whenLayerView(this.layer):null}_getStartTimeParameters(){if("now"===this.departureTime){return{startTime:this.serviceDescription?.capabilities.supportsNow??!1?"now":new Date,startTimeIsUTC:!0}}return null==this.departureTime?{startTime:void 0,startTimeIsUTC:void 0}:{startTime:this.departureTime,startTimeIsUTC:!1}}async _load(){if(null==this.layer)return;null!=this._loadController&&(this._loadController.abort(),this._loadController=null),this._loadController=new AbortController;const{signal:e}=this._loadController;try{this._serviceDescriptionStatus=0;const t=await _(this.layer.url,this.apiKey,{signal:e});this._set("serviceDescription",t),this._serviceDescriptionStatus=1}catch(t){if(n(t))return void(this._serviceDescriptionStatus=3);if("identity-manager:user-aborted"===t.name)return void(this._serviceDescriptionStatus=4);const e=new r("directions-view-model:service-metadata-unavailable","Cannot load route service metadata",{error:t});throw this._serviceDescriptionStatus=2,this._set("lastError",e),e}finally{this._loadController=null}}_resolveDefaultTravelMode(e){if(null==this.serviceDescription)return null;const{defaultTravelMode:t,supportedTravelModes:r}=this.serviceDescription,i=/^<(?<name>.*)>$/i.exec(e.name)?.groups?.name;if(i){const o=r?.find(({name:e})=>e.toLocaleLowerCase()===i.trim().toLocaleLowerCase()),s=o??t;return D.fromJSON({...s?.toJSON(),...e.toJSON()})}const o=r?.find(({name:t})=>t.toLocaleLowerCase()===e.name.toLocaleLowerCase());return o??t}};e([p()],F.prototype,"_directionsLanguage",null),e([p()],F.prototype,"_layerView",void 0),e([p()],F.prototype,"_routeController",void 0),e([p()],F.prototype,"_serviceDescriptionStatus",void 0),e([p()],F.prototype,"apiKey",void 0),e([p()],F.prototype,"autoSolve",void 0),e([p({nonNullable:!0})],F.prototype,"autoSolveOn",void 0),e([p()],F.prototype,"defaultTravelMode",void 0),e([p()],F.prototype,"departureTime",void 0),e([p()],F.prototype,"formattedEta",null),e([p({readOnly:!0})],F.prototype,"impedanceAttribute",null),e([p()],F.prototype,"lastError",void 0),e([p({readOnly:!0})],F.prototype,"lastRoute",void 0),e([p()],F.prototype,"layer",void 0),e([p({type:Number,range:{min:2,max:50},nonNullable:!0})],F.prototype,"maxStops",void 0),e([p({type:T,nonNullable:!0})],F.prototype,"routeParameters",void 0),e([p({readOnly:!0})],F.prototype,"selectedNetworkFeatures",null),e([p({type:D})],F.prototype,"selectedTravelMode",null),e([p({readOnly:!0})],F.prototype,"serviceDescription",void 0),e([p({readOnly:!0})],F.prototype,"state",null),e([p({readOnly:!0})],F.prototype,"timeAttribute",null),e([p()],F.prototype,"travelModes",null),e([p()],F.prototype,"view",void 0),e([p()],F.prototype,"zoomToRoute",null),F=e([m("esri.widgets.Directions.DirectionsViewModel")],F);const G=F;export{G as default};