Zulma-Theme/static/js/switchcss.js
2019-05-06 23:18:32 +01:00

44 lines
No EOL
1.1 KiB
JavaScript

const THEME_KEY = "ZULMA_THEME";
let theme = localStorage.getItem(THEME_KEY);
if (theme) {
changeTheme(theme);
window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('#theme-select>option').forEach(element => {
if (element.value === theme) {
element.selected = 'selected';
}
});
});
}
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('theme-select').onchange = function () {
changeTheme(this.value);
}
});
function changeTheme(themeName) {
let alternates = [];
document.querySelectorAll('link.stylesheet').forEach(element => {
if (element.id === themeName) {
element.disabled = false;
element.media = '';
}
else {
alternates.push(element);
}
});
alternates.forEach(element => {
element.disabled = true;
element.media = 'none';
});
saveTheme(themeName);
}
function saveTheme(themeName) {
localStorage.setItem(THEME_KEY, themeName);
}