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