import { Injectable } from '@angular/core' ; import { ImageryCommunicatorService , IImageryMapPosition , IMapInstanceChanged , IMapSettings } from '@ansyn/imagery' ; import { Store } from '@ngrx/store' ; import { Observable } from 'rxjs' ; import { LogMessageFromImagery , MapInstanceChangedAction , PositionChangedAction , } from '../actions/map.actions' ; import { LayoutKey } from '../models/maps-layout' ; import { IMapState , selectLayout , selectMapsList } from '../reducers/map.reducer' ; @ Injectable ( { providedIn : 'root' } ) export class MapFacadeService { subscribers : { [ key ] : any [ ] ; } = { } ; mapsList$ = this . store . select ( selectMapsList ) ; mapsList : IMapSettings [ ] = [ ] ; layout : LayoutKey ; layout$ = this . store . select ( selectLayout ) ; constructor ( protected store , protected imageryCommunicatorService ) { ( < Observable < any > > this . mapsList$ ) . subscribe ( ( mapsList ) => this . mapsList = mapsList ) ; ( < Observable < any > > this . layout$ ) . subscribe ( ( layout ) => this . layout = layout ) ; } static isOverlayGeoRegistered ( overlay ) { if ( ! overlay ) { return true ; } return overlay . isGeoRegistered !== 'notGeoRegistered' ; } static activeMap ( mapState ) { return mapState . entities [ mapState . activeMapId ] ; } static mapById ( mapsList , mapId ) { return Boolean ( mapsList ) ? mapsList . find ( ( { id } ) => id === mapId ) : undefined ; } initEmitters ( id ) { this . removeEmitters ( id ) ; const communicator = this . imageryCommunicatorService . provide ( id ) ; const communicatorSubscribers = [ ] ; communicatorSubscribers . push ( communicator . positionChanged . subscribe ( ( position ) => this . positionChanged ( { id : communicator . id , position } ) ) , communicator . mapInstanceChanged . subscribe ( this . mapInstanceChanged . bind ( this ) ) , communicator . logMessagesEmitter . subscribe ( ( message ) => { this . store . dispatch ( new LogMessageFromImagery ( message ) ) ; } ) ) ; this . subscribers [ id ] = communicatorSubscribers ; } removeEmitters ( id ) { if ( this . subscribers [ id ] ) { this . subscribers [ id ] . forEach ( ( subscriber ) => subscriber . unsubscribe ( ) ) ; delete this . subscribers [ id ] ; } } mapInstanceChanged ( $event ) { this . store . dispatch ( new MapInstanceChangedAction ( $event ) ) ; } positionChanged ( $event ) { if ( Boolean ( this . mapsList ) ) { const mapInstance = < IMapSettings > MapFacadeService . mapById ( this . mapsList , $event . id ) ; this . store . dispatch ( new PositionChangedAction ( { ... $event , mapInstance } ) ) ; } } }