| 1 | import { ajax } from "./utils/ajax"; |
| 2 | import { getRandString } from "./utils/alea"; |
| 3 | import params from "./parameters"; //for socket connection |
| 4 | |
| 5 | // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87 |
| 6 | export const store = |
| 7 | { |
| 8 | state: { |
| 9 | variants: [], |
| 10 | tr: {}, |
| 11 | user: {}, |
| 12 | conn: null, |
| 13 | settings: {}, |
| 14 | lang: "", |
| 15 | }, |
| 16 | socketCloseListener: null, |
| 17 | initialize() { |
| 18 | ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); |
| 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 | } |
| 25 | this.state.user = { |
| 26 | id: localStorage["myid"] || 0, |
| 27 | name: localStorage["myname"] || "", //"" for "anonymous" |
| 28 | email: "", //unknown yet |
| 29 | notify: false, //email notifications |
| 30 | sid: mysid, |
| 31 | }; |
| 32 | if (this.state.user.id > 0) |
| 33 | { |
| 34 | ajax("/whoami", "GET", res => { |
| 35 | this.state.user.email = res.email; |
| 36 | this.state.user.notify = res.notify; |
| 37 | }); |
| 38 | // TODO: fetch is simpler, but does not set req.xhr (for security check) |
| 39 | // fetch(params.serverUrl + "/whoami", { |
| 40 | // method: "GET", |
| 41 | // credentials: params.cors ? "include" : "omit", |
| 42 | // }).then((res) => { |
| 43 | // return res.json() |
| 44 | // }).then((user) => { |
| 45 | // this.state.user.email = user.email; |
| 46 | // this.state.user.notify = user.notify; |
| 47 | // }); |
| 48 | } |
| 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"]), |
| 58 | }; |
| 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); |
| 62 | }; |
| 63 | this.state.conn.onclose = this.socketCloseListener; |
| 64 | const supportedLangs = ["en","es","fr"]; |
| 65 | this.state.lang = localStorage["lang"] || |
| 66 | supportedLangs.includes(navigator.language) |
| 67 | ? navigator.language |
| 68 | : "en"; |
| 69 | this.setTranslations(); |
| 70 | }, |
| 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; |
| 75 | }, |
| 76 | setLanguage(lang) { |
| 77 | this.state.lang = lang; |
| 78 | this.setTranslations(); |
| 79 | }, |
| 80 | }; |