Client now running. Toward fully 1-page (with filters for variants)
[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 },
ccd4a2b7
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// },
24// $route: function(newRoute) {
25// //console.log(this.$route.params);
26// console.log("navig to " + newRoute);
27// //TODO: conn.send("enter", newRoute)
28// },
29// },
625022fd 30 created: function() {
98db2082
BA
31 const supportedLangs = ["en","es","fr"];
32 Vue.prototype.$lang = localStorage["lang"] ||
33 supportedLangs.includes(navigator.language)
34 ? navigator.language
35 : "en";
ccd4a2b7 36 Vue.prototype.$variants = []; //avoid runtime error
590b75f9 37 ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
98db2082 38 Vue.prototype.$tr = {}; //to avoid a compiler error
ccd4a2b7 39 Vue.prototype.$user = {}; //TODO: from storage
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);
8d61fc4a
BA
45 // Settings initialized with values from localStorage
46 Vue.prototype.$settings = {
47 bcolor: localStorage["bcolor"] || "lichess",
48 sound: parseInt(localStorage["sound"]) || 2,
49 hints: parseInt(localStorage["hints"]) || 1,
50 coords: !!eval(localStorage["coords"]),
51 highlight: !!eval(localStorage["highlight"]),
52 sqSize: parseInt(localStorage["sqSize"]),
53 };
54 const socketCloseListener = () => {
55 Vue.prototype.$conn = new WebSocket(params.socketUrl + "/?sid=" + myid);
56 }
57 Vue.prototype.$conn.onclose = socketCloseListener;
03470390
BA
58 //TODO: si une partie en cours dans storage, rediriger vers cette partie
59 //(à condition que l'URL n'y corresponde pas déjà !)
60 // TODO: à l'arrivée sur le site : set peerID (un identifiant unique
61 // en tout cas...) si pas trouvé dans localStorage "myid"
62 // (l'identifiant de l'utilisateur si connecté)
8d61fc4a
BA
63// if (!!localStorage["variant"])
64// location.hash = "#game?id=" + localStorage["gameId"];
625022fd 65 },
03470390
BA
66 // Later, for icons (if using feather):
67// mounted: function() {
68// feather.replace();
69// },
625022fd
BA
70}).$mount("#app");
71
72// TODO: get rules, dynamic import
73// Load a rules page (AJAX)
74// router.get("/rules/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => {
75// const lang = selectLanguage(req, res);
76// res.render("rules/" + req.params["vname"] + "/" + lang);
77// });
03470390
BA
78//
79// board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
80// game : aussi systématique
81// problems: on-demand
82//
03470390 83// See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes
8d61fc4a
BA
84// created: function() {
85// window.onhashchange = this.setDisplay;
86// },
87//});