2019-05-07 13:46:46 +01:00
|
|
|
(function (switch_css) {
|
|
|
|
//Constants
|
|
|
|
const THEME_KEY = "ZULMA_THEME";
|
|
|
|
const STOP_LINK_CSS_ID = "stop-blink";
|
2019-05-06 22:16:37 +01:00
|
|
|
|
2019-05-07 13:46:46 +01:00
|
|
|
//Variables
|
|
|
|
let link = null;
|
|
|
|
let theme = localStorage.getItem(THEME_KEY);
|
2019-05-06 23:18:32 +01:00
|
|
|
|
2019-05-07 13:46:46 +01:00
|
|
|
//Private Methods
|
|
|
|
function changeTheme(themeName, firstLoad) {
|
|
|
|
var fileref = document.createElement("link");
|
|
|
|
fileref.rel = "stylesheet";
|
|
|
|
fileref.type = "text/css";
|
|
|
|
fileref.href = `/${themeName}.css`;
|
|
|
|
link = document.getElementsByTagName("head")[0].appendChild(fileref);
|
2019-05-06 00:23:38 +00:00
|
|
|
|
2019-05-07 13:46:46 +01:00
|
|
|
link.addEventListener('load', onLinkLoad);
|
|
|
|
if (firstLoad) {
|
|
|
|
document.querySelectorAll('.stylesheet').forEach((el) => {
|
|
|
|
el.remove();
|
|
|
|
});
|
2019-05-06 00:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 13:46:46 +01:00
|
|
|
saveTheme(themeName);
|
|
|
|
};
|
|
|
|
|
|
|
|
function onLinkLoad() {
|
|
|
|
link.removeEventListener('load', onLinkLoad);
|
|
|
|
document.querySelectorAll('.stylesheet').forEach((el) => {
|
|
|
|
el.remove();
|
|
|
|
});
|
|
|
|
link.className += 'stylesheet';
|
|
|
|
addcss('body{visibility:visible;}');
|
|
|
|
};
|
|
|
|
|
|
|
|
function saveTheme(themeName) {
|
|
|
|
localStorage.setItem(THEME_KEY, themeName);
|
|
|
|
};
|
2019-05-06 00:23:38 +00:00
|
|
|
|
2019-05-07 13:46:46 +01:00
|
|
|
function addcss(css) {
|
|
|
|
let el = document.getElementById(STOP_LINK_CSS_ID);
|
|
|
|
if (el) {
|
|
|
|
el.innerText = css;
|
|
|
|
} else {
|
|
|
|
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) { // IE
|
|
|
|
style.styleSheet.cssText = css;
|
|
|
|
} else { // the world
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
}
|
|
|
|
head.insertBefore(style, head.firstChild);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//Public Methods
|
|
|
|
switch_css.init = function () {
|
|
|
|
if (theme && !document.getElementById(theme)) {
|
|
|
|
addcss('body{visibility:hidden;}')
|
|
|
|
changeTheme(theme, true);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}(switch_css = window.switch_css || {}));
|
2019-05-06 00:23:38 +00:00
|
|
|
|
2019-05-07 13:46:46 +01:00
|
|
|
switch_css.init();
|