Client now running. Toward fully 1-page (with filters for variants)
[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 // console.log("navig to " + newRoute);
27 // //TODO: conn.send("enter", newRoute)
28 // },
29 // },
30 created: function() {
31 const supportedLangs = ["en","es","fr"];
32 Vue.prototype.$lang = localStorage["lang"] ||
33 supportedLangs.includes(navigator.language)
34 ? navigator.language
35 : "en";
36 Vue.prototype.$variants = []; //avoid runtime error
37 ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
38 Vue.prototype.$tr = {}; //to avoid a compiler error
39 Vue.prototype.$user = {}; //TODO: from storage
40 // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
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);
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;
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é)
63 // if (!!localStorage["variant"])
64 // location.hash = "#game?id=" + localStorage["gameId"];
65 },
66 // Later, for icons (if using feather):
67 // mounted: function() {
68 // feather.replace();
69 // },
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 // });
78 //
79 // board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
80 // game : aussi systématique
81 // problems: on-demand
82 //
83 // See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes
84 // created: function() {
85 // window.onhashchange = this.setDisplay;
86 // },
87 //});