all repos — nasg @ 370858a79580a31b80c9cca4b66b0acab4ae4704

templates/themeswitcher.js (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
var DEFAULT_THEME = 'dark';
var ALT_THEME = 'light';
var STORAGE_KEY = 'theme';
var theme_container = document.getElementById("header-forms");
var theme_insbefore = document.getElementById("search");
var colorscheme = [];
var mql = window.matchMedia('(prefers-color-scheme: ' + ALT_THEME + ')');

function indicateTheme(mode) {
    for(var i = colorscheme.length; i--; ) {
        if(colorscheme[i].value == mode) {
            colorscheme[i].checked = true;
        }
    }
}

function applyTheme(mode) {
    var st = document.getElementById('css_alt');
    if (mode == ALT_THEME) {
        st.setAttribute('media', 'all');
    }
    else {
        st.setAttribute('media', 'speech');
    }
}

function setTheme(e) {
    var mode = e.target.value;
    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)) {
        localStorage.removeItem(STORAGE_KEY);
    }
    else {
        if(confirm("I\'ll need to store your choice in your browser, in a place called localStorage.\n\nAre you OK with this?")) {
            localStorage.setItem(STORAGE_KEY, mode);
        }
    }
    autoTheme(mql);
}

function autoTheme(e) {
    var mode = DEFAULT_THEME;
    try {
        var current = localStorage.getItem(STORAGE_KEY);
    } catch(e) {
        var current = DEFAULT_THEME;
    }
    if ( current != null) {
        mode = current;
    }
    else if (e.matches) {
        mode = ALT_THEME;
    }
    applyTheme(mode);
    indicateTheme(mode);
}

function doTheme() {
    var themeform = document.createElement('form');
    themeform.className = "theme";
    themeform.innerHTML='<svg width="16" height="16"><use xlink:href="#icon-contrast"></use></svg>';
    theme_container.insertBefore(themeform, theme_insbefore);
    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);
}

var test = 'ping';
try {
    localStorage.setItem(test, test);
    localStorage.removeItem(test);
    doTheme();
} catch(e) {
    console.log('localStorage is not available, manual theme switching is disabled');
}