treehouse/static/js/settings.js

30 lines
738 B
JavaScript
Raw Normal View History

2024-03-03 21:23:37 +01:00
import { addSpell } from "treehouse/spells.js";
2024-02-21 23:17:19 +01:00
const settingsKey = "treehouse.settings";
const settings = JSON.parse(localStorage.getItem(settingsKey)) || {};
const defaultSettingValues = {
showNewPostIndicator: true,
};
function saveSettings() {
localStorage.setItem(settingsKey, JSON.stringify(settings));
}
export function getSettingValue(setting) {
return settings[setting] ?? defaultSettingValues[setting];
}
2024-03-03 21:23:37 +01:00
class SettingCheckbox {
constructor(element) {
element.checked = getSettingValue(element.id);
2024-02-21 23:17:19 +01:00
2024-03-03 21:23:37 +01:00
element.addEventListener("change", () => {
settings[element.id] = element.checked;
2024-02-21 23:17:19 +01:00
saveSettings();
});
}
}
2024-03-03 21:23:37 +01:00
addSpell("setting-checkbox", SettingCheckbox);