loader.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { mapperManager } from './mappers';
  2. import { ViewerConfiguration } from './configuration';
  3. import { getConfigurationType } from './types';
  4. import * as deepmerge from '../../assets/deepmerge.min.js';
  5. export class ConfigurationLoader {
  6. private configurationCache: { [url: string]: any };
  7. constructor() {
  8. this.configurationCache = {};
  9. }
  10. public loadConfiguration(initConfig: ViewerConfiguration = {}, callback?: (config: ViewerConfiguration) => void): Promise<ViewerConfiguration> {
  11. let loadedConfig: ViewerConfiguration = deepmerge({}, initConfig);
  12. let extendedConfiguration = getConfigurationType(loadedConfig.extends || "");
  13. loadedConfig = deepmerge(extendedConfiguration, loadedConfig);
  14. if (loadedConfig.configuration) {
  15. let mapperType = "json";
  16. return Promise.resolve().then(() => {
  17. if (typeof loadedConfig.configuration === "string" || (loadedConfig.configuration && loadedConfig.configuration.url)) {
  18. // a file to load
  19. let url: string = '';
  20. if (typeof loadedConfig.configuration === "string") {
  21. url = loadedConfig.configuration;
  22. }
  23. // if configuration is an object
  24. if (typeof loadedConfig.configuration === "object" && loadedConfig.configuration.url) {
  25. url = loadedConfig.configuration.url;
  26. let type = loadedConfig.configuration.mapper;
  27. // empty string?
  28. if (!type) {
  29. // load mapper type from filename / url
  30. type = loadedConfig.configuration.url.split('.').pop();
  31. }
  32. mapperType = type || mapperType;
  33. }
  34. return this.loadFile(url);
  35. } else {
  36. if (typeof loadedConfig.configuration === "object") {
  37. mapperType = loadedConfig.configuration.mapper || mapperType;
  38. return loadedConfig.configuration.payload || {};
  39. }
  40. return {};
  41. }
  42. }).then((data: any) => {
  43. let mapper = mapperManager.getMapper(mapperType);
  44. let parsed = mapper.map(data);
  45. let merged = deepmerge(loadedConfig, parsed);
  46. if (callback) callback(merged);
  47. return merged;
  48. });
  49. } else {
  50. if (callback) callback(loadedConfig);
  51. return Promise.resolve(loadedConfig);
  52. }
  53. }
  54. private loadFile(url: string): Promise<any> {
  55. let cacheReference = this.configurationCache;
  56. if (cacheReference[url]) {
  57. return Promise.resolve(cacheReference[url]);
  58. }
  59. return new Promise(function (resolve, reject) {
  60. var xhr = new XMLHttpRequest();
  61. xhr.open('GET', url);
  62. xhr.send();
  63. xhr.onreadystatechange = function () {
  64. var DONE = 4;
  65. var OK = 200;
  66. if (xhr.readyState === DONE) {
  67. if (xhr.status === OK) {
  68. cacheReference[url] = xhr.responseText;
  69. resolve(xhr.responseText); // 'This is the returned text.'
  70. } else {
  71. console.log('Error: ' + xhr.status, url);
  72. reject('Error: ' + xhr.status); // An error occurred during the request.
  73. }
  74. }
  75. }
  76. });
  77. }
  78. }
  79. export let configurationLoader = new ConfigurationLoader();
  80. export default configurationLoader;