12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.25/esri/copyright.txt for details.
- */
- import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../Color.js";import r from"../../Graphic.js";import"../../symbols.js";import{getAccentColor as i}from"../../core/analysisThemeUtils.js";import o from"../../core/Collection.js";import{deprecatedProperty as s,deprecated as l}from"../../core/deprecate.js";import a from"../../core/Error.js";import n from"../../core/Evented.js";import{HandleOwnerMixin as c}from"../../core/HandleOwner.js";import h from"../../core/Logger.js";import{isNone as u,unwrapOr as p,isSome as d,unwrap as m,applySome as y}from"../../core/maybe.js";import{isAbortError as g}from"../../core/promiseUtils.js";import{on as v,watch as w,syncAndInitial as f,initial as b}from"../../core/reactiveUtils.js";import{property as _}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import{cast as S}from"../../core/accessorSupport/decorators/cast.js";import{subclass as L}from"../../core/accessorSupport/decorators/subclass.js";import{getLocale as C}from"../../intl/locale.js";import D from"../../layers/GraphicsLayer.js";import T from"../../layers/RouteLayer.js";import j from"../../layers/support/RouteStopSymbols.js";import A from"../../layers/support/RouteSymbols.js";import{fetchServiceDescription as M}from"../../rest/networkService.js";import z from"../../rest/support/RouteParameters.js";import O from"../../rest/support/Stop.js";import{GoToMixin as R}from"../support/GoTo.js";import P from"../../symbols/SimpleLineSymbol.js";import x from"../../symbols/SimpleMarkerSymbol.js";import U from"../../symbols/PictureMarkerSymbol.js";function E(e){return"esri.Graphic"===e?.declaredClass}function I(e){return Array.isArray(e)&&e.length&&E(e[0])}function H(e){return o.isCollection(e)&&e.length&&E(e.getItemAt(0))}function k(e){return"esri.rest.support.Stop"===e?.declaredClass}var G;!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"}(G||(G={}));const N="esri.widgets.Directions.DirectionsViewModel",F=h.getLogger(N);let K=class extends(c(R(n.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=G.Idle,this.apiKey=void 0,this.departureTime="now",this.lastError=null,this.lastRoute=null,this.layer=null,this.maxStops=50,this.routeParameters=new z({directionsLengthUnits:"kilometers",findBestSequence:!1,restrictUTurns:"at-dead-ends-and-intersections",returnZ:!0,startTime:null,startTimeIsUTC:!0,useHierarchy:!0,useTimeWindows:!1}),this.serviceDescription=null,this.view=null}initialize(){this.addHandles([v((()=>y(this.layer,(e=>e.stops))),"change",(()=>{this.clearResults()})),w((()=>this.layer),((e,t)=>{if(d(e))for(;e.stops.length<2;)e.stops.add(new O);u(e)&&(this.layer=this.legacyLayer),d(t)&&t===this.legacyLayer&&this.view?.map?.remove(t)}),f),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)}),b)])}destroy(){this.view?.map?.remove(this._highlightLayer),this.layer===this.legacyLayer&&this.view?.map?.remove(this.layer)}get _directionsLanguage(){if(u(this.serviceDescription))return;const e=this.serviceDescription.directionsSupportedLanguages;if(!e)return;const t=p(this.routeParameters.directionsLanguage,C()).slice(0,2);return e.find((e=>e.toLowerCase().slice(0,2)===t))}get impedanceAttribute(){const e=d(this.serviceDescription)?this.serviceDescription.impedance:null,t=m(this.routeParameters.travelMode)?.impedanceAttributeName??m(this.routeParameters.impedanceAttribute)??e;return this.getCostAttribute(t)}get legacyLayer(){return u(this._legacyLayer)&&(this._legacyLayer=this._getLegacyLayer()),this._legacyLayer}set routeServiceUrl(e){s(F,"routeServiceUrl",{replacement:"layer.url",version:"4.24",warnOnce:!0}),this.legacyLayer.url=e}get routeServiceUrl(){return s(F,"routeServiceUrl",{replacement:"layer.url",version:"4.24",warnOnce:!0}),this.legacyLayer.url}set routeSymbol(e){s(F,"routeSymbol",{replacement:"layer.defaultSymbols.directionLines",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.directionLines=e}get routeSymbol(){return s(F,"routeSymbol",{replacement:"layer.defaultSymbols.directionLines",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.directionLines}get selectedTravelMode(){const{serviceDescription:e}=this;if(u(e))return null;const{defaultTravelMode:t,supportedTravelModes:r=[]}=e;return r.find((e=>e.id===t))||r[0]||null}set selectedTravelMode(e){this._override("selectedTravelMode",e)}get state(){if(d(this._routeController))return"routing";if(d(this.lastError))return"error";switch(this._serviceDescriptionStatus){case G.Suspended:return"unauthenticated";case G.Idle:return"disabled";case G.Active:return"initializing";case G.Failed:return"error";default:return"ready"}}set stops(e){s(F,"stops",{replacement:"layer.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.stops=e}get stops(){return s(F,"stops",{replacement:"layer.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.stops}castStops(e){return I(e)||H(e)?e.map((e=>O.fromGraphic(e))):e}set stopSymbols(e){s(F,"stopSymbols",{replacement:"layer.defaultSymbols.stops",version:"4.24",warnOnce:!0}),this.legacyLayer.defaultSymbols.stops=e}get stopSymbols(){return s(F,"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)??(d(this.serviceDescription)?this.serviceDescription.directionsTimeAttribute:null);return this.getCostAttribute(e)}get travelModes(){return d(this.serviceDescription)?this.serviceDescription.supportedTravelModes:[]}async load(){if(d(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(F,"highlightSegment",{replacement:"highlight",version:"4.24",warnOnce:!0});const{geometry:i,symbol:o}=e;if(u(this.view)||u(i)||u(o))return;this.clearHighlights();const s=o.clone();s.color=new t([0,0,0,.8]),s.width=Math.ceil(s.width/2);const a=new r({geometry:i,symbol:s});this._highlightLayer.add(a)}clearHighlights(){this._highlightLayer.removeAll(),d(this._highlight)&&(this._highlight.remove(),this._highlight=null)}centerAt(e){if(u(this.view))return;const t=k(e)||E(e)?e.geometry:e;u(t)||this.callGoTo({target:t})}clearResults(){this._set("lastRoute",null),this.layer.removeResult()}async getDirections(){const{apiKey:e,departureTime:t,layer:r,state:i}=this;if(u(r))throw new a("directions-view-model:missing-route-layer","A route layer must be associated with the view model.");if(r===this.legacyLayer&&l(F,"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"===i||"initializing"===i||"disabled"===i||this._serviceDescriptionStatus===G.Failed)throw new a("directions-view-model:not-loaded","Cannot get directions until view model loads.");this._set("lastError",null),d(this._routeController)&&(this._routeController.abort(),this._routeController=null);const o="now"===t,s="now"===t?new Date:t;if(s&&!o){const e=60*s.getTimezoneOffset()*1e3;s.setTime(s.getTime()-e)}const n=d(this.view)?this.view.spatialReference:null,c=this.routeParameters.clone();c.set({startTime:s,startTimeIsUTC:o,directionsLanguage:this._directionsLanguage,apiKey:e,outSpatialReference:n}),d(this.selectedTravelMode)&&(c.travelMode=this.selectedTravelMode);if(r.stops.filter((e=>d(e.geometry))).length<2){const e=new a("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 p=null;try{p=await r.solve(c,{signal:h})}catch(m){if(!g(m)){const e=new a("directions-view-model:unable-to-route","Unable to route to these addresses",{error:m});throw this._set("lastError",e),this._set("lastRoute",null),e}}finally{this._routeController=null}for(const l of p.stops)u(l.geometry)&&(l.name=null);return r.update(p),this._set("lastRoute",p),this.zoomToRoute(),p}getCostAttribute(e){return(d(this.serviceDescription)?this.serviceDescription.networkDataset.networkAttributes:[]).find((t=>t.name===e&&"cost"===t.usageType))??null}reset(){this.clearHighlights(),this.clearResults(),d(this.layer)&&(this.layer.removeAll(),this.layer.stops=new o([new O,new O]))}save(){return this.layer.save()}saveAs(e,t={}){return this.layer.saveAs(e,t)}zoomToRoute(){const{view:e,layer:{routeInfo:t}}=this;if(u(e)||u(t))return;const{geometry:r}=t;if(u(r))return;const{extent:i}=r,o=i.width>i.height,s=i.clone().expand(o?2:1);this.callGoTo({target:s})}_getLegacyLayer(){const e=i();return new T({listMode:"hide",defaultSymbols:new A({directionLines:new P({color:e,width:7,cap:"round",join:"round"}),directionPoints:null,routeInfo:null,stops:new j({break:new x({color:[255,255,255],size:10,outline:{color:[20,89,127],width:2.5}}),first:new x({color:e,size:19,outline:{color:[51,51,51],width:3}}),last:new U({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 x({color:[51,51,51],size:12,outline:{color:e,width:3}}),unlocated:new U({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 x({color:[255,255,255],size:10,outline:{color:[20,89,127],width:2.5}})})}),stops:[{},{}]})}async _load(){if(d(this.serviceDescription)||u(this.layer))return;d(this._loadController)&&(this._loadController.abort(),this._loadController=null),this._loadController=new AbortController;const{signal:e}=this._loadController;try{this._serviceDescriptionStatus=G.Active;const t=await M(this.layer.url,this.apiKey,{signal:e});this._set("serviceDescription",t),this._serviceDescriptionStatus=G.Complete}catch(t){if(g(t))return void(this._serviceDescriptionStatus=G.Idle);if("identity-manager:user-aborted"===t.name)return void(this._serviceDescriptionStatus=G.Suspended);const e=new a("directions-view-model:service-metadata-unavailable","Cannot load route service metadata",{error:t});throw this._serviceDescriptionStatus=G.Failed,this._set("lastError",e),e}finally{this._loadController=null}}};e([_()],K.prototype,"_directionsLanguage",null),e([_()],K.prototype,"_routeController",void 0),e([_()],K.prototype,"_serviceDescriptionStatus",void 0),e([_()],K.prototype,"apiKey",void 0),e([_()],K.prototype,"departureTime",void 0),e([_({readOnly:!0})],K.prototype,"impedanceAttribute",null),e([_()],K.prototype,"lastError",void 0),e([_({readOnly:!0})],K.prototype,"lastRoute",void 0),e([_()],K.prototype,"layer",void 0),e([_({readOnly:!0})],K.prototype,"legacyLayer",null),e([_({type:Number,range:{min:2,max:50},nonNullable:!0})],K.prototype,"maxStops",void 0),e([_({type:z,nonNullable:!0})],K.prototype,"routeParameters",void 0),e([_()],K.prototype,"routeServiceUrl",null),e([_()],K.prototype,"routeSymbol",null),e([_()],K.prototype,"selectedTravelMode",null),e([_({readOnly:!0})],K.prototype,"serviceDescription",void 0),e([_({readOnly:!0})],K.prototype,"state",null),e([_()],K.prototype,"stops",null),e([S("stops")],K.prototype,"castStops",null),e([_()],K.prototype,"stopSymbols",null),e([_({readOnly:!0})],K.prototype,"timeAttribute",null),e([_()],K.prototype,"travelModes",null),e([_()],K.prototype,"view",void 0),e([_()],K.prototype,"getDirections",null),e([_()],K.prototype,"zoomToRoute",null),K=e([L(N)],K);const $=K;export{$ as default};
|