TODO: where to put template: in views/ or components/?
[vchess.git] / client / src / main.js
CommitLineData
625022fd
BA
1import Vue from "vue";
2import App from "./App.vue";
3import router from "./router";
03470390 4import params from "./parameters"; //for socket connection
625022fd 5import { ajax } from "./utils/ajax";
98db2082 6import { util } from "./utils/misc";
625022fd
BA
7
8Vue.config.productionTip = false;
9
10new Vue({
11 router,
12 render: function(h) {
13 return h(App);
98db2082
BA
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 },
590b75f9
BA
27 $route: function(newRoute) {
28 console.log(this.$route.params);
29 //TODO: conn.send("enter", newRoute)
30 },
625022fd
BA
31 },
32 created: function() {
98db2082
BA
33 const supportedLangs = ["en","es","fr"];
34 Vue.prototype.$lang = localStorage["lang"] ||
35 supportedLangs.includes(navigator.language)
36 ? navigator.language
37 : "en";
590b75f9 38 ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
98db2082 39 Vue.prototype.$tr = {}; //to avoid a compiler error
b644ef7f 40 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
03470390
BA
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é)
625022fd 50 },
03470390
BA
51 // Later, for icons (if using feather):
52// mounted: function() {
53// feather.replace();
54// },
625022fd
BA
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// });
03470390
BA
63//
64// board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
65// game : aussi systématique
66// problems: on-demand
67//
03470390 68// See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes