123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { mapperManager } from './mappers';
- import { ViewerConfiguration } from './configuration';
- import { getConfigurationType } from './types';
- import * as deepmerge from '../../assets/deepmerge.min.js';
- export class ConfigurationLoader {
- private configurationCache: { [url: string]: any };
- constructor() {
- this.configurationCache = {};
- }
- public loadConfiguration(initConfig: ViewerConfiguration = {}, callback?: (config: ViewerConfiguration) => void): Promise<ViewerConfiguration> {
- let loadedConfig: ViewerConfiguration = deepmerge({}, initConfig);
- let extendedConfiguration = getConfigurationType(loadedConfig.extends || "");
- loadedConfig = deepmerge(extendedConfiguration, loadedConfig);
- if (loadedConfig.configuration) {
- let mapperType = "json";
- return Promise.resolve().then(() => {
- if (typeof loadedConfig.configuration === "string" || (loadedConfig.configuration && loadedConfig.configuration.url)) {
- // a file to load
- let url: string = '';
- if (typeof loadedConfig.configuration === "string") {
- url = loadedConfig.configuration;
- }
- // if configuration is an object
- if (typeof loadedConfig.configuration === "object" && loadedConfig.configuration.url) {
- url = loadedConfig.configuration.url;
- let type = loadedConfig.configuration.mapper;
- // empty string?
- if (!type) {
- // load mapper type from filename / url
- type = loadedConfig.configuration.url.split('.').pop();
- }
- mapperType = type || mapperType;
- }
- return this.loadFile(url);
- } else {
- if (typeof loadedConfig.configuration === "object") {
- mapperType = loadedConfig.configuration.mapper || mapperType;
- return loadedConfig.configuration.payload || {};
- }
- return {};
- }
- }).then((data: any) => {
- let mapper = mapperManager.getMapper(mapperType);
- let parsed = mapper.map(data);
- let merged = deepmerge(loadedConfig, parsed);
- if (callback) callback(merged);
- return merged;
- });
- } else {
- if (callback) callback(loadedConfig);
- return Promise.resolve(loadedConfig);
- }
- }
- private loadFile(url: string): Promise<any> {
- let cacheReference = this.configurationCache;
- if (cacheReference[url]) {
- return Promise.resolve(cacheReference[url]);
- }
- return new Promise(function (resolve, reject) {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url);
- xhr.send();
- xhr.onreadystatechange = function () {
- var DONE = 4;
- var OK = 200;
- if (xhr.readyState === DONE) {
- if (xhr.status === OK) {
- cacheReference[url] = xhr.responseText;
- resolve(xhr.responseText); // 'This is the returned text.'
- } else {
- console.log('Error: ' + xhr.status, url);
- reject('Error: ' + xhr.status); // An error occurred during the request.
- }
- }
- }
- });
- }
- }
- export let configurationLoader = new ConfigurationLoader();
- export default configurationLoader;
|