UNPKG

handsontable

Version:

Handsontable is a JavaScript Data Grid available for React, Angular and Vue.

131 lines (121 loc) 4.03 kB
"use strict"; exports.__esModule = true; exports.getTheme = getTheme; exports.getThemeNames = getThemeNames; exports.getThemes = getThemes; exports.hasTheme = hasTheme; exports.registerTheme = registerTheme; exports.reinitTheme = reinitTheme; var _object = require("../helpers/object"); var _console = require("../helpers/console"); var _staticRegister = require("../utils/staticRegister"); var _engine = require("./engine"); const { hasItem, getItem, getNames, getValues, register } = (0, _staticRegister.staticRegister)('themes'); /** * Check if a theme with the specified name is registered. * * @param {string} themeName The theme name. * @returns {boolean} */ function hasTheme(themeName) { return hasItem(themeName); } /** * Get a registered theme by name. * * @param {string} themeName The theme name. * @returns {object|undefined} The theme or undefined if not found. */ function getTheme(themeName) { if (!hasTheme(themeName)) { (0, _console.warn)(`Theme "${themeName}" is not registered. Please ensure it is registered before using it.`); return undefined; } return getItem(themeName); } /** * Get all registered theme names. * * @returns {string[]} */ function getThemeNames() { return getNames(); } /** * Get all registered themes. * * @returns {object[]} */ function getThemes() { return getValues(); } /** * Parse theme name and config from arguments. * * @private * @param {string|object} themeNameOrConfig Theme name for specific theme or object representing theme config. * @param {object} [themeConfig] The theme config object (optional if first parameter has already theme config). * @returns {{themeName: string, themeConfigObject: object}} Parsed theme name and config object. */ function parseThemeArgs(themeNameOrConfig, themeConfig) { let themeName = themeNameOrConfig; let themeConfigObject = (0, _object.deepClone)(themeConfig); if (typeof themeNameOrConfig === 'string' && (0, _object.isObject)(themeConfig)) { themeConfigObject.name = themeNameOrConfig; } else if ((0, _object.isObject)(themeNameOrConfig)) { themeConfigObject = (0, _object.deepClone)(themeNameOrConfig); themeName = themeConfigObject.name; } return { themeName, themeConfigObject }; } /** * Register a theme. * * @param {string|object} themeNameOrConfig Theme name for specific theme or object representing theme config. * @param {object} [themeConfig] The theme config object (optional if first parameter has already theme config). * @returns {object} The registered theme (ThemeBuilder instance). */ function registerTheme(themeNameOrConfig, themeConfig) { const { themeName, themeConfigObject } = parseThemeArgs(themeNameOrConfig, themeConfig); const theme = (0, _engine.createTheme)(themeConfigObject); if (hasTheme(themeName)) { (0, _console.warn)(`Theme "${themeName}" is already registered. Registration skipped.`); return getTheme(themeName); } register(themeName, theme); return theme; } /** * Reinitialize an existing theme with a new configuration. * * @param {string|object} themeNameOrConfig Theme name for specific theme or object representing theme config. * @param {object} [themeConfig] The theme config object to reinitialize (optional if first parameter has already theme config). * @returns {object|undefined} The reinitialized theme (ThemeBuilder instance) or undefined if theme not found. */ function reinitTheme(themeNameOrConfig, themeConfig) { const { themeName, themeConfigObject } = parseThemeArgs(themeNameOrConfig, themeConfig); if (!hasTheme(themeName)) { (0, _console.warn)(`Theme "${themeName}" is not registered. Cannot reinitialize a non-existent theme.`); return undefined; } // Create a new theme with the new config const reinitializedTheme = (0, _engine.createTheme)(themeConfigObject); // Re-register the theme (this will replace the existing one) register(themeName, reinitializedTheme); return reinitializedTheme; }