spanish-quizzer

An app to quiz you on Spanish vocabulary and verb conjugations
git clone https://git.ashermorgan.net/spanish-quizzer/
Log | Files | Refs | README

global.js (4459B)


      1 /**
      2  * Set the app theme.
      3  * @param {Boolean} darkTheme - Whether to use the dark theme. If null, a theme will be automatically chosen.
      4  */
      5 function SetTheme(darkTheme = null) {
      6     // Get theme if null
      7     if (darkTheme === null) {
      8         darkTheme = getSettings().darkTheme;
      9     }
     10 
     11     // Apply theme
     12     if (darkTheme) {
     13         document.body.classList.add("dark");
     14     }
     15     else {
     16         document.body.classList.remove("dark");
     17     }
     18 }
     19 
     20 
     21 
     22 /**
     23  * Load settings from localStorage.
     24  * @returns {Object} The settings.
     25  */
     26 function getSettings() {
     27     // Initialize settings
     28     let settings = {
     29         darkTheme: false,
     30         conjugationColors: true,
     31 
     32         promptType: "Text",
     33         inputType: "Text",
     34         multiplePrompts: "Show together",
     35         removeDuplicates: false,
     36 
     37         onMissedPrompt: "Correct me",
     38         repeatPrompts: "Never",
     39         multipleAnswers: "Require all",
     40         showDiff: "For single answers",
     41 
     42         defaultFilters: {
     43             verbs: {tense:"All Tenses", type:"All Types", subject:"All Subjects", direction:"Eng. → Conj."},
     44             vocab: {category:"All Categories", type:"All Types", direction:"Eng. ↔ Esp."},
     45         },
     46     };
     47 
     48     // Detect prefered theme
     49     try {
     50         settings.darkTheme = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
     51     }
     52     catch { }
     53 
     54     // Parse settings
     55     let parsedSettings;
     56     try {
     57         parsedSettings = JSON.parse(localStorage.getItem("settings"));
     58     }
     59     catch { return settings; }
     60     if (!parsedSettings) { return settings; }
     61 
     62     // Load settings
     63     if ([true, false].includes(parsedSettings.darkTheme)) {
     64         settings.darkTheme = parsedSettings.darkTheme;
     65     }
     66     if ([true, false].includes(parsedSettings.conjugationColors)) {
     67         settings.conjugationColors = parsedSettings.conjugationColors;
     68     }
     69 
     70     if (["Text", "Audio", "Both"].includes(parsedSettings.promptType)) {
     71         settings.promptType = parsedSettings.promptType;
     72     }
     73     if (["Text", "Voice", "Either"].includes(parsedSettings.inputType)) {
     74         settings.inputType = parsedSettings.inputType;
     75     }
     76     if (["Show together", "Show separately", "Show one"].includes(parsedSettings.multiplePrompts)) {
     77         settings.multiplePrompts = parsedSettings.multiplePrompts;
     78     }
     79     if ([true, false].includes(parsedSettings.removeDuplicates)) {
     80         settings.removeDuplicates = parsedSettings.removeDuplicates;
     81     }
     82 
     83     if (["Correct me", "Tell me", "Ignore it"].includes(parsedSettings.onMissedPrompt)) {
     84         settings.onMissedPrompt = parsedSettings.onMissedPrompt;
     85     }
     86     if (["Never", "Immediately", "5 prompts later", "5 & 10 prompts later", "At the end"].includes(parsedSettings.repeatPrompts)) {
     87         settings.repeatPrompts = parsedSettings.repeatPrompts;
     88     }
     89     if (["Require all", "Require any"].includes(parsedSettings.multipleAnswers)) {
     90         settings.multipleAnswers = parsedSettings.multipleAnswers;
     91     }
     92     if (["Never", "For single answers", "Always"].includes(parsedSettings.showDiff)) {
     93         settings.showDiff = parsedSettings.showDiff;
     94     }
     95 
     96     if (parsedSettings.defaultFilters) {
     97         if (parsedSettings.defaultFilters.verbs) settings.defaultFilters.verbs = parsedSettings.defaultFilters.verbs;
     98         if (parsedSettings.defaultFilters.vocab) settings.defaultFilters.vocab = parsedSettings.defaultFilters.vocab;
     99     }
    100 
    101     // Return parsed settings
    102     return settings;
    103 }
    104 
    105 
    106 
    107 /**
    108  * Save settings to localStorage.
    109  * @param {Object} value The settings.
    110  */
    111 function setSettings(value) {
    112     // Update settings in localStorage
    113     localStorage.setItem("settings", JSON.stringify(value));
    114 
    115     // Apply theme
    116     SetTheme(value.darkTheme);
    117 }
    118 
    119 
    120 
    121 /**
    122  * Loads Spanish-Quizzer data.
    123  */
    124 function loadData() {
    125     return new Promise(function(resolve, reject) {
    126         // Initialize variables
    127         let setNames = ["verbs", "vocab"];
    128         let progress = 0;
    129         let sets = {};
    130 
    131         // Load data
    132         for (let setName of setNames) {
    133             Papa.parse(`data/${setName}.csv`, {
    134                 download: true,
    135                 skipEmptyLines: true,
    136                 complete: function(results) {
    137                     // Load data
    138                     sets[setName] = results.data;
    139 
    140                     // Update progress
    141                     progress++;
    142                     if (progress === setNames.length) resolve(sets);
    143                 }
    144             });
    145         }
    146     });
    147 }