X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariant.js;h=5a6c224a020e77bb68effe242dbe6424ec18108b;hb=a3ab5fdb6e9d614f55bb7ecef5887ddb7875df4b;hp=e9e956ca068f24feb45e6ba051533cc40488785f;hpb=643479f8d7c3622b57fc49c4f10d9950793ebf4f;p=vchess.git diff --git a/public/javascripts/variant.js b/public/javascripts/variant.js index e9e956ca..5a6c224a 100644 --- a/public/javascripts/variant.js +++ b/public/javascripts/variant.js @@ -1,25 +1,66 @@ new Vue({ - el: "#variantPage", + el: "#VueElement", data: { - display: getCookie("display-"+variant,""), //default: do not show anything... - problem: undefined, //current problem in view + 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() { + 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: { - toggleDisplay: function(elt) { - if (this.display == elt) - { - this.display = ""; //hide - setCookie("display-"+variant, ""); - } - else - { - this.display = elt; //show - setCookie("display-"+variant, 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(); }, - showProblem: function(problemTxt) { - this.problem = JSON.parse(problemTxt); - this.display = "game"; + 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) + menuToggle.checked = false; }, }, });