import { Injectable } from '@angular/core' ; import { Layer } from 'ol/layer' ; import { Source } from 'ol/source' ; import { HsEventBusService } from '../../components/core/event-bus.service' ; import { HsLayerManagerService } from '../../components/layermanager/layermanager.service' ; import { HsMapService } from '../../components/map/map.service' ; import { HsUtilsService } from '../../components/utils/utils.service' ; import { getBase } from '../layer-extensions' ; export class LayerListItem { title : string ; layer : Layer < Source > ; active ? : boolean ; visible ? : boolean ; } @ Injectable ( { providedIn : 'root' , } ) export class HsLayerShiftingService { layersCopy : LayerListItem [ ] = [ ] ; constructor ( public hsMapService , public hsLayerManagerService , public hsUtilsService , public hsEventBusService ) { } private get availableLayers ( ) { return this . hsLayerManagerService . data . layers ; } private get mapLayers ( ) : Layer < Source > [ ] { return this . hsMapService . getLayersArray ( ) . filter ( ( layer ) => getBase ( layer ) !== true ) ; } /**
     * Copies layers from Layermanager layer list for the physical layer list
     */ fillLayers ( ) { if ( ! this . availableLayers ) { return ; } this . layersCopy = this . hsLayerManagerService . sortLayersByZ ( this . availableLayers . map ( ( l ) => { return { title : l . title , layer : l . layer } ; } ) ) ; } param layer moveTo ( layer , target ) { if ( this . hsUtilsService . instOf ( target , LayerListItem ) ) { target = ( target as LayerListItem ) . layer . getZIndex ( ) ; } else if ( this . hsUtilsService . instOf ( target , Layer ) ) { target = ( target as Layer < Source > ) . getZIndex ( ) ; } this . moveAndShift ( this . getOlLayer ( layer ) , target as number ) ; } param providedLayer param preferredZIndex private moveAndShift ( providedLayer , preferredZIndex ) { if ( providedLayer === undefined ) { return ; } if ( providedLayer . getZIndex ( ) != preferredZIndex ) { const indexFrom = providedLayer . getZIndex ( ) ; const indexTo = preferredZIndex ; const incrementValue = indexTo > indexFrom ? - 1 : 1 ; for ( const lyr of this . mapLayers . filter ( ( lyr ) => lyr != providedLayer ) ) { const currentZIndex = lyr . getZIndex ( ) ; if ( ( currentZIndex >= indexFrom && currentZIndex <= indexTo ) || ( currentZIndex <= indexFrom && currentZIndex >= indexTo ) ) { lyr . setZIndex ( lyr . getZIndex ( ) + incrementValue ) ; } } providedLayer . setZIndex ( preferredZIndex ) ; this . hsEventBusService . layerManagerUpdates . next ( providedLayer ) ; } } param layer returns private getOlLayer ( providedLayer ) { if ( this . hsUtilsService . instOf ( providedLayer , Layer ) ) { return providedLayer as Layer < Source > ; } else { return ( providedLayer as LayerListItem ) . layer ; } } returns private zIndexList ( ) { return this . mapLayers . map ( ( lyr ) => lyr . getZIndex ( ) || 0 ) ; } returns getMaxZ ( ) { return Math . max ( ... this . zIndexList ( ) ) ; } returns getMinZ ( ) { return Math . min ( ... this . zIndexList ( ) ) ; } param indexTo param layer private setLayerZIndex ( indexTo , layer ) { const layerSwitchedWith = this . layersCopy [ indexTo ] . layer ; const interactedLayerZIndex = layer . getZIndex ( ) ; layer . setZIndex ( layerSwitchedWith . getZIndex ( ) ) ; layerSwitchedWith . setZIndex ( interactedLayerZIndex ) ; this . hsEventBusService . layerManagerUpdates . next ( layer ) ; } param layer moveToBottom ( layer ) { this . moveAndShift ( this . getOlLayer ( layer ) , this . getMinZ ( ) ) ; } param layer moveToTop ( layer ) { this . moveAndShift ( this . getOlLayer ( layer ) , this . getMaxZ ( ) ) ; } param baseLayer param direction swapSibling ( baseLayer , direction ) { const currentLayerIndex = this . layersCopy . indexOf ( baseLayer ) ; switch ( direction . toLocaleLowerCase ( ) ) { case 'up' : if ( currentLayerIndex != 0 ) { this . setLayerZIndex ( currentLayerIndex - 1 , baseLayer . layer ) ; } break ; case 'down' : if ( currentLayerIndex < this . layersCopy . length - 1 ) { this . setLayerZIndex ( currentLayerIndex + 1 , baseLayer . layer ) ; } break ; default : } } }