use setting functions
authorLaurent Mazet <mazet@softndesign.org>
Thu, 10 Sep 2020 06:04:40 +0000 (08:04 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 10 Sep 2020 06:04:40 +0000 (08:04 +0200)
public/trivabble.js

index b978a8719351ad92b4e47373f1274620890b6429..5bcce7878fb4e62ca8e5a54d6a03e3832583dea2 100644 (file)
                 (type === "number") ||
                 (type === "object") ||
                 (type === "string")) {
-                localStore.setItem(key, value);
+                localStorage.setItem(key, value);
 
                 /* store type into localstorage if no default value in configuration */
-                if (Object.prototype.hasOwnProperty.call(Conf, key)) {
-                    localStore.setItem(key + "_type", type);
+                if (!Object.prototype.hasOwnProperty.call(Conf, key)) {
+                    localStorage.setItem(key + "_type", type);
                 }
             } else {
                 console.error("Unsupported type");
             return;
         }
 
-        if (!force && localStorage.spellCheckerEnabled !== "true") {
+        if (!force && getSetting("spellCheckerEnabled") !== "true") {
             myConfirm(
                 _("Spell checking requires Trivabble to download a dictionary. Do you confirm?"),
                 function () {
-                    localStorage.spellCheckerEnabled = "true";
+                    setSetting("spellCheckerEnabled", "true");
                     getDictionary(code, callback, true);
                 }
             );
     }
 
     function spellCheckerSettingChecked() {
-        localStorage.spellCheckerEnabled = document.getElementById("enable-spell-checker").checked.toString();
+        setSetting("spellCheckerEnabled", document.getElementById("enable-spell-checker").checked);
         initSpellChecker();
     }
 
     function set(key, value) {
         switch (key) {
             case "playerName":
-                name.textContent = localStorage.trivabblePlayerName = value;
+                name.textContent = value;
+                setSetting("trivabblePlayerName", value);
                 break;
             case "gameNumber":
-                document.getElementById("number").textContent = localStorage.trivabbleGameNumber = value;
+                document.getElementById("number").textContent = value;
+                setSetting("trivabbleGameNumber", value);
                 break;
             case "boardLang":
-                localStorage.trivabbleBoardLang = value;
                 document.getElementById("board-lang").value = value;
+                setSetting("trivabbleBoardLang", value);
                 break;
         }
     }
                 myAlert(
                     format(
                         _("You are about to leave the current game. To recover it, please note its number: {0}"),
-                        localStorage.trivabbleGameNumber
+                        getSetting("trivabbleGameNumber")
                     ),
                     f
                 );
 
         chatMessages.scrollTop = chatMessages.scrollHeight;
 
-        if (sender && sender !== localStorage.trivabblePlayerName) {
+        if (sender && sender !== getSetting("trivabblePlayerName")) {
             if (msgSound.checked) {
                 audioChat.play();
             }
                 case "changeBoardLang": {
                     const newLang = boardLangSelect.querySelector("[value=" + msg.specialMsg.newBoardLang + "]").textContent;
                     infoMessage(
-                        (msg.sender === localStorage.trivabblePlayerName)
+                        (msg.sender === getSetting("trivabblePlayerName"))
                             ? format(_("You changed the language of the board to {0}"), newLang)
                             : format(_("{0} changed the language of the board to {1}"), msg.sender, newLang)
                     );
                 }
 
                 if (data.to === "board") {
-                    setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== localStorage.trivabblePlayerName);
+                    setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== getSetting("trivabblePlayerName"));
                 } else if (data.to === "rack") {
                     setRackCell(data.indexTo, data.letter);
                 }
                 break;
             case "setCell":
-                setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== localStorage.trivabblePlayerName);
+                setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== getSetting("trivabblePlayerName"));
                 if ((data.letter !== "") && (currentTilePlayed[data.indexTo] === "-")) {
                     currentTilePlayed[data.indexTo] = data.letter;
                 }
         }
 
         if (data.gameNumber) {
-            set("gameNumber", data.gameNumber);
+            set("gameNumber", Number(data.gameNumber));
         }
 
         if (data.availableBoardLangs) {
                     startConnection();
                     retryPollingTimeout = 0;
                 },
