UNPKG

angular2-google-maps

Version:

Angular 2 components for Google Maps

101 lines 4.4 kB
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; import { Inject, Injectable, OpaqueToken } from '@angular/core'; import { DocumentRef, WindowRef } from '../../utils/browser-globals'; import { MapsAPILoader } from './maps-api-loader'; export var GoogleMapsScriptProtocol; (function (GoogleMapsScriptProtocol) { GoogleMapsScriptProtocol[GoogleMapsScriptProtocol["HTTP"] = 1] = "HTTP"; GoogleMapsScriptProtocol[GoogleMapsScriptProtocol["HTTPS"] = 2] = "HTTPS"; GoogleMapsScriptProtocol[GoogleMapsScriptProtocol["AUTO"] = 3] = "AUTO"; })(GoogleMapsScriptProtocol || (GoogleMapsScriptProtocol = {})); /** * Token for the config of the LazyMapsAPILoader. Please provide an object of type {@link * LazyMapsAPILoaderConfig}. */ export var LAZY_MAPS_API_CONFIG = new OpaqueToken('angular2-google-maps LAZY_MAPS_API_CONFIG'); export var LazyMapsAPILoader = (function (_super) { __extends(LazyMapsAPILoader, _super); function LazyMapsAPILoader(config, w, d) { _super.call(this); this._config = config || {}; this._windowRef = w; this._documentRef = d; } LazyMapsAPILoader.prototype.load = function () { var _this = this; if (this._scriptLoadingPromise) { return this._scriptLoadingPromise; } var script = this._documentRef.getNativeDocument().createElement('script'); script.type = 'text/javascript'; script.async = true; script.defer = true; var callbackName = "angular2GoogleMapsLazyMapsAPILoader"; script.src = this._getScriptSrc(callbackName); this._scriptLoadingPromise = new Promise(function (resolve, reject) { _this._windowRef.getNativeWindow()[callbackName] = function () { resolve(); }; script.onerror = function (error) { reject(error); }; }); this._documentRef.getNativeDocument().body.appendChild(script); return this._scriptLoadingPromise; }; LazyMapsAPILoader.prototype._getScriptSrc = function (callbackName) { var protocolType = (this._config && this._config.protocol) || GoogleMapsScriptProtocol.HTTPS; var protocol; switch (protocolType) { case GoogleMapsScriptProtocol.AUTO: protocol = ''; break; case GoogleMapsScriptProtocol.HTTP: protocol = 'http:'; break; case GoogleMapsScriptProtocol.HTTPS: protocol = 'https:'; break; } var hostAndPath = this._config.hostAndPath || 'maps.googleapis.com/maps/api/js'; var queryParams = { v: this._config.apiVersion || '3', callback: callbackName, key: this._config.apiKey, client: this._config.clientId, channel: this._config.channel, libraries: this._config.libraries, region: this._config.region, language: this._config.language }; var params = Object.keys(queryParams) .filter(function (k) { return queryParams[k] != null; }) .filter(function (k) { // remove empty arrays return !Array.isArray(queryParams[k]) || (Array.isArray(queryParams[k]) && queryParams[k].length > 0); }) .map(function (k) { // join arrays as comma seperated strings var i = queryParams[k]; if (Array.isArray(i)) { return { key: k, value: i.join(',') }; } return { key: k, value: queryParams[k] }; }) .map(function (entry) { return entry.key + "=" + entry.value; }) .join('&'); return protocol + "//" + hostAndPath + "?" + params; }; LazyMapsAPILoader.decorators = [ { type: Injectable }, ]; /** @nocollapse */ LazyMapsAPILoader.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [LAZY_MAPS_API_CONFIG,] },] }, { type: WindowRef, }, { type: DocumentRef, }, ]; }; return LazyMapsAPILoader; }(MapsAPILoader)); //# sourceMappingURL=lazy-maps-api-loader.js.map