Code reorganization
[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 watch: {
16 $lang: async function(newLang) {
17 // Fill modalWelcome, and import translations from "./translations/$lang.js"
18 document.getElementById("modalWelcome").innerHTML =
19 require("raw-loader!pug-plain-loader!./modals/welcome/" + newLang + ".pug");
20 const tModule = await import("./translations/" + newLang + ".js");
21 Vue.prototype.$tr = tModule.translations;
22 //console.log(tModule.translations);
23 },
24 $route: function(newRoute) {
25 console.log(this.$route.params);
26 //TODO: conn.send("enter", newRoute)
27 },
28 },
29 created: function() {
30 const supportedLangs = ["en","es","fr"];
31 Vue.prototype.$lang = localStorage["lang"] ||
32 supportedLangs.includes(navigator.language)
33 ? navigator.language
34 : "en";
35 ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
36 Vue.prototype.$tr = {}; //to avoid a compiler error
37 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
38 const myid = localStorage["myid"] || util.getRandString();
39 // NOTE: in this version, we don't say on which page we are, yet
40 // ==> we'll say "enter/leave" page XY (in fact juste "enter", seemingly)
41 Vue.prototype.$conn = new WebSocket(params.socketUrl + "/?sid=" + myid);
42 // Settings initialized with values from localStorage
43 Vue.prototype.$settings = {
44 bcolor: localStorage["bcolor"] || "lichess",
45 sound: parseInt(localStorage["sound"]) || 2,
46 hints: parseInt(localStorage["hints"]) || 1,
47 coords: !!eval(localStorage["coords"]),
48 highlight: !!eval(localStorage["highlight"]),
49 sqSize: parseInt(localStorage["sqSize"]),
50 };
51 const socketCloseListener = () => {
52 Vue.prototype.$conn = new WebSocket(params.socketUrl + "/?sid=" + myid);
53 }
54 Vue.prototype.$conn.onclose = socketCloseListener;
55 //TODO: si une partie en cours dans storage, rediriger vers cette partie
56 //(à condition que l'URL n'y corresponde pas déjà !)
57 // TODO: à l'arrivée sur le site : set peerID (un identifiant unique
58 // en tout cas...) si pas trouvé dans localStorage "myid"
59 // (l'identifiant de l'utilisateur si connecté)
60 // if (!!localStorage["variant"])
61 // location.hash = "#game?id=" + localStorage["gameId"];
62 },
63 // Later, for icons (if using feather):
64 // mounted: function() {
65 // feather.replace();
66 // },
67 }).$mount("#app");
68
69 // TODO: get rules, dynamic import
70 // Load a rules page (AJAX)
71 // router.get("/rules/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => {
72 // const lang = selectLanguage(req, res);
73 // res.render("rules/" + req.params["vname"] + "/" + lang);
74 // });
75 //
76 // board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
77 // game : aussi systématique
78 // problems: on-demand
79 //
80 // See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes
81 // created: function() {
82 // window.onhashchange = this.setDisplay;
83 // },
84 //});