renderOnlyLoader.ts 6.8 KB

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