1 // NOTE: do not use ajax() here because ajax.js requires the store
2 import params
from "./parameters"; //for server URL
3 import { getRandString
} from "./utils/alea";
6 // https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
17 "Content-Type": "application/json;charset=UTF-8",
18 "X-Requested-With": "XMLHttpRequest"
21 params
.serverUrl
+ "/variants",
27 .then(res
=> res
.json())
29 if (!Array
.isArray(json
.variantArray
)) {
30 alert("Variants loading failed: reload the page");
33 this.state
.variants
= json
.variantArray
34 .sort((v1
,v2
) => v1
.name
.localeCompare(v2
.name
));
36 let mysid
= localStorage
.getItem("mysid");
37 // Assign mysid only once (until next time user clear browser data)
39 mysid
= getRandString();
40 localStorage
.setItem("mysid", mysid
);
42 // Quick user setup using local storage:
44 id: localStorage
.getItem("myid") || 0,
45 name: localStorage
.getItem("myname") || "", //"" for "anonymous"
46 email: "", //unknown yet
47 notify: false, //email notifications
50 // Slow verification through the server:
51 // NOTE: still superficial identity usurpation possible, but difficult.
53 params
.serverUrl
+ "/whoami",
57 credentials: params
.credentials
60 .then(res
=> res
.json())
62 this.state
.user
.id
= json
.id
;
63 const storedId
= localStorage
.getItem("myid");
64 if (json
.id
> 0 && !storedId
)
65 // User cleared localStorage
66 localStorage
.setItem("myid", json
.id
);
67 else if (json
.id
== 0 && !!storedId
)
68 // User cleared cookie
69 localStorage
.removeItem("myid");
70 this.state
.user
.name
= json
.name
;
71 const storedName
= localStorage
.getItem("myname");
72 if (!!json
.name
&& !storedName
)
73 // User cleared localStorage
74 localStorage
.setItem("myname", json
.name
);
75 else if (!json
.name
&& !!storedName
)
76 // User cleared cookie
77 localStorage
.removeItem("myname");
78 this.state
.user
.email
= json
.email
;
79 this.state
.user
.notify
= json
.notify
;
81 // Settings initialized with values from localStorage
82 const getItemDefault
= (item
, defaut
) => {
83 const value
= localStorage
.getItem(item
);
84 if (!value
) return defaut
;
85 return value
== "true";
87 this.state
.settings
= {
88 bcolor: localStorage
.getItem("bcolor") || "lichess",
89 sound: getItemDefault("sound", true),
90 hints: getItemDefault("hints", true),
91 highlight: getItemDefault("highlight", true),
92 gotonext: getItemDefault("gotonext", true),
93 scrollmove: getItemDefault("scrollmove", false)
95 const supportedLangs
= ["en", "es", "fr"];
96 const navLanguage
= navigator
.language
.substr(0, 2);
98 localStorage
["lang"] ||
99 (supportedLangs
.includes(navLanguage
) ? navLanguage : "en");
100 this.setTranslations();
102 updateSetting: function(propName
, value
) {
103 this.state
.settings
[propName
] = value
;
104 localStorage
.setItem(propName
, value
);
106 setTranslations: async
function() {
107 // Import translations from "./translations/$lang.js"
108 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
109 this.state
.tr
= tModule
.translations
;
112 this.state
.lang
= lang
;
113 this.setTranslations();