ng2-heremaps
Version:
Here Maps for Angular 6
166 lines • 5.49 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
import { Injectable, Inject } from '@angular/core';
import { BaseMapsApiLoader, LAZY_LOADER_OPTIONS } from './base-maps-api-loader';
import { ScriptLoaderProtocol } from './script-loader-protocol';
export class LazyMapsApiLoader extends BaseMapsApiLoader {
/**
* @param {?} options
*/
constructor(options) {
super();
this.platformReady = new Promise((resolve, reject) => {
this._rejectPlatform = reject;
this._resolvePlatform = resolve;
});
this._initialized = false;
this._modules = new Map([
['core', ['js']],
['service', ['js']],
['mapevents', ['js']],
['ui', ['js', 'css']],
['clustering', ['js']],
['data', ['js']],
['places', ['js']],
['pano', ['js']]
]);
this._options = Object.assign({}, {
apiKey: '',
appId: '',
apiVersion: '3.0',
protocol: ScriptLoaderProtocol.AUTO,
libraries: []
}, options);
const /** @type {?} */ libs = this._options.libraries.filter(l => l !== 'core' && l !== 'service');
this._options.libraries = ['service', ...libs];
}
/**
* @return {?}
*/
load() {
if (this._initialized === false) {
this.loadModules()
.then(_ => {
const /** @type {?} */ platform = new H.service.Platform({
app_id: this._options.appId,
app_code: this._options.apiKey,
useHTTPS: (/** @type {?} */ (document.location)).protocol === 'https:'
});
this._resolvePlatform(platform);
})
.catch(error => {
this._rejectPlatform(error);
});
}
return this.platformReady;
}
/**
* @return {?}
*/
loadModules() {
// Load the Core first then the rest of the files
return this.loadModule('core').then(() => Promise.all(this._options.libraries
.reduce(this.distinct, [])
.map(moduleName => this.loadModule(moduleName))));
}
/**
* @param {?} moduleName
* @return {?}
*/
loadModule(moduleName) {
const /** @type {?} */ mod = this._modules.get(moduleName);
if (mod === void 0) {
const /** @type {?} */ error = new Error(`Unknown module ${moduleName}`);
return Promise.reject(error);
}
if (mod.indexOf('css') > -1) {
document.body.appendChild(this.createStylesheet(moduleName));
}
return new Promise((resolve, reject) => {
const /** @type {?} */ el = this.createScript(moduleName, resolve, reject);
document.body.appendChild(el);
});
}
/**
* @param {?} moduleName
* @return {?}
*/
createStylesheet(moduleName) {
const /** @type {?} */ element = document.createElement('link');
element.rel = 'stylesheet';
element.href = this.createModuleUrl(moduleName, 'css');
if (console !== void 0) {
element.onerror = console.error.bind(console);
}
return element;
}
/**
* @param {?} moduleName
* @param {?} onLoad
* @param {?} onError
* @return {?}
*/
createScript(moduleName, onLoad, onError) {
const /** @type {?} */ script = document.createElement('script');
script.type = 'text/javascript';
script.src = this.createModuleUrl(moduleName, 'js');
script.async = true;
script.defer = true;
script.addEventListener('error', onError);
script.addEventListener('load', onLoad);
return script;
}
/**
* @param {?} module
* @param {?=} ext
* @return {?}
*/
createModuleUrl(module, ext = 'js') {
const /** @type {?} */ protocol = 'https:', /** @type {?} */
// (document.location as any).protocol,
version = this._options.apiVersion;
return `${protocol}//js.api.here.com/v3/${version}/mapsjs-${module}.${ext}`;
}
/**
* @param {?} acc
* @param {?} next
* @return {?}
*/
distinct(acc, next) {
if (acc.indexOf(next) > -1) {
return acc;
}
return [...acc, next];
}
}
LazyMapsApiLoader.decorators = [
{ type: Injectable }
];
/** @nocollapse */
LazyMapsApiLoader.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [LAZY_LOADER_OPTIONS,] },] },
];
function LazyMapsApiLoader_tsickle_Closure_declarations() {
/** @type {!Array<{type: !Function, args: (undefined|!Array<?>)}>} */
LazyMapsApiLoader.decorators;
/**
* @nocollapse
* @type {function(): !Array<(null|{type: ?, decorators: (undefined|!Array<{type: !Function, args: (undefined|!Array<?>)}>)})>}
*/
LazyMapsApiLoader.ctorParameters;
/** @type {?} */
LazyMapsApiLoader.prototype.platformReady;
/** @type {?} */
LazyMapsApiLoader.prototype._initialized;
/** @type {?} */
LazyMapsApiLoader.prototype._options;
/** @type {?} */
LazyMapsApiLoader.prototype._resolvePlatform;
/** @type {?} */
LazyMapsApiLoader.prototype._rejectPlatform;
/** @type {?} */
LazyMapsApiLoader.prototype._modules;
}
//# sourceMappingURL=lazy-maps-api-loader.js.map