summaryrefslogtreecommitdiffstats
path: root/assets/js/settings.js
blob: 1dd3bfc6cdd257f5452f2b010fb9ed11ef278d78 (plain) (blame)
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
// settings.js -- TODO

async function setLanguage(langCode)  {
    localforage.setItem("chosenLang", langCode).then((value) => {
        console.log("Language set: " + value);
        UIAlert(D("languageSet"), "setLanguage(): languageSet");
    });
}

async function setTheme(targetTheme) {
    localforage.setItem("theme", targetTheme).then((value) => {
        console.log("Theme set: " + value);
        UIAlert(D("themeSet"), "setTheme(): themeSet");
    });
}

async function setErrorReporting(targetE) {
    localforage.setItem("errorReporting", targetE).then((value) => {
        console.log("ErrorReporing set: " + value);
        UIAlert(D("errorReportingSet"), "setErrorReporting(): errorReportingSet");
    });
}

document.addEventListener("DOMContentLoaded", async () => {

    $("#select-language").on("change", function() {
        setLanguage($(this).find(":selected").val());
    });

    $("#select-theme").on("change", function() {
        setTheme($(this).find(":selected").val());
    });

    $("#select-errorreporting").on("change", function() {
        setErrorReporting($(this).find(":selected").val());
    });

    localforage.getItem("chosenLang").then((value) => {
        let selectedLanguage = value;
				if(value == null || value.length < 1) {
 					selectedLanguage = "sl";
				}
        $(`#option-${selectedLanguage}`).attr("selected", true);
    }).catch(() => {});

    localforage.getItem("theme").then((value) => {
        let selectedTheme = value;
				if(value == null || value.length < 1) {
					selectedTheme = "themeLight";
				}
        $(`#option-${selectedTheme}`).attr("selected", true);
    }).catch(() => {});

    localforage.getItem("errorReporting").then((value) => {
        let selectedE = value;
				if(value == null || value.length < 1) {
					selectedE = "on";
				}
        $(`#option-${selectedE}`).attr("selected", true);
    }).catch(() => {});

    // Setup side menu
    const menus = document.querySelectorAll(".side-menu");
    M.Sidenav.init(menus, { edge: "right", draggable: true });

    var elems = document.querySelectorAll(".theme-select");
    M.FormSelect.init(elems, {});
    
    var elems = document.querySelectorAll(".errorreporting-select");
    M.FormSelect.init(elems, {});
    
    // Setup language select dropdown
    var elems = document.querySelectorAll(".lang-select");
    M.FormSelect.init(elems, {});
});