UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

116 lines (115 loc) 4.94 kB
import GirafeSingleton from '../../base/GirafeSingleton'; import GirafeConfig from './girafeconfig'; import UserDataManager from '../userdata/userdatamanager'; import { getPropertyByPath, mergeObjects } from '../utils/pathUtils'; class ConfigManager extends GirafeSingleton { constructor() { super(...arguments); Object.defineProperty(this, "config", { enumerable: true, configurable: true, writable: true, value: null }); // Backup of config before applying user overrides Object.defineProperty(this, "defaultConfig", { enumerable: true, configurable: true, writable: true, value: null }); Object.defineProperty(this, "loadingPromise", { enumerable: true, configurable: true, writable: true, value: null }); Object.defineProperty(this, "storagePathForOverrides", { enumerable: true, configurable: true, writable: true, value: 'configOverrides' }); } get Config() { return this.config; } async loadConfig() { if (this.loadingPromise) { // There's already a promise for loading the configuration // => return it instead of starting another request return this.loadingPromise; } if (this.config) { // Config was already loaded. // => stop here return Promise.resolve(this.config); } // Load config this.loadingPromise = (async () => { const configNames = document .querySelector('meta[name=configs]') ?.getAttribute('content') ?.split(',') .map((name) => name.trim()); if (!configNames) { throw new Error("No configuration names found in 'configs' meta tag."); } let jsonConfig = {}; for (const configName of configNames) { const configUrl = document.querySelector(`link[rel=config-${configName}-url]`)?.getAttribute('href'); if (!configUrl) { throw new Error(`Configuration URL for '${configName}' not found in 'config-${configName}-url' meta tag.`); } try { const response = await fetch(configUrl); const newJsonConfig = await response.json(); jsonConfig = this.mergeConfigs(jsonConfig, newJsonConfig); } catch { // TODO REG: Manage better the errors at the aplication start: // - the window.gAlert fuction should be callable at the very beggining of the app // - The ErrorManager should handled suches case, but it seems to be initialized too late. // - normal alerts seems to be blocked on mobile. const errorMessage = `Error while reading the configuration file ${configUrl}. Please verify your configuration.`; window.alert(errorMessage); throw new Error(errorMessage); } } // Create a backup of the default config before applying overrides this.defaultConfig = new GirafeConfig(structuredClone(jsonConfig)); // Load config overrides and merge them with the default config const configOverrides = this.getConfigOverrides(this.defaultConfig.userdata.source); jsonConfig = this.mergeConfigs(jsonConfig, configOverrides); this.config = new GirafeConfig(jsonConfig); console.log('Application Configuration loaded.'); return this.config; })(); return this.loadingPromise; } /** * Merges two configuration objects recursively. */ mergeConfigs(obj1, obj2) { return mergeObjects(obj1, obj2); } /** * Retrieves saved configuration overrides based on the user data source. * @param userDataSource The source of user data. */ getConfigOverrides(userDataSource) { const userDataManager = UserDataManager.getInstance(); // Set the user data source first before requesting config overrides from the userDataManager userDataManager.setSource(userDataSource); return userDataManager.getUserData(this.storagePathForOverrides) || {}; } /** * Retrieves the original (default) configuration value before applying overrides. * @param path The path to the configuration property. */ getDefaultConfigValue(path) { const { found, parentObject, lastKey } = getPropertyByPath(this.defaultConfig, path); return found && parentObject && lastKey ? parentObject[lastKey] : undefined; } } export default ConfigManager;