Skip to content

Commit

Permalink
fix(InfoWindow): fire infoWindowClose Event
Browse files Browse the repository at this point in the history
Closes #728
Closes #811
  • Loading branch information
sebholstein committed Dec 22, 2016
1 parent 0789a17 commit b669aea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/core/directives/google-map-info-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class SebmGoogleMapInfoWindow implements OnDestroy, OnChanges, OnInit {
this._infoWindowManager.addInfoWindow(this);
this._infoWindowAddedToManager = true;
this._updateOpenState();
this._registerEventListeners();
}

/** @internal */
Expand All @@ -129,8 +130,15 @@ export class SebmGoogleMapInfoWindow implements OnDestroy, OnChanges, OnInit {
this._setInfoWindowOptions(changes);
}

private _registerEventListeners() {
this._infoWindowManager.createEventObservable('closeclick', this).subscribe(() => {
this.isOpen = false;
this.infoWindowClose.emit();
});
}

private _updateOpenState() {
this.isOpen ? this._infoWindowManager.open(this) : this._infoWindowManager.close(this);
this.isOpen ? this.open() : this.close();
}

private _setInfoWindowOptions(changes: {[key: string]: SimpleChange}) {
Expand All @@ -150,7 +158,7 @@ export class SebmGoogleMapInfoWindow implements OnDestroy, OnChanges, OnInit {
* Closes the info window.
*/
close(): Promise<void> {
return this._infoWindowManager.close(this).then(() => { this.infoWindowClose.emit(void 0); });
return this._infoWindowManager.close(this).then(() => { this.infoWindowClose.emit(); });
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/core/services/google-maps-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export interface MapTypeStyler {
weight?: number;
}

export interface InfoWindow {
export interface InfoWindow extends MVCObject {
constructor(opts?: InfoWindowOptions): void;
close(): void;
getContent(): string|Node;
Expand Down
13 changes: 13 additions & 0 deletions src/core/services/managers/info-window-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import {Injectable, NgZone} from '@angular/core';

import {SebmGoogleMapInfoWindow} from '../../directives/google-map-info-window';
Expand Down Expand Up @@ -72,4 +74,15 @@ export class InfoWindowManager {
const infoWindowPromise = this._mapsWrapper.createInfoWindow(options);
this._infoWindows.set(infoWindow, infoWindowPromise);
}

/**
* Creates a Google Maps event listener for the given InfoWindow as an Observable
*/
createEventObservable<T>(eventName: string, infoWindow: SebmGoogleMapInfoWindow): Observable<T> {
return Observable.create((observer: Observer<T>) => {
this._infoWindows.get(infoWindow).then((i: InfoWindow) => {
i.addListener(eventName, (e: T) => this._zone.run(() => observer.next(e)));
});
});
}
}

0 comments on commit b669aea

Please sign in to comment.