Zulma-Theme/static/js/switchcss.js

101 lines
3.3 KiB
JavaScript
Raw Normal View History

(function (switch_css) {
//Constants
const THEME_KEY = "ZULMA_THEME";
const STOP_LINK_CSS_ID = "stop-blink";
2019-05-06 22:16:37 +01:00
//Variables
let link = null;
let theme = localStorage.getItem(THEME_KEY);
2019-05-06 23:18:32 +01:00
//Private Methods
2019-05-07 14:19:42 +01:00
/* Called when the theme is changed */
function changeTheme(themeName, firstLoad) {
2019-05-07 14:19:42 +01:00
//create the css link element
var fileref = document.createElement("link");
fileref.rel = "stylesheet";
fileref.type = "text/css";
fileref.href = `/${themeName}.css`;
2019-05-07 14:19:42 +01:00
//append it to the head
link = document.getElementsByTagName("head")[0].appendChild(fileref);
2019-05-07 14:19:42 +01:00
//when it's loaded, call onLinkLoad
link.addEventListener('load', onLinkLoad);
2019-05-07 14:19:42 +01:00
//if this is the first load of the page, remove the current stylesheet early to avoid flash of wrongly styled content
if (firstLoad) {
document.querySelectorAll('.stylesheet').forEach((el) => {
el.remove();
});
}
saveTheme(themeName);
};
2019-05-07 14:19:42 +01:00
/* The function called when the css has finished loading */
function onLinkLoad() {
link.removeEventListener('load', onLinkLoad);
2019-05-07 14:19:42 +01:00
//remove the previous stylesheet(s)
document.querySelectorAll('.stylesheet').forEach((el) => {
el.remove();
});
2019-05-07 14:19:42 +01:00
//add stylesheet class
link.className += 'stylesheet';
2019-05-07 14:19:42 +01:00
//make body visible again if it was hidden
showBody();
};
2019-05-07 14:19:42 +01:00
/* Saves the current theme in localstorage */
function saveTheme(themeName) {
localStorage.setItem(THEME_KEY, themeName);
};
2019-05-07 14:19:42 +01:00
/* Adds the given css to the head. */
function hideBody() {
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.id = STOP_LINK_CSS_ID;
style.setAttribute('type', 'text/css');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
2019-05-07 14:19:42 +01:00
style.appendChild(document.createTextNode('body{visibility:hidden;}'));
}
2019-05-07 14:19:42 +01:00
head.appendChild(style);
};
function showBody() {
let css = document.getElementById(STOP_LINK_CSS_ID);
if (css)
css.remove();
};
//Public Methods
switch_css.init = function () {
2019-05-07 14:19:42 +01:00
//if user has selected and theme and it is not the current theme
if (theme && !document.getElementById(theme)) {
2019-05-07 14:19:42 +01:00
//hide the body to stop FOUC
hideBody();
//change the theme
changeTheme(theme, true);
2019-05-07 14:19:42 +01:00
//when the DOM is loaded, change the select to their current choice
window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('#theme-select>option').forEach(element => {
if (element.value === theme) {
element.selected = 'selected';
}
});
});
}
2019-05-07 14:19:42 +01:00
//when the DOM is loaded, set the dropdown to trigger the theme change
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('theme-select').onchange = function () {
changeTheme(this.value);
}
});
}
}(switch_css = window.switch_css || {}));
switch_css.init();