import { FiniteFaultOverlaysPipe } from './finite-fault-overlays.pipe' ; import { DyfiOverlaysPipe } from '@shared/dyfi-overlays.pipe' ; import { Pipe , PipeTransform } from '@angular/core' ; import { ParamMap } from '@angular/router' ; import * as L from 'leaflet' ; import { Event } from '../event' ; import { GroundFailureOverlaysPipe } from '@shared/ground-failure-overlays.pipe' ; import { RegionInfoOverlaysPipe } from '@shared/region-info-overlays.pipe' ; import { ShakemapOverlaysPipe } from '@shared/shakemap-overlays.pipe' ; import { getUnique } from '../unique' ; import { LandscanPopulationOverlay } from './map-overlay/landscan-population-overlay' ; import { TectonicPlatesOverlay } from './map-overlay/tectonic-plates-overlay' ; import { USFaultsOverlay } from './map-overlay/us-faults-overlay' ; @ Pipe ( { name : 'interactiveMapOverlays' } ) export class InteractiveMapOverlaysPipe implements PipeTransform { defaultOverlays : any = { epicenter : true , 'shakemap-mmi-contours' : true } ; lastEvent : Event = null ; overlayCache : any = { } ; overlayFactory : any = { dyfi : new DyfiOverlaysPipe ( ) , 'finite-fault' : new FiniteFaultOverlaysPipe ( ) , 'ground-failure' : new GroundFailureOverlaysPipe ( ) , origin : new RegionInfoOverlaysPipe ( ) , shakemap : new ShakemapOverlaysPipe ( ) } ; overlayFactoryOrder = [ 'origin' , 'shakemap' , 'ground-failure' , 'dyfi' , 'finite-fault' ] ; staticOverlays : L . Layer [ ] = [ new LandscanPopulationOverlay ( ) , new TectonicPlatesOverlay ( ) , new USFaultsOverlay ( ) ] ; param event param params param type return { Array < L . Layer > } getOverlays ( event , params , type ) { const product = this . getProduct ( event , params , type ) ; const cache = this . overlayCache [ type ] || { overlays : [ ] , product : null } ; if ( cache . product !== product ) { cache . product = product ; cache . overlays = this . overlayFactory [ type ] . transform ( product ) ; this . overlayCache [ type ] = cache ; } return cache . overlays ; } param event param params param type return { any } getProduct ( event , params , type ) { let code , source ; if ( params ) { code = params . get ( type + '-code' ) ; source = params . get ( type + '-source' ) ; } return event . getProduct ( type , source , code ) ; } param overlays param params setEnabled ( overlays , params ) { overlays . forEach ( overlay => { const enabledParam = params ? params . get ( overlay . id ) : false ; if ( enabledParam ) { overlay . enabled = enabledParam === 'true' ; } else { overlay . enabled = this . defaultOverlays [ overlay . id ] ; } } ) ; } param event param params return { any } transform ( event , params = null ) { if ( this . lastEvent !== event ) { this . lastEvent = event ; this . overlayCache = { } ; } if ( ! event || ! event . id ) { return [ ] ; } let overlays = [ ] ; this . overlayFactoryOrder . forEach ( type => { overlays . push ( ... this . getOverlays ( event , params , type ) ) ; } ) ; overlays . push ( ... this . staticOverlays ) ; overlays = getUnique ( overlays ) ; this . setEnabled ( overlays , params ) ; return overlays ; } }