Commit | Line | Data |
---|---|---|
c66a829b BA |
1 | import { ajax } from "./utils/ajax"; |
2 | import { getRandString } from "./utils/alea"; | |
3 | import params from "./parameters"; //for socket connection | |
4 | ||
4486a21e | 5 | // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87 |
c66a829b BA |
6 | export const store = |
7 | { | |
8 | state: { | |
9 | variants: [], | |
10 | tr: {}, | |
11 | user: {}, | |
12 | conn: null, | |
13 | settings: {}, | |
14 | lang: "", | |
15 | }, | |
cdb34c93 | 16 | socketCloseListener: null, |
5f131484 | 17 | initialize(page) { |
c66a829b | 18 | ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); |
98f48579 BA |
19 | let mysid = localStorage["mysid"]; |
20 | if (!mysid) | |
21 | { | |
22 | mysid = getRandString(); | |
23 | localStorage["mysid"] = mysid; //done only once (unless user clear browser data) | |
24 | } | |
c66a829b | 25 | this.state.user = { |
98f48579 BA |
26 | id: localStorage["myid"] || 0, |
27 | name: localStorage["myname"] || "", //"" for "anonymous" | |
a7f9f050 BA |
28 | email: "", //unknown yet |
29 | notify: false, //email notifications | |
98f48579 | 30 | sid: mysid, |
c66a829b | 31 | }; |
98f48579 BA |
32 | if (this.state.user.id > 0) |
33 | { | |
317b8a56 | 34 | ajax("/whoami", "GET", res => { |
a7f9f050 BA |
35 | this.state.user.email = res.email; |
36 | this.state.user.notify = res.notify; | |
98f48579 BA |
37 | }); |
38 | } | |
5f131484 BA |
39 | this.state.conn = new WebSocket( |
40 | params.socketUrl + "/?sid=" + mysid + "&page=" + page); | |
c66a829b BA |
41 | // Settings initialized with values from localStorage |
42 | this.state.settings = { | |
43 | bcolor: localStorage["bcolor"] || "lichess", | |
44 | sound: parseInt(localStorage["sound"]) || 2, | |
45 | hints: parseInt(localStorage["hints"]) || 1, | |
46 | coords: !!eval(localStorage["coords"]), | |
47 | highlight: !!eval(localStorage["highlight"]), | |
48 | sqSize: parseInt(localStorage["sqSize"]), | |
49 | }; | |
cdb34c93 | 50 | this.socketCloseListener = () => { |
66d03f23 | 51 | // Next line may fail at first, but should retry and eventually success (TODO?) |
c66a829b | 52 | this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid); |
f6f2bef1 | 53 | }; |
cdb34c93 | 54 | this.state.conn.onclose = this.socketCloseListener; |
c66a829b BA |
55 | const supportedLangs = ["en","es","fr"]; |
56 | this.state.lang = localStorage["lang"] || | |
57 | supportedLangs.includes(navigator.language) | |
58 | ? navigator.language | |
59 | : "en"; | |
60 | this.setTranslations(); | |
61 | }, | |
62 | setTranslations: async function() { | |
7c3cd379 | 63 | // Import translations from "./translations/$lang.js" |
c66a829b BA |
64 | const tModule = await import("@/translations/" + this.state.lang + ".js"); |
65 | this.state.tr = tModule.translations; | |
66 | }, | |
67 | setLanguage(lang) { | |
68 | this.state.lang = lang; | |
69 | this.setTranslations(); | |
70 | }, | |
71 | }; |