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)
26 id: localStorage
["myid"] || 0,
27 name: localStorage
["myname"] || "", //"" for "anonymous"
28 email: "", //unknown yet
29 notify: false, //email notifications
32 if (this.state
.user
.id
> 0)
34 fetch(params
.serverUrl
+ "/whoami", {
36 credentials: params
.cors
? "include" : "omit",
38 this.state
.user
.email
= res
.email
;
39 this.state
.user
.notify
= res
.notify
;
42 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
);
43 // Settings initialized with values from localStorage
44 this.state
.settings
= {
45 bcolor: localStorage
["bcolor"] || "lichess",
46 sound: parseInt(localStorage
["sound"]) || 2,
47 hints: parseInt(localStorage
["hints"]) || 1,
48 coords: !!eval(localStorage
["coords"]),
49 highlight: !!eval(localStorage
["highlight"]),
50 sqSize: parseInt(localStorage
["sqSize"]),
52 this.socketCloseListener
= () => {
53 // Next line may fail at first, but should retry and eventually success (TODO?)
54 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
);
56 this.state
.conn
.onclose
= this.socketCloseListener
;
57 const supportedLangs
= ["en","es","fr"];
58 this.state
.lang
= localStorage
["lang"] ||
59 supportedLangs
.includes(navigator
.language
)
62 this.setTranslations();
64 setTranslations: async
function() {
65 // Import translations from "./translations/$lang.js"
66 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
67 this.state
.tr
= tModule
.translations
;
70 this.state
.lang
= lang
;
71 this.setTranslations();