handsontable
Version:
Handsontable is a JavaScript Data Grid available for React, Angular and Vue.
251 lines (240 loc) • 9.54 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.createTheme = createTheme;
var _object = require("../../helpers/object");
var _sizing = _interopRequireDefault(require("../static/variables/sizing"));
var _density = _interopRequireDefault(require("../static/variables/density"));
var _validation = require("./utils/validation");
var _console = require("../../helpers/console");
var _errors = require("../../helpers/errors");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
/**
* Config keys that support deep merging when updating theme params.
*
* @type {string[]}
*/
const MERGEABLE_CONFIG_KEYS = ['sizing', 'icons', 'colors', 'tokens'];
/**
* Required config keys.
*
* @type {string[]}
*/
const REQUIRED_CONFIG_KEYS = ['name', 'icons', 'colors', 'tokens'];
/**
* ThemeBuilder class provides methods to build and configure themes.
*
* @class ThemeBuilder
*/
var _initThemeConfig = /*#__PURE__*/new WeakMap();
var _themeConfig = /*#__PURE__*/new WeakMap();
var _listeners = /*#__PURE__*/new WeakMap();
var _colorScheme = /*#__PURE__*/new WeakMap();
var _densityType = /*#__PURE__*/new WeakMap();
var _ThemeBuilder_brand = /*#__PURE__*/new WeakSet();
class ThemeBuilder {
/**
* The theme builder constructor.
*
* @param {object} themeConfig - The theme config object with `name`, `sizing`, `density`, `icons`, `colors`, `tokens`, and `colorScheme` properties.
*/
constructor(themeConfig) {
/**
* Notifies all listeners about a theme configuration change.
*
* @private
*/
_classPrivateMethodInitSpec(this, _ThemeBuilder_brand);
/**
* The initial theme configuration object.
*
* @private
* @type {object}
*/
_classPrivateFieldInitSpec(this, _initThemeConfig, void 0);
/**
* Theme configuration object.
*
* @private
* @type {object}
*/
_classPrivateFieldInitSpec(this, _themeConfig, void 0);
/**
* Registered listeners notified when the theme configuration changes.
*
* @private
* @type {Set<Function>}
*/
_classPrivateFieldInitSpec(this, _listeners, new Set());
/**
* Current color mode ('light', 'dark', or 'auto').
*
* @private
* @default 'auto'
* @type {string}
*/
_classPrivateFieldInitSpec(this, _colorScheme, 'auto');
/**
* Current density type ('compact', 'default', or 'comfortable').
*
* @private
* @default 'default'
* @type {string}
*/
_classPrivateFieldInitSpec(this, _densityType, 'default');
(0, _validation.validateParams)(themeConfig, 'themeConfig', {
requiredFields: REQUIRED_CONFIG_KEYS
});
_classPrivateFieldSet(_initThemeConfig, this, {
name: undefined,
sizing: _sizing.default,
density: {
type: _classPrivateFieldGet(_densityType, this),
sizes: _density.default
},
icons: {},
colors: {},
tokens: {},
colorScheme: _classPrivateFieldGet(_colorScheme, this)
});
_assertClassBrand(_ThemeBuilder_brand, this, _setParams).call(this, themeConfig);
_classPrivateFieldSet(_initThemeConfig, this, _classPrivateFieldGet(_themeConfig, this));
}
/**
* Subscribes to theme configuration changes.
*
* @param {Function} listener The callback invoked with the updated theme configuration.
* @returns {Function} An unsubscribe function.
*/
subscribe(listener) {
if (typeof listener !== 'function') {
(0, _errors.throwWithCause)('[ThemeBuilder] listener must be a function.');
}
_classPrivateFieldGet(_listeners, this).add(listener);
return () => _classPrivateFieldGet(_listeners, this).delete(listener);
}
/**
* Sets theme configuration parameters.
*
* @param {object} paramsObject An object with theme configuration parameters.
* @returns {ThemeBuilder} The ThemeBuilder instance for chaining.
*/
params(paramsObject) {
(0, _validation.validateParams)(paramsObject, 'params');
_assertClassBrand(_ThemeBuilder_brand, this, _setParams).call(this, paramsObject);
_assertClassBrand(_ThemeBuilder_brand, this, _notifyChange).call(this);
return this;
}
/**
* Sets the color mode (light, dark, or auto).
*
* @param {string} mode The color mode ('light', 'dark', or 'auto').
* @returns {ThemeBuilder} The ThemeBuilder instance for chaining.
*/
setColorScheme(mode) {
_classPrivateFieldSet(_colorScheme, this, (0, _validation.validateColorScheme)(mode));
_classPrivateFieldGet(_themeConfig, this).colorScheme = _classPrivateFieldGet(_colorScheme, this);
_classPrivateFieldGet(_initThemeConfig, this).colorScheme = _classPrivateFieldGet(_colorScheme, this);
_assertClassBrand(_ThemeBuilder_brand, this, _notifyChange).call(this);
return this;
}
/**
* Sets the density type (compact, default, or comfortable).
*
* @param {string} type The density type ('compact', 'default', or 'comfortable').
* @returns {ThemeBuilder} The ThemeBuilder instance for chaining.
*/
setDensityType(type) {
_classPrivateFieldSet(_densityType, this, (0, _validation.validateDensityType)(type));
_classPrivateFieldGet(_themeConfig, this).density.type = _classPrivateFieldGet(_densityType, this);
_classPrivateFieldGet(_initThemeConfig, this).density.type = _classPrivateFieldGet(_densityType, this);
_assertClassBrand(_ThemeBuilder_brand, this, _notifyChange).call(this);
return this;
}
/**
* Gets the current theme configuration.
*
* @returns {ThemeConfig} The theme configuration object.
*/
getThemeConfig() {
return _classPrivateFieldGet(_themeConfig, this);
}
}
/**
* Creates a new ThemeBuilder instance.
*
* @param {object} baseTheme The base theme object with theme configuration parameters.
* @returns {ThemeBuilder} The ThemeBuilder instance.
*/
function _notifyChange() {
const config = this.getThemeConfig();
_classPrivateFieldGet(_listeners, this).forEach(listener => listener(config));
}
/**
* Applies density configuration from the params object.
*
* @private
* @param {object} config The config object to modify.
* @param {string|object} density The density value from params.
*/
function _applyDensityConfig(config, density) {
if ((0, _object.isObject)(density)) {
var _classPrivateFieldGet2;
config.density = (0, _object.deepMerge)(config.density, density);
_classPrivateFieldSet(_densityType, this, density.type);
if ((_classPrivateFieldGet2 = _classPrivateFieldGet(_initThemeConfig, this)) !== null && _classPrivateFieldGet2 !== void 0 && _classPrivateFieldGet2.density) {
_classPrivateFieldGet(_initThemeConfig, this).density.type = density.type;
}
} else if (typeof density === 'string') {
var _classPrivateFieldGet3;
config.density.type = density;
_classPrivateFieldSet(_densityType, this, density);
if ((_classPrivateFieldGet3 = _classPrivateFieldGet(_initThemeConfig, this)) !== null && _classPrivateFieldGet3 !== void 0 && _classPrivateFieldGet3.density) {
_classPrivateFieldGet(_initThemeConfig, this).density.type = density;
}
}
}
/**
* Sets the parameters to the theme configuration.
*
* @private
* @param {object} paramsObject The parameters object to set.
*/
function _setParams(paramsObject) {
const config = (0, _object.deepClone)(_classPrivateFieldGet(_initThemeConfig, this));
if (paramsObject.name !== undefined) {
if (_classPrivateFieldGet(_initThemeConfig, this).name !== undefined) {
(0, _console.warn)('[ThemeBuilder] The "name" property can only be set during ' + '`registerTheme()` and cannot be updated via `params()`.');
} else {
config.name = paramsObject.name;
}
}
// Apply mergeable config keys
MERGEABLE_CONFIG_KEYS.forEach(key => {
if (paramsObject[key] !== undefined && (0, _object.isObject)(paramsObject[key])) {
config[key] = (0, _object.deepMerge)(config[key], paramsObject[key]);
}
});
// Apply density (special handling for string or object)
if (paramsObject.density !== undefined) {
_assertClassBrand(_ThemeBuilder_brand, this, _applyDensityConfig).call(this, config, paramsObject.density);
}
// Apply color scheme
if (paramsObject.colorScheme !== undefined) {
var _classPrivateFieldGet4;
config.colorScheme = paramsObject.colorScheme;
_classPrivateFieldSet(_colorScheme, this, paramsObject.colorScheme);
if ((_classPrivateFieldGet4 = _classPrivateFieldGet(_initThemeConfig, this)) !== null && _classPrivateFieldGet4 !== void 0 && _classPrivateFieldGet4.colorScheme) {
_classPrivateFieldGet(_initThemeConfig, this).colorScheme = paramsObject.colorScheme;
}
}
_classPrivateFieldSet(_themeConfig, this, config);
}
function createTheme(baseTheme) {
return new ThemeBuilder(baseTheme);
}