import { Directive , EventEmitter , Input , NgZone , OnChanges , OnDestroy , OnInit , Output , SimpleChange } from '@angular/core' ; import { Layer , LeafletEvent } from 'leaflet' ; import { LeafletDirective } from '../core/leaflet.directive' ; import { LeafletDirectiveWrapper } from '../core/leaflet.directive.wrapper' ; import { LeafletUtil } from '../core/leaflet.util' ; /**
 * Layer directive
 *
 * This directive is used to directly control a single map layer. The purpose of this directive is to
 * be used as part of a child structural directive of the map element.
 *
 */ @ Directive ( { selector : '[leafletLayer]' } ) export class LeafletLayerDirective implements OnChanges , OnDestroy , OnInit { @ Input ( 'leafletLayer' ) layer : Layer ; @ Output ( 'leafletLayerAdd' ) onAdd = new EventEmitter < LeafletEvent > ( ) ; @ Output ( 'leafletLayerRemove' ) onRemove = new EventEmitter < LeafletEvent > ( ) ; private onAddLayerHandler : any ; private onRemoveLayerHandler : any ; private leafletDirective : LeafletDirectiveWrapper ; constructor ( leafletDirective , private zone ) { this . leafletDirective = new LeafletDirectiveWrapper ( leafletDirective ) ; } ngOnInit ( ) { this . leafletDirective . init ( ) ; } ngOnDestroy ( ) { if ( null != this . layer ) { this . removeLayerEventListeners ( this . layer ) ; this . layer . remove ( ) ; } } ngOnChanges ( changes ) { if ( changes [ 'layer' ] ) { const p = changes [ 'layer' ] . previousValue ; const n = changes [ 'layer' ] . currentValue ; this . zone . runOutsideAngular ( ( ) => { if ( null != p ) { this . removeLayerEventListeners ( p ) ; p . remove ( ) ; } if ( null != n ) { this . addLayerEventListeners ( n ) ; this . leafletDirective . getMap ( ) . addLayer ( n ) ; } } ) ; } } private addLayerEventListeners ( l ) { this . onAddLayerHandler = ( e ) => LeafletUtil . handleEvent ( this . zone , this . onAdd , e ) ; l . on ( 'add' , this . onAddLayerHandler ) ; this . onRemoveLayerHandler = ( e ) => LeafletUtil . handleEvent ( this . zone , this . onRemove , e ) ; l . on ( 'remove' , this . onRemoveLayerHandler ) ; } private removeLayerEventListeners ( l ) { l . off ( 'add' , this . onAddLayerHandler ) ; l . off ( 'remove' , this . onRemoveLayerHandler ) ; } }