/** * Settings. */ import { assertNotNull, assertNotUndefined } from './utils/assert'; import { $, $$ } from './utils/dom'; import store from './utils/store'; export function setupSettings() { if (!$('#js-setting-table')) return; const localCheckboxes = $$('[data-tab="local"] input[type="checkbox"]'); const themeSelect = assertNotNull($('#user_theme_name')); const themeColorSelect = assertNotNull($('#user_theme_color')); const themePaths: Record = JSON.parse( assertNotUndefined(assertNotNull($('#js-theme-paths')).dataset.themePaths), ); const styleSheet = assertNotNull($('#js-theme-stylesheet')); // Local settings localCheckboxes.forEach((checkbox) => { checkbox.addEventListener('change', () => { store.set(checkbox.id.replace('user_', ''), checkbox.checked); }); }); // Theme preview const themePreviewCallback = () => { const themeName = assertNotUndefined(themeSelect.options[themeSelect.selectedIndex].value); const themeColor = assertNotUndefined(themeColorSelect.options[themeColorSelect.selectedIndex].value); styleSheet.href = themePaths[`${themeName}-${themeColor}`]; }; themeSelect.addEventListener('change', themePreviewCallback); themeColorSelect.addEventListener('change', themePreviewCallback); }