2026-04-08 21:31:56 -04:00
|
|
|
import { withBasePath } from './base-path.mjs';
|
|
|
|
|
|
2026-04-08 12:49:21 -04:00
|
|
|
const THEME_STORAGE_KEY = 'settings-theme-select';
|
|
|
|
|
const DEFAULT_THEME = 'default';
|
2026-04-08 21:31:56 -04:00
|
|
|
|
2026-04-08 12:49:21 -04:00
|
|
|
const BUILTIN_ASSETS = {
|
2026-04-08 21:31:56 -04:00
|
|
|
background1: 'images/backgrounds/1.png',
|
|
|
|
|
background1Chart: 'images/backgrounds/1-chart.png',
|
|
|
|
|
background2: 'images/backgrounds/2.png',
|
|
|
|
|
background3: 'images/backgrounds/3.png',
|
|
|
|
|
background4: 'images/backgrounds/4.png',
|
|
|
|
|
background5: 'images/backgrounds/5.png',
|
2026-04-09 22:35:27 -04:00
|
|
|
background6: 'images/backgrounds/6.png',
|
2026-04-08 12:49:21 -04:00
|
|
|
logoCorner: 'images/logos/logo-corner.png',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getThemeAssets = () => window.WS4KP_THEME_ASSETS ?? {};
|
|
|
|
|
const getAvailableThemes = () => window.WS4KP_THEMES ?? [DEFAULT_THEME];
|
|
|
|
|
|
|
|
|
|
const getStoredTheme = () => {
|
|
|
|
|
const storedTheme = localStorage.getItem(THEME_STORAGE_KEY) ?? DEFAULT_THEME;
|
|
|
|
|
return getAvailableThemes().includes(storedTheme) ? storedTheme : DEFAULT_THEME;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getThemeAssetUrl = (themeName, assetKey) => {
|
|
|
|
|
if (themeName === DEFAULT_THEME) {
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(BUILTIN_ASSETS[assetKey]);
|
2026-04-08 12:49:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const themeAssetAvailability = getThemeAssets()[themeName] ?? {};
|
|
|
|
|
if (!themeAssetAvailability[assetKey]) {
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(BUILTIN_ASSETS[assetKey]);
|
2026-04-08 12:49:21 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 15:16:20 -04:00
|
|
|
switch (assetKey) {
|
2026-04-08 12:49:21 -04:00
|
|
|
case 'background1':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/1.png`);
|
2026-04-08 15:16:20 -04:00
|
|
|
case 'background1Chart':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/1-chart.png`);
|
2026-04-08 12:49:21 -04:00
|
|
|
case 'background2':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/2.png`);
|
2026-04-08 15:16:20 -04:00
|
|
|
case 'background3':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/3.png`);
|
2026-04-08 15:16:20 -04:00
|
|
|
case 'background4':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/4.png`);
|
2026-04-08 15:16:20 -04:00
|
|
|
case 'background5':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/5.png`);
|
2026-04-09 22:35:27 -04:00
|
|
|
case 'background6':
|
|
|
|
|
return withBasePath(`themes/${themeName}/6.png`);
|
2026-04-08 12:49:21 -04:00
|
|
|
case 'logoCorner':
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(`themes/${themeName}/logo-corner.png`);
|
2026-04-08 12:49:21 -04:00
|
|
|
default:
|
2026-04-08 21:31:56 -04:00
|
|
|
return withBasePath(BUILTIN_ASSETS[assetKey]);
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-04-08 12:49:21 -04:00
|
|
|
|
|
|
|
|
const applyTheme = (themeName) => {
|
|
|
|
|
const selectedTheme = getAvailableThemes().includes(themeName) ? themeName : DEFAULT_THEME;
|
|
|
|
|
localStorage.setItem(THEME_STORAGE_KEY, selectedTheme);
|
|
|
|
|
|
|
|
|
|
document.documentElement.style.setProperty('--theme-background-1', `url('${getThemeAssetUrl(selectedTheme, 'background1')}')`);
|
2026-04-08 15:16:20 -04:00
|
|
|
document.documentElement.style.setProperty('--theme-background-1-chart', `url('${getThemeAssetUrl(selectedTheme, 'background1Chart')}')`);
|
2026-04-08 12:49:21 -04:00
|
|
|
document.documentElement.style.setProperty('--theme-background-2', `url('${getThemeAssetUrl(selectedTheme, 'background2')}')`);
|
2026-04-08 15:16:20 -04:00
|
|
|
document.documentElement.style.setProperty('--theme-background-3', `url('${getThemeAssetUrl(selectedTheme, 'background3')}')`);
|
|
|
|
|
document.documentElement.style.setProperty('--theme-background-4', `url('${getThemeAssetUrl(selectedTheme, 'background4')}')`);
|
|
|
|
|
document.documentElement.style.setProperty('--theme-background-5', `url('${getThemeAssetUrl(selectedTheme, 'background5')}')`);
|
2026-04-09 22:35:27 -04:00
|
|
|
document.documentElement.style.setProperty('--theme-background-6', `url('${getThemeAssetUrl(selectedTheme, 'background6')}')`);
|
2026-04-08 12:49:21 -04:00
|
|
|
|
|
|
|
|
document.querySelectorAll('.theme-logo').forEach((img) => {
|
|
|
|
|
img.src = getThemeAssetUrl(selectedTheme, 'logoCorner');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const select = document.querySelector('#theme-select');
|
|
|
|
|
if (select && select.value !== selectedTheme) {
|
|
|
|
|
select.value = selectedTheme;
|
|
|
|
|
}
|
|
|
|
|
return selectedTheme;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
applyTheme(getStoredTheme());
|
|
|
|
|
const select = document.querySelector('#theme-select');
|
|
|
|
|
if (!select) return;
|
|
|
|
|
select.addEventListener('change', (event) => applyTheme(event.target.value));
|
|
|
|
|
select.value = getStoredTheme();
|
|
|
|
|
applyTheme(select.value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
applyTheme,
|
|
|
|
|
getStoredTheme,
|
|
|
|
|
};
|