Advance on client side
[vchess.git] / client / client_OLD / javascripts / variant.js
CommitLineData
f5d3e4f5 1new Vue({
b57dbd12 2 el: "#VueElement",
c794dbb8 3 data: {
a3ab5fdb 4 display: "", //default to main hall; see "created()" function
97da8720
BA
5 gameRef: undefined, //...for now
6 probId: undefined,
81bc1102 7 conn: null,
a3ab5fdb
BA
8 mode: "analyze",
9 allowChat: false,
10 allowMovelist: true,
582df349
BA
11 // Settings initialized with values from localStorage
12 settings: {
13 bcolor: localStorage["bcolor"] || "lichess",
14 sound: parseInt(localStorage["sound"]) || 2,
15 hints: parseInt(localStorage["hints"]) || 1,
16 coords: !!eval(localStorage["coords"]),
17 highlight: !!eval(localStorage["highlight"]),
18 sqSize: parseInt(localStorage["sqSize"]),
19 },
c794dbb8 20 },
d449ae46 21 created: function() {
97da8720 22 window.onhashchange = this.setDisplay;
582df349 23 if (!!localStorage["variant"])
582df349 24 location.hash = "#game?id=" + localStorage["gameId"];
582df349
BA
25 else
26 this.setDisplay();
a3ab5fdb
BA
27 // Our ID, always set (DB id if registered, collision-free random string otherwise)
28 this.myid = user.id || localStorage["myid"] || "anon-" + getRandString();
d44df0b0 29 this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id);
81bc1102 30 const socketCloseListener = () => {
d44df0b0 31 this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id);
81bc1102
BA
32 }
33 this.conn.onclose = socketCloseListener;
d449ae46 34 },
4ecf423b 35 methods: {
a3ab5fdb
BA
36 // Game is over, clear storage and put it in indexedDB
37 archiveGame: function() {
38 // TODO: ...
39 //clearStorage();
40 },
d44df0b0 41 setDisplay: function() {
582df349
BA
42 // Prevent set display if there is a running game
43 if (!!localStorage["variant"])
44 return;
4608eed9
BA
45 if (!location.hash)
46 location.hash = "#room"; //default
582df349
BA
47 const hashParts = location.hash.substr(1).split("?");
48 this.display = hashParts[0];
97da8720
BA
49 if (hashParts[0] == "problems" && !!hashParts[1])
50 {
51 // Show a specific problem
52 this.probId = hashParts[1].split("=")[1];
53 }
54 else if (hashParts[0] == "game" && !!hashParts[1])
55 {
56 // Show a specific game, maybe with a user ID
57 const params = hashParts[1].split("&").filter(h => h.split("=")[1]);
58 // TODO: Vue.set(...) probably required here
59 this.gameRef = {
60 id: params[0],
61 uid: params[1], //may be undefined
62 };
63 }
b6487fb9 64 // Close menu on small screens:
a5d56686
BA
65 let menuToggle = document.getElementById("drawer-control");
66 if (!!menuToggle)
67 menuToggle.checked = false;
e6dcb115 68 },
4ecf423b 69 },
f5d3e4f5 70});