@gitlab/ui
Version:
GitLab UI Components
87 lines (76 loc) • 2.5 kB
JavaScript
import { NAME } from '../constants/config';
import { cloneDeep } from './clone-deep';
import { getRaw } from './get';
import { isPlainObject, isArray, isString, isUndefined } from './inspect';
import { getOwnPropertyNames } from './object';
import { warn } from './warn';
// Config manager class
class BvConfig {
constructor() {
this.$_config = {};
}
// Method to merge in user config parameters
setConfig() {
let config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
/* istanbul ignore next */
if (!isPlainObject(config)) {
return;
}
const configKeys = getOwnPropertyNames(config);
configKeys.forEach(key => {
/* istanbul ignore next */
const subConfig = config[key];
if (key === 'breakpoints') {
/* istanbul ignore if */
if (!isArray(subConfig) || subConfig.length < 2 || subConfig.some(b => !isString(b) || b.length === 0)) {
warn('"breakpoints" must be an array of at least 2 breakpoint names', NAME);
} else {
this.$_config[key] = cloneDeep(subConfig);
}
} else if (isPlainObject(subConfig)) {
// Component prop defaults
this.$_config[key] = getOwnPropertyNames(subConfig).reduce((config, prop) => {
if (!isUndefined(subConfig[prop])) {
config[prop] = cloneDeep(subConfig[prop]);
}
return config;
}, this.$_config[key] || {});
}
});
}
// Clear the config
resetConfig() {
this.$_config = {};
}
// Returns a deep copy of the user config
getConfig() {
return cloneDeep(this.$_config);
}
// Returns a deep copy of the config value
getConfigValue(key) {
let defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
return cloneDeep(getRaw(this.$_config, key, defaultValue));
}
}
// Module-level singleton instance
let bvConfig = null;
// Get or create the config instance
const getConfigInstance = () => {
if (!bvConfig) {
bvConfig = new BvConfig();
}
return bvConfig;
};
// Method for applying a global config
const setConfig = function () {
let config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
getConfigInstance().setConfig(config);
};
// Method for resetting the user config
// Exported for testing purposes only
const resetConfig = () => {
if (bvConfig) {
bvConfig.resetConfig();
}
};
export { getConfigInstance, resetConfig, setConfig };