import { Injectable } from '@angular/core' ; import { HsLayerDescriptor } from '../layer-descriptor.interface' ; import { HsLayerManagerService } from '../layermanager.service' ; import { HsLayerSelectorService } from './layer-selector.service' ; import { HsLayerUtilsService } from '../../utils/layer-utils.service' ; import { getCachedCapabilities } from '../../../common/layer-extensions' ; export type KeyBooleanDict = { [ key ] : boolean ; } ; @ Injectable ( { providedIn : 'root' , } ) export class HsLayerEditorSublayerService { checkedSubLayers : KeyBooleanDict = { } ; withChildren : KeyBooleanDict = { } ; populatedLayers : Array < any > = [ ] ; withChildrenTmp : KeyBooleanDict = { } ; checkedSubLayersTmp : KeyBooleanDict = { } ; constructor ( public HsLayerManagerService , public HsLayerSelectorService , private HsLayerUtilsService ) { this . HsLayerSelectorService . layerSelected . subscribe ( ( layer ) => { this . resetSublayers ( layer ) ; } ) ; } resetSublayers ( layer ) { if ( this . HsLayerManagerService . currentLayer ) { this . checkedSubLayers = layer . checkedSubLayers ; this . checkedSubLayersTmp = layer . checkedSubLayersTmp ; this . withChildren = layer . withChildren ; this . withChildrenTmp = layer . withChildrenTmp ; } } hasSubLayers ( ) { const subLayers = getCachedCapabilities ( this . HsLayerManagerService . currentLayer . layer ) ?. Layer ; return subLayers != undefined && subLayers . length > 0 ; } getSubLayers ( ) { if ( this . HsLayerManagerService . currentLayer === null ) { return ; } this . populateSubLayers ( ) ; return ( getCachedCapabilities ( this . HsLayerManagerService . currentLayer . layer ) ?. Layer || [ ] ) ; } populateSubLayers ( ) { const wrapper = this . HsLayerManagerService . currentLayer ; const layer = wrapper . layer ; if ( this . populatedLayers . includes ( wrapper . uid ) ) { return ; } const subLayers = getCachedCapabilities ( layer ) ?. Layer ; if ( subLayers ?. length > 0 ) { const visible = layer . getVisible ( ) ; const clone = ( src ) => Object . assign ( { } , src ) ; const toDictionary = ( d , layer ) => ( ( d [ layer . Name ] = visible ) , d ) ; this . populatedLayers . push ( wrapper . uid ) ; const subLayersWithChild = subLayers . filter ( ( sl ) => sl . Layer ) ; const subSubLayers = subLayersWithChild . map ( ( sl ) => sl . Layer ) . flat ( ) ; wrapper . withChildren = subLayersWithChild . reduce ( toDictionary , { } ) ; const leafs = subSubLayers . length > 0 ? subSubLayers : subLayers ; wrapper . checkedSubLayers = leafs . reduce ( toDictionary , { } ) ; this . checkedSubLayers = wrapper . checkedSubLayers ; this . withChildren = wrapper . withChildren ; this . checkedSubLayersTmp = clone ( this . checkedSubLayers ) ; wrapper . checkedSubLayersTmp = this . checkedSubLayersTmp ; this . withChildrenTmp = clone ( this . withChildren ) ; wrapper . withChildrenTmp = this . withChildrenTmp ; if ( ! this . HsLayerManagerService . currentLayer . visible ) { for ( const dict of [ this . checkedSubLayersTmp , this . withChildrenTmp ] ) { Object . keys ( dict ) . forEach ( ( v ) => ( dict [ v ] = true ) ) ; } } } } subLayerSelected ( ) { const layer = this . HsLayerManagerService . currentLayer ; const params = this . HsLayerUtilsService . getLayerParams ( layer . layer ) ; params . LAYERS = Object . keys ( this . checkedSubLayers ) . filter ( ( key ) => this . checkedSubLayers [ key ] && ! this . withChildren [ key ] ) . join ( ',' ) ; if ( params . LAYERS == '' ) { this . HsLayerManagerService . changeLayerVisibility ( ! layer . visible , layer ) ; return ; } if ( layer . visible == false ) { this . HsLayerManagerService . changeLayerVisibility ( ! layer . visible , layer ) ; } this . HsLayerUtilsService . updateLayerParams ( layer . layer , params ) ; } }