UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 9.8 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import r from"./config.js";import t from"./request.js";import s from"./core/Collection.js";import{referenceSetter as a}from"./core/collectionUtils.js";import{JSONSupportMixin as o}from"./core/JSONSupport.js";import{isClonable as i,clone as n}from"./core/lang.js";import{Loadable as l}from"./core/Loadable.js";import{loadAll as p}from"./core/loadAll.js";import y from"./core/Logger.js";import{destroyMaybe as d}from"./core/maybe.js";import{throwIfAborted as c}from"./core/promiseUtils.js";import{isProtocolRelative as u,urlToObject as h}from"./core/urlUtils.js";import{property as m,subclass as f}from"./core/accessorSupport/decorators.js";import g from"./geometry/SpatialReference.js";import L from"./portal/Portal.js";import b from"./portal/PortalItem.js";import{getBasemapTitle as w,esriBasemapDefinitions as I}from"./support/basemapDefinitions.js";import v from"./support/BasemapStyle.js";import{BasemapSymbol as S}from"./support/userTypeGuards/isBasemap.js";import{getLayerJSON as _}from"./webdoc/support/writeUtils.js";import{writer as j}from"./core/accessorSupport/decorators/writer.js";var U,$;let x=class extends(o(l)){static{U=S}static{$=this}constructor(e){super(e),this[U]=!0,this.id=null,this.portalItem=null,this.spatialReference=null,this.style=null,this.thumbnailUrl=null,this.title="Basemap",this.type="Basemap",this.id=Date.now().toString(16)+"-basemap-"+$.idCounter++,this.baseLayers=new s,this.referenceLayers=new s,this.groundLayers=new s;const r=e=>{const{parent:r}=e;r&&r!==this&&"remove"in r&&r.remove?.(e),e.parent=this,"elevation"!==e.type&&"integrated-mesh-3dtiles"!==e.type||y.getLogger(this).error(`Layer '${e.title}, id:${e.id}' of type '${e.type}' is not supported as a basemap layer and will therefore be ignored.`)},t=e=>{const{parent:r}=e;r&&r!==this&&"remove"in r&&r.remove?.(e),e.parent=this},a=e=>{const r=e.item;return r.parent===this?(y.getLogger(this).error(`Layer '${r.title}, id:${r.id}' of type '${r.type}' already a ground layer, second add will be ignored.`),void e.preventDefault()):"integrated-mesh-3dtiles"!==r.type?(y.getLogger(this).error(`Layer '${r.title}, id:${r.id}' of type '${r.type}' is not supported as a ground layer and will therefore be ignored.`),void e.preventDefault()):void 0},o=e=>{e.parent=null},i=e=>{e.parent===this&&(e.parent=null)};this.addHandles([this.baseLayers.on("after-add",e=>r(e.item)),this.baseLayers.on("after-remove",e=>o(e.item)),this.referenceLayers.on("after-add",e=>r(e.item)),this.referenceLayers.on("after-remove",e=>o(e.item)),this.groundLayers.on("before-add",a),this.groundLayers.on("after-add",e=>t(e.item)),this.groundLayers.on("after-remove",e=>i(e.item))])}static{this.idCounter=0}initialize(){this.when().catch(e=>{y.getLogger(this).error("#load()",`Failed to load basemap (title: '${this.title}', id: '${this.id}')`,e)}),this.resourceInfo&&this.read(this.resourceInfo.data,this.resourceInfo.context)}destroy(){const e=this.baseLayers.toArray();for(const s of e)s.destroyed||s.destroy();const r=this.groundLayers.toArray();for(const s of r)s.destroyed||s.destroy();const t=this.referenceLayers.toArray();for(const s of t)s.destroyed||s.destroy();this.baseLayers.destroy(),this.groundLayers.destroy(),this.referenceLayers.destroy(),this.portalItem=d(this.portalItem)}removeChildLayer(e){for(const r of[this.baseLayers,this.referenceLayers,this.groundLayers])r.remove(e)}normalizeCtorArgs(e){return e&&"resourceInfo"in e&&(this._set("resourceInfo",e.resourceInfo),delete(e={...e}).resourceInfo),e}get baseLayers(){return this._get("baseLayers")}set baseLayers(e){this._set("baseLayers",a(e,this._get("baseLayers")))}_writeBaseLayers(e,r,t){const s=[];e?(t={...t,layerContainerType:"basemap-base-layers"},this.baseLayers.forEach(e=>{const r=_(e,t.webmap?t.webmap.getLayerJSONFromResourceInfo(e):null,t);null!=r&&s.push(r)}),this.referenceLayers.forEach(e=>{const r=_(e,t.webmap?t.webmap.getLayerJSONFromResourceInfo(e):null,t);null!=r&&("scene"!==e.type&&(r.isReference=!0),s.push(r))}),r.baseMapLayers=s):r.baseMapLayers=s}set groundLayers(e){this._set("groundLayers",a(e,this._get("groundLayers")))}_writeGroundLayers(e,r,t){const s=[];e&&(t={...t,layerContainerType:"basemap-ground-layers"},this.groundLayers.forEach(e=>{const r=_(e,t.webmap?t.webmap.getLayerJSONFromResourceInfo(e):null,t);null!=r&&s.push(r)}),s.length>0&&(r.groundLayers=s))}get loaded(){return super.loaded}get referenceLayers(){return this._get("referenceLayers")}set referenceLayers(e){this._set("referenceLayers",a(e,this._get("referenceLayers")))}writeTitle(e,r){r.title=e||"Basemap"}load(e){return this.addResolvingPromise(this._loadFromSource(e)),Promise.resolve(this)}loadAll(){return p(this,e=>{e(this.baseLayers,this.groundLayers,this.referenceLayers)})}clone(){const e={id:this.id,title:this.title,portalItem:this.portalItem,baseLayers:this.baseLayers.map(e=>i(e)?e.clone():e),groundLayers:this.groundLayers.map(e=>i(e)?e.clone():e),referenceLayers:this.referenceLayers.map(e=>i(e)?e.clone():e)};return this.loaded&&(e.loadStatus="loaded"),new $({resourceInfo:this.resourceInfo}).set(e)}read(e,r){this.resourceInfo||this._set("resourceInfo",{data:e,context:r}),super.read(e,r)}write(e,r){return e=e||{},r?.origin||(r={origin:"web-map",...r}),super.write(e,r),!this.loaded&&this.resourceInfo?.data.baseMapLayers&&(e.baseMapLayers=this.resourceInfo.data.baseMapLayers.map(e=>{const r=n(e);return r.url&&u(r.url)&&(r.url=`https:${r.url}`),r.templateUrl&&u(r.templateUrl)&&(r.templateUrl=`https:${r.templateUrl}`),r})),e}async _loadFromSource(e){const{resourceInfo:r,portalItem:t,style:s}=this;c(e);const a=[];if(r){const t=r.context?r.context.url:null;if(a.push(this._loadLayersFromJSON(r.data,t,e)),r.data.id&&!r.data.title){const e=r.data.id;a.push(w(e).then(e=>{e&&this.read({title:e},r.context)}))}}else t?a.push(this._loadFromItem(t,e)):s&&a.push(this._loadFromStylesService(s,e));await Promise.all(a)}async _loadLayersFromJSON(e,r,t){const s=this.resourceInfo?.context,a=this.portalItem?.portal||s?.portal||null,o=O[s?.origin||""]??"web-map",{populateOperationalLayers:i}=await import("./layers/support/layersCreator.js"),n=[];if(c(t),e.baseMapLayers&&Array.isArray(e.baseMapLayers)){const t={context:{...s,origin:o,url:r,portal:a,layerContainerType:"basemap-base-layers"},defaultLayerType:"DefaultTileLayer"},l=e=>"web-scene"===o&&"ArcGISSceneServiceLayer"===e.layerType||e.isReference,p=i(this.baseLayers,e.baseMapLayers.filter(e=>!l(e)),t);n.push(p);const y=i(this.referenceLayers,e.baseMapLayers.filter(l),t);n.push(y)}if(e.groundLayers&&Array.isArray(e.groundLayers)){const t={context:{...s,origin:o,url:r,portal:a,layerContainerType:"basemap-ground-layers"},defaultLayerType:"IntegratedMesh3DTilesLayer"},l=i(this.groundLayers,e.groundLayers,t);n.push(l)}await Promise.allSettled(n)}async _loadFromItem(e,r){const t=await e.load(r),s=await t.fetchData("json",r),a=h(e.itemUrl??"");return this._set("resourceInfo",{data:s.baseMap??{},context:{origin:M[e.type||""]??"web-map",portal:e.portal||L.getDefault(),url:a}}),this.read(this.resourceInfo.data,this.resourceInfo.context),this.read({spatialReference:s.spatialReference},this.resourceInfo.context),this.read({title:e.title,thumbnailUrl:e.thumbnailUrl},{origin:"portal-item",portal:e.portal||L.getDefault(),url:a}),this._loadLayersFromJSON(this.resourceInfo.data,a,r)}async _loadFromStylesService(e,s){const a=e.serviceUrl.endsWith("/webmaps")?e.serviceUrl.slice(0,-8):e.serviceUrl,o=`${a}/styles/${e.id}/self`,i=`${a}/webmaps/${e.id}`,n=e.apiKey??(r.sessions?.basemap?.enabled?void 0:r.apiKeys.basemapStyles),[l,p]=await Promise.all([(await t(o,{query:{token:n},signal:s?.signal})).data,(await t(i,{query:{language:e.languageParameter,places:e.places,worldview:e.worldview,token:n},signal:s?.signal})).data]);this.thumbnailUrl??=l.thumbnailUrl;const y=h(i);if(this._set("resourceInfo",{data:p.baseMap??{},context:{origin:"web-map",url:y}}),this.read(this.resourceInfo.data,this.resourceInfo.context),this.read({spatialReference:p.spatialReference},this.resourceInfo.context),await this._loadLayersFromJSON(this.resourceInfo.data,y,s),n)for(const r of[...this.baseLayers,...this.groundLayers,...this.referenceLayers])"apiKey"in r&&(r.apiKey=n)}static fromId(e){const r=I[e];return r?r.itemId?new $({portalItem:{id:r.itemId,portal:{url:"https://www.arcgis.com"}}}):$.fromJSON(r,r.is3d?{origin:"web-scene",portal:new L({url:"https://www.arcgis.com"})}:{origin:"web-map"}):null}};e([m({json:{read:!1,write:{ignoreOrigin:!0,target:"baseMapLayers",writer(e,r,t,s){this._writeBaseLayers(e,r,s)}},origins:{"web-scene":{write:{ignoreOrigin:!0,target:{baseMapLayers:{type:s}},writer(e,r,t,s){this._writeBaseLayers(e,r,s)}}}}}})],x.prototype,"baseLayers",null),e([m({json:{read:!1,write:{ignoreOrigin:!0,target:"groundLayers",writer(e,r,t,s){this._writeGroundLayers(e,r,s)}},origins:{"web-scene":{write:{ignoreOrigin:!0,target:{groundLayers:{type:s}},writer(e,r,t,s){this._writeGroundLayers(e,r,s)}}}}}})],x.prototype,"groundLayers",null),e([m({type:String,json:{origins:{"web-scene":{write:!0}}}})],x.prototype,"id",void 0),e([m({type:b})],x.prototype,"portalItem",void 0),e([m()],x.prototype,"referenceLayers",null),e([m({readOnly:!0})],x.prototype,"resourceInfo",void 0),e([m({type:g})],x.prototype,"spatialReference",void 0),e([m({type:v})],x.prototype,"style",void 0),e([m()],x.prototype,"thumbnailUrl",void 0),e([m({type:String,json:{origins:{"web-scene":{write:{isRequired:!0}}}}})],x.prototype,"title",void 0),e([j("title")],x.prototype,"writeTitle",null),x=$=e([f("esri.Basemap")],x);const M={"Web Scene":"web-scene","Web Map":"web-map","Link Chart":"link-chart"},O={"web-scene":"web-scene","web-map":"web-map","link-chart":"link-chart"},F=x;export{F as default};