2018-11-15 13:35:47 +00:00
|
|
|
var DEFAULT_THEME = 'dark';
|
|
|
|
var ALT_THEME = 'light';
|
2018-11-15 14:06:35 +00:00
|
|
|
var STORAGE_KEY = 'theme';
|
2019-04-29 09:34:47 +01:00
|
|
|
var theme_container = document.getElementById("header-forms");
|
|
|
|
var theme_insbefore = document.getElementById("search");
|
2019-04-10 09:27:37 +01:00
|
|
|
var colorscheme = [];
|
|
|
|
var mql = window.matchMedia('(prefers-color-scheme: ' + ALT_THEME + ')');
|
2018-11-15 13:35:47 +00:00
|
|
|
|
2018-11-16 22:39:12 +00:00
|
|
|
function indicateTheme(mode) {
|
|
|
|
for(var i = colorscheme.length; i--; ) {
|
|
|
|
if(colorscheme[i].value == mode) {
|
|
|
|
colorscheme[i].checked = true;
|
|
|
|
}
|
2018-11-15 13:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:06:35 +00:00
|
|
|
function applyTheme(mode) {
|
|
|
|
var st = document.getElementById('css_alt');
|
|
|
|
if (mode == ALT_THEME) {
|
|
|
|
st.setAttribute('media', 'all');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
st.setAttribute('media', 'speech');
|
|
|
|
}
|
2018-11-15 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 22:39:12 +00:00
|
|
|
function setTheme(e) {
|
|
|
|
var mode = e.target.value;
|
2018-12-01 10:43:13 +00:00
|
|
|
var mql = window.matchMedia('(prefers-color-scheme: ' + ALT_THEME + ')');
|
|
|
|
/* user wants == mql match => remove storage */
|
|
|
|
if ((mode == DEFAULT_THEME && !mql.matches) || (mode == ALT_THEME && mql.matches)) {
|
2018-11-16 22:39:12 +00:00
|
|
|
localStorage.removeItem(STORAGE_KEY);
|
2018-11-15 13:35:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-12-13 14:11:05 +00:00
|
|
|
localStorage.setItem(STORAGE_KEY, mode);
|
2018-11-15 13:35:47 +00:00
|
|
|
}
|
2018-12-01 10:43:13 +00:00
|
|
|
autoTheme(mql);
|
2018-11-15 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 22:39:12 +00:00
|
|
|
function autoTheme(e) {
|
2018-12-01 10:43:13 +00:00
|
|
|
var mode = DEFAULT_THEME;
|
2019-01-15 21:28:58 +00:00
|
|
|
try {
|
|
|
|
var current = localStorage.getItem(STORAGE_KEY);
|
|
|
|
} catch(e) {
|
|
|
|
var current = DEFAULT_THEME;
|
|
|
|
}
|
2018-11-16 22:39:12 +00:00
|
|
|
if ( current != null) {
|
2018-12-01 10:43:13 +00:00
|
|
|
mode = current;
|
2018-11-16 22:39:12 +00:00
|
|
|
}
|
|
|
|
else if (e.matches) {
|
|
|
|
mode = ALT_THEME;
|
|
|
|
}
|
|
|
|
applyTheme(mode);
|
2018-12-01 10:43:13 +00:00
|
|
|
indicateTheme(mode);
|
2018-11-16 22:39:12 +00:00
|
|
|
}
|
2018-11-15 14:06:35 +00:00
|
|
|
|
2019-04-10 09:27:37 +01:00
|
|
|
function doTheme() {
|
|
|
|
var themeform = document.createElement('form');
|
2019-06-19 13:19:20 +01:00
|
|
|
themeform.id = "theme";
|
2019-04-10 09:27:37 +01:00
|
|
|
themeform.innerHTML='<svg width="16" height="16"><use xlink:href="#icon-contrast"></use></svg>';
|
2019-04-29 09:34:47 +01:00
|
|
|
theme_container.insertBefore(themeform, theme_insbefore);
|
2019-04-10 09:27:37 +01:00
|
|
|
var schemes = ["dark", "light"];
|
|
|
|
for (var i = 0; i < schemes.length; i++) {
|
|
|
|
var span = document.createElement('span');
|
|
|
|
themeform.appendChild(span);
|
|
|
|
|
|
|
|
var input = document.createElement('input');
|
|
|
|
input.name = 'colorscheme';
|
|
|
|
input.type = 'radio';
|
|
|
|
input.id = schemes[i] + input.name;
|
|
|
|
input.value = schemes[i];
|
|
|
|
span.appendChild(input);
|
|
|
|
|
|
|
|
var label = document.createElement('label');
|
|
|
|
label.htmlFor = input.id;
|
|
|
|
label.innerHTML = schemes[i];
|
|
|
|
span.appendChild(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
colorscheme = document.getElementsByName('colorscheme');
|
|
|
|
for(var i = colorscheme.length; i--; ) {
|
|
|
|
colorscheme[i].onclick = setTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
autoTheme(mql);
|
|
|
|
mql.addListener(autoTheme);
|
|
|
|
}
|
2018-11-16 22:39:12 +00:00
|
|
|
|
2019-01-15 21:28:58 +00:00
|
|
|
var test = 'ping';
|
|
|
|
try {
|
|
|
|
localStorage.setItem(test, test);
|
|
|
|
localStorage.removeItem(test);
|
2019-04-10 09:27:37 +01:00
|
|
|
doTheme();
|
2019-01-15 21:28:58 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log('localStorage is not available, manual theme switching is disabled');
|
2018-12-01 10:43:13 +00:00
|
|
|
}
|