-                delay || Conf.POLLING_DELAY
+                delay || getSetting("POLLING_DELAY")
             );
         } else {
             retriedImmediately = true;
             } else {
                 webSocketErrors++;
 
-                if (webSocketErrors > Conf.MAX_WEBSOCKET_ERRORS) {
+                if (webSocketErrors > getSetting("MAX_WEBSOCKET_ERRORS")) {
                     blacklistWebsockets = true;
                 }
             }
     }
 
     function pollServerWithEventSource() {
-        if (canConnect() && Conf.ENABLE_EVENT_SOURCE && window.EventSource) {
+        if (canConnect() && getSetting("ENABLE_EVENT_SOURCE") && window.EventSource) {
             closeConnections();
             pollingServer = true;
 
     }
 
     function pollServerWithWebSocket() {
-        if (canConnect() && Conf.ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) {
+        if (canConnect() && getSetting("ENABLE_WEBSOCKETS") && !blacklistWebsockets && window.WebSocket) {
             closeConnections();
             pollingServer = true;
 
         xhrRequest(cmdsWithContext(cmds), function (xhr) {
             if (xhr.readyState === 4) {
                 if (xhr.status === 0 || xhr.status >= 300) {
-                    setTimeout(sendCmds.bind(null, cmds), Conf.POLLING_DELAY);
+                    setTimeout(sendCmds.bind(null, cmds), getSetting("POLLING_DELAY"));
                     return;
                 }
 
 
     function cmdsWithContext(cmds) {
         return {
-            gameNumber: localStorage.trivabbleGameNumber || "",
-            playerName: localStorage.trivabblePlayerName,
-            boardLang: localStorage.trivabbleBoardLang,
+            gameNumber: getSetting("trivabbleGameNumber") || "",
+            playerName: getSetting("trivabblePlayerName"),
+            boardLang: getSetting("trivabbleBoardLang"),
             version: VERSION,
             cmds: cmds
         };
 
         if (!pollingReady) {
             startConnection();
-            setTimeout(sendCmds, Conf.POLLING_DELAY, cmds);
+            setTimeout(sendCmds, getSetting("POLLING_DELAY"), cmds);
             return;
         }
 
         checkGameInProgress(
             function () {
                 myPrompt(
-                    _("To join a game, please give the number which is displayed on your adversary(ies)' screen.\nIf you do not know it, ask them.\n\nWarning: your adversary must not take your number, (s)he must keep his/her own. If you whish to recover your current game, please not the following number: {0}.").replace("{0}", localStorage.trivabbleGameNumber),
+                    _("To join a game, please give the number which is displayed on your adversary(ies)' screen.\nIf you do not know it, ask them.\n\nWarning: your adversary must not take your number, (s)he must keep his/her own. If you whish to recover your current game, please not the following number: {0}.").replace("{0}", getSetting("trivabbleGameNumber")),
                     function (n) {
                         n = parseInt(n);
                         if (isNaN(n)) {
                             myAlert(_("It seems your did not give a correct number, or you clicked on “Cancel”. As a result, the current game continues, if any. To join a game, click on “Join a game” again."));
                         } else {
-                            localStorage.trivabbleGameNumber = n;
+                            setSetting("trivabbleGameNumber", n);
                             location.reload();
                         }
                     }
             _("To change your name, enter a new one. You can keep using your current name by cancelling. Please note that if you change your name and you have games in progress, you will not be able to keep playing them anymore unless you get back to your current name."),
             function (newName) {
                 if (newName && newName.trim()) {
-                    localStorage.trivabblePlayerName = newName.trim();
-                    name.textContent = localStorage.trivabblePlayerName;
+                    setSetting("trivabblePlayerName", newName.trim());
+                    name.textContent = getSetting("trivabblePlayerName");
                 }
             },
-            localStorage.trivabblePlayerName
+            getSetting("trivabblePlayerNamer")
         );
     }
 
     function startGame(number) {
         if (number) {
-            localStorage.trivabbleGameNumber = number;
+            setSetting("trivabbleGameNumber", number);
         }
         startConnection();
     }
                 sendCmds([{cmd: "changeBoard", lang: code}]);
             },
             function () {
-                boardLangSelect.value = localStorage.trivabbleBoardLang;
+                boardLangSelect.value = getSetting("trivabbleBoardLang");
             }
         );
     }
     }
 
     function checkSpellingClicked() {
-        if (localStorage.spellCheckerEnabled === "false") {
+        if (getSetting("spellCheckerEnabled") === "false") {
             return;
         }
 
-        getDictionary(localStorage.trivabbleBoardLang, checkSpelling);
+        getDictionary(getSetting("trivabbleBoardLang"), checkSpelling);
     }
 
     function checkSpelling(dictionary) {
             return;
         }
 
-        if ((laserTouch.last !== cell) || (Date.now() - laserTouch.date > localStorage.doubleTapDuration)) {
+        if ((laserTouch.last !== cell) || (Date.now() - laserTouch.date > getSetting("doubleTapDuration"))) {
             laserTouch.last = cell;
             laserTouch.date = Date.now();
             return;
         placeholder.classList.add("laser-highlighting");
         placeholder.classList.add("laser-highlight");
 
-        const sixthFlashLightDuration = Number(localStorage.flashLightDuration) / 6;
+        const sixthFlashLightDuration = getSetting("flashLightDuration") / 6;
 
         function on() {
             placeholder.classList.add("laser-highlight");
         msgSound = document.getElementById("msg-sound");
 
         tilesSound.onclick = function () {
-            localStorage.trivabbleTileSound = tilesSound.checked;
+            setSetting("trivabbleTileSound", tilesSound.checked);
         };
 
         msgSound.onclick = function () {
-            localStorage.trivabbleMsgSound = msgSound.checked;
+            setSetting("trivabbleMsgSound", msgSound.checked);
         };
 
-        if (Object.prototype.hasOwnProperty.call(localStorage, "trivabbleMsgSound")) {
-            msgSound.checked = localStorage.trivabbleMsgSound === "true";
+        if (isSetting("trivabbleMsgSound")) {
+            msgSound.checked = getSetting("trivabbleMsgSound");
         } else {
-            localStorage.trivabbleMsgSound = msgSound.checked;
+            setSetting("trivabbleMsgSound", msgSound.checked);
         }
 
-        if (Object.prototype.hasOwnProperty.call(localStorage, "trivabbleTileSound")) {
-            tilesSound.checked = localStorage.trivabbleTileSound === "true";
+        if (isSetting("trivabbleTileSound")) {
+            tilesSound.checked = getSetting("trivabbleTileSound");
         } else {
-            localStorage.trivabbleTilesSound = tilesSound.checked;
+            setSetting("trivabbleTilesSound", tilesSound.checked);
         }
     }
 
     function initSpellChecker() {
-        if (Conf.ENABLE_SPELL_CHECKER) {
+        if (getSetting("ENABLE_SPELL_CHECKER")) {
             document.getElementById("enable-spell-checker-outer").style.display = "";
         }
 
         document.getElementById("enable-spell-checker").onclick = spellCheckerSettingChecked;
 
-        if (!Conf.ENABLE_SPELL_CHECKER || localStorage.spellCheckerEnabled === "false") {
+        if (!getSetting("ENABLE_SPELL_CHECKER") || !getSetting("spellCheckerEnabled")) {
             document.getElementById("check-spelling").style.display = "none";
             document.getElementById("enable-spell-checker").checked = false;
             document.getElementById("info-spell-checking").style.display = "none";
         document.body.addEventListener("dblclick", laserDblClick);
         document.body.addEventListener("touchstart", laserTouch);
 
-        if (!Object.prototype.hasOwnProperty.call(localStorage, "doubleTapDuration")) {
-            localStorage.doubleTapDuration = Conf.DOUBLE_TAP_DURATIONS[Math.floor(Conf.DOUBLE_TAP_DURATIONS.length / 2)];
+        if (!isSetting("doubleTapDuration")) {
+            setSetting("doubleTapDuration", getSetting("DOUBLE_TAP_DURATIONS")[Math.floor(getSetting("DOUBLE_TAP_DURATIONS").length / 2)]);
         }
         const doubleTapDuration = document.getElementById("double-tap-duration");
         if (doubleTapDuration) {
-            doubleTapDuration.max = Conf.DOUBLE_TAP_DURATIONS.length - 1;
-            doubleTapDuration.value = translateDuration(localStorage.doubleTapDuration, Conf.DOUBLE_TAP_DURATIONS);
+            doubleTapDuration.max = getSetting("DOUBLE_TAP_DURATIONS").length - 1;
+            doubleTapDuration.value = translateDuration(getSetting("doubleTapDuration"), getSetting("DOUBLE_TAP_DURATIONS"));
             doubleTapDuration.onchange = function () {
-                localStorage.doubleTapDuration = translateDuration(
+                setSetting("doubleTapDuration", translateDuration(
                     document.getElementById("double-tap-duration").value,
-                    Conf.DOUBLE_TAP_DURATIONS
-                );
+                    getSetting("DOUBLE_TAP_DURATIONS")
+                ));
             };
         }
 
-        if (!Object.prototype.hasOwnProperty.call(localStorage, "flashLightDuration")) {
-            localStorage.flashLightDuration = Conf.FLASH_LIGHT_DURATIONS[Math.floor(Conf.FLASH_LIGHT_DURATIONS.length / 2)];
+        if (!isSetting("flashLightDuration")) {
+            setSetting("flashLightDuration", getSetting("FLASH_LIGHT_DURATIONS")[Math.floor(getSetting("FLASH_LIGHT_DURATIONS").length / 2)]);
         }
         const flashLightDuration = document.getElementById("flash-light-duration");
         if (flashLightDuration) {
-            flashLightDuration.max = Conf.FLASH_LIGHT_DURATIONS.length - 1;
-            flashLightDuration.value = translateDuration(localStorage.flashLightDuration, Conf.FLASH_LIGHT_DURATIONS);
+            flashLightDuration.max = getSetting("FLASH_LIGHT_DURATIONS").length - 1;
+            flashLightDuration.value = translateDuration(getSetting("flashLightDuration"), getSetting("FLASH_LIGHT_DURATIONS"));
             flashLightDuration.onchange = function () {
-                localStorage.flashLightDuration = translateDuration(
+                setSetting("flashLightDuration", translateDuration(
                     document.getElementById("flash-light-duration").value,
-                    Conf.FLASH_LIGHT_DURATIONS
-                );
+                    getSetting("FLASH_LIGHT_DURATIONS")
+                ));
             };
         }
 
-        if (!Object.prototype.hasOwnProperty.call(localStorage, "flashLightColor")) {
-            localStorage.flashLightColor = Conf.FLASH_LIGHT_COLOR;
-        }
         const flashLightColor = document.getElementById("flash-light-color");
         if (flashLightColor) {
-            flashLightColor.value = localStorage.flashLightColor;
+            flashLightColor.value = getSetting("FLASH_LIGHT_COLOR");
             flashLightColor.onchange = function () {
-                localStorage.flashLightColor = document.getElementById("flash-light-color").value;
+                setSetting("FLASH_LIGHT_COLOR", document.getElementById("flash-light-color").value);
                 const style = document.createElement("style");
-                style.textContent = ".laser-highlight {background-color: " + localStorage.flashLightColor + ";}";
+                style.textContent = ".laser-highlight {background-color: " + getSetting("FLASH_LIGHT_COLOR") + ";}";
                 document.body.appendChild(style);
             };
             flashLightColor.onchange();
     }
 
     function repromptName(f) {
-        if (localStorage.trivabblePlayerName && localStorage.trivabblePlayerName.trim()) {
+        if (getSetting("trivabblePlayerName") && getSetting("trivabblePlayerName").trim()) {
             f();
         } else {
             myPrompt(
                 _("It seems your did not give your name. You need to do it for the game to run properly."),
                 function (name) {
                     if (name && name.trim()) {
-                        localStorage.trivabblePlayerName = name.trim();
+                        setSetting("trivabblePlayerName", name.trim());
                     }
 
                     repromptName(f);
             );
         });
 
-        boardLangSelect.value = localStorage.trivabbleBoardLang;
+        boardLangSelect.value = getSetting("trivabbleBoardLang");
     }
 
     function langSelectionChange(e) {
-        localStorage.trivabbleLang = e.target.value;
+        setSetting("trivabbleLang", e.target.value);
         location.reload();
     }
 
     }
 
     function initGame() {
-        if (!localStorage.trivabblePlayerName) {
+        if (!getSetting("trivabblePlayerName")) {
             myPrompt(
                 _("Hello! To begin, enter your name. Your adversaries will see this name when you play with them."),
                 function (name) {
                     if (name && name.trim()) {
-                        localStorage.trivabblePlayerName = name;
+                        setSetting("trivabblePlayerName", name);
                     }
                     repromptName(initGame);
                 }
             return;
         }
 
-        name.textContent = localStorage.trivabblePlayerName;
+        name.textContent = getSetting("trivabblePlayerName");
 
         const letters = "ABCDEFGHIJKLMNO";
 
 
         board.lastChild.appendChild(board.rows[0].cells[0].cloneNode(false));
 
-        if (localStorage.trivabbleGameNumber) {
-            document.getElementById("number").textContent = localStorage.trivabbleGameNumber;
+        if (getSetting("trivabbleGameNumber")) {
+            document.getElementById("number").textContent = getSetting("trivabbleGameNumber");
         }
 
-        if (localStorage.trivabbleBoardLang) {
-            document.getElementById("board-lang").value = localStorage.trivabbleBoardLang;
+        if (getSetting("trivabbleBoardLang")) {
+            document.getElementById("board-lang").value = getSetting("trivabbleBoardLang");
         }
 
-        startGame(localStorage.trivabbleGameNumber);
+        startGame(getSetting("trivabbleGameNumber"));
     }
 
     function initLang() {
-        const lang = libD.lang = localStorage.trivabbleLang || libD.lang;
+        const lang = libD.lang = getSetting("trivabbleLang") || libD.lang;
 
         const langSel = document.getElementById("select-lang");
         langSel.value = lang;