BasemapGalleryViewModel.js 4.3 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../Basemap.js";import i from"../../core/Collection.js";import{HandleOwnerMixin as s}from"../../core/HandleOwner.js";import a from"../../core/Loadable.js";import{isSome as r}from"../../core/maybe.js";import{watch as o,on as p}from"../../core/reactiveUtils.js";import{property as n}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import{cast as c}from"../../core/accessorSupport/decorators/cast.js";import{subclass as m}from"../../core/accessorSupport/decorators/subclass.js";import{canProjectWithoutEngine as l,isLoaded as u,load as d}from"../../geometry/projection.js";import{equals as h}from"../../geometry/support/spatialReferenceUtils.js";import{contentEquals as v}from"../../support/basemapUtils.js";import{default3DCompatibility as f,default2DCompatibility as y}from"./support/basemapCompatibilityUtils.js";import b from"./support/BasemapGalleryItem.js";import w from"./support/LocalBasemapsSource.js";import j from"./support/PortalBasemapsSource.js";const g=i.ofType(b);function B(e){return e&&"esri.portal.Portal"===e.declaredClass}function _(e){return e&&!(e instanceof j)&&(!!e.portal||!!e.query)}function I(e){return e&&"basemaps"in e&&"state"in e&&"refresh"in e}let R=class extends(s(a)){constructor(e){super(e),this._loadingProjectionEngine=!1,this.items=new g,this.source=new j,this.view=null}initialize(){const e=()=>this._recreateItems();this.handles.add([o((()=>"ready"===this.state?this.compatibilityFunction:null),(()=>this._updateItems())),p((()=>this.source?.basemaps),"change",e,{onListenerAdd:e})])}get activeBasemap(){return this.view?.map?.basemap??null}set activeBasemap(e){if(!this.view.map)return;const i="string"==typeof e?t.fromId(e):e;if(!i||!this.view.ready)return this.view.map.basemap=i,void this._clearOverride("activeBasemap");const s=i.spatialReference||this.items?.find((e=>this.basemapEquals(i,e.basemap)))?.spatialReference;if(s&&"spatialReferenceLocked"in this.view&&!this.view.spatialReferenceLocked){const t=this.view.spatialReference;if(r(s)&&!h(t,s)&&!l(this.view.spatialReference,s)&&!u())return this._override("activeBasemap",i),this._loadingProjectionEngine=!0,void d().then((()=>{this._get("activeBasemap")===e&&(this.view.map.basemap=e,this.view.spatialReference=s,this._clearOverride("activeBasemap"))}),(()=>{})).then((()=>{this._loadingProjectionEngine=!1}));this.view.map.basemap=i,this._clearOverride("activeBasemap"),r(s)&&!h(this.view.spatialReference,s)&&(this.view.spatialReference=s)}else this.view.map.basemap=i,this._clearOverride("activeBasemap")}get activeBasemapIndex(){const{state:e,items:t,activeBasemap:i}=this;if("ready"!==e)return-1;const s=t.findIndex((e=>e.basemap===i));return-1===s?t.findIndex((e=>this.basemapEquals(e.basemap,i))):s}get compatibilityFunction(){return"3d"===this.view?.type?f:y}set compatibilityFunction(e){this._overrideIfSome("compatibilityFunction",e)}castSource(e){return Array.isArray(e)||i.isCollection(e)?new w({basemaps:e}):B(e)?new j({portal:e}):_(e)?new j(e):I(e)?e:null}get state(){return this.view?.ready&&this.source?this._loadingProjectionEngine?"loading":"ready":"disabled"}basemapEquals(e,t){return v(e,t)}refresh(){this._recreateItems()}load(e){return this.addResolvingPromise(a.isLoadable(this.source)?this.source.load(e):null),Promise.resolve(this)}_recreateItems(){const e=this.source?.basemaps,{view:t,compatibilityFunction:i}=this;this.items.removeAll().forEach((e=>e.destroy())),e&&this.items.addMany(e.map((e=>new b({basemap:e,compatibilityFunction:i,view:t}))))}_updateItems(){for(const e of this.items)e.compatibilityFunction=this.compatibilityFunction,e.view=this.view}};e([n()],R.prototype,"_loadingProjectionEngine",void 0),e([n()],R.prototype,"activeBasemap",null),e([n({readOnly:!0})],R.prototype,"activeBasemapIndex",null),e([n()],R.prototype,"compatibilityFunction",null),e([n({readOnly:!0,type:g})],R.prototype,"items",void 0),e([n()],R.prototype,"source",void 0),e([c("source")],R.prototype,"castSource",null),e([n({readOnly:!0})],R.prototype,"state",null),e([n()],R.prototype,"view",void 0),R=e([m("esri.widgets.BasemapGallery.BasemapGalleryViewModel")],R);const E=R;export{E as default};