Skip to content

Commit 04a97e2

Browse files
committed
feat(google-maps): Add map-info-window component
Refactor code so that parent map is injected in info window instead of loading info window in the map.
1 parent 6ef2be9 commit 04a97e2

File tree

8 files changed

+43
-83
lines changed

8 files changed

+43
-83
lines changed

src/google-maps/google-map/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ ng_module(
1414
exclude = ["**/*.spec.ts"],
1515
),
1616
deps = [
17-
"//src/google-maps/map-info-window",
1817
"//src/google-maps/map-marker",
1918
"@npm//@angular/core",
2019
"@npm//@types/googlemaps",
@@ -30,7 +29,6 @@ ng_test_library(
3029
),
3130
deps = [
3231
":google-map",
33-
"//src/google-maps/map-info-window",
3432
"//src/google-maps/map-marker",
3533
"//src/google-maps/testing",
3634
"@npm//@angular/platform-browser",

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {Component} from '@angular/core';
22
import {async, TestBed} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
44

5-
import {MapInfoWindow, MapInfoWindowModule} from '../map-info-window/index';
65
import {MapMarker, MapMarkerModule} from '../map-marker/index';
76
import {
87
createMapConstructorSpy,
@@ -41,7 +40,6 @@ describe('GoogleMap', () => {
4140
TestBed.configureTestingModule({
4241
imports: [
4342
GoogleMapModule,
44-
MapInfoWindowModule,
4543
MapMarkerModule,
4644
],
4745
declarations: [TestApp],
@@ -256,19 +254,6 @@ describe('GoogleMap', () => {
256254

257255
expect(markerComponent._setMap).toHaveBeenCalledWith(mapSpy);
258256
});
259-
260-
it('calls setMap on child info window components', () => {
261-
mapSpy = createMapSpy(DEFAULT_OPTIONS);
262-
createMapConstructorSpy(mapSpy).and.callThrough();
263-
264-
const fixture = TestBed.createComponent(TestApp);
265-
const infoWindowComponent =
266-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
267-
spyOn(infoWindowComponent, '_setMap').and.callThrough();
268-
fixture.detectChanges();
269-
270-
expect(infoWindowComponent._setMap).toHaveBeenCalledWith(mapSpy);
271-
});
272257
});
273258

274259
@Component({
@@ -282,7 +267,6 @@ describe('GoogleMap', () => {
282267
(centerChanged)="handleCenterChanged()"
283268
(mapRightclick)="handleRightclick($event)">
284269
<map-marker></map-marker>
285-
<map-info-window>test content</map-info-window>
286270
</google-map>`,
287271
})
288272
class TestApp {

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
import {BehaviorSubject, combineLatest, Observable, Subject} from 'rxjs';
1616
import {map, shareReplay, take, takeUntil} from 'rxjs/operators';
1717

18-
import {MapInfoWindow} from '../map-info-window/index';
1918
import {MapMarker} from '../map-marker/index';
2019

2120
interface GoogleMapsWindow extends Window {
@@ -180,10 +179,8 @@ export class GoogleMap implements OnChanges, OnInit, AfterContentInit, OnDestroy
180179

181180
@ContentChildren(MapMarker) _markers: QueryList<MapMarker>;
182181

183-
@ContentChildren(MapInfoWindow) _infoWindows: QueryList<MapInfoWindow>;
184-
185182
private _mapEl: HTMLElement;
186-
private _googleMap!: UpdatedGoogleMap;
183+
_googleMap!: UpdatedGoogleMap;
187184

188185
private _googleMapChanges!: Observable<google.maps.Map>;
189186

@@ -230,11 +227,7 @@ export class GoogleMap implements OnChanges, OnInit, AfterContentInit, OnDestroy
230227
for (const marker of this._markers.toArray()) {
231228
marker._setMap(this._googleMap);
232229
}
233-
for (const infoWindow of this._infoWindows.toArray()) {
234-
infoWindow._setMap(this._googleMap);
235-
}
236230
this._watchForMarkerChanges();
237-
this._watchForInfoWindowChanges();
238231
}
239232

240233
ngOnDestroy() {
@@ -478,14 +471,4 @@ export class GoogleMap implements OnChanges, OnInit, AfterContentInit, OnDestroy
478471
}
479472
});
480473
}
481-
482-
private _watchForInfoWindowChanges() {
483-
combineLatest(this._googleMapChanges, this._infoWindows.changes)
484-
.pipe(takeUntil(this._destroy))
485-
.subscribe(([googleMap, infoWindows]) => {
486-
for (let infoWindow of infoWindows) {
487-
infoWindow._setMap(googleMap);
488-
}
489-
});
490-
}
491474
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ng_module(
1616
),
1717
assets = [":map-info-window.css"],
1818
deps = [
19+
"//src/google-maps/google-map",
1920
"//src/google-maps/map-marker",
2021
"@npm//@angular/core",
2122
"@npm//@types/googlemaps",
@@ -36,6 +37,7 @@ ng_test_library(
3637
),
3738
deps = [
3839
":map-info-window",
40+
"//src/google-maps/google-map",
3941
"//src/google-maps/map-marker",
4042
"//src/google-maps/testing",
4143
"@npm//@angular/platform-browser",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:host {
1+
.map-info-window-container {
22
.map-info-window-content {
33
display: none;
44
}

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,36 @@ import {Component} from '@angular/core';
22
import {async, TestBed} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
44

5+
import {DEFAULT_OPTIONS, GoogleMapModule, UpdatedGoogleMap} from '../google-map/index';
56
import {MapMarker} from '../map-marker/index';
67
import {
78
createInfoWindowConstructorSpy,
89
createInfoWindowSpy,
10+
createMapConstructorSpy,
11+
createMapSpy,
912
TestingWindow
1013
} from '../testing/fake-google-map-utils';
1114

1215
import {MapInfoWindow, MapInfoWindowModule} from './index';
1316

1417
describe('MapInfoWindow', () => {
18+
let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>;
19+
1520
beforeEach(async(() => {
1621
TestBed.configureTestingModule({
17-
imports: [MapInfoWindowModule],
22+
imports: [
23+
GoogleMapModule,
24+
MapInfoWindowModule,
25+
],
1826
declarations: [TestApp],
1927
});
2028
}));
2129

2230
beforeEach(() => {
2331
TestBed.compileComponents();
32+
33+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
34+
createMapConstructorSpy(mapSpy).and.callThrough();
2435
});
2536

2637
afterEach(() => {
@@ -34,10 +45,6 @@ describe('MapInfoWindow', () => {
3445
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
3546

3647
const fixture = TestBed.createComponent(TestApp);
37-
const fakeMap = {} as unknown as google.maps.Map;
38-
const infoWindowComponent =
39-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
40-
infoWindowComponent._setMap(fakeMap);
4148
fixture.detectChanges();
4249

4350
expect(infoWindowConstructorSpy).toHaveBeenCalledWith({
@@ -47,17 +54,13 @@ describe('MapInfoWindow', () => {
4754
});
4855

4956
it('sets position', () => {
50-
const fakeMap = {} as unknown as google.maps.Map;
5157
const position: google.maps.LatLngLiteral = {lat: 5, lng: 7};
5258
const infoWindowSpy = createInfoWindowSpy({position});
5359
const infoWindowConstructorSpy =
5460
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
5561

5662
const fixture = TestBed.createComponent(TestApp);
5763
fixture.componentInstance.position = position;
58-
const infoWindowComponent =
59-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
60-
infoWindowComponent._setMap(fakeMap);
6164
fixture.detectChanges();
6265

6366
expect(infoWindowConstructorSpy).toHaveBeenCalledWith({
@@ -67,7 +70,6 @@ describe('MapInfoWindow', () => {
6770
});
6871

6972
it('sets options', () => {
70-
const fakeMap = {} as unknown as google.maps.Map;
7173
const options: google.maps.InfoWindowOptions = {
7274
position: {lat: 3, lng: 5},
7375
maxWidth: 50,
@@ -79,9 +81,6 @@ describe('MapInfoWindow', () => {
7981

8082
const fixture = TestBed.createComponent(TestApp);
8183
fixture.componentInstance.options = options;
82-
const infoWindowComponent =
83-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
84-
infoWindowComponent._setMap(fakeMap);
8584
fixture.detectChanges();
8685

8786
expect(infoWindowConstructorSpy).toHaveBeenCalledWith({
@@ -91,7 +90,6 @@ describe('MapInfoWindow', () => {
9190
});
9291

9392
it('gives preference to position over options', () => {
94-
const fakeMap = {} as unknown as google.maps.Map;
9593
const position: google.maps.LatLngLiteral = {lat: 5, lng: 7};
9694
const options: google.maps.InfoWindowOptions = {
9795
position: {lat: 3, lng: 5},
@@ -105,9 +103,6 @@ describe('MapInfoWindow', () => {
105103
const fixture = TestBed.createComponent(TestApp);
106104
fixture.componentInstance.options = options;
107105
fixture.componentInstance.position = position;
108-
const infoWindowComponent =
109-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
110-
infoWindowComponent._setMap(fakeMap);
111106
fixture.detectChanges();
112107

113108
expect(infoWindowConstructorSpy).toHaveBeenCalledWith({
@@ -117,8 +112,7 @@ describe('MapInfoWindow', () => {
117112
});
118113
});
119114

120-
it('exposes meethods that change the configuration of the info window', () => {
121-
const fakeMap = {} as unknown as google.maps.Map;
115+
it('exposes methods that change the configuration of the info window', () => {
122116
const fakeMarker = {} as unknown as google.maps.Marker;
123117
const fakeMarkerComponent = {_marker: fakeMarker} as unknown as MapMarker;
124118
const infoWindowSpy = createInfoWindowSpy({});
@@ -127,25 +121,22 @@ describe('MapInfoWindow', () => {
127121
const fixture = TestBed.createComponent(TestApp);
128122
const infoWindowComponent =
129123
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
130-
infoWindowComponent._setMap(fakeMap);
131124
fixture.detectChanges();
132125

133126
infoWindowComponent.close();
134127
expect(infoWindowSpy.close).toHaveBeenCalled();
135128

136129
infoWindowComponent.open(fakeMarkerComponent);
137-
expect(infoWindowSpy.open).toHaveBeenCalledWith(fakeMap, fakeMarker);
130+
expect(infoWindowSpy.open).toHaveBeenCalledWith(mapSpy, fakeMarker);
138131
});
139132

140133
it('exposes methods that provide information about the info window', () => {
141-
const fakeMap = {} as unknown as google.maps.Map;
142134
const infoWindowSpy = createInfoWindowSpy({});
143135
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
144136

145137
const fixture = TestBed.createComponent(TestApp);
146138
const infoWindowComponent =
147139
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
148-
infoWindowComponent._setMap(fakeMap);
149140
fixture.detectChanges();
150141

151142
infoWindowSpy.getContent.and.returnValue('test content');
@@ -159,14 +150,10 @@ describe('MapInfoWindow', () => {
159150
});
160151

161152
it('initializes info window event handlers', () => {
162-
const fakeMap = {} as unknown as google.maps.Map;
163153
const infoWindowSpy = createInfoWindowSpy({});
164154
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
165155

166156
const fixture = TestBed.createComponent(TestApp);
167-
const infoWindowComponent =
168-
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
169-
infoWindowComponent._setMap(fakeMap);
170157
fixture.detectChanges();
171158

172159
expect(infoWindowSpy.addListener).toHaveBeenCalledWith('closeclick', jasmine.any(Function));
@@ -182,9 +169,13 @@ describe('MapInfoWindow', () => {
182169

183170
@Component({
184171
selector: 'test-app',
185-
template: `<map-info-window [position]="position"
186-
[options]="options"
187-
(closeclick)="handleClose()">test content</map-info-window>`,
172+
template: `<google-map>
173+
<map-info-window [position]="position"
174+
[options]="options"
175+
(closeclick)="handleClose()">
176+
test content
177+
</map-info-window>
178+
</google-map>`,
188179
})
189180
class TestApp {
190181
position?: google.maps.LatLngLiteral;

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import {BehaviorSubject, combineLatest, Observable, ReplaySubject, Subject} from 'rxjs';
1212
import {map, takeUntil} from 'rxjs/operators';
1313

14+
import {GoogleMap} from '../google-map/index';
1415
import {MapMarker} from '../map-marker/index';
1516

1617
/**
@@ -19,8 +20,10 @@ import {MapMarker} from '../map-marker/index';
1920
*/
2021
@Component({
2122
selector: 'map-info-window',
22-
template: `<div #infoWindowContent class="map-info-window-content">
23-
<ng-content></ng-content>
23+
template: `<div class="map-info-window-container">
24+
<div #infoWindowContent class="map-info-window-content">
25+
<ng-content></ng-content>
26+
</div>
2427
</div>`,
2528
styleUrls: ['map-info-window.css'],
2629
})
@@ -81,9 +84,10 @@ export class MapInfoWindow implements OnInit, OnDestroy {
8184

8285
private readonly _destroy = new Subject<void>();
8386

84-
private _map?: google.maps.Map;
8587
private _infoWindow?: google.maps.InfoWindow;
8688

89+
constructor(private readonly googleMap: GoogleMap) {}
90+
8791
ngOnInit() {
8892
this._combineOptions().pipe(takeUntil(this._destroy)).subscribe(options => {
8993
if (this._infoWindow) {
@@ -144,14 +148,8 @@ export class MapInfoWindow implements OnInit, OnDestroy {
144148
*/
145149
open(anchor?: MapMarker) {
146150
const marker = anchor ? anchor._marker : undefined;
147-
if (this._map) {
148-
this._infoWindow!.open(this._map, marker);
149-
}
150-
}
151-
152-
_setMap(googleMap: google.maps.Map) {
153-
if (!this._map) {
154-
this._map = googleMap;
151+
if (this.googleMap._googleMap) {
152+
this._infoWindow!.open(this.googleMap._googleMap, marker);
155153
}
156154
}
157155

src/google-maps/testing/fake-google-map-utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ export function createInfoWindowConstructorSpy(
8686
return infoWindowSpy;
8787
});
8888
const testingWindow: TestingWindow = window;
89-
testingWindow.google = {
90-
maps: {
91-
'InfoWindow': infoWindowConstructorSpy,
92-
},
93-
};
89+
if (testingWindow.google && testingWindow.google.maps) {
90+
testingWindow.google.maps['InfoWindow'] = infoWindowConstructorSpy;
91+
} else {
92+
testingWindow.google = {
93+
maps: {
94+
'InfoWindow': infoWindowConstructorSpy,
95+
},
96+
};
97+
}
9498
return infoWindowConstructorSpy;
9599
}

0 commit comments

Comments
 (0)