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";
4 import { delCookie
} from "./utils/cookie";
7 // https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
18 "Content-Type": "application/json;charset=UTF-8",
19 "X-Requested-With": "XMLHttpRequest"
22 params
.serverUrl
+ "/variants",
28 .then(res
=> res
.json())
30 if (!Array
.isArray(json
.variantArray
)) {
31 alert("Variants loading failed: reload the page");
34 this.state
.variants
= json
.variantArray
35 .sort((v1
,v2
) => v1
.name
.localeCompare(v2
.name
));
37 let mysid
= localStorage
.getItem("mysid");
38 // Assign mysid only once (until next time user clear browser data)
40 mysid
= getRandString();
41 localStorage
.setItem("mysid", mysid
);
43 // Quick user setup using local storage:
45 id: localStorage
.getItem("myid") || 0,
46 name: localStorage
.getItem("myname") || "", //"" for "anonymous"
47 email: "", //unknown yet
48 notify: false, //email notifications
51 // Slow verification through the server:
53 params
.serverUrl
+ "/whoami",
57 credentials: params
.credentials
60 .then(res
=> res
.json())
63 // Removed, or wrong token
64 if (this.state
.user
.id
> 0) {
65 this.state
.user
.id
= 0;
66 localStorage
.removeItem("myid");
68 if (!!this.state
.user
.name
) {
69 this.state
.user
.name
= "";
70 localStorage
.removeItem("myname");
72 if (document
.cookie
.indexOf("token") >= 0) delCookie("token");
75 if (this.state
.user
.id
!= json
.id
) {
76 this.state
.user
.id
= json
.id
;
77 localStorage
.setItem("myid", json
.id
);
79 if (this.state
.user
.name
!= json
.name
) {
80 this.state
.user
.name
= json
.name
;
81 localStorage
.setItem("myname", json
.name
);
83 this.state
.user
.email
= json
.email
;
84 this.state
.user
.notify
= json
.notify
;
87 // Settings initialized with values from localStorage
88 const getItemDefault
= (item
, defaut
) => {
89 const value
= localStorage
.getItem(item
);
90 if (!value
) return defaut
;
91 return value
== "true";
93 this.state
.settings
= {
94 bcolor: localStorage
.getItem("bcolor") || "lichess",
95 sound: getItemDefault("sound", true),
96 hints: getItemDefault("hints", true),
97 highlight: getItemDefault("highlight", true),
98 gotonext: getItemDefault("gotonext", true),
99 scrollmove: getItemDefault("scrollmove", false)
101 const supportedLangs
= ["en", "es", "fr"];
102 const navLanguage
= navigator
.language
.substr(0, 2);
104 localStorage
["lang"] ||
105 (supportedLangs
.includes(navLanguage
) ? navLanguage : "en");
106 this.setTranslations();
108 updateSetting: function(propName
, value
) {
109 this.state
.settings
[propName
] = value
;
110 localStorage
.setItem(propName
, value
);
112 setTranslations: async
function() {
113 // Import translations from "./translations/$lang.js"
114 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
115 this.state
.tr
= tModule
.translations
;
118 this.state
.lang
= lang
;
119 this.setTranslations();