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 ajax("/whoami", "GET", res
=> {
35 this.state
.user
.email
= res
.email
;
36 this.state
.user
.notify
= res
.notify
;
38 // TODO: fetch is simpler, but does not set req.xhr (for security check)
39 // fetch(params.serverUrl + "/whoami", {
41 // credentials: params.cors ? "include" : "omit",
44 // }).then((user) => {
45 // this.state.user.email = user.email;
46 // this.state.user.notify = user.notify;
49 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
);
50 // Settings initialized with values from localStorage
51 this.state
.settings
= {
52 bcolor: localStorage
["bcolor"] || "lichess",
53 sound: parseInt(localStorage
["sound"]) || 2,
54 hints: parseInt(localStorage
["hints"]) || 1,
55 coords: !!eval(localStorage
["coords"]),
56 highlight: !!eval(localStorage
["highlight"]),
57 sqSize: parseInt(localStorage
["sqSize"]),
59 this.socketCloseListener
= () => {
60 // Next line may fail at first, but should retry and eventually success (TODO?)
61 this.state
.conn
= new WebSocket(params
.socketUrl
+ "/?sid=" + mysid
);
63 this.state
.conn
.onclose
= this.socketCloseListener
;
64 const supportedLangs
= ["en","es","fr"];
65 this.state
.lang
= localStorage
["lang"] ||
66 supportedLangs
.includes(navigator
.language
)
69 this.setTranslations();
71 setTranslations: async
function() {
72 // Import translations from "./translations/$lang.js"
73 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
74 this.state
.tr
= tModule
.translations
;
77 this.state
.lang
= lang
;
78 this.setTranslations();