import { EventEmitter , KeyValueChanges , NgZone } from '@angular/core' ; import { control , Control , Layer } from 'leaflet' ; import { LeafletControlLayersChanges } from './leaflet-control-layers-changes.model' ; export class LeafletControlLayersWrapper { protected layersControl : Control . Layers ; protected layersControlReady : EventEmitter < Control . Layers > ; constructor ( private zone , layersControlReady ) { this . layersControlReady = layersControlReady ; } getLayersControl ( ) { return this . layersControl ; } init ( controlConfig , controlOptions ) { const baseLayers = controlConfig . baseLayers || { } ; const overlays = controlConfig . overlays || { } ; this . zone . runOutsideAngular ( ( ) => { this . layersControl = control . layers ( baseLayers , overlays , controlOptions ) ; } ) ; this . layersControlReady . emit ( this . layersControl ) ; return this . layersControl ; } applyBaseLayerChanges ( changes ) { let results = new LeafletControlLayersChanges ( ) ; if ( null != this . layersControl ) { results = this . applyChanges ( changes , this . layersControl . addBaseLayer ) ; } return results ; } applyOverlayChanges ( changes ) { let results = new LeafletControlLayersChanges ( ) ; if ( null != this . layersControl ) { results = this . applyChanges ( changes , this . layersControl . addOverlay ) ; } return results ; } private applyChanges ( changes , addFn ) { const results = new LeafletControlLayersChanges ( ) ; if ( null != changes ) { this . zone . runOutsideAngular ( ( ) => { changes . forEachChangedItem ( ( c ) => { this . layersControl . removeLayer ( c . previousValue ) ; addFn . call ( this . layersControl , c . currentValue , c . key ) ; results . layersChanged ++ ; } ) ; changes . forEachRemovedItem ( ( c ) => { this . layersControl . removeLayer ( c . previousValue ) ; results . layersRemoved ++ ; } ) ; changes . forEachAddedItem ( ( c ) => { addFn . call ( this . layersControl , c . currentValue , c . key ) ; results . layersAdded ++ ; } ) ; } ) ; } return results ; } }