1 import { ajax
} from "./utils/ajax";
2 import { getRandString
} from "./utils/alea";
3 import params
from "./parameters"; //for socket connection
5 // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
16 socketCloseListener: null,
18 ajax("/variants", "GET", res
=> { this.state
.variants
= res
.variantArray
; });
19 let mysid
= localStorage
["mysid"];
22 mysid
= getRandString();
23 localStorage
["mysid"] = mysid
; //done only once (unless user clear browser data)
25 // Quick user setup using local storage:
27 id: localStorage
["myid"] || 0,
28 name: localStorage
["myname"] || "", //"" for "anonymous"
29 email: "", //unknown yet
30 notify: false, //email notifications
33 // Slow verification through the server:
34 // NOTE: still superficial identity usurpation possible, but difficult.
35 ajax("/whoami", "GET", res
=> {
36 this.state
.user
.id
= res
.id
;
37 this.state
.user
.name
= res
.name
;
38 this.state
.user
.email
= res
.email
;
39 this.state
.user
.notify
= res
.notify
;
41 const supportedLangs
= ["en","es","fr"];
42 this.state
.lang
= localStorage
["lang"] ||
43 (supportedLangs
.includes(navigator
.language
)
46 this.setTranslations();
47 // Initialize connection (even if the current page doesn't need it)
48 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
+
49 "&page=" + encodeURIComponent(page
));
50 // Settings initialized with values from localStorage
51 this.state
.settings
= {
52 bcolor: localStorage
.getItem("bcolor") || "lichess",
53 sound: parseInt(localStorage
.getItem("sound")) || 1,
54 hints: localStorage
.getItem("hints") == "true",
55 highlight: localStorage
.getItem("highlight") == "true",
57 this.socketCloseListener
= () => {
58 // Next line may fail at first, but should retry and eventually success (TODO?)
59 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
+
60 "&page=" + encodeURIComponent(page
));
62 this.state
.conn
.onclose
= this.socketCloseListener
;
64 updateSetting: function(propName
, value
) {
65 this.state
.settings
[propName
] = value
;
66 localStorage
.setItem(propName
, value
);
68 setTranslations: async
function() {
69 // Import translations from "./translations/$lang.js"
70 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
71 this.state
.tr
= tModule
.translations
;
74 this.state
.lang
= lang
;
75 this.setTranslations();