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
17 ajax("/variants", "GET", res
=> { this.state
.variants
= res
.variantArray
; });
18 let mysid
= localStorage
["mysid"];
21 mysid
= getRandString();
22 localStorage
["mysid"] = mysid
; //done only once (unless user clear browser data)
25 id: localStorage
["myid"] || 0,
26 name: localStorage
["myname"] || "", //"" for "anonymous"
27 email: "", //unknown yet
28 notify: false, //email notifications
31 if (this.state
.user
.id
> 0)
33 fetch(params
.serverUrl
+ "/whoami", {
35 credentials: params
.cors
? "include" : "omit",
37 this.state
.user
.email
= res
.email
;
38 this.state
.user
.notify
= res
.notify
;
41 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + this.state
.user
.sid
);
42 // Settings initialized with values from localStorage
43 this.state
.settings
= {
44 bcolor: localStorage
["bcolor"] || "lichess",
45 sound: parseInt(localStorage
["sound"]) || 2,
46 hints: parseInt(localStorage
["hints"]) || 1,
47 coords: !!eval(localStorage
["coords"]),
48 highlight: !!eval(localStorage
["highlight"]),
49 sqSize: parseInt(localStorage
["sqSize"]),
51 const socketCloseListener
= () => {
52 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
);
54 this.state
.conn
.onclose
= socketCloseListener
;
55 const supportedLangs
= ["en","es","fr"];
56 this.state
.lang
= localStorage
["lang"] ||
57 supportedLangs
.includes(navigator
.language
)
60 this.setTranslations();
62 setTranslations: async
function() {
63 // Import translations from "./translations/$lang.js"
64 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
65 this.state
.tr
= tModule
.translations
;
68 this.state
.lang
= lang
;
69 this.setTranslations();