Tiny changes...
[vchess.git] / client / src / main.js
1 import Vue from "vue";
2 import App from "./App.vue";
3 import router from "./router";
4 import params from "./parameters"; //for socket connection
5 import { ajax } from "./utils/ajax";
6 import { util } from "./utils/misc";
7
8 Vue.config.productionTip = false;
9
10 new Vue({
11 router,
12 render: function(h) {
13 return h(App);
14 },
15 // data: {
16 // lang: "",
17 // },
18 watch: {
19 $lang: async function(newLang) {
20 // Fill modalWelcome, and import translations from "./translations/$lang.js"
21 document.getElementById("modalWelcome").innerHTML =
22 require("raw-loader!pug-plain-loader!./modals/welcome/" + newLang + ".pug");
23 const tModule = await import("./translations/" + newLang + ".js");
24 Vue.prototype.$tr = tModule.translations;
25 //console.log(tModule.translations);
26 },
27 $route: function(newRoute) {
28 console.log(this.$route.params);
29 //TODO: conn.send("enter", newRoute)
30 },
31 },
32 created: function() {
33 const supportedLangs = ["en","es","fr"];
34 Vue.prototype.$lang = localStorage["lang"] ||
35 supportedLangs.includes(navigator.language)
36 ? navigator.language
37 : "en";
38 ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
39 Vue.prototype.$tr = {}; //to avoid a compiler error
40 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
41 const myid = localStorage["myid"] || util.getRandString();
42 // NOTE: in this version, we don't say on which page we are, yet
43 // ==> we'll say "enter/leave" page XY (in fact juste "enter", seemingly)
44 Vue.prototype.$conn = new WebSocket(params.socketUrl + "/?sid=" + myid);
45 //TODO: si une partie en cours dans storage, rediriger vers cette partie
46 //(à condition que l'URL n'y corresponde pas déjà !)
47 // TODO: à l'arrivée sur le site : set peerID (un identifiant unique
48 // en tout cas...) si pas trouvé dans localStorage "myid"
49 // (l'identifiant de l'utilisateur si connecté)
50 },
51 // Later, for icons (if using feather):
52 // mounted: function() {
53 // feather.replace();
54 // },
55 }).$mount("#app");
56
57 // TODO: get rules, dynamic import
58 // Load a rules page (AJAX)
59 // router.get("/rules/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => {
60 // const lang = selectLanguage(req, res);
61 // res.render("rules/" + req.params["vname"] + "/" + lang);
62 // });
63 //
64 // board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
65 // game : aussi systématique
66 // problems: on-demand
67 //
68 // See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes