Basemap.js 6.6 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 r from"./core/Collection.js";import{referenceSetter as t}from"./core/collectionUtils.js";import{deprecated as s}from"./core/deprecate.js";import{JSONSupportMixin as o}from"./core/JSONSupport.js";import{clone as a}from"./core/lang.js";import i from"./core/Loadable.js";import{loadAll as n}from"./core/loadAll.js";import l from"./core/Logger.js";import{isSome as p}from"./core/maybe.js";import{throwIfAborted as c,eachAlways as m}from"./core/promiseUtils.js";import{isProtocolRelative as h,urlToObject as u}from"./core/urlUtils.js";import{property as d}from"./core/accessorSupport/decorators/property.js";import"./core/accessorSupport/ensureType.js";import{subclass as y}from"./core/accessorSupport/decorators/subclass.js";import{writer as f}from"./core/accessorSupport/decorators/writer.js";import b from"./geometry/SpatialReference.js";import L from"./portal/Portal.js";import g from"./portal/PortalItem.js";import{getBasemapTitle as w,esriBasemapDefinitions as I}from"./support/basemapDefinitions.js";import{getLayerJSON as j}from"./webdoc/support/writeUtils.js";var v;let S=0;const _="esri.Basemap";let U=v=class extends(o(i)){constructor(e){super(e),this.id=null,this.portalItem=null,this.spatialReference=null,this.thumbnailUrl=null,this.title="Basemap",this.id=Date.now().toString(16)+"-basemap-"+S++,this.baseLayers=new r,this.referenceLayers=new r;const t=e=>{e.parent&&e.parent!==this&&"remove"in e.parent&&e.parent.remove(e),e.parent=this,"elevation"===e.type&&l.getLogger(this.declaredClass).error(`Layer '${e.title}, id:${e.id}' of type '${e.type}' is not supported as a basemap layer and will therefore be ignored.`)},s=e=>{e.parent=null};this.baseLayers.on("after-add",(e=>t(e.item))),this.referenceLayers.on("after-add",(e=>t(e.item))),this.baseLayers.on("after-remove",(e=>s(e.item))),this.referenceLayers.on("after-remove",(e=>s(e.item)))}initialize(){this.when().catch((e=>{l.getLogger(this.declaredClass).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.removeAll();for(const t of e)t.destroy();const r=this.referenceLayers.removeAll();for(const t of r)t.destroy();this.baseLayers.destroy(),this.referenceLayers.destroy(),this.portalItem?.destroy(),this.portalItem=null}normalizeCtorArgs(e){return e&&"resourceInfo"in e&&(this._set("resourceInfo",e.resourceInfo),delete(e={...e}).resourceInfo),e}set baseLayers(e){this._set("baseLayers",t(e,this._get("baseLayers")))}_writeBaseLayers(e,r,t){const s=[];e?(t={...t,layerContainerType:"basemap"},this.baseLayers.forEach((e=>{const r=j(e,t.webmap?t.webmap.getLayerJSONFromResourceInfo(e):null,t);p(r)&&s.push(r)})),this.referenceLayers.forEach((e=>{const r=j(e,t.webmap?t.webmap.getLayerJSONFromResourceInfo(e):null,t);p(r)&&(r.isReference=!0,s.push(r))})),r.baseMapLayers=s):r.baseMapLayers=s}set referenceLayers(e){this._set("referenceLayers",t(e,this._get("referenceLayers")))}writeTitle(e,r){r.title=e||"Basemap"}load(e){return this.addResolvingPromise(this._loadFromSource(e)),Promise.resolve(this)}loadAll(){return n(this,(e=>{e(this.baseLayers,this.referenceLayers)}))}clone(){const e={id:this.id,title:this.title,portalItem:this.portalItem,baseLayers:this.baseLayers.slice(),referenceLayers:this.referenceLayers.slice()};return this.loaded&&(e.loadStatus="loaded"),new v({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&&r.origin||(r={origin:"web-map",...r}),super.write(e,r),!this.loaded&&this.resourceInfo&&this.resourceInfo.data.baseMapLayers&&(e.baseMapLayers=this.resourceInfo.data.baseMapLayers.map((e=>{const r=a(e);return r.url&&h(r.url)&&(r.url=`https:${r.url}`),r.templateUrl&&h(r.templateUrl)&&(r.templateUrl=`https:${r.templateUrl}`),r}))),e}async _loadFromSource(e){const{resourceInfo:r,portalItem:t}=this;c(e);const s=[];if(r){const t=r.context?r.context.url:null;if(s.push(this._loadLayersFromJSON(r.data,t,e)),r.data.id&&!r.data.title){const e=r.data.id;s.push(w(e).then((e=>{e&&this.read({title:e},r.context)})))}}else t&&s.push(this._loadFromItem(t,e));await Promise.all(s)}async _loadLayersFromJSON(e,r,t){const s=this.resourceInfo&&this.resourceInfo.context,o=this.portalItem&&this.portalItem.portal||s&&s.portal||null,a=s&&"web-scene"===s.origin?"web-scene":"web-map",{populateOperationalLayers:i}=await import("./layers/support/layersCreator.js"),n=[];if(c(t),e.baseMapLayers&&Array.isArray(e.baseMapLayers)){const t={context:{origin:a,url:r,portal:o,layerContainerType:"basemap"},defaultLayerType:"DefaultTileLayer"},s=i(this.baseLayers,e.baseMapLayers.filter((e=>!e.isReference)),t);n.push(s);const l=i(this.referenceLayers,e.baseMapLayers.filter((e=>e.isReference)),t);n.push(l)}await m(n)}async _loadFromItem(e,r){const t=await e.load(r),s=await t.fetchData("json",r),o=u(e.itemUrl??"");return this._set("resourceInfo",{data:s.baseMap??{},context:{origin:"web-map",portal:e.portal||L.getDefault(),url:o}}),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:o}),this._loadLayersFromJSON(this.resourceInfo.data,o,r)}static fromId(e){const r=I[e];if(r){if(r.deprecated){let r=null;"dark-gray"===e?r="dark-gray-vector":"gray"===e?r="gray-vector":"streets"===e?r="streets-vector":"topo"===e&&(r="topo-vector"),s(l.getLogger(_),`The ${e} basemap has entered mature support and is no longer being updated.`,{replacement:r,see:"https://arcg.is/1iq8aD",warnOnce:!0})}return v.fromJSON(r)}return null}};e([d({json:{write:{ignoreOrigin:!0,target:"baseMapLayers",writer(e,r,t,s){this._writeBaseLayers(e,r,s)}},origins:{"web-scene":{write:{ignoreOrigin:!0,target:{baseMapLayers:{type:r}},writer(e,r,t,s){this._writeBaseLayers(e,r,s)}}}}}})],U.prototype,"baseLayers",null),e([d({type:String,json:{origins:{"web-scene":{write:!0}}}})],U.prototype,"id",void 0),e([d({type:g})],U.prototype,"portalItem",void 0),e([d()],U.prototype,"referenceLayers",null),e([d({readOnly:!0})],U.prototype,"resourceInfo",void 0),e([d({type:b})],U.prototype,"spatialReference",void 0),e([d()],U.prototype,"thumbnailUrl",void 0),e([d({type:String,json:{origins:{"web-scene":{write:{isRequired:!0}}}}})],U.prototype,"title",void 0),e([f("title")],U.prototype,"writeTitle",null),U=v=e([y(_)],U);const x=U;export{x as default};