add spell checker enabler into configuration window
authorLaurent Mazet <mazet@softndesign.org>
Fri, 14 Aug 2020 08:54:16 +0000 (10:54 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 14 Aug 2020 08:54:16 +0000 (10:54 +0200)
public/index.html
public/trivabble.css
public/trivabble.js

index 46110936b6ca4535249bac3a8ce798a1641dd472..22d6993e83cf937d62fa512fdb48eec2ca8bb7dd 100644 (file)
@@ -53,6 +53,7 @@
                 <div id="prefs-sound">
                     <p><label><input type="checkbox" id="tiles-sound" checked="checked" /><span data-l10n="text-content">Sound of the tiles</span></label></p>
                     <p><label><input type="checkbox" id="msg-sound" checked="checked" /><span data-l10n="text-content">Sound of messages</span></label></p>
+                    <p><label><input type="checkbox" id="enable-spell-checker" /> <span data-l10n="text-content">Enable spell checker</span></label></p>
                 </div>
             </div>
         </div>
@@ -77,7 +78,6 @@
             <p id="help-turn" data-l10n="text-content">Who's turn? Click on the Turn button!</p>
             <button id="clear-rack" class="minibutton" data-l10n="text-content">Put back all the tiles of&#10;your rack in the bag</button>
             <button id="show-rack" class="minibutton" data-l10n="text-content">Show my rack to other players</button>
-            <button id="enable-spell-checker" class="minibutton" data-l10n="text-content">Enable spell checker</button>
             <button id="check-spelling" class="minibutton" data-l10n="text-content" style="display: none">Check spelling for new words</button>
             <button id="modal-config-btn" class="text-content" data-l10n="text-content">Configuration</button>
         </div>
index 09129f01aa88e17f06990844e4310e26aa9048e4..da4138a52d72f416d169dfd3ef76d0552a536a72 100644 (file)
@@ -520,3 +520,7 @@ td.blink {
     transform: scale(1.0);
     transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
 }
+
+.alert {
+    z-index: 2;
+}
index 9163a600f05dc5f20319d85414be72987ef9effc..e7d20113e05b75bf5bea91c7d8d305bbf470e30b 100644 (file)
@@ -42,6 +42,7 @@
     setConf("ENABLE_EVENT_SOURCE",  true);
     setConf("MAX_WEBSOCKET_ERRORS", 1);
     setConf("APP_PATH",             "");
+    setConf("ENABLE_SPELL_CHECKER", false);
 
     const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {
         return s;
         }
     }
 
-    function downloadDictionary(code) {
-        if (localStorage.spellChecker) {
-            document.getElementById("enable-spell-checker").style.display = "none";
+    function checkSpellCheckerState(code) {
+        if (code !== null) {
+            localStorage.spellChecker = code;
+        }
+        if (localStorage.spellChecker !== "false") {
+            document.getElementById("enable-spell-checker").checked = true;
             document.getElementById("check-spelling").style.display = "block";
         } else {
-            document.getElementById("enable-spell-checker").style.display = "block";
+            document.getElementById("enable-spell-checker").checked = false;
             document.getElementById("check-spelling").style.display = "none";
         }
+    }
 
-        /* static variable */
-        if (downloadDictionary.code === code) {
+    function downloadDictionary(code) {
+        if (localStorage.spellChecker === code) {
             return;
         }
-        downloadDictionary.code = code;
 
         const file = new XMLHttpRequest();
         file.overrideMimeType("application/dictionary");
         file.open("GET", "dict/" + code + ".dict", true);
         file.onreadystatechange = function () {
-            if ((file.readyState === 4) && (file.status == "200")) {
-                try {
-                    dictionary = file.responseText.split("\n");
-                    document.getElementById("enable-spell-checker").style.display = "none";
-                    document.getElementById("check-spelling").style.display = "block";
-                } catch (e) {
-                    myAlert(_("Incorrect dictionary file."));
+            if (file.readyState === 4) {
+                if (file.status == "200") {
+                    try {
+                        dictionary = file.responseText.split("\n");
+                        checkSpellCheckerState(code);
+                    } catch (e) {
+                        myAlert(_("Incorrect dictionary file."));
+                        checkSpellCheckerState("false");
+                    }
+                } else {
+                    myAlert(_("Can't load dictionary file."));
+                    checkSpellCheckerState("false");
                 }
             }
         };
     }
 
     function enableSpellChecker() {
-        myConfirm(
-            _("Spell checking requires Trivabble to download a dictionary. This dictionary is based on Aspell. Do you confirm?"),
-            function () {
-                localStorage.spellChecker = true;
-                downloadDictionary(localStorage.trivabbleBoardLang);
-            },
-            function () {
-                localStorage.spellChecker = false;
-                downloadDictionary(localStorage.trivabbleBoardLang);
-            });
+        if (document.getElementById("enable-spell-checker").checked) {
+            myConfirm(
+                _("Spell checking requires Trivabble to download a dictionary. This dictionary is based on Aspell. Do you confirm?"),
+                function () {
+                    downloadDictionary(localStorage.trivabbleBoardLang);
+                },
+                function () {
+                    checkSpellCheckerState("false");
+                });
+        } else {
+            checkSpellCheckerState("false");
+        }
     }
 
     function removeElem(elem) {
             case "boardLang":
                 localStorage.trivabbleBoardLang = value;
                 document.getElementById("board-lang").value = value;
-                downloadDictionary(value);
+                if (localStorage.spellChecker !== "false") {
+                    downloadDictionary(value);
+                }
                 break;
         }
     }
     }
 
     function checkSpelling() {
+        if (localStorage.spellChecker === false) {
+            return;
+        }
 
         const newWords = searchNewWords();
         if (newWords.length === 0) {
         }
     }
 
+    function initSpellChecker() {
+        if (!Object.prototype.hasOwnProperty.call(localStorage, "spellChecker")) {
+           localStorage.spellChecker = Conf.SPELL_CHECKER;
+        }
+
+        document.getElementById("enable-spell-checker").onclick = enableSpellChecker;
+
+        if (localStorage.spellChecker !== "false") {
+            downloadDictionary(localStorage.spellChecker)
+        } else {
+            checkSpellCheckerState("false");
+        }
+    }
+
     function repromptName(f) {
         if (localStorage.trivabblePlayerName && localStorage.trivabblePlayerName.trim()) {
             f();
         document.getElementById("join-game").onclick   = joinGame;
         document.getElementById("clear-rack").onclick  = clearRack;
         document.getElementById("show-rack").onclick   = showRack;
-        document.getElementById("enable-spell-checker").onclick = enableSpellChecker;
         document.getElementById("check-spelling").onclick = checkSpelling;
         helpClear.onclick  = clearGame;
 
         initChat();
         initGame();
         initSound();
+        initSpellChecker();
     };
 
     trivabble.l10nError = trivabble.run;