Skip to content

Commit 224de73

Browse files
authored
feat(google-maps): add event emitter for gm_authFailure callback (#22953)
1 parent 9047985 commit 224de73

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

src/dev-app/google-map/google-map-demo.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
width="750px"
44
[center]="center"
55
[zoom]="zoom"
6+
(authFailure)="authFailure()"
67
(mapClick)="handleClick($event)"
78
(mapMousemove)="handleMove($event)"
89
(mapRightclick)="handleRightclick()"

src/dev-app/google-map/google-map-demo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export class GoogleMapDemo {
107107

108108
constructor(private readonly _mapDirectionsService: MapDirectionsService) {}
109109

110+
authFailure() {
111+
console.log('Auth failure event emitted');
112+
}
113+
110114
handleClick(event: google.maps.MapMouseEvent) {
111115
this.markerPositions.push(event.latLng.toJSON());
112116
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('GoogleMap', () => {
3939

4040
afterEach(() => {
4141
(window.google as any) = undefined;
42+
(window as any).gm_authFailure = undefined;
4243
});
4344

4445
it('throws an error is the Google Maps JavaScript API was not loaded', () => {
@@ -358,6 +359,28 @@ describe('GoogleMap', () => {
358359
expect(mapConstructorSpy.calls.mostRecent()?.args[1].mapTypeId).toBe('satellite');
359360
});
360361

362+
it('should emit authFailure event when window.gm_authFailure is called', () => {
363+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
364+
mapConstructorSpy = createMapConstructorSpy(mapSpy);
365+
366+
expect((window as any).gm_authFailure).toBeUndefined();
367+
368+
const createFixture = () => {
369+
const fixture = TestBed.createComponent(TestApp);
370+
fixture.detectChanges();
371+
spyOn(fixture.componentInstance.map.authFailure, 'emit');
372+
return fixture;
373+
};
374+
375+
const fixture1 = createFixture();
376+
const fixture2 = createFixture();
377+
378+
expect((window as any).gm_authFailure).toBeDefined();
379+
(window as any).gm_authFailure();
380+
381+
expect(fixture1.componentInstance.map.authFailure.emit).toHaveBeenCalled();
382+
expect(fixture2.componentInstance.map.authFailure.emit).toHaveBeenCalled();
383+
});
361384
});
362385

363386
@Component({

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import {
2323
PLATFORM_ID,
2424
NgZone,
2525
SimpleChanges,
26+
EventEmitter,
2627
} from '@angular/core';
2728
import {isPlatformBrowser} from '@angular/common';
2829
import {Observable} from 'rxjs';
2930
import {MapEventManager} from '../map-event-manager';
3031

3132
interface GoogleMapsWindow extends Window {
33+
gm_authFailure?: () => void;
3234
google?: typeof google;
3335
}
3436

@@ -60,6 +62,7 @@ export const DEFAULT_WIDTH = '500px';
6062
export class GoogleMap implements OnChanges, OnInit, OnDestroy {
6163
private _eventManager: MapEventManager = new MapEventManager(this._ngZone);
6264
private _mapEl: HTMLElement;
65+
private _existingAuthFailureCallback: GoogleMapsWindow['gm_authFailure'];
6366

6467
/**
6568
* The underlying google.maps.Map object
@@ -101,6 +104,12 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
101104
}
102105
private _options = DEFAULT_OPTIONS;
103106

107+
/**
108+
* See
109+
* https://developers.google.com/maps/documentation/javascript/events#auth-errors
110+
*/
111+
@Output() readonly authFailure: EventEmitter<void> = new EventEmitter<void>();
112+
104113
/**
105114
* See
106115
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.bounds_changed
@@ -245,6 +254,14 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
245254
'https://developers.google.com/maps/documentation/javascript/' +
246255
'tutorial#Loading_the_Maps_API');
247256
}
257+
258+
this._existingAuthFailureCallback = googleMapsWindow.gm_authFailure;
259+
googleMapsWindow.gm_authFailure = () => {
260+
if (this._existingAuthFailureCallback) {
261+
this._existingAuthFailureCallback();
262+
}
263+
this.authFailure.emit();
264+
};
248265
}
249266
}
250267

@@ -293,6 +310,11 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
293310

294311
ngOnDestroy() {
295312
this._eventManager.destroy();
313+
314+
if (this._isBrowser) {
315+
const googleMapsWindow: GoogleMapsWindow = window;
316+
googleMapsWindow.gm_authFailure = this._existingAuthFailureCallback;
317+
}
296318
}
297319

298320
/**

tools/public_api_guard/google-maps/google-maps.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
22
_isBrowser: boolean;
3+
readonly authFailure: EventEmitter<void>;
34
readonly boundsChanged: Observable<void>;
45
set center(center: google.maps.LatLngLiteral | google.maps.LatLng);
56
readonly centerChanged: Observable<void>;
@@ -46,7 +47,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
4647
panBy(x: number, y: number): void;
4748
panTo(latLng: google.maps.LatLng | google.maps.LatLngLiteral): void;
4849
panToBounds(latLngBounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral, padding?: number | google.maps.Padding): void;
49-
static ɵcmp: i0.ɵɵComponentDeclaration<GoogleMap, "google-map", ["googleMap"], { "height": "height"; "width": "width"; "mapTypeId": "mapTypeId"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never, ["*"]>;
50+
static ɵcmp: i0.ɵɵComponentDeclaration<GoogleMap, "google-map", ["googleMap"], { "height": "height"; "width": "width"; "mapTypeId": "mapTypeId"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "authFailure": "authFailure"; "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never, ["*"]>;
5051
static ɵfac: i0.ɵɵFactoryDeclaration<GoogleMap, never>;
5152
}
5253

0 commit comments

Comments
 (0)