Skip to content

feat(google-maps): add event emitter for gm_authFailure callback #22953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dev-app/google-map/google-map-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
width="750px"
[center]="center"
[zoom]="zoom"
(authFailure)="authFailure()"
(mapClick)="handleClick($event)"
(mapMousemove)="handleMove($event)"
(mapRightclick)="handleRightclick()"
Expand Down
4 changes: 4 additions & 0 deletions src/dev-app/google-map/google-map-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export class GoogleMapDemo {

constructor(private readonly _mapDirectionsService: MapDirectionsService) {}

authFailure() {
console.log('Auth failure event emitted');
}

handleClick(event: google.maps.MapMouseEvent) {
this.markerPositions.push(event.latLng.toJSON());
}
Expand Down
23 changes: 23 additions & 0 deletions src/google-maps/google-map/google-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('GoogleMap', () => {

afterEach(() => {
(window.google as any) = undefined;
(window as any).gm_authFailure = undefined;
});

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

it('should emit authFailure event when window.gm_authFailure is called', () => {
mapSpy = createMapSpy(DEFAULT_OPTIONS);
mapConstructorSpy = createMapConstructorSpy(mapSpy);

expect((window as any).gm_authFailure).toBeUndefined();

const createFixture = () => {
const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();
spyOn(fixture.componentInstance.map.authFailure, 'emit');
return fixture;
};

const fixture1 = createFixture();
const fixture2 = createFixture();

expect((window as any).gm_authFailure).toBeDefined();
(window as any).gm_authFailure();

expect(fixture1.componentInstance.map.authFailure.emit).toHaveBeenCalled();
expect(fixture2.componentInstance.map.authFailure.emit).toHaveBeenCalled();
});
});

@Component({
Expand Down
22 changes: 22 additions & 0 deletions src/google-maps/google-map/google-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import {
PLATFORM_ID,
NgZone,
SimpleChanges,
EventEmitter,
} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {Observable} from 'rxjs';
import {MapEventManager} from '../map-event-manager';

interface GoogleMapsWindow extends Window {
gm_authFailure?: () => void;
google?: typeof google;
}

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

/**
* The underlying google.maps.Map object
Expand Down Expand Up @@ -101,6 +104,12 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
}
private _options = DEFAULT_OPTIONS;

/**
* See
* https://developers.google.com/maps/documentation/javascript/events#auth-errors
*/
@Output() readonly authFailure: EventEmitter<void> = new EventEmitter<void>();

/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.bounds_changed
Expand Down Expand Up @@ -245,6 +254,14 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
'https://developers.google.com/maps/documentation/javascript/' +
'tutorial#Loading_the_Maps_API');
}

this._existingAuthFailureCallback = googleMapsWindow.gm_authFailure;
googleMapsWindow.gm_authFailure = () => {
if (this._existingAuthFailureCallback) {
this._existingAuthFailureCallback();
}
this.authFailure.emit();
};
}
}

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

ngOnDestroy() {
this._eventManager.destroy();

if (this._isBrowser) {
const googleMapsWindow: GoogleMapsWindow = window;
googleMapsWindow.gm_authFailure = this._existingAuthFailureCallback;
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/google-maps/google-maps.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
_isBrowser: boolean;
readonly authFailure: EventEmitter<void>;
readonly boundsChanged: Observable<void>;
set center(center: google.maps.LatLngLiteral | google.maps.LatLng);
readonly centerChanged: Observable<void>;
Expand Down Expand Up @@ -46,7 +47,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
panBy(x: number, y: number): void;
panTo(latLng: google.maps.LatLng | google.maps.LatLngLiteral): void;
panToBounds(latLngBounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral, padding?: number | google.maps.Padding): void;
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, ["*"]>;
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, ["*"]>;
static ɵfac: i0.ɵɵFactoryDeclaration<GoogleMap, never>;
}

Expand Down