import { mapperManager } from './mappers' ; import { ViewerConfiguration } from './configuration' ; import { processConfigurationCompatibility } from './configurationCompatibility' ; import { deepmerge } from '../helper' ; import { Tools } from 'babylonjs/Misc/tools' ; import { extendedConfiguration } from './types/extended' ; import { renderOnlyDefaultConfiguration } from './types/renderOnlyDefault' ; import { IFileRequest } from 'babylonjs/Misc/fileRequest' ; /**
 * The configuration loader will load the configuration object from any source and will use the defined mapper to
 * parse the object and return a conform ViewerConfiguration.
 * It is a private member of the scene.
 */ export class RenderOnlyConfigurationLoader { private _configurationCache : { [ url ] : any ; } ; private _loadRequests : Array < IFileRequest > ; constructor ( private _enableCache = false ) { this . _configurationCache = { } ; this . _loadRequests = [ ] ; } private _getConfigurationTypeExcludeTemplate ( types ) { let config = { } ; let typesSeparated = types . split ( "," ) ; typesSeparated . forEach ( ( type ) => { switch ( type . trim ( ) ) { case 'default' : config = deepmerge ( config , renderOnlyDefaultConfiguration ) ; break ; case 'none' : break ; case 'extended' : default : config = deepmerge ( config , extendedConfiguration ) ; break ; } if ( config . extends ) { config = deepmerge ( config , this . _getConfigurationTypeExcludeTemplate ( config . extends ) ) ; } } ) ; return config ; } ; protected getExtendedConfig ( type ) { return this . _getConfigurationTypeExcludeTemplate ( type || "extended" ) ; } param initConfig param callback returns public loadConfiguration ( initConfig = { } , callback ? ) { let loadedConfig = deepmerge ( { } , initConfig ) ; this . _processInitialConfiguration ( loadedConfig ) ; let extendedConfiguration = this . getExtendedConfig ( loadedConfig . extends ) ; if ( loadedConfig . configuration ) { let mapperType = "json" ; return Promise . resolve ( ) . then ( ( ) => { if ( typeof loadedConfig . configuration === "string" || ( loadedConfig . configuration && loadedConfig . configuration . url ) ) { let url = '' ; if ( typeof loadedConfig . configuration === "string" ) { url = loadedConfig . configuration ; } if ( typeof loadedConfig . configuration === "object" && loadedConfig . configuration . url ) { url = loadedConfig . configuration . url ; let type = loadedConfig . configuration . mapper ; if ( ! type ) { 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 ) => { let mapper = mapperManager . getMapper ( mapperType ) ; let parsed = deepmerge ( mapper . map ( data ) , loadedConfig ) ; let merged = deepmerge ( extendedConfiguration , parsed ) ; processConfigurationCompatibility ( merged ) ; if ( callback ) { callback ( merged ) ; } return merged ; } ) ; } else { loadedConfig = deepmerge ( extendedConfiguration , loadedConfig ) ; processConfigurationCompatibility ( loadedConfig ) ; if ( callback ) { callback ( loadedConfig ) ; } return Promise . resolve ( loadedConfig ) ; } } /**
     * Dispose the configuration loader. This will cancel file requests, if active.
     */ public dispose ( ) { this . _loadRequests . forEach ( ( request ) => { request . abort ( ) ; } ) ; this . _loadRequests . length = 0 ; } param config private _processInitialConfiguration ( config ) { if ( config . model ) { if ( typeof config . model === "string" ) { config . model = { url : config . model } ; } } } private _loadFile ( url ) { let cacheReference = this . _configurationCache ; if ( this . _enableCache && cacheReference [ url ] ) { return Promise . resolve ( cacheReference [ url ] ) ; } return new Promise ( ( resolve , reject ) => { let fileRequest = Tools . LoadFile ( url , ( result ) => { let idx = this . _loadRequests . indexOf ( fileRequest ) ; if ( idx !== - 1 ) { this . _loadRequests . splice ( idx , 1 ) ; } if ( this . _enableCache ) { cacheReference [ url ] = result ; } resolve ( result ) ; } , undefined , undefined , false , ( request , error ) => { let idx = this . _loadRequests . indexOf ( fileRequest ) ; if ( idx !== - 1 ) { this . _loadRequests . splice ( idx , 1 ) ; } reject ( error ) ; } ) ; this . _loadRequests . push ( fileRequest ) ; } ) ; } }