ng2-bingmaps
Version:
Angular 2 components for Bing Maps
130 lines (128 loc) • 5.29 kB
JavaScript
/**
* ng2-bingmaps - Angular 2 components for Bing Maps
* @version v0.2.0
* @link https://github.com/youjustgo/ng2-bingmaps
* @license MIT
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { Injectable, Optional } from '@angular/core';
import { MapsAPILoader } from './maps-api-loader';
export var ScriptProtocol;
(function (ScriptProtocol) {
ScriptProtocol[ScriptProtocol["HTTP"] = 0] = "HTTP";
ScriptProtocol[ScriptProtocol["HTTPS"] = 1] = "HTTPS";
ScriptProtocol[ScriptProtocol["AUTO"] = 2] = "AUTO";
})(ScriptProtocol || (ScriptProtocol = {}));
export class LazyMapsAPILoaderConfig {
constructor() {
/**
* The Bing Maps API Key (see:
* https://developers.google.com/maps/documentation/javascript/get-api-key)
*/
this.apiKey = null;
/**
* The Google Maps client ID (for premium plans).
* When you have a Google Maps APIs Premium Plan license, you must authenticate
* 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.
*/
this.clientId = null;
/**
* 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.
*/
this.channel = null;
/**
* Google Maps API version.
*/
this.apiVersion = '3';
/**
* Host and Path used for the `<script>` tag.
*/
this.hostAndPath = 'www.bing.com/api/maps/mapcontrol';
/**
* Protocol used for the `<script>` tag.
*/
this.protocol = ScriptProtocol.HTTPS;
/**
* The branch to use: Release, Experimental, or Frozen.
* Set to Experimental to use the newest features, or if it contains a bug fix you require. Otherwise leave blank or set to Release.
*/
this.branch = 'release';
}
}
const DEFAULT_CONFIGURATION = new LazyMapsAPILoaderConfig();
export let LazyMapsAPILoader = class LazyMapsAPILoader extends MapsAPILoader {
constructor(_config) {
super();
this._config = _config;
if (this._config === null || this._config === undefined) {
this._config = DEFAULT_CONFIGURATION;
}
}
load() {
if (this._scriptLoadingPromise) {
return this._scriptLoadingPromise;
}
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.defer = true;
const callbackName = `angular2bingmaps${new Date().getMilliseconds()}`;
script.src = this._getScriptSrc(callbackName);
this._scriptLoadingPromise = new Promise((resolve, reject) => {
window[callbackName] = () => { resolve(); };
script.onerror = (error) => { reject(error); };
});
document.body.appendChild(script);
return this._scriptLoadingPromise;
}
_getScriptSrc(callbackName) {
let protocolType = (this._config && this._config.protocol) || DEFAULT_CONFIGURATION.protocol;
let protocol;
switch (protocolType) {
case ScriptProtocol.AUTO:
protocol = '';
break;
case ScriptProtocol.HTTP:
protocol = 'http:';
break;
case ScriptProtocol.HTTPS:
protocol = 'https:';
break;
}
const hostAndPath = this._config.hostAndPath || DEFAULT_CONFIGURATION.hostAndPath;
const branch = this._config.branch || DEFAULT_CONFIGURATION.branch;
const queryParams = {
callback: callbackName
};
if (branch) {
queryParams['branch'] = branch;
}
const params = Object.keys(queryParams)
.map((k, i) => {
let param = (i === 0) ? '?' : '&';
return param += `${k}=${queryParams[k]}`;
})
.join('');
return `${protocol}//${hostAndPath}${params}`;
}
};
LazyMapsAPILoader = __decorate([
Injectable(),
__param(0, Optional()),
__metadata('design:paramtypes', [LazyMapsAPILoaderConfig])
], LazyMapsAPILoader);
//# sourceMappingURL=lazy-maps-api-loader.js.map