Code reorganization
[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 14 },
98db2082
BA
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 },
590b75f9
BA
24 $route: function(newRoute) {
25 console.log(this.$route.params);
26 //TODO: conn.send("enter", newRoute)
27 },
625022fd
BA
28 },
29 created: function() {
98db2082
BA
30 const supportedLangs = ["en","es","fr"];
31 Vue.prototype.$lang = localStorage["lang"] ||
32 supportedLangs.includes(navigator.language)
33 ? navigator.language
34 : "en";
590b75f9 35 ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
98db2082 36 Vue.prototype.$tr = {}; //to avoid a compiler error
b644ef7f 37 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
03470390
BA
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);
8d61fc4a
BA
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;
03470390
BA
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é)
8d61fc4a
BA
60// if (!!localStorage["variant"])
61// location.hash = "#game?id=" + localStorage["gameId"];
625022fd 62 },
03470390
BA
63 // Later, for icons (if using feather):
64// mounted: function() {
65// feather.replace();
66// },
625022fd
BA
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// });
03470390
BA
75//
76// board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
77// game : aussi systématique
78// problems: on-demand
79//
03470390 80// See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes
8d61fc4a
BA
81// created: function() {
82// window.onhashchange = this.setDisplay;
83// },
84//});