renderOnlyLoader.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { mapperManager } from './mappers';
  2. import { ViewerConfiguration } from './configuration';
  3. import { processConfigurationCompatibility } from './configurationCompatibility';
  4. import { deepmerge } from '../helper';
  5. import { IFileRequest, Tools } from 'babylonjs/Misc/tools';
  6. import { extendedConfiguration } from './types/extended';
  7. import { renderOnlyDefaultConfiguration } from './types/renderOnlyDefault';
  8. /**
  9. * The configuration loader will load the configuration object from any source and will use the defined mapper to
  10. * parse the object and return a conform ViewerConfiguration.
  11. * It is a private member of the scene.
  12. */
  13. export class RenderOnlyConfigurationLoader {
  14. private _configurationCache: { [url: string]: any };
  15. private _loadRequests: Array<IFileRequest>;
  16. constructor(private _enableCache: boolean = false) {
  17. this._configurationCache = {};
  18. this._loadRequests = [];
  19. }
  20. private _getConfigurationTypeExcludeTemplate (types: string): ViewerConfiguration {
  21. let config: ViewerConfiguration = {};
  22. let typesSeparated = types.split(",");
  23. typesSeparated.forEach((type) => {
  24. switch (type.trim()) {
  25. case 'default':
  26. config = deepmerge(config, renderOnlyDefaultConfiguration);
  27. break;
  28. case 'none':
  29. break;
  30. case 'extended':
  31. default:
  32. config = deepmerge(config, extendedConfiguration);
  33. break;
  34. }
  35. if (config.extends) {
  36. config = deepmerge(config, this._getConfigurationTypeExcludeTemplate(config.extends));
  37. }
  38. });
  39. return config;
  40. };
  41. protected getExtendedConfig(type:string|undefined){
  42. return this._getConfigurationTypeExcludeTemplate(type || "extended");
  43. }
  44. /**
  45. * load a configuration object that is defined in the initial configuration provided.
  46. * The viewer configuration can extend different types of configuration objects and have an extra configuration defined.
  47. *
  48. * @param initConfig the initial configuration that has the definitions of further configuration to load.
  49. * @param callback an optional callback that will be called sync, if noconfiguration needs to be loaded or configuration is payload-only
  50. * @returns A promise that delivers the extended viewer configuration, when done.
  51. */
  52. public loadConfiguration(initConfig: ViewerConfiguration = {}, callback?: (config: ViewerConfiguration) => void): Promise<ViewerConfiguration> {
  53. let loadedConfig: ViewerConfiguration = deepmerge({}, initConfig);
  54. this._processInitialConfiguration(loadedConfig);
  55. let extendedConfiguration = this.getExtendedConfig(loadedConfig.extends);
  56. if (loadedConfig.configuration) {
  57. let mapperType = "json";
  58. return Promise.resolve().then(() => {
  59. if (typeof loadedConfig.configuration === "string" || (loadedConfig.configuration && loadedConfig.configuration.url)) {
  60. // a file to load
  61. let url: string = '';
  62. if (typeof loadedConfig.configuration === "string") {
  63. url = loadedConfig.configuration;
  64. }
  65. // if configuration is an object
  66. if (typeof loadedConfig.configuration === "object" && loadedConfig.configuration.url) {
  67. url = loadedConfig.configuration.url;
  68. let type = loadedConfig.configuration.mapper;
  69. // empty string?
  70. if (!type) {
  71. // load mapper type from filename / url
  72. type = loadedConfig.configuration.url.split('.').pop();
  73. }
  74. mapperType = type || mapperType;
  75. }
  76. return this._loadFile(url);
  77. } else {
  78. if (typeof loadedConfig.configuration === "object") {
  79. mapperType = loadedConfig.configuration.mapper || mapperType;
  80. return loadedConfig.configuration.payload || {};
  81. }
  82. return {};
  83. }
  84. }).then((data: any) => {
  85. let mapper = mapperManager.getMapper(mapperType);
  86. let parsed = deepmerge(mapper.map(data), loadedConfig);
  87. let merged = deepmerge(extendedConfiguration, parsed);
  88. processConfigurationCompatibility(merged);
  89. if (callback) { callback(merged); }
  90. return merged;
  91. });
  92. } else {
  93. loadedConfig = deepmerge(extendedConfiguration, loadedConfig);
  94. processConfigurationCompatibility(loadedConfig);
  95. if (callback) { callback(loadedConfig); }
  96. return Promise.resolve(loadedConfig);
  97. }
  98. }
  99. /**
  100. * Dispose the configuration loader. This will cancel file requests, if active.
  101. */
  102. public dispose() {
  103. this._loadRequests.forEach((request) => {
  104. request.abort();
  105. });
  106. this._loadRequests.length = 0;
  107. }
  108. /**
  109. * This function will process the initial configuration and make needed changes for the viewer to work.
  110. * @param config the mutable(!) initial configuration to process
  111. */
  112. private _processInitialConfiguration(config: ViewerConfiguration) {
  113. if (config.model) {
  114. if (typeof config.model === "string") {
  115. config.model = {
  116. url: config.model
  117. };
  118. }
  119. }
  120. }
  121. private _loadFile(url: string): Promise<any> {
  122. let cacheReference = this._configurationCache;
  123. if (this._enableCache && cacheReference[url]) {
  124. return Promise.resolve(cacheReference[url]);
  125. }
  126. return new Promise((resolve, reject) => {
  127. let fileRequest = Tools.LoadFile(url, (result) => {
  128. let idx = this._loadRequests.indexOf(fileRequest);
  129. if (idx !== -1) {
  130. this._loadRequests.splice(idx, 1);
  131. }
  132. if (this._enableCache) { cacheReference[url] = result; }
  133. resolve(result);
  134. }, undefined, undefined, false, (request, error: any) => {
  135. let idx = this._loadRequests.indexOf(fileRequest);
  136. if (idx !== -1) {
  137. this._loadRequests.splice(idx, 1);
  138. }
  139. reject(error);
  140. });
  141. this._loadRequests.push(fileRequest);
  142. });
  143. }
  144. }