@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 4.49 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{_ as e}from"../../chunks/tslib.es6.js";import r from"../../core/Collection.js";import s from"../../core/CollectionFlattener.js";import t from"../../core/Error.js";import o from"../../core/Logger.js";import{watch as a,sync as l}from"../../core/reactiveUtils.js";import{property as i}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/RandomLCG.js";import{getProperties as n}from"../../core/accessorSupport/utils.js";import{subclass as u}from"../../core/accessorSupport/decorators/subclass.js";import{OriginId as y,nameToId as c,idToName as b}from"../../core/accessorSupport/PropertyOrigin.js";import d from"../support/Sublayer.js";import{isSublayerOverhaul as p}from"../support/sublayerUtils.js";function h(e,r){const s=[],t={};return e?(e.forEach((e=>{const o=new d;if(o.read(e,r),t[o.id]=o,null!=e.parentLayerId&&-1!==e.parentLayerId){const r=t[e.parentLayerId];r.sublayers||(r.sublayers=[]),r.sublayers.unshift(o)}else s.unshift(o)})),s):s}const f=r.ofType(d);function S(e,r){e&&e.forEach((e=>{r(e),e.sublayers&&e.sublayers.length&&S(e.sublayers,r)}))}const m=m=>{let E=class extends m{constructor(...e){super(...e),this.allSublayers=new s({getCollections:()=>[this.sublayers],getChildrenFunction:e=>e.sublayers}),this.sublayersSourceJSON={[y.SERVICE]:{},[y.PORTAL_ITEM]:{},[y.WEB_SCENE]:{},[y.WEB_MAP]:{},[y.LINK_CHART]:{}},this.subtables=null,this.addHandles([a((()=>this.sublayers),((e,r)=>this._handleSublayersChange(e,r)),l),a((()=>this.subtables),((e,r)=>this._handleSublayersChange(e,r)),l)])}destroy(){this.allSublayers.destroy()}readSublayers(e,r){if(!r||!e)return;const{sublayersSourceJSON:s}=this,t=c(r.origin);if(t<y.SERVICE)return;if(s[t]={context:r,visibleLayers:e.visibleLayers||s[t].visibleLayers,layers:e.layers||s[t].layers},t>y.SERVICE)return;this._set("serviceSublayers",this.createSublayersForOrigin("service").sublayers);const{sublayers:o,origin:a}=this.createSublayersForOrigin("web-document"),l=n(this);l.setDefaultOrigin(a),this._set("sublayers",new f(o)),l.setDefaultOrigin("user")}findSublayerById(e){return this.allSublayers.find((r=>r.id===e))}createServiceSublayers(){return this.createSublayersForOrigin("service").sublayers}createSublayersForOrigin(e){let r;const s=c("web-document"===e?"web-map":e);let t=y.SERVICE,o=this.sublayersSourceJSON[y.SERVICE].layers,a=this.sublayersSourceJSON[y.SERVICE].context,l=null;const i=[y.PORTAL_ITEM,y.WEB_SCENE,y.WEB_MAP].filter((e=>e<=s));for(const y of i){const e=this.sublayersSourceJSON[y];p(e.layers)&&(t=y,o=e.layers,a=e.context,e.visibleLayers&&(l={visibleLayers:e.visibleLayers,context:e.context}))}const n=[y.PORTAL_ITEM,y.WEB_SCENE,y.WEB_MAP].filter((e=>e>t&&e<=s));let u=null;for(const y of n){const{layers:e,visibleLayers:s,context:t}=this.sublayersSourceJSON[y];e&&(u={layers:e,context:t},r??=y),s&&(l={visibleLayers:s,context:t})}const d=h(o,a),m=new Map,E=new Set;if(u)for(const y of u.layers)m.set(y.id,y);if(l?.visibleLayers)for(const y of l.visibleLayers)E.add(y);return S(d,(e=>{u&&e.read(m.get(e.id),u.context),l&&e.read({defaultVisibility:E.has(e.id)},l.context)})),{origin:b(t),originWithPartialOverrides:r?b(r):null,sublayers:new f({items:d})}}read(e,r){super.read(e,r),this.readSublayers(e,r)}_handleSublayersChange(e,r){r&&(r.forEach((e=>{e.parent=null,e.layer=null})),this.removeHandles("sublayers-owner")),e&&(e.forEach((e=>{e.parent=this,e.layer=this})),this.addHandles([e.on("after-add",(({item:e})=>{e.parent=this,e.layer=this})),e.on("after-remove",(({item:e})=>{e.parent=null,e.layer=null}))],"sublayers-owner"),"tile"===this.type&&this.addHandles(e.on("before-changes",(e=>{o.getLogger("esri.layers.TileLayer").error(new t("tilelayer:sublayers-non-modifiable","ISublayer can't be added, moved, or removed from the layer's sublayers",{layer:this})),e.preventDefault()})),"sublayers-owner"))}};return e([i({readOnly:!0})],E.prototype,"allSublayers",void 0),e([i({readOnly:!0,type:r.ofType(d)})],E.prototype,"serviceSublayers",void 0),e([i({value:null,type:f,json:{read:!1,write:{allowNull:!0,ignoreOrigin:!0}}})],E.prototype,"sublayers",void 0),e([i({readOnly:!0})],E.prototype,"sublayersSourceJSON",void 0),e([i({type:f,json:{read:{source:"tables"}}})],E.prototype,"subtables",void 0),E=e([u("esri.layers.mixins.SublayersOwner")],E),E};export{m as SublayersOwner,S as forEachSublayer};