UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 3.05 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as t}from"tslib";import a from"../../../Basemap.js";import e from"../../../core/Collection.js";import s from"../../../core/Error.js";import{LoadableMixin as r}from"../../../core/Loadable.js";import o from"../../../core/Logger.js";import{EsriPromiseMixin as l}from"../../../core/Promise.js";import{ignoreAbortErrors as i,isAbortError as p}from"../../../core/promiseUtils.js";import{watch as n,initial as m}from"../../../core/reactiveUtils.js";import{property as h,subclass as u}from"../../../core/accessorSupport/decorators.js";import c from"../../../portal/Portal.js";import d from"./LocalBasemapsSource.js";const y=e.ofType(a);let f=class extends(r(l(d))){constructor(t){super(t),this._lastPortalBasemapFetchController=null,this.basemaps=new y,this.filterFunction=null,this.portal=c.getDefault(),this.query=null,this.updateBasemapsCallback=null,this.viewType=null}initialize(){this.addHandles(n(()=>[this.filterFunction,this.loadStatus,this.portal?.basemapGalleryGroupQuery,this.portal?.basemapGalleryGroupQuery3D,this.portal?.user,this.query,this.updateBasemapsCallback],()=>i(this.refresh()),m))}destroy(){this.filterFunction=null,this.portal=null,this.basemaps.forEach(t=>t.destroy())}get state(){return"not-loaded"===this.loadStatus?"not-loaded":"loading"===this.loadStatus||this._lastPortalBasemapFetchController?"loading":"ready"}load(t){return this.addResolvingPromise(this.portal.load(t)),Promise.resolve(this)}async refresh(){if("loaded"!==this.loadStatus)return;this._lastPortalBasemapFetchController&&(this._lastPortalBasemapFetchController.abort(),this._lastPortalBasemapFetchController=null);const t=this.portal,a=new AbortController;this._lastPortalBasemapFetchController=a,this.notifyChange("state");try{const e=await t.fetchBasemaps(this._toQueryString(this.query),{signal:a.signal,include3d:"3d"===this.viewType||void 0});await this._updateBasemaps(e)}catch(e){if(p(e))throw e;o.getLogger(this).warn(new s("basemap-source:fetch-basemaps-error","Could not fetch basemaps from portal.",{error:e})),await this._updateBasemaps()}this._lastPortalBasemapFetchController=null,this.notifyChange("state")}_toQueryString(t){return t&&"string"!=typeof t?Object.keys(t).map(a=>`${a}:${t[a]}`).join(" AND "):t}async _updateBasemaps(t=[]){let a=await this._filterBasemaps(t);a=this.updateBasemapsCallback?await this.updateBasemapsCallback(a):a,this.basemaps.removeAll(),this.basemaps.addMany(a)}async _filterBasemaps(t){if(!this.filterFunction)return t;const a=t.map(this.filterFunction),e=await Promise.all(a);return t.filter((t,a)=>e[a])}};t([h({readOnly:!0,type:y})],f.prototype,"basemaps",void 0),t([h()],f.prototype,"filterFunction",void 0),t([h({type:c})],f.prototype,"portal",void 0),t([h()],f.prototype,"query",void 0),t([h({readOnly:!0})],f.prototype,"state",null),t([h()],f.prototype,"updateBasemapsCallback",void 0),t([h()],f.prototype,"viewType",void 0),f=t([u("esri.widgets.BasemapGallery.support.PortalBasemapsSource")],f);const B=f;export{B as default};