-
Notifications
You must be signed in to change notification settings - Fork 817
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SebmGoogleMapInfoWindow): Basic support
- Loading branch information
1 parent
a5b909a
commit a3df794
Showing
9 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import {SebmGoogleMap} from './directives/google-map'; | ||
import {SebmGoogleMapMarker} from './directives/google-map-marker'; | ||
import {SebmGoogleMapInfoWindow} from './directives/google-map-info-window'; | ||
|
||
export const ANGULAR2_GOOGLE_MAPS_DIRECTIVES: any[] = [SebmGoogleMap, SebmGoogleMapMarker]; | ||
export const ANGULAR2_GOOGLE_MAPS_DIRECTIVES: any[] = | ||
[SebmGoogleMap, SebmGoogleMapMarker, SebmGoogleMapInfoWindow]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export {SebmGoogleMap} from './directives/google-map'; | ||
export {SebmGoogleMapMarker} from './directives/google-map-marker'; | ||
export {SebmGoogleMapInfoWindow} from './directives/google-map-info-window'; | ||
export {ANGULAR2_GOOGLE_MAPS_DIRECTIVES} from './directives-const'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import {Component, SimpleChange, OnDestroy, OnChanges, ElementRef} from 'angular2/core'; | ||
import {InfoWindowManager} from '../services/info-window-manager'; | ||
import {SebmGoogleMapMarker} from './google-map-marker'; | ||
|
||
let infoWindowId = 0; | ||
|
||
/** | ||
* SebmGoogleMapInfoWindow renders a info window inside a {@link SebmGoogleMapMarker} or standalone. | ||
* | ||
* ### Example | ||
* ```typescript | ||
* import {Component} from 'angular2/core'; | ||
* import {SebmGoogleMap, SebmGoogleMapMarker, SebmGoogleMapInfoWindow} from | ||
* 'angular2-google-maps/core'; | ||
* | ||
* @Component({ | ||
* selector: 'my-map-cmp', | ||
* directives: [SebmGoogleMap, SebmGoogleMapMarker, SebmGoogleMapInfoWindow], | ||
* styles: [` | ||
* .sebm-google-map-container { | ||
* height: 300px; | ||
* } | ||
* `], | ||
* template: ` | ||
* <sebm-google-map [latitude]="lat" [longitude]="lng" [zoom]="zoom"> | ||
* <sebm-google-map-marker [latitude]="lat" [longitude]="lng" [label]="'M'"> | ||
* <sebm-google-map-info-window [disableAutoPan]="true"> | ||
* Hi, this is the content of the <strong>info window</strong> | ||
* </sebm-google-map-info-window> | ||
* </sebm-google-map-marker> | ||
* </sebm-google-map> | ||
* ` | ||
* }) | ||
* ``` | ||
*/ | ||
@Component({ | ||
selector: 'sebm-google-map-info-window', | ||
inputs: ['latitude', 'longitude', 'disableAutoPan'], | ||
template: ` | ||
<div class='sebm-google-map-info-window-content'> | ||
<ng-content></ng-content> | ||
</div> | ||
` | ||
}) | ||
export class SebmGoogleMapInfoWindow implements OnDestroy, | ||
OnChanges { | ||
/** | ||
* The latitude position of the info window (only usefull if you use it ouside of a {@link | ||
* SebmGoogleMapMarker}). | ||
*/ | ||
latitude: number; | ||
|
||
/** | ||
* The longitude position of the info window (only usefull if you use it ouside of a {@link | ||
* SebmGoogleMapMarker}). | ||
*/ | ||
longitude: number; | ||
|
||
/** | ||
* Disable auto-pan on open. By default, the info window will pan the map so that it is fully | ||
* visible when it opens. | ||
*/ | ||
disableAutoPan: boolean; | ||
|
||
/** | ||
* All InfoWindows are displayed on the map in order of their zIndex, with higher values | ||
* displaying in front of InfoWindows with lower values. By default, InfoWindows are displayed | ||
* according to their latitude, with InfoWindows of lower latitudes appearing in front of | ||
* InfoWindows at higher latitudes. InfoWindows are always displayed in front of markers. | ||
*/ | ||
zIndex: number; | ||
|
||
/** | ||
* Maximum width of the infowindow, regardless of content's width. This value is only considered | ||
* if it is set before a call to open. To change the maximum width when changing content, call | ||
* close, update maxWidth, and then open. | ||
*/ | ||
maxWidth: number; | ||
|
||
/** | ||
* Holds the marker that is the host of the info window (if available) | ||
*/ | ||
hostMarker: SebmGoogleMapMarker; | ||
|
||
/** | ||
* Holds the native element that is used for the info window content. | ||
*/ | ||
content: Node; | ||
|
||
private static _infoWindowOptionsInputs: string[] = ['disableAutoPan', 'maxWidth']; | ||
private _infoWindowAddedToManager: boolean = false; | ||
private _id: string = (infoWindowId++).toString(); | ||
|
||
constructor(private _infoWindowManager: InfoWindowManager, private _el: ElementRef) {} | ||
|
||
ngOnInit() { | ||
this.content = this._el.nativeElement.querySelector('.sebm-google-map-info-window-content'); | ||
this._addToManager(); | ||
} | ||
|
||
/** @internal */ | ||
ngOnChanges(changes: {[key: string]: SimpleChange}) { | ||
this._addToManager(); | ||
if ((changes['latitude'] || changes['longitude']) && typeof this.latitude === 'number' && | ||
typeof this.longitude === 'number') { | ||
this._infoWindowManager.setPosition(this); | ||
} | ||
if (changes['zIndex']) { | ||
this._infoWindowManager.setZIndex(this); | ||
} | ||
this._setInfoWindowOptions(changes); | ||
} | ||
|
||
private _setInfoWindowOptions(changes: {[key: string]: SimpleChange}) { | ||
let options: {[propName: string]: any} = {}; | ||
let optionKeys = Object.keys(changes).filter( | ||
k => SebmGoogleMapInfoWindow._infoWindowOptionsInputs.indexOf(k) !== -1); | ||
optionKeys.forEach((k) => { options[k] = changes[k].currentValue; }); | ||
this._infoWindowManager.setOptions(this, options); | ||
} | ||
|
||
private _addToManager() { | ||
if (!this._infoWindowAddedToManager) { | ||
this._infoWindowAddedToManager = true; | ||
this._infoWindowManager.addInfoWindow(this); | ||
} | ||
} | ||
|
||
/** | ||
* Opens the info window. | ||
*/ | ||
open(): Promise<void> { return this._infoWindowManager.open(this); } | ||
|
||
/** | ||
* Closes the info window. | ||
*/ | ||
close(): Promise<void> { return this._infoWindowManager.close(this); } | ||
|
||
/** @internal */ | ||
id(): string { return this._id; } | ||
|
||
/** @internal */ | ||
toString(): string { return 'SebmGoogleMapInfoWindow-' + this._id.toString(); } | ||
|
||
/** @internal */ | ||
ngOnDestroy() { this._infoWindowManager.deleteInfoWindow(this); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {Injectable, NgZone} from 'angular2/core'; | ||
import {SebmGoogleMapInfoWindow} from '../directives/google-map-info-window'; | ||
import {GoogleMapsAPIWrapper} from './google-maps-api-wrapper'; | ||
import {MarkerManager} from './marker-manager'; | ||
import {InfoWindow, InfoWindowOptions} from './google-maps-types'; | ||
|
||
@Injectable() | ||
export class InfoWindowManager { | ||
private _infoWindows: Map<SebmGoogleMapInfoWindow, Promise<InfoWindow>> = | ||
new Map<SebmGoogleMapInfoWindow, Promise<InfoWindow>>(); | ||
|
||
constructor( | ||
private _mapsWrapper: GoogleMapsAPIWrapper, private _zone: NgZone, | ||
private _markerManager: MarkerManager) {} | ||
|
||
deleteInfoWindow(infoWindow: SebmGoogleMapInfoWindow): Promise<void> { | ||
const iWindow = this._infoWindows.get(infoWindow); | ||
if (iWindow == null) { | ||
// info window already deleted | ||
return Promise.resolve(); | ||
} | ||
return iWindow.then((i: InfoWindow) => { | ||
return this._zone.run(() => { | ||
i.close(); | ||
this._infoWindows.delete(infoWindow); | ||
}); | ||
}); | ||
} | ||
|
||
setPosition(infoWindow: SebmGoogleMapInfoWindow): Promise<void> { | ||
return this._infoWindows.get(infoWindow).then((i: InfoWindow) => i.setPosition({ | ||
lat: infoWindow.latitude, | ||
lng: infoWindow.longitude | ||
})); | ||
} | ||
|
||
setZIndex(infoWindow: SebmGoogleMapInfoWindow): Promise<void> { | ||
return this._infoWindows.get(infoWindow) | ||
.then((i: InfoWindow) => i.setZIndex(infoWindow.zIndex)); | ||
} | ||
|
||
open(infoWindow: SebmGoogleMapInfoWindow): Promise<void> { | ||
return this._infoWindows.get(infoWindow).then((w) => { | ||
if (infoWindow.hostMarker != null) { | ||
return this._markerManager.getNativeMarker(infoWindow.hostMarker).then((marker) => { | ||
return this._mapsWrapper.getMap().then((map) => w.open(map, marker)); | ||
}); | ||
} | ||
return this._mapsWrapper.getMap().then((map) => w.open(map)); | ||
}); | ||
} | ||
|
||
close(infoWindow: SebmGoogleMapInfoWindow): Promise<void> { | ||
return this._infoWindows.get(infoWindow).then((w) => w.close()); | ||
} | ||
|
||
setOptions(infoWindow: SebmGoogleMapInfoWindow, options: InfoWindowOptions) { | ||
return this._infoWindows.get(infoWindow).then((i: InfoWindow) => i.setOptions(options)); | ||
} | ||
|
||
addInfoWindow(infoWindow: SebmGoogleMapInfoWindow) { | ||
const options: InfoWindowOptions = { | ||
content: infoWindow.content, | ||
}; | ||
if (typeof infoWindow.latitude === 'number' && typeof infoWindow.longitude === 'number') { | ||
options.position = {lat: infoWindow.latitude, lng: infoWindow.longitude}; | ||
} | ||
const infoWindowPromise = this._mapsWrapper.createInfoWindow(options); | ||
this._infoWindows.set(infoWindow, infoWindowPromise); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters