UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 9.06 kB
import{_ as e}from"../../chunks/tslib.es6.js";import"../../intl.js";import r from"../../core/Accessor.js";import i from"../../core/Collection.js";import t from"../../core/Handles.js";import{watch as s,initial as a,when as n}from"../../core/reactiveUtils.js";import{property as o}from"../../core/accessorSupport/decorators/property.js";import"../../core/accessorSupport/ensureType.js";import"../../core/arrayUtils.js";import{subclass as l}from"../../core/accessorSupport/decorators/subclass.js";import y from"./support/ActiveLayerInfo.js";import{onLocaleChange as d}from"../../intl/locale.js";const h={state:"state",view:"view",allLayerViews:"all-layer-views",legendProperties:"legend-properties"},c=i.ofType(y),L=["esri.layers.BuildingSceneLayer","esri.layers.CSVLayer","esri.layers.FeatureLayer","esri.layers.GeoJSONLayer","esri.layers.GeoRSSLayer","esri.layers.GroupLayer","esri.layers.HeatmapLayer","esri.layers.ImageryLayer","esri.layers.ImageryTileLayer","esri.layers.MapImageLayer","esri.layers.OGCFeatureLayer","esri.layers.PointCloudLayer","esri.layers.StreamLayer","esri.layers.SceneLayer","esri.layers.SubtypeGroupLayer","esri.layers.TileLayer","esri.layers.VoxelLayer","esri.layers.WFSLayer","esri.layers.WMSLayer","esri.layers.WMTSLayer","esri.layers.WCSLayer","esri.layers.LinkChartLayer","esri.layers.knowledgeGraph.KnowledgeGraphSubLayer"],u="view.basemapView.baseLayerViews",_="view.groundView.layerViews",f="view.basemapView.referenceLayerViews",p=[u,_,"view.layerViews",f];let v=class extends r{constructor(e){super(e),this._handles=new t,this._layerViewByLayerId={},this._layerInfosByLayerViewId={},this._activeLayerInfosByLayerViewId={},this._activeLayerInfosWithNoParent=new i,this.activeLayerInfos=new c,this.basemapLegendVisible=!1,this.groundLegendVisible=!1,this.hideLayersNotInCurrentView=!1,this.keepCacheOnDestroy=!1,this.respectLayerVisibility=!0,this.layerInfos=[],this.view=null}initialize(){this._handles.add(s((()=>this.view),(()=>this._viewHandles()),a),h.view),this._handles.add(d((()=>this._refresh())))}destroy(){this._destroyViewActiveLayerInfos(),this._handles.destroy(),this._handles=null,this.view=null}get state(){return this.get("view.ready")?"ready":"disabled"}_viewHandles(){this._handles.remove(h.state),this.view&&this._handles.add(s((()=>this.state),(()=>this._stateHandles()),a),h.state)}_stateHandles(){this._resetAll(),"ready"===this.state&&this._watchPropertiesAndAllLayerViews()}_resetAll(){this._handles.remove([h.allLayerViews,h.legendProperties]),this._destroyViewActiveLayerInfos(),this.activeLayerInfos.removeAll()}_destroyViewActiveLayerInfos(){Object.keys(this._activeLayerInfosByLayerViewId).forEach(this._destroyViewActiveLayerInfo,this)}_destroyViewActiveLayerInfo(e){this._handles.remove(e);const r=this._activeLayerInfosByLayerViewId[e];delete this._activeLayerInfosByLayerViewId[e],r&&r.parent&&r.parent.children.remove(r)}_watchPropertiesAndAllLayerViews(){const{allLayerViews:e}=this.view;e.length&&this._refresh(),this._handles.add(e.on("change",(e=>this._allLayerViewsChangeHandle(e))),h.allLayerViews),this._handles.add(s((()=>[this.layerInfos,this.basemapLegendVisible,this.groundLegendVisible]),(()=>this._propertiesChangeHandle())),h.legendProperties)}_allLayerViewsChangeHandle(e){e.removed.forEach((e=>this._destroyViewActiveLayerInfo(e.uid))),this._refresh()}_propertiesChangeHandle(){this._destroyViewActiveLayerInfos(),this._refresh()}_refresh(){this._layerInfosByLayerViewId={},this.activeLayerInfos.removeAll(),this._generateLayerViews().filter(this._filterLayerViewsByLayerInfos,this).filter(this._isLayerViewSupported,this).forEach(this._generateActiveLayerInfo,this),this._sortActiveLayerInfos(this.activeLayerInfos)}_sortActiveLayerInfos(e){if(e.length<2)return;const r=[];e.forEach((i=>{if(!i.parent){const t=i.layer.parent,s=t&&"uid"in t&&this._layerViewByLayerId[t.uid],a=s&&this._activeLayerInfosByLayerViewId[s.uid];a&&e.includes(a)&&(r.push(i),i.parent=a,a.children.add(i),this._sortActiveLayerInfos(a.children))}})),e.removeMany(r);const i={};this.view.allLayerViews.forEach(((e,r)=>i[e.layer.uid]=r)),e.sort(((e,r)=>{const t=i[e.layer.uid]||0;return(i[r.layer.uid]||0)-t}))}_generateLayerViews(){const e=[];return p.filter(this._filterLayerViews,this).map(this.get,this).filter((e=>null!=e)).forEach(this._collectLayerViews("layerViews",e)),e}_filterLayerViews(e){const r=!this.basemapLegendVisible&&(e===u||e===f),i=!this.groundLegendVisible&&e===_;return!r&&!i}_collectLayerViews(e,r){const i=t=>(t&&t.forEach((t=>{r.push(t),i(t[e])})),r);return i}_filterLayerViewsByLayerInfos(e){const r=this.layerInfos;return!r||!r.length||r.some((r=>this._hasLayerInfo(r,e)))}_hasLayerInfo(e,r){const i=this._isLayerUIDMatching(e.layer,r.layer.uid);return i&&(this._layerInfosByLayerViewId[r.uid]=e),i}_isLayerUIDMatching(e,r){return e&&(e.uid===r||this._hasLayerUID(e.layers,r))}_hasLayerUID(e,r){return e&&e.some((e=>this._isLayerUIDMatching(e,r)))}_isLayerViewSupported(e){return!!L.includes(e.layer.declaredClass)&&(this._layerViewByLayerId[e.layer.uid]=e,!0)}_generateActiveLayerInfo(e){this._isLayerActive(e)?this._buildActiveLayerInfo(e):(this._handles.remove(e.uid),this._handles.add(s((()=>[e.legendEnabled,e.layer?.legendEnabled]),(()=>this._layerActiveHandle(e))),e.uid))}_layerActiveHandle(e){this._isLayerActive(e)&&(this._handles.remove(e.uid),this._buildActiveLayerInfo(e))}_isLayerActive(e){return!this.respectLayerVisibility||e.legendEnabled&&e.get("layer.legendEnabled")}_buildActiveLayerInfo(e){const r=e.layer,i=e.uid,t=this._layerInfosByLayerViewId[i];let o=this._activeLayerInfosByLayerViewId[i];if(!o){const s=t&&void 0!==t.title&&t.layer.uid===r.uid;o=new y({layer:r,layerView:e,title:s?t.title:r.title,view:this.view,respectLayerVisibility:this.respectLayerVisibility,hideLayersNotInCurrentView:this.hideLayersNotInCurrentView,keepCacheOnDestroy:this.keepCacheOnDestroy,sublayerIds:t&&t.sublayerIds||[]}),this._activeLayerInfosByLayerViewId[i]=o}const l=r.parent&&"uid"in r.parent&&this._layerViewByLayerId[r.parent?.uid];if(o.parent=this._activeLayerInfosByLayerViewId[l?.uid],!this._handles.has(i)){const t=[s((()=>r.title),(e=>this._titleHandle(e,o))),s((()=>[r.opacity,"renderer"in r&&r.renderer,"pointSymbol"in r&&r.pointSymbol,"lineSymbol"in r&&r.lineSymbol,"polygonSymbol"in r&&r.polygonSymbol]),(()=>this._constructLegendElements(o))),n((()=>!0===this.view?.stationary),(()=>this._scaleHandle(o)),a),s((()=>e._effectiveRenderer),(()=>this._constructLegendElements(o))),s((()=>"effect"in r&&r.effect),(()=>this._constructLegendElements(o)))];if(this.respectLayerVisibility){const i=s((()=>e.legendEnabled),(e=>this._legendEnabledHandle(e,o))),a=s((()=>r.legendEnabled),(e=>this._legendEnabledHandle(e,o)));t.push(i,a)}this._handles.add(t,i)}o.isScaleDriven||this._constructLegendElements(o),this._addActiveLayerInfo(o)}_titleHandle(e,r){r.title=e,this._constructLegendElements(r)}_legendEnabledHandle(e,r){e?this._addActiveLayerInfo(r):this._removeActiveLayerInfo(r)}_scaleHandle(e){(e.isScaleDriven||e.hideLayersNotInCurrentView)&&this._constructLegendElements(e)}_addActiveLayerInfo(e){const{layerView:r,layer:i}=e;if(this._isLayerActive(r)&&!this.activeLayerInfos.includes(e)){const r=e.parent;if(r)r.children.includes(e)||r.children.push(e),this._sortActiveLayerInfos(r.children);else{const r=this.layerInfos?.some((e=>e.layer.uid===i.uid));i.parent&&"uid"in i.parent&&!r?this._activeLayerInfosWithNoParent.add(e):(this.activeLayerInfos.add(e),this._sortActiveLayerInfos(this.activeLayerInfos))}if(this._activeLayerInfosWithNoParent.length){const e=[];this._activeLayerInfosWithNoParent.forEach((r=>{const i=r.layer.parent,t=i&&"uid"in i&&this._layerViewByLayerId[i?.uid],s=this._activeLayerInfosByLayerViewId[t?.uid];s&&(e.push(r),r.parent=s)})),e.length&&(this._activeLayerInfosWithNoParent.removeMany(e),e.forEach((e=>this._addActiveLayerInfo(e))))}}}_removeActiveLayerInfo(e){const r=e.parent;r?r.children.remove(e):this.activeLayerInfos.remove(e)}_constructLegendElements(e){const r=e.layer;"featureCollections"in r&&r.featureCollections?e.buildLegendElementsForFeatureCollections(r.featureCollections):"featureReduction"in r&&r.featureReduction&&"renderer"in r.featureReduction&&("binning"===r.featureReduction.type||"cluster"===r.featureReduction.type)?e.buildLegendElementsForFeatureReduction(r.featureReduction):"renderer"in r&&r.renderer&&!("sublayers"in r)?e.buildLegendElementsForRenderer(r.renderer):"url"in r&&r.url?e.buildLegendElementsForTools():e.children.forEach((e=>this._constructLegendElements(e)))}};e([o({type:c})],v.prototype,"activeLayerInfos",void 0),e([o()],v.prototype,"basemapLegendVisible",void 0),e([o()],v.prototype,"groundLegendVisible",void 0),e([o()],v.prototype,"hideLayersNotInCurrentView",void 0),e([o()],v.prototype,"keepCacheOnDestroy",void 0),e([o()],v.prototype,"respectLayerVisibility",void 0),e([o()],v.prototype,"layerInfos",void 0),e([o({readOnly:!0})],v.prototype,"state",null),e([o()],v.prototype,"view",void 0),v=e([l("esri.widgets.Legend.LegendViewModel")],v);const I=v;export{I as default};