import { EventEmitter , SimpleChanges , Output , OnInit , OnChanges , OnDestroy } from '@angular/core' ; import { OptionBuilder } from '../services/option-builder' ; import { NguiMap } from '../services/ngui-map' ; import { NguiMapComponent } from '../components/ngui-map.component' ; import { missingLibraryError } from '../services/util' ; export abstract class BaseMapDirective implements OnInit , OnChanges , OnDestroy { @ Output ( ) initialized$ : EventEmitter < any > = new EventEmitter ( ) ; public mapObject : any ; public objectOptions : any ; public nguiMap : NguiMap ; public optionBuilder : OptionBuilder ; public libraryName : string ; protected _subscriptions = [ ] ; constructor ( protected nguiMapComponent , public mapObjectName , protected inputs , protected outputs ) { this . nguiMap = this . nguiMapComponent [ 'nguiMap' ] ; this . optionBuilder = this . nguiMapComponent [ 'optionBuilder' ] ; this . outputs . forEach ( output => this [ output ] = new EventEmitter ( ) ) ; this . mapObjectName = mapObjectName ; } ngOnInit ( ) { if ( this . nguiMapComponent . mapIdledOnce ) { this . initialize ( ) ; } else { this . nguiMapComponent . mapReady$ . subscribe ( map => this . initialize ( ) ) ; } } initialize ( ) { this . objectOptions = this . optionBuilder . googlizeAllInputs ( this . inputs , this ) ; console . log ( this . mapObjectName , 'initialization options' , this . objectOptions ) ; typeof this . objectOptions . position === 'string' && ( delete this . objectOptions . position ) ; typeof this . objectOptions . center === 'string' && ( delete this . objectOptions . center ) ; if ( this . libraryName ) { if ( ! google . maps [ this . libraryName ] ) { throw missingLibraryError ( this . mapObjectName , this . libraryName ) ; } this . mapObject = new google . maps [ this . libraryName ] [ this . mapObjectName ] ( this . objectOptions ) ; } else { this . mapObject = new google . maps [ this . mapObjectName ] ( this . objectOptions ) ; } this . mapObject . setMap ( this . nguiMapComponent . map ) ; this . mapObject [ 'mapObjectName' ] = this . mapObjectName ; this . mapObject [ 'nguiMapComponent' ] = this . nguiMapComponent ; this . nguiMap . setObjectEvents ( this . outputs , this , 'mapObject' ) ; this . nguiMapComponent . addToMapObjectGroup ( this . mapObjectName , this . mapObject ) ; this . initialized$ . emit ( this . mapObject ) ; } ngOnChanges ( changes ) { console . log ( this . mapObjectName , 'objectOptions are changed' , changes ) ; this . nguiMap . updateGoogleObject ( this . mapObject , changes ) ; } ngOnDestroy ( ) { this . _subscriptions . map ( subscription => subscription . unsubscribe ( ) ) ; this . nguiMapComponent . removeFromMapObjectGroup ( this . mapObjectName , this . mapObject ) ; if ( this . mapObject ) { this . nguiMap . clearObjectEvents ( this . outputs , this , 'mapObject' ) ; } } }