From 997aa80b607ebb10ebbb0923cbebdcbb78531972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20M=C3=BCller?= Date: Sat, 11 Jun 2016 23:59:43 +0200 Subject: [PATCH] feat(LazyMapsAPILoader): provide shortcut Now, you can use a shortcut to configure the LazyMapsAPILoader: Before: ```typescript bootstrap(AppComponent, [ ANGULAR2_GOOGLE_MAPS_PROVIDERS, provide(LazyMapsAPILoaderConfig, {useFactory: () => { let config = new LazyMapsAPILoaderConfig(); config.apiKey = 'mykey'; return config; }}) ]) ``` After: ```typescript import {provide} from 'angular2/core'; import {provideLazyMapsAPILoaderConfig} from 'angular2-google-maps/core'; bootstrap(AppComponent, [ GOOGLE_MAPS_PROVIDERS, provideLazyMapsAPILoaderConfig({ apiKey: 'myKey' }) ]) ``` Closes #388 Closes #420 --- src/core/index.ts | 7 +- src/core/services.ts | 2 +- .../maps-api-loader/lazy-maps-api-loader.ts | 142 +++++++++++------- src/core/utils/browser-globals.ts | 4 + .../lazy-maps-api-loader.spec.ts | 41 +++++ 5 files changed, 142 insertions(+), 54 deletions(-) create mode 100644 src/core/utils/browser-globals.ts create mode 100644 test/services/maps-api-loader/lazy-maps-api-loader.spec.ts diff --git a/src/core/index.ts b/src/core/index.ts index d3e4f11dd..ab20929ce 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,13 +1,16 @@ -import {Provider} from '@angular/core'; +import {provide} from '@angular/core'; import {LazyMapsAPILoader} from './services/maps-api-loader/lazy-maps-api-loader'; import {MapsAPILoader} from './services/maps-api-loader/maps-api-loader'; +import {BROWSER_GLOBALS_PROVIDERS} from './utils/browser-globals'; + // main modules export * from './directives'; export * from './services'; export * from './events'; export const GOOGLE_MAPS_PROVIDERS: any[] = [ - new Provider(MapsAPILoader, {useClass: LazyMapsAPILoader}), + ...BROWSER_GLOBALS_PROVIDERS, + provide(MapsAPILoader, {useClass: LazyMapsAPILoader}), ]; diff --git a/src/core/services.ts b/src/core/services.ts index 9bdb89f32..d66172b56 100644 --- a/src/core/services.ts +++ b/src/core/services.ts @@ -1,6 +1,6 @@ export {GoogleMapsAPIWrapper} from './services/google-maps-api-wrapper'; export {InfoWindowManager} from './services/info-window-manager'; -export {GoogleMapsScriptProtocol, LazyMapsAPILoader, LazyMapsAPILoaderConfig} from './services/maps-api-loader/lazy-maps-api-loader'; +export {GoogleMapsScriptProtocol, LazyMapsAPILoader, LazyMapsAPILoaderConfig, LazyMapsAPILoaderConfigLiteral, provideLazyMapsAPILoaderConfig} from './services/maps-api-loader/lazy-maps-api-loader'; export {MapsAPILoader} from './services/maps-api-loader/maps-api-loader'; export {NoOpMapsAPILoader} from './services/maps-api-loader/noop-maps-api-loader'; export {MarkerManager} from './services/marker-manager'; diff --git a/src/core/services/maps-api-loader/lazy-maps-api-loader.ts b/src/core/services/maps-api-loader/lazy-maps-api-loader.ts index 21c01bdf3..cf5b6beba 100644 --- a/src/core/services/maps-api-loader/lazy-maps-api-loader.ts +++ b/src/core/services/maps-api-loader/lazy-maps-api-loader.ts @@ -1,4 +1,5 @@ -import {Injectable, Optional} from '@angular/core'; +import {Injectable, Optional, Provider, provide} from '@angular/core'; + import {MapsAPILoader} from './maps-api-loader'; export enum GoogleMapsScriptProtocol { @@ -7,12 +8,15 @@ export enum GoogleMapsScriptProtocol { AUTO } -export class LazyMapsAPILoaderConfig { +/** + * Config literal used to create an instance of LazyMapsAPILoaderConfig. + */ +export interface LazyMapsAPILoaderConfigLiteral { /** * The Google Maps API Key (see: * https://developers.google.com/maps/documentation/javascript/get-api-key) */ - apiKey: string = null; + apiKey?: string; /** * The Google Maps client ID (for premium plans). @@ -20,34 +24,34 @@ export class LazyMapsAPILoaderConfig { * your application with either an API key or a client ID. * The Google Maps API will fail to load if both a client ID and an API key are included. */ - clientId: string = null; + clientId?: string; /** * The Google Maps channel name (for premium plans). * A channel parameter is an optional parameter that allows you to track usage under your client * ID by assigning a distinct channel to each of your applications. */ - channel: string = null; + channel?: string; /** * Google Maps API version. */ - apiVersion: string = '3'; + apiVersion?: string; /** * Host and Path used for the `