load dictionnary for new players
authorLaurent Mazet <mazet@softndesign.org>
Sun, 31 May 2020 22:39:22 +0000 (00:39 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 9 Jun 2020 20:24:28 +0000 (22:24 +0200)
public/trivabble.js

index 3d6420111a42d5a785b778fb943293c2e625de9c..96f09234b7f4b4247ff63a8d64947bcfccfbf206 100644 (file)
         }
     }
 
+    function downloadDictionnary(code) {
+        if (typeof downloadDictionnary.code === "undefined") {
+            downloadDictionnary.code = "";
+        } else if (downloadDictionnary.code === code) {
+            return;
+        }
+        downloadDictionnary.code = code;
+
+        const file = new XMLHttpRequest();
+        file.overrideMimeType("application/json");
+        file.open("GET", "dict/" + code + ".json", true);
+        file.onreadystatechange = function () {
+            if ((file.readyState === 4) && (file.status == "200")) {
+                try {
+                    dictionnary = JSON.parse(file.responseText);
+                    document.getElementById("check-spelling").disabled = false;
+                } catch (e) {
+                    console.error(e);
+                }
+            }
+        };
+        file.send(null);
+        return;
+    }
+
     function removeElem(elem) {
         elem.parentNode.removeChild(elem);
     }
             case "boardLang":
                 localStorage.trivabbleBoardLang = value;
                 document.getElementById("board-lang").value = value;
+                document.getElementById("check-spelling").disabled = true;
+                downloadDictionnary(value);
                 break;
         }
     }
         return content;
     }
 
-    function downloadDictionnary(code) {
-        const file = new XMLHttpRequest();
-        file.overrideMimeType("application/json");
-        file.open("GET", "dict/" + code + ".json", true);
-        file.onreadystatechange = function () {
-            if ((file.readyState === 4) && (file.status == "200")) {
-                try {
-                    dictionnary = JSON.parse(file.responseText);
-                    document.getElementById("check-spelling").disabled = false;
-                } catch (e) {
-                    console.error(e);
-                }
-            }
-        };
-        file.send(null);
-        return;
-    }
-
     function handleChatMessage(msg) {
         if (msg.specialMsg) {
             switch (msg.specialMsg.type) {
                             ? format(_("You changed the language of the board to {0}"), newLang)
                             : format(_("{0} changed the language of the board to {1}"), msg.sender, newLang)
                     );
-                    document.getElementById("check-spelling").disabled = true;
-                    downloadDictionnary(msg.specialMsg.newBoardLang);
                 }
             }
         }
         }
 
         if (localStorage.trivabbleBoardLang) {
-            document.getElementById("check-spelling").disabled = true;
             document.getElementById("board-lang").value = localStorage.trivabbleBoardLang;
-            downloadDictionnary(localStorage.trivabbleBoardLang);
         }
 
         startGame(localStorage.trivabbleGameNumber, localStorage.trivabbleBoardLang);