opengist/public/admin.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

document.addEventListener('DOMContentLoaded', () => {
2023-05-11 13:16:05 +00:00
let elems = Array.from(document.getElementsByClassName("toggle-button"));
for (let elem of elems) {
elem.addEventListener('click', () => {
registerDomSetting(elem as HTMLElement)
})
}
});
const setSetting = (key: string, value: string) => {
const data = new URLSearchParams();
data.append('key', key);
data.append('value', value);
data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value));
2023-05-11 13:16:05 +00:00
return fetch('/admin-panel/set-setting', {
method: 'PUT',
credentials: 'same-origin',
body: data,
});
};
2023-05-11 13:16:05 +00:00
const registerDomSetting = (el: HTMLElement) => {
// @ts-ignore
el.dataset["bool"] = !(el.dataset["bool"] === 'true');
setSetting(el.id, el.dataset["bool"] === 'true' ? '1' : '0')
.then(() => {
el.classList.toggle("bg-primary-600");
2023-05-27 11:58:08 +00:00
el.classList.toggle("dark:bg-gray-400");
el.classList.toggle("bg-gray-300");
2023-05-11 13:16:05 +00:00
(el.childNodes.item(1) as HTMLElement).classList.toggle("translate-x-5");
});
};