UNPKG

ng2-heremaps

Version:
204 lines 6.74 kB
/** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ import * as tslib_1 from "tslib"; import { Injectable, Inject } from '@angular/core'; import { BaseMapsApiLoader, LAZY_LOADER_OPTIONS } from './base-maps-api-loader'; import { ScriptLoaderProtocol } from './script-loader-protocol'; var LazyMapsApiLoader = /** @class */ (function (_super) { tslib_1.__extends(LazyMapsApiLoader, _super); function LazyMapsApiLoader(options) { var _this = _super.call(this) || this; _this.platformReady = new Promise(function (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); var /** @type {?} */ libs = _this._options.libraries.filter(function (l) { return l !== 'core' && l !== 'service'; }); _this._options.libraries = ['service'].concat(libs); return _this; } /** * @return {?} */ LazyMapsApiLoader.prototype.load = /** * @return {?} */ function () { var _this = this; if (this._initialized === false) { this.loadModules() .then(function (_) { var /** @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(function (error) { _this._rejectPlatform(error); }); } return this.platformReady; }; /** * @return {?} */ LazyMapsApiLoader.prototype.loadModules = /** * @return {?} */ function () { var _this = this; // Load the Core first then the rest of the files return this.loadModule('core').then(function () { return Promise.all(_this._options.libraries .reduce(_this.distinct, []) .map(function (moduleName) { return _this.loadModule(moduleName); })); }); }; /** * @param {?} moduleName * @return {?} */ LazyMapsApiLoader.prototype.loadModule = /** * @param {?} moduleName * @return {?} */ function (moduleName) { var _this = this; var /** @type {?} */ mod = this._modules.get(moduleName); if (mod === void 0) { var /** @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(function (resolve, reject) { var /** @type {?} */ el = _this.createScript(moduleName, resolve, reject); document.body.appendChild(el); }); }; /** * @param {?} moduleName * @return {?} */ LazyMapsApiLoader.prototype.createStylesheet = /** * @param {?} moduleName * @return {?} */ function (moduleName) { var /** @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 {?} */ LazyMapsApiLoader.prototype.createScript = /** * @param {?} moduleName * @param {?} onLoad * @param {?} onError * @return {?} */ function (moduleName, onLoad, onError) { var /** @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 {?} */ LazyMapsApiLoader.prototype.createModuleUrl = /** * @param {?} module * @param {?=} ext * @return {?} */ function (module, ext) { if (ext === void 0) { ext = 'js'; } var /** @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 {?} */ LazyMapsApiLoader.prototype.distinct = /** * @param {?} acc * @param {?} next * @return {?} */ function (acc, next) { if (acc.indexOf(next) > -1) { return acc; } return acc.concat([next]); }; LazyMapsApiLoader.decorators = [ { type: Injectable } ]; /** @nocollapse */ LazyMapsApiLoader.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [LAZY_LOADER_OPTIONS,] },] }, ]; }; return LazyMapsApiLoader; }(BaseMapsApiLoader)); export { LazyMapsApiLoader }; 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