ns2-front-module-common
Version:
NS2 common module
160 lines • 5.83 kB
JavaScript
import { Injectable } from "@angular/core";
var ExternalResourceService = (function () {
function ExternalResourceService() {
/**
* Список уже загруженных на страницу скриптов
*
* @type {Array}
*/
this.loadedScripts = [];
/**
* Список уже загруженных на страницу стилей
*
* @type {Array}
*/
this.loadedStyles = [];
/**
* Список уже загруженных изображений
*
* @type {Array}
*/
this.loadedImages = [];
}
/**
* Загрузка сразу нескольких стилей
*
* @param styles
* @returns {Promise<any>}
*/
ExternalResourceService.prototype.loadStyles = function (styles) {
var _this = this;
return Promise.all(styles.map(function (style) {
return _this.loadStyle(style.href, style.optionalAttributes || {});
}));
};
/**
* Загрузка сразу нескольких скриптов
*
* @param scripts
* @returns {Promise<any[]>}
*/
ExternalResourceService.prototype.loadScripts = function (scripts) {
var _this = this;
return Promise.all(scripts.map(function (script) {
return _this.loadScript(script.src, script.optionalAttributes || {});
}));
};
/**
* Загрузка сразу нескольких изображений
*
* @param images
* @returns {Promise<any[]>}
*/
ExternalResourceService.prototype.loadImages = function (images) {
var _this = this;
return Promise.all(images.map(function (image) {
return _this.loadImage(image.src, image.optionalAttributes || {});
}));
};
/**
* Загрузка внешнего CSS-файла. Вернет promise в случае успеха загрузки файла, будет вызван resolve(), иначе reject()
*
* @param href
* @param optionalAttributes
* @returns {Promise<any>}
*/
ExternalResourceService.prototype.loadStyle = function (href, optionalAttributes) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.loadedStyles.indexOf(href) !== -1) {
resolve();
}
else {
var link = document.createElement('link');
var attributes = Object.assign(ExternalResourceService.defaultStyleAttributes, optionalAttributes || {});
for (var name_1 in attributes) {
link.setAttribute(name_1, attributes[name_1]);
}
link.href = href;
link.onload = function () {
_this.loadedStyles.push(href);
resolve();
};
link.onerror = reject;
document.head.appendChild(link);
}
});
};
/**
* Загрузка внешнего JS-файла. Вернет promise в случае успеха загрузки файла, будет вызван resolve(), иначе reject()
*
* @param src
* @param optionalAttributes
* @returns {Promise<any>}
*/
ExternalResourceService.prototype.loadScript = function (src, optionalAttributes) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.loadedScripts.indexOf(src) !== -1) {
resolve();
}
else {
var script = document.createElement('script');
var attributes = Object.assign(ExternalResourceService.defaultScriptAttributes, optionalAttributes || {});
for (var name_2 in attributes) {
script.setAttribute(name_2, attributes[name_2]);
}
script.src = src;
script.onload = function () {
_this.loadedScripts.push(src);
resolve();
};
script.onerror = reject;
document.body.appendChild(script);
}
});
};
/**
* Загрузка изображения. Вернет promise в случае успеха загрузки файла, будет вызван resolve(), иначе reject()
*
* @param src
* @param optionalAttributes
* @returns {Promise<any>}
*/
ExternalResourceService.prototype.loadImage = function (src, optionalAttributes) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.loadedImages.indexOf(src) !== -1) {
resolve();
}
else {
var image = new Image();
var attributes = optionalAttributes || {};
for (var name_3 in attributes) {
image.setAttribute(name_3, attributes[name_3]);
}
image.onload = function () {
_this.loadedImages.push(src);
resolve();
};
image.onerror = reject;
image.src = src;
}
});
};
return ExternalResourceService;
}());
export { ExternalResourceService };
ExternalResourceService.defaultStyleAttributes = {
type: 'text/css',
rel: 'stylesheet'
};
ExternalResourceService.defaultScriptAttributes = {
type: 'text/javascript'
};
ExternalResourceService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
ExternalResourceService.ctorParameters = function () { return []; };
//# sourceMappingURL=external-resource.service.js.map