X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariant.js;h=5a6c224a020e77bb68effe242dbe6424ec18108b;hb=badeb466c977ed9a8e1b464a2236001126decb9e;hp=ae576d8911174b70d770fe62ed614f1ac7426f55;hpb=b6487fb9c41705187cf97215fc9e8f86a59057c7;p=vchess.git diff --git a/public/javascripts/variant.js b/public/javascripts/variant.js index ae576d89..5a6c224a 100644 --- a/public/javascripts/variant.js +++ b/public/javascripts/variant.js @@ -1,18 +1,62 @@ new Vue({ - el: "#variantPage", + el: "#VueElement", data: { - display: "room", //default: main hall + display: "", //default to main hall; see "created()" function + gameid: undefined, //...yet + queryHash: "", + conn: null, + mode: "analyze", + allowChat: false, + allowMovelist: true, + // Settings initialized with values from localStorage + settings: { + bcolor: localStorage["bcolor"] || "lichess", + sound: parseInt(localStorage["sound"]) || 2, + hints: parseInt(localStorage["hints"]) || 1, + coords: !!eval(localStorage["coords"]), + highlight: !!eval(localStorage["highlight"]), + sqSize: parseInt(localStorage["sqSize"]), + }, }, created: function() { - // TODO: navigation becomes a little more complex - const url = window.location.href; - const hashPos = url.indexOf("#"); - if (hashPos >= 0) - this.setDisplay(url.substr(hashPos+1)); + if (!!localStorage["variant"]) + { + location.hash = "#game?id=" + localStorage["gameId"]; + this.display = location.hash.substr(1); + } + else + this.setDisplay(); + window.onhashchange = this.setDisplay; + // Our ID, always set (DB id if registered, collision-free random string otherwise) + this.myid = user.id || localStorage["myid"] || "anon-" + getRandString(); + this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id); + const socketCloseListener = () => { + this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id); + } + this.conn.onclose = socketCloseListener; }, methods: { - setDisplay: function(elt) { - this.display = elt; + updateSettings: function(event) { + const propName = + event.target.id.substr(3).replace(/^\w/, c => c.toLowerCase()) + localStorage[propName] = ["highlight","coords"].includes(propName) + ? event.target.checked + : event.target.value; + }, + // Game is over, clear storage and put it in indexedDB + archiveGame: function() { + // TODO: ... + //clearStorage(); + }, + setDisplay: function() { + // Prevent set display if there is a running game + if (!!localStorage["variant"]) + return; + if (!location.hash) + location.hash = "#room"; //default + const hashParts = location.hash.substr(1).split("?"); + this.display = hashParts[0]; + this.queryHash = hashParts[1]; //may be empty, undefined... // Close menu on small screens: let menuToggle = document.getElementById("drawer-control"); if (!!menuToggle) @@ -20,6 +64,3 @@ new Vue({ }, }, }); - -// TODO: -// si quand on arrive il y a une continuation "humaine" : display="game" et retour à la partie !