UNPKG

@adaptabletools/adaptable

Version:

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

118 lines (117 loc) 5.12 kB
import { DARK_THEME, LIGHT_THEME } from '../Utilities/Constants/GeneralConstants'; export class AgGridThemeAdapter { _adaptableInstance; agGridThemeMode; constructor(_adaptableInstance) { this._adaptableInstance = _adaptableInstance; } destroy() { } get logger() { return this._adaptableInstance.logger; } get api() { return this._adaptableInstance.api; } setAgGridThemeMode(themeMode) { this.agGridThemeMode = themeMode; } getAgGridThemeMode() { return this.agGridThemeMode; } applyAgGridThemeOnAdaptableThemeChange(adaptableTheme, variantTheme, agGridContainer, themesToRemove) { if (this.agGridThemeMode === 'legacy') { this.legacy_applyAgGridThemeOnAdaptableThemeChange(adaptableTheme, variantTheme, agGridContainer, themesToRemove); } const themeName = adaptableTheme.Name; const isSystemTheme = this.api.themeApi.internalApi.isSystemTheme(themeName); if (adaptableTheme && (isSystemTheme || variantTheme)) { if ((variantTheme || themeName) === LIGHT_THEME) { document.body.dataset.agThemeMode = adaptableTheme.AgThemeMode ?? 'light'; } if ((variantTheme || themeName) === DARK_THEME) { document.body.dataset.agThemeMode = adaptableTheme.AgThemeMode ?? 'dark'; } } } getAgGridContainerElement() { return this._adaptableInstance.getAgGridContainerElement(); } legacy_applyAgGridThemeOnAdaptableThemeChange(adaptableTheme, variantTheme, agGridContainer, themesToRemove) { const themeName = adaptableTheme.Name; const isSystemTheme = this.api.themeApi.internalApi.isSystemTheme(themeName); const getAgGridLightThemeName = () => this.getAgGridLightThemeName(); const getAgGridDarkThemeName = () => getAgGridLightThemeName() + '-dark'; if (adaptableTheme && (isSystemTheme || variantTheme)) { if ((variantTheme || themeName) === LIGHT_THEME) { adaptableTheme.AgGridClassName = adaptableTheme.AgGridClassName || getAgGridLightThemeName(); } if ((variantTheme || themeName) === DARK_THEME) { adaptableTheme.AgGridClassName = adaptableTheme.AgGridClassName || getAgGridDarkThemeName(); } } if (!adaptableTheme.AgGridClassName) { adaptableTheme.AgGridClassName = getAgGridLightThemeName(); } if (agGridContainer != null) { if (themesToRemove.length) { themesToRemove.forEach((theme) => { if (theme.AgGridClassName) { agGridContainer.classList.remove(theme.AgGridClassName); } }); } const agGridClassNamesToRemove = []; agGridContainer.classList.forEach((x) => { if (x && x.indexOf('ag-theme-') === 0) { agGridClassNamesToRemove.push(x); } }); agGridClassNamesToRemove.forEach((x) => agGridContainer.classList.remove(x)); if (adaptableTheme && adaptableTheme.AgGridClassName) { agGridContainer.classList.add(adaptableTheme.AgGridClassName); } } } getAgGridCurrentThemeClassNames() { if (this.agGridThemeMode === 'legacy') { this.legacy_getAgGridCurrentThemeClassNames(); } const currentAgGridTheme = this._adaptableInstance.agGridAdapter.getGridOption('theme'); if (currentAgGridTheme === 'legacy') { return this.legacy_getAgGridCurrentThemeClassNames(); } const currentAgGridTheme__cssClassCache = currentAgGridTheme?._cssClassCache; return currentAgGridTheme__cssClassCache ?? ''; } getAgGridLightThemeName() { const container = this.getAgGridContainerElement(); if (container && container.classList) { const classList = container.classList; for (let i = 0, len = classList.length; i < len; i++) { const cls = classList[i]; if (cls.indexOf('ag-theme-') === 0) { return cls.replace('-dark', ''); } } } else { this.logger.warn('AG Grid container not found. Defaulting to ag-theme-balham for the light theme.'); } this.logger.warn('No ag-theme-* class found on the grid container. Defaulting to ag-theme-balham.'); return 'ag-theme-balham'; } legacy_getAgGridCurrentThemeClassNames() { const container = this.getAgGridContainerElement(); if (container && container.classList) { const classList = container.classList; for (let i = 0, len = classList.length; i < len; i++) { const cls = classList[i]; if (cls.indexOf('ag-theme-') === 0) { return cls; } } } return this.getAgGridLightThemeName(); } }