X-Git-Url: https://git.auder.net/variants/%24%7Bobj.vname%7D/style.css?a=blobdiff_plain;f=app.js;h=a011513a48b74177845875b71d7cd1345b2eca32;hb=8022d544e472df7a213f95bd66f64d6f9cb14aaa;hp=f89706233b24ef2fb380202f9cc0f59cceb24c7f;hpb=41534b92f0bcfc8ef5f58d8040706a5e7ce088c6;p=xogo.git
diff --git a/app.js b/app.js
index f897062..a011513 100644
--- a/app.js
+++ b/app.js
@@ -30,6 +30,22 @@ if (!localStorage.getItem("name"))
const sid = localStorage.getItem("sid");
$.getElementById("myName").value = localStorage.getItem("name");
+// "Material" input field name
+let inputName = document.getElementById("myName");
+let formField = document.getElementById("ng-name");
+const setActive = (active) => {
+ if (active) formField.classList.add("form-field--is-active");
+ else {
+ formField.classList.remove("form-field--is-active");
+ inputName.value === "" ?
+ formField.classList.remove("form-field--is-filled") :
+ formField.classList.add("form-field--is-filled");
+ }
+};
+inputName.onblur = () => setActive(false);
+inputName.onfocus = () => setActive(true);
+inputName.focus();
+
/////////
// Utils
@@ -39,10 +55,24 @@ function setName() {
// Turn a "tab" on, and "close" all others
function toggleVisible(element) {
- for (elt of document.querySelectorAll('body > div')) {
+ for (elt of document.querySelectorAll('main > div')) {
if (elt.id != element) elt.style.display = "none";
else elt.style.display = "block";
}
+ if (element.id == "boardContainer") {
+ // Avoid smartphone scrolling effects (TODO?)
+ document.querySelector("html").style.overflow = "hidden";
+ document.body.style.overflow = "hidden";
+ }
+ else {
+ document.querySelector("html").style.overflow = "visible";
+ document.body.style.overflow = "visible";
+ if (element.id == "newGame") {
+ // Workaround "superposed texts" effect
+ inputName.focus();
+ inputName.blur();
+ }
+ }
}
let seek_vname;
@@ -74,8 +104,8 @@ function showNewGameForm() {
$.getElementById("selectColor").selectedIndex = 0;
toggleVisible("newGameForm");
import(`/variants/${vname}/class.js`).then(module => {
- const Rules = module.default;
- prepareOptions(Rules);
+ window.V = module.default;
+ prepareOptions();
});
}
}
@@ -87,50 +117,68 @@ function toggleStyle(e, word) {
}
let options;
-function prepareOptions(Rules) {
+function prepareOptions() {
options = {};
- let optHtml = "";
- for (let select of Rules.Options.select) {
- optHtml += `
-
-
-
-
- ${txt}
-
- `;
+ ${txt}
+
+
+
`;
$.getElementById("gameInfos").innerHTML = htmlContent;
});
};
@@ -207,7 +263,7 @@ const messageCenter = (msg) => {
switch (obj.code) {
// Start new game:
case "gamestart": {
- if (!$.hasFocus()) notifyMe("game");
+ if (document.hidden) notifyMe("game");
gid = obj.gid;
initializeGame(obj);
break;
@@ -239,7 +295,7 @@ const messageCenter = (msg) => {
break;
// Receive opponent's move:
case "newmove":
- if (!$.hasFocus()) notifyMe("move");
+ if (document.hidden) notifyMe("move");
vr.playReceivedMove(obj.moves, () => {
if (vr.getCurrentScore(obj.moves[obj.moves.length-1]) != "*") {
localStorage.removeItem("gid");
@@ -314,7 +370,7 @@ function notifyMe(code) {
}
if (Notification.permission === 'granted') doNotify();
else if (Notification.permission !== 'denied') {
- Notification.requestPermission().then((permission) => {
+ Notification.requestPermission().then(permission => {
if (permission === 'granted') doNotify();
});
}
@@ -328,7 +384,7 @@ const afterPlay = (move) => { //pack into one moves array, then send
start: move.start,
end: move.end
});
- if (vr.turn != color) {
+ if (vr.turn != playerColor) {
toggleTurnIndicator(false);
send("newmove", { gid: gid, moves: curMoves, fen: vr.getFen() });
curMoves = [];
@@ -354,35 +410,45 @@ const conditionalLoadCSS = (vname) => {
}
};
-let vr, color;
+let vr, playerColor;
function initializeGame(obj) {
const options = obj.options || {};
import(`/variants/${obj.vname}/class.js`).then(module => {
- const Rules = module.default;
+ window.V = module.default;
conditionalLoadCSS(obj.vname);
- color = (sid == obj.players[0].sid ? "w" : "b");
+ playerColor = (sid == obj.players[0].sid ? "w" : "b");
// Init + remove potential extra DOM elements from a previous game:
document.getElementById("boardContainer").innerHTML = `
-
+
-
+
`;
- vr = new Rules({
+ vr = new V({
seed: obj.seed, //may be null if FEN already exists (running game)
fen: obj.fen,
element: "chessboard",
- color: color,
+ color: playerColor,
afterPlay: afterPlay,
options: options
});
if (!obj.fen) {
// Game creation
- if (color == "w") send("setfen", {gid: obj.gid, fen: vr.getFen()});
+ if (playerColor == "w") send("setfen", {gid: obj.gid, fen: vr.getFen()});
localStorage.setItem("gid", obj.gid);
}
const select = $.getElementById("selectVariant");
@@ -393,10 +459,10 @@ function initializeGame(obj) {
break;
}
}
- fillGameInfos(obj, color == "w" ? 1 : 0);
+ fillGameInfos(obj, playerColor == "w" ? 1 : 0);
if (obj.randvar) toggleVisible("gameInfos");
else toggleVisible("boardContainer");
- toggleTurnIndicator(vr.turn == color);
+ toggleTurnIndicator(vr.turn == playerColor);
});
}
@@ -411,7 +477,11 @@ function confirmStopGame() {
function toggleGameInfos() {
if ($.getElementById("gameInfos").style.display == "none")
toggleVisible("gameInfos");
- else toggleVisible("boardContainer");
+ else {
+ toggleVisible("boardContainer");
+ // Quickfix for the "vanished piece" bug (move played while on game infos)
+ vr.setupPieces(); //TODO: understand better
+ }
}
$.body.addEventListener("keydown", (e) => {