philomena/assets/js/settings.ts

38 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-10-05 02:09:52 +02:00
/**
* Settings.
*/
import { assertNotNull, assertNotUndefined } from './utils/assert';
2019-10-05 02:09:52 +02:00
import { $, $$ } from './utils/dom';
import store from './utils/store';
export function setupSettings() {
if (!$('#js-setting-table')) return;
const localCheckboxes = $$<HTMLInputElement>('[data-tab="local"] input[type="checkbox"]');
2024-07-03 21:32:27 +02:00
const themeSelect = assertNotNull($<HTMLSelectElement>('#user_theme_name'));
const themeColorSelect = assertNotNull($<HTMLSelectElement>('#user_theme_color'));
2024-07-03 22:00:12 +02:00
const themePaths: Record<string, string> = JSON.parse(
2024-07-03 22:54:14 +02:00
assertNotUndefined(assertNotNull($<HTMLDivElement>('#js-theme-paths')).dataset.themePaths),
2024-07-03 22:00:12 +02:00
);
2024-07-03 21:32:27 +02:00
const styleSheet = assertNotNull($<HTMLLinkElement>('#js-theme-stylesheet'));
2019-10-05 02:09:52 +02:00
// Local settings
2024-07-03 22:54:14 +02:00
localCheckboxes.forEach((checkbox) => {
2019-10-05 02:09:52 +02:00
checkbox.addEventListener('change', () => {
store.set(checkbox.id.replace('user_', ''), checkbox.checked);
2019-10-05 02:09:52 +02:00
});
});
// Theme preview
2024-07-03 21:32:27 +02:00
const themePreviewCallback = () => {
const themeName = assertNotUndefined(themeSelect.options[themeSelect.selectedIndex].value);
const themeColor = assertNotUndefined(themeColorSelect.options[themeColorSelect.selectedIndex].value);
2024-07-03 22:00:12 +02:00
styleSheet.href = themePaths[`${themeName}-${themeColor}`];
2024-07-03 21:32:27 +02:00
};
themeSelect.addEventListener('change', themePreviewCallback);
themeColorSelect.addEventListener('change', themePreviewCallback);
2019-10-05 02:09:52 +02:00
}