Skip to content

Commit ceb3a34

Browse files
committed
feat(google-maps): Add map-info-window directive
Change MapInfoWindow from a component to a directive.
1 parent c2e860d commit ceb3a34

File tree

5 files changed

+19
-51
lines changed

5 files changed

+19
-51
lines changed

src/google-maps/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ ng_module(
99
["**/*.ts"],
1010
exclude = ["**/*.spec.ts"],
1111
),
12-
assets = ["//src/google-maps/map-info-window/assets:map-info-window.css"],
1312
module_name = "@angular/google-maps",
1413
deps = [
1514
"@npm//@angular/core",

src/google-maps/map-info-window/assets/BUILD.bazel

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/google-maps/map-info-window/assets/map-info-window.scss

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/google-maps/map-info-window/map-info-window.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ describe('MapInfoWindow', () => {
119119
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
120120

121121
const fixture = TestBed.createComponent(TestApp);
122-
const infoWindowComponent =
123-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
122+
const infoWindowComponent = fixture.debugElement.query(By.directive(
123+
MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow);
124124
fixture.detectChanges();
125125

126126
infoWindowComponent.close();
@@ -135,8 +135,8 @@ describe('MapInfoWindow', () => {
135135
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
136136

137137
const fixture = TestBed.createComponent(TestApp);
138-
const infoWindowComponent =
139-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
138+
const infoWindowComponent = fixture.debugElement.query(By.directive(
139+
MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow);
140140
fixture.detectChanges();
141141

142142
infoWindowSpy.getContent.and.returnValue('test content');

src/google-maps/map-info-window/map-info-window.ts

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
*/
88

99
import {
10-
ChangeDetectionStrategy,
11-
Component,
10+
Directive,
1211
ElementRef,
1312
EventEmitter,
1413
Input,
1514
OnDestroy,
1615
OnInit,
1716
Output,
18-
ViewChild,
19-
ViewEncapsulation,
2017
} from '@angular/core';
21-
import {BehaviorSubject, combineLatest, Observable, ReplaySubject, Subject} from 'rxjs';
18+
import {BehaviorSubject, combineLatest, Observable, Subject} from 'rxjs';
2219
import {map, takeUntil} from 'rxjs/operators';
2320

2421
import {GoogleMap} from '../google-map/index';
@@ -28,17 +25,9 @@ import {MapMarker} from '../map-marker/index';
2825
* Angular component that renders a Google Maps info window via the Google Maps JavaScript API.
2926
* @see developers.google.com/maps/documentation/javascript/reference/info-window
3027
*/
31-
@Component({
32-
moduleId: module.id,
28+
@Directive({
3329
selector: 'map-info-window',
34-
template: `<div class="map-info-window-container">
35-
<div #infoWindowContent class="map-info-window-content">
36-
<ng-content></ng-content>
37-
</div>
38-
</div>`,
39-
styleUrls: ['./assets/map-info-window.css'],
40-
changeDetection: ChangeDetectionStrategy.OnPush,
41-
encapsulation: ViewEncapsulation.None,
30+
host: {'style': 'display: none'},
4231
})
4332
export class MapInfoWindow implements OnInit, OnDestroy {
4433
@Input()
@@ -84,22 +73,17 @@ export class MapInfoWindow implements OnInit, OnDestroy {
8473
*/
8574
@Output() zindexChanged = new EventEmitter<void>();
8675

87-
@ViewChild('infoWindowContent', {static: false})
88-
set content(content: ElementRef) {
89-
this._content.next(content.nativeElement);
90-
}
91-
9276
private readonly _options = new BehaviorSubject<google.maps.InfoWindowOptions>({});
9377
private readonly _position = new BehaviorSubject<google.maps.LatLngLiteral|undefined>(undefined);
94-
private readonly _content = new ReplaySubject<Node>(1);
9578

9679
private readonly _listeners: google.maps.MapsEventListener[] = [];
9780

9881
private readonly _destroy = new Subject<void>();
9982

10083
private _infoWindow?: google.maps.InfoWindow;
10184

102-
constructor(private readonly googleMap: GoogleMap) {}
85+
constructor(private readonly googleMap: GoogleMap, private _elementRef: ElementRef<HTMLElement>) {
86+
}
10387

10488
ngOnInit() {
10589
this._combineOptions().pipe(takeUntil(this._destroy)).subscribe(options => {
@@ -162,20 +146,20 @@ export class MapInfoWindow implements OnInit, OnDestroy {
162146
open(anchor?: MapMarker) {
163147
const marker = anchor ? anchor._marker : undefined;
164148
if (this.googleMap._googleMap) {
149+
this._elementRef.nativeElement.style.display = '';
165150
this._infoWindow!.open(this.googleMap._googleMap, marker);
166151
}
167152
}
168153

169154
private _combineOptions(): Observable<google.maps.InfoWindowOptions> {
170-
return combineLatest(this._options, this._position, this._content)
171-
.pipe(map(([options, position, content]) => {
172-
const combinedOptions: google.maps.InfoWindowOptions = {
173-
...options,
174-
position: position || options.position,
175-
content,
176-
};
177-
return combinedOptions;
178-
}));
155+
return combineLatest(this._options, this._position).pipe(map(([options, position]) => {
156+
const combinedOptions: google.maps.InfoWindowOptions = {
157+
...options,
158+
position: position || options.position,
159+
content: this._elementRef.nativeElement,
160+
};
161+
return combinedOptions;
162+
}));
179163
}
180164

181165
private _initializeEventHandlers() {

0 commit comments

Comments
 (0)