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
// 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 == "newGame") {
+ // Workaround "superposed texts" effect
+ inputName.focus();
+ inputName.blur();
+ }
}
let seek_vname;
let options;
function prepareOptions(Rules) {
options = {};
- let optHtml = "";
- for (let select of Rules.Options.select) {
- optHtml += `
- <label for="var_${select.variable}">${select.label}</label>
- <select id="var_${select.variable}" data-numeric="1">`;
- for (option of select.options) {
- const defaut = option.value == select.defaut;
- optHtml += `<option value="${option.value}"`;
- if (defaut) optHtml += 'selected="true"';
- optHtml += `>${option.label}</option>`;
+ let optHtml = Rules.Options.select.map(select => { return `
+ <div class="option-select">
+ <label for="var_${select.variable}">${select.label}</label>
+ <div class="select">
+ <select id="var_${select.variable}" data-numeric="1">` +
+ select.options.map(option => { return `
+ <option
+ value="${option.value}"
+ ${option.value == select.defaut ? " selected" : ""}
+ >
+ ${option.label}
+ </option>`;
+ }).join("") + `
+ </select>
+ <span class="focus"></span>
+ </div>
+ </div>`;
+ }).join("");
+ optHtml += Rules.Options.check.map(check => {
+ return `
+ <div class="option-check">
+ <label class="checkbox">
+ <input id="var_${check.variable}"
+ type="checkbox"${check.defaut ? " checked" : ""}/>
+ <span class="spacer"></span>
+ <span>${check.label}</span>
+ </label>
+ </div>`;
+ }).join("");
+ if (Rules.Options.styles.length >= 1) {
+ optHtml += '<div class="words">';
+ let i = 0;
+ const stylesLength = Rules.Options.styles.length;
+ while (i < stylesLength) {
+ optHtml += '<div class="row">';
+ for (let j=i; j<i+4; j++) {
+ if (j == stylesLength) break;
+ const style = Rules.Options.styles[j];
+ optHtml +=
+ `<span onClick="toggleStyle(event, '${style}')">${style}</span>`;
+ }
+ optHtml += "</div>";
+ i += 4;
}
- optHtml += '</select>';
- }
- for (let check of Rules.Options.check) {
- optHtml += `
- <label for="var_${check.variable}">${check.label}</label>
- <input id="var_${check.variable}"
- type="checkbox"`;
- if (check.defaut) optHtml += 'checked="true"';
- optHtml += '/>';
- }
- if (Rules.Options.styles.length >= 1) optHtml += "<p>";
- for (let style of Rules.Options.styles) {
- optHtml += `
- <span class="word" onClick="toggleStyle(event, '${style}')">
- ${style}
- </span>`;
+ optHtml += "</div>";
}
- if (Rules.Options.styles.length >= 1) optHtml += "</p>";
$.getElementById("gameOptions").innerHTML = optHtml;
}
function getGameLink() {
const vname = $.getElementById("selectVariant").value;
const color = $.getElementById("selectColor").value;
- for (const select of $.querySelectorAll("#gameOptions > select")) {
+ for (const select of $.querySelectorAll("#gameOptions select")) {
let value = select.value;
if (select.attributes["data-numeric"]) value = parseInt(value, 10);
options[ select.id.split("_")[1] ] = value;
}
- for (const check of $.querySelectorAll("#gameOptions > input"))
+ for (const check of $.querySelectorAll("#gameOptions input"))
options[ check.id.split("_")[1] ] = check.checked;
send("creategame", {
vname: vname,
.then(res => res.text())
.then(txt => {
let htmlContent = `
- <p>
- <strong>${gameInfos.vdisp}</strong>
- <span>vs. ${gameInfos.players[oppIndex].name}</span>
- </p>
- <hr>
- <p>`;
- htmlContent +=
- Object.entries(gameInfos.options).map(opt => {
- return (
- '<span class="option">' +
- (opt[1] === true ? opt[0] : `${opt[0]}:${opt[1]}`) +
- '</span>'
- );
- })
- .join(", ");
+ <div class="players-info">
+ <p>
+ <span class="bold">${gameInfos.vdisp}</span>
+ <span>vs. ${gameInfos.players[oppIndex].name}</span>
+ </p>
+ </div>`;
+ const options = Object.entries(gameInfos.options);
+ if (options.length > 0) {
+ htmlContent += '<div class="options-info">';
+ let i = 0;
+ while (i < options.length) {
+ htmlContent += '<div class="row">';
+ for (let j=i; j<i+4; j++) {
+ if (j == options.length) break;
+ const opt = options[j];
+ htmlContent +=
+ '<span class="option">' +
+ (opt[1] === true ? opt[0] : `${opt[0]}:${opt[1]}`) + " " +
+ '</span>';
+ }
+ htmlContent += "</div>";
+ i += 4;
+ }
+ htmlContent += "</div>";
+ }
htmlContent += `
- </p>
- <hr>
- <div class="rules">
- ${txt}
- </div>
- <button onClick="toggleGameInfos()">Back to game</button>`;
+ <div class="rules">${txt}</div>
+ <div class="btn-wrap">
+ <button onClick="toggleGameInfos()">Back to game</button>
+ </div>`;
$.getElementById("gameInfos").innerHTML = htmlContent;
});
};
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 = [];
}
};
-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;
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 = `
<div id="upLeftInfos"
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");
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);
});
}
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) => {