UNPKG

@adaptabletools/adaptable

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

93 lines (92 loc) 3.61 kB
import * as ThemeRedux from '../../Redux/ActionsReducers/ThemeRedux'; import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants'; import { ApiBase } from './ApiBase'; import { StaticThemes } from '../../themes'; import { ThemeInternalApi } from '../Internal/ThemeInternalApi'; import AdaptableHelper from '../../Utilities/Helpers/AdaptableHelper'; import { logDeprecation } from '../../Utilities/logDeprecation'; export class ThemeApiImpl extends ApiBase { constructor(_adaptable) { super(_adaptable); this.internalApi = new ThemeInternalApi(_adaptable); } getThemeState() { return this.getAdaptableState().Theme; } loadTheme(theme) { this.dispatchAction(ThemeRedux.ThemeSelect(theme)); } loadLightTheme() { this.loadTheme('light'); } loadDarkTheme() { this.loadTheme('dark'); } getCurrentTheme() { return this.getAdaptableState().Theme.CurrentTheme; } getCurrentThemeObject() { const currentTheme = this.getCurrentTheme(); return this.getThemes().find((themeObj) => themeObj.Name === currentTheme); } setSystemThemes(systemThemes) { this.dispatchAction(ThemeRedux.ThemeSetSystemThemes(systemThemes)); } setUserThemes(userThemes) { this.dispatchAction(ThemeRedux.ThemeSetUserThemes(userThemes)); } getSystemThemes() { const themes = this.getAdaptableState().Theme.SystemThemes ?? []; return themes.map((theme) => { if (typeof theme === 'string') { const description = StaticThemes.find((staticTheme) => staticTheme.Name === theme)?.Description ?? theme; return { Name: theme, Description: description, }; } return theme; }); } applyCurrentTheme() { const currentTheme = this.getThemes().filter((theme) => typeof theme === 'string' ? theme === this.getCurrentTheme() : theme.Name === this.getCurrentTheme())[0]; if (!currentTheme) { return; } this._adaptable.applyAdaptableTheme(currentTheme); this.getEventApi().internalApi.fireThemeChangedEvent(currentTheme, 'ThemeSelected'); } getUserThemes() { return this.getAdaptableState().Theme.UserThemes ?? []; } getThemes() { return [...this.getSystemThemes(), ...this.getUserThemes()]; } getThemeByName(themeName) { return this.getThemes().find((theme) => theme.Name === themeName); } openThemeSettingsPanel() { this.showModulePopup(ModuleConstants.ThemeModuleId); } getAgGridCurrentThemeName() { logDeprecation(this._adaptable.logger, 'ThemeApi', 'getAgGridCurrentThemeName', null, `This is required only for AG Grid's legacy themes. See https://www.ag-grid.com/javascript-data-grid/theming-v32/`); return this._adaptable.agGridThemeAdapter.getAgGridCurrentThemeClassNames(); } editTheme(theme) { this.dispatchAction(ThemeRedux.ThemeEdit(theme)); this.getEventApi().internalApi.fireThemeChangedEvent(theme, 'ThemeEdited'); const currentTheme = this.getCurrentTheme(); if (currentTheme === theme.Name) { this._adaptable.applyAdaptableTheme(theme); } } addUserTheme(theme) { AdaptableHelper.addAdaptableObjectPrimitives(theme); this.dispatchAction(ThemeRedux.ThemeAdd(theme)); } deleteUserTheme(theme) { this.dispatchAction(ThemeRedux.ThemeDelete(theme)); } }