import Map from 'ol/Map' ; import { Component } from '@angular/core' ; import { Layer } from 'ol/layer' ; import { Source } from 'ol/source' ; import { HsLanguageService } from '../language/language.service' ; import { HsLayerUtilsService } from '../utils/layer-utils.service' ; import { HsLayoutService } from '../layout/layout.service' ; import { HsLegendDescriptor } from './legend-descriptor.interface' ; import { HsLegendService } from './legend.service' ; import { HsMapService } from '../map/map.service' ; import { HsPanelBaseComponent } from '../layout/panels/panel-base.component' ; import { HsQueuesService } from '../../common/queues/queues.service' ; import { HsSidebarService } from '../sidebar/sidebar.service' ; import { HsUtilsService } from '../utils/utils.service' ; @ Component ( { selector : 'hs-legend' , templateUrl : './legend.component.html' , } ) export class HsLegendComponent extends HsPanelBaseComponent { layerDescriptors = [ ] ; titleSearch = '' ; name = 'legend' ; constructor ( public hsLegendService , public hsMapService , public hsLayerUtilsService , public hsUtilsService , public hsQueuesService , hsLayoutService , hsLanguageService , hsSidebarService ) { super ( hsLayoutService ) ; hsSidebarService . buttons . push ( { panel : 'legend' , module : 'hs.legend' , order : 1 , fits : true , title : ( ) => hsLanguageService . getTranslation ( 'PANEL_HEADER.LEGEND' ) , description : ( ) => hsLanguageService . getTranslation ( 'SIDEBAR.descriptions.LEGEND' ) , icon : 'icon-dotlist' , } ) ; this . hsMapService . loaded ( ) . then ( ( map ) => this . init ( map ) ) ; } param layer async addLayerToLegends ( layer ) { const descriptor = await this . hsLegendService . getLayerLegendDescriptor ( layer ) ; if ( descriptor ) { this . layerDescriptors . push ( descriptor ) ; this . refreshList ( ) ; layer . on ( 'change:visible' , ( e ) => this . layerVisibilityChanged ( e ) ) ; if ( this . hsLayerUtilsService . isLayerWMS ( layer ) ) { layer . getSource ( ) . on ( 'change' , ( e ) => { this . hsUtilsService . debounce ( this . layerSourcePropChanged ( e ) , 100 , false , this ) ; } ) ; } } } rebuildLegends ( ) { this . layerDescriptors = [ ] ; this . buildLegendsForLayers ( this . hsMapService . map ) ; } filterDescriptors ( ) { return this . layerDescriptors ; } legendFilter = ( item ) : boolean => { const r = new RegExp ( this . titleSearch , 'i' ) ; return r . test ( item . title ) ; } ; returns noLayerExists ( ) { const visibleLayers = this . layerDescriptors . filter ( ( check ) => check . visible ) ; return visibleLayers . length == 0 ; } param layer removeLayerFromLegends ( layer ) { for ( let i = 0 ; i < this . layerDescriptors . length ; i ++ ) { if ( this . layerDescriptors [ i ] . lyr == layer ) { this . layerDescriptors . splice ( i , 1 ) ; this . refreshList ( ) ; break ; } } } param map init ( map ) { map . getLayers ( ) . on ( 'add' , ( e ) => this . layerAdded ( e ) ) ; map . getLayers ( ) . on ( 'remove' , ( e ) => { this . removeLayerFromLegends ( e . element ) ; } ) ; this . buildLegendsForLayers ( map ) ; } buildLegendsForLayers ( map ) { map . getLayers ( ) . forEach ( ( lyr ) => { this . layerAdded ( { element : lyr , } ) ; } ) ; } param e layerAdded ( e ) { const que = this . hsQueuesService . ensureQueue ( 'addLayerToLegends' , 3 ) ; que . push ( async ( cb ) => { await this . addLayerToLegends ( e . element ) ; cb ( null ) ; } ) ; } param e layerVisibilityChanged ( e ) { const descriptor = this . findLayerDescriptor ( e . target ) ; if ( descriptor ) { descriptor . visible = e . target . getVisible ( ) ; } } param e layerSourcePropChanged ( e ) { const descriptor = this . findLayerDescriptorBySource ( e . target ) ; if ( descriptor ) { this . hsLegendService . getLayerLegendDescriptor ( descriptor . lyr ) . then ( ( newDescriptor ) => { if ( newDescriptor . subLayerLegends != descriptor . subLayerLegends || newDescriptor . title != descriptor . title ) { this . layerDescriptors [ this . layerDescriptors . indexOf ( descriptor ) ] = newDescriptor ; } } ) ; } } param layer returns findLayerDescriptor ( layer ) { return this . layerDescriptors . find ( ( ld ) => ld . lyr == layer ) ; } param source findLayerDescriptorBySource ( source ) { const found = this . layerDescriptors . filter ( ( ld ) => ld . lyr . getSource ( ) == source ) ; if ( found . length > 0 ) { return found [ 0 ] ; } } refreshList ( ) { this . layerDescriptors = Array . from ( this . layerDescriptors ) ; } }