UNPKG

angular2-google-maps

Version:

Angular 2 components for Google Maps

103 lines 4.55 kB
"use strict"; 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 __()); }; var core_1 = require('@angular/core'); var browser_globals_1 = require('../../utils/browser-globals'); var maps_api_loader_1 = require('./maps-api-loader'); (function (GoogleMapsScriptProtocol) { GoogleMapsScriptProtocol[GoogleMapsScriptProtocol["HTTP"] = 1] = "HTTP"; GoogleMapsScriptProtocol[GoogleMapsScriptProtocol["HTTPS"] = 2] = "HTTPS"; GoogleMapsScriptProtocol[GoogleMapsScriptProtocol["AUTO"] = 3] = "AUTO"; })(exports.GoogleMapsScriptProtocol || (exports.GoogleMapsScriptProtocol = {})); var GoogleMapsScriptProtocol = exports.GoogleMapsScriptProtocol; /** * Token for the config of the LazyMapsAPILoader. Please provide an object of type {@link * LazyMapsAPILoaderConfig}. */ exports.LAZY_MAPS_API_CONFIG = new core_1.OpaqueToken('angular2-google-maps LAZY_MAPS_API_CONFIG'); 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: core_1.Injectable }, ]; /** @nocollapse */ LazyMapsAPILoader.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: core_1.Inject, args: [exports.LAZY_MAPS_API_CONFIG,] },] }, { type: browser_globals_1.WindowRef, }, { type: browser_globals_1.DocumentRef, }, ]; }; return LazyMapsAPILoader; }(maps_api_loader_1.MapsAPILoader)); exports.LazyMapsAPILoader = LazyMapsAPILoader; //# sourceMappingURL=lazy-maps-api-loader.js.map