1 // NOTE: do not use ajax() here because ajax.js require the store for translations
2 import params
from "./parameters"; //for server URL
3 import { getRandString
} from "./utils/alea";
5 // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
14 socketCloseListener: null,
17 params
.serverUrl
+ "/variants",
20 .then(res
=> res
.json())
22 this.state
.variants
= json
.variantArray
.sort(
23 (v1
,v2
) => v1
.name
.localeCompare(v2
.name
));
25 let mysid
= localStorage
.getItem("mysid");
26 // Assign mysid only once (until next time user clear browser data)
28 mysid
= getRandString();
29 localStorage
.setItem("mysid", mysid
);
31 // Quick user setup using local storage:
33 id: localStorage
.getItem("myid") || 0,
34 name: localStorage
.getItem("myname") || "", //"" for "anonymous"
35 email: "", //unknown yet
36 notify: false, //email notifications
39 // Slow verification through the server:
40 // NOTE: still superficial identity usurpation possible, but difficult.
42 params
.serverUrl
+ "/whoami",
45 credentials: params
.credentials
48 .then(res
=> res
.json())
50 this.state
.user
.id
= json
.id
;
51 const storedId
= localStorage
.getItem("myid");
52 if (json
.id
> 0 && !storedId
)
53 // User cleared localStorage
54 localStorage
.setItem("myid", json
.id
);
55 else if (json
.id
== 0 && !!storedId
)
56 // User cleared cookie
57 localStorage
.removeItem("myid");
58 this.state
.user
.name
= json
.name
;
59 const storedName
= localStorage
.getItem("myname");
60 if (!!json
.name
&& !storedName
)
61 // User cleared localStorage
62 localStorage
.setItem("myname", json
.name
);
63 else if (!json
.name
&& !!storedName
)
64 // User cleared cookie
65 localStorage
.removeItem("myname");
66 this.state
.user
.email
= json
.email
;
67 this.state
.user
.notify
= json
.notify
;
69 // Settings initialized with values from localStorage
70 const getItemDefaultTrue
= (item
) => {
71 const value
= localStorage
.getItem(item
);
72 if (!value
) return true;
73 return value
== "true";
75 this.state
.settings
= {
76 bcolor: localStorage
.getItem("bcolor") || "lichess",
77 sound: getItemDefaultTrue("sound"),
78 hints: getItemDefaultTrue("hints"),
79 highlight: getItemDefaultTrue("highlight")
81 const supportedLangs
= ["en", "es", "fr"];
82 const navLanguage
= navigator
.language
.substr(0,2);
84 localStorage
["lang"] ||
85 (supportedLangs
.includes(navLanguage
) ? navLanguage : "en");
86 this.setTranslations();
88 updateSetting: function(propName
, value
) {
89 this.state
.settings
[propName
] = value
;
90 localStorage
.setItem(propName
, value
);
92 setTranslations: async
function() {
93 // Import translations from "./translations/$lang.js"
94 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
95 this.state
.tr
= tModule
.translations
;
98 this.state
.lang
= lang
;
99 this.setTranslations();