1 import { ajax
} from "./utils/ajax";
2 import { getRandString
} from "./utils/alea";
4 // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
14 socketCloseListener: null,
16 ajax("/variants", "GET", res
=> { this.state
.variants
= res
.variantArray
; });
17 let mysid
= localStorage
.getItem("mysid");
20 mysid
= getRandString();
21 localStorage
.setItem("mysid", mysid
); //done only once (unless user clear browser data)
23 // Quick user setup using local storage:
25 id: localStorage
.getItem("myid") || 0,
26 name: localStorage
.getItem("myname") || "", //"" for "anonymous"
27 email: "", //unknown yet
28 notify: false, //email notifications
31 // Slow verification through the server:
32 // NOTE: still superficial identity usurpation possible, but difficult.
33 ajax("/whoami", "GET", res
=> {
34 this.state
.user
.id
= res
.id
;
35 const storedId
= localStorage
.getItem("myid");
36 if (res
.id
> 0 && !storedId
) //user cleared localStorage
37 localStorage
.setItem("myid", res
.id
);
38 else if (res
.id
== 0 && !!storedId
) //user cleared cookie
39 localStorage
.removeItem("myid");
40 this.state
.user
.name
= res
.name
;
41 const storedName
= localStorage
.getItem("myname");
42 if (!!res
.name
&& !storedName
) //user cleared localStorage
43 localStorage
.setItem("myname", res
.name
);
44 else if (!res
.name
&& !!storedName
) //user cleared cookie
45 localStorage
.removeItem("myname");
46 this.state
.user
.email
= res
.email
;
47 this.state
.user
.notify
= res
.notify
;
49 // Settings initialized with values from localStorage
50 this.state
.settings
= {
51 bcolor: localStorage
.getItem("bcolor") || "lichess",
52 sound: parseInt(localStorage
.getItem("sound")) || 1,
53 hints: localStorage
.getItem("hints") == "true",
54 highlight: localStorage
.getItem("highlight") == "true",
56 const supportedLangs
= ["en","es","fr"];
57 this.state
.lang
= localStorage
["lang"] ||
58 (supportedLangs
.includes(navigator
.language
)
61 this.setTranslations();
63 updateSetting: function(propName
, value
) {
64 this.state
.settings
[propName
] = value
;
65 localStorage
.setItem(propName
, value
);
67 setTranslations: async
function() {
68 // Import translations from "./translations/$lang.js"
69 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
70 this.state
.tr
= tModule
.translations
;
73 this.state
.lang
= lang
;
74 this.setTranslations();