792d2a5b2a7f3eb59ab94cdbab327eba28545ca3
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 "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
48 newsRead: localStorage
.getItem("newsRead") || 0,
51 // Slow verification through the server:
52 // NOTE: still superficial identity usurpation possible, but difficult.
54 params
.serverUrl
+ "/whoami",
58 credentials: params
.credentials
61 .then(res
=> res
.json())
63 this.state
.user
.id
= json
.id
;
64 const storedId
= localStorage
.getItem("myid");
65 if (json
.id
> 0 && !storedId
)
66 // User cleared localStorage
67 localStorage
.setItem("myid", json
.id
);
68 else if (json
.id
== 0 && !!storedId
)
69 // User cleared cookie
70 localStorage
.removeItem("myid");
71 this.state
.user
.name
= json
.name
;
72 const storedName
= localStorage
.getItem("myname");
73 if (!!json
.name
&& !storedName
)
74 // User cleared localStorage
75 localStorage
.setItem("myname", json
.name
);
76 else if (!json
.name
&& !!storedName
)
77 // User cleared cookie
78 localStorage
.removeItem("myname");
79 this.state
.user
.email
= json
.email
;
80 this.state
.user
.notify
= json
.notify
;
81 if (!!json
.newsRead
&& json
.newsRead
> this.state
.user
.newsRead
)
82 this.state
.user
.newsRead
= json
.newsRead
;
84 // Settings initialized with values from localStorage
85 const getItemDefaultTrue
= (item
) => {
86 const value
= localStorage
.getItem(item
);
87 if (!value
) return true;
88 return value
== "true";
90 this.state
.settings
= {
91 bcolor: localStorage
.getItem("bcolor") || "lichess",
92 sound: getItemDefaultTrue("sound"),
93 hints: getItemDefaultTrue("hints"),
94 highlight: getItemDefaultTrue("highlight"),
95 gotonext: getItemDefaultTrue("gotonext"),
96 randomness: parseInt(localStorage
.getItem("randomness"))
98 if (isNaN(this.state
.settings
.randomness
))
99 // Default: random asymmetric
100 this.state
.settings
.randomness
= 2;
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();