Arrange sockets.js (unimplemented yet)
[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 },
625022fd
BA
27 },
28 created: function() {
98db2082
BA
29 const supportedLangs = ["en","es","fr"];
30 Vue.prototype.$lang = localStorage["lang"] ||
31 supportedLangs.includes(navigator.language)
32 ? navigator.language
33 : "en";
03470390
BA
34 ajax("/variants", "GET", res => {
35 Vue.prototype.$variants = res.variantArray;
625022fd 36 });
98db2082 37 Vue.prototype.$tr = {}; //to avoid a compiler error
b644ef7f 38 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
03470390
BA
39 const myid = localStorage["myid"] || util.getRandString();
40 // NOTE: in this version, we don't say on which page we are, yet
41 // ==> we'll say "enter/leave" page XY (in fact juste "enter", seemingly)
42 Vue.prototype.$conn = new WebSocket(params.socketUrl + "/?sid=" + myid);
43 //TODO: si une partie en cours dans storage, rediriger vers cette partie
44 //(à condition que l'URL n'y corresponde pas déjà !)
45 // TODO: à l'arrivée sur le site : set peerID (un identifiant unique
46 // en tout cas...) si pas trouvé dans localStorage "myid"
47 // (l'identifiant de l'utilisateur si connecté)
625022fd 48 },
03470390
BA
49 // Later, for icons (if using feather):
50// mounted: function() {
51// feather.replace();
52// },
625022fd
BA
53}).$mount("#app");
54
55// TODO: get rules, dynamic import
56// Load a rules page (AJAX)
57// router.get("/rules/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => {
58// const lang = selectLanguage(req, res);
59// res.render("rules/" + req.params["vname"] + "/" + lang);
60// });
03470390
BA
61//
62// board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
63// game : aussi systématique
64// problems: on-demand
65//
66// It works (to watch for route change), in a component:
67//watch: {
68// $route: function(newRoute) {
69// console.log(this.$route.params);
70// },
71//},
72// See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes