Arrange sockets.js (unimplemented yet)
[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 },
28 created: function() {
29 const supportedLangs = ["en","es","fr"];
30 Vue.prototype.$lang = localStorage["lang"] ||
31 supportedLangs.includes(navigator.language)
32 ? navigator.language
33 : "en";
34 ajax("/variants", "GET", res => {
35 Vue.prototype.$variants = res.variantArray;
36 });
37 Vue.prototype.$tr = {}; //to avoid a compiler error
38 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
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é)
48 },
49 // Later, for icons (if using feather):
50 // mounted: function() {
51 // feather.replace();
52 // },
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 // });
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