Spaces:
Running
Running
Commit
·
f794d93
1
Parent(s):
edd78c3
Update static/app.js
Browse files- static/app.js +59 -42
static/app.js
CHANGED
|
@@ -1,63 +1,80 @@
|
|
| 1 |
-
const RESULTS_PER_PAGE =
|
| 2 |
let allResults = [];
|
| 3 |
let currentPage = 0;
|
| 4 |
|
| 5 |
-
var board = Chessboard(
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
});
|
| 12 |
|
| 13 |
function updateInfo() {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
}
|
| 18 |
|
| 19 |
function search() {
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
document.getElementById(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
});
|
| 35 |
}
|
| 36 |
|
| 37 |
function displayPage() {
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
function nextPage() {
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
}
|
| 54 |
|
| 55 |
function prevPage() {
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
}
|
| 62 |
|
| 63 |
-
updateInfo();
|
|
|
|
| 1 |
+
const RESULTS_PER_PAGE = 20;
|
| 2 |
let allResults = [];
|
| 3 |
let currentPage = 0;
|
| 4 |
|
| 5 |
+
var board = Chessboard("board", {
|
| 6 |
+
draggable: true,
|
| 7 |
+
dropOffBoard: "trash",
|
| 8 |
+
sparePieces: true,
|
| 9 |
+
pieceTheme: "/static/img/chesspieces/wikipedia/{piece}.png",
|
| 10 |
+
onSnapEnd: updateInfo,
|
| 11 |
});
|
| 12 |
|
| 13 |
function updateInfo() {
|
| 14 |
+
document.getElementById("fen").textContent = "FEN: " + board.fen();
|
| 15 |
+
let pos = board.position();
|
| 16 |
+
let pieces = Object.keys(pos).length;
|
| 17 |
}
|
| 18 |
|
| 19 |
function search() {
|
| 20 |
+
let pos = board.position();
|
| 21 |
+
if (Object.keys(pos).length < 3) {
|
| 22 |
+
document.getElementById("results").innerHTML =
|
| 23 |
+
"<p>Add at least 3 pieces to search.</p>";
|
| 24 |
+
return;
|
| 25 |
+
}
|
| 26 |
+
document.getElementById("results").innerHTML = "<p>Searching...</p>";
|
| 27 |
+
fetch("/search", {
|
| 28 |
+
method: "POST",
|
| 29 |
+
headers: { "Content-Type": "application/json" },
|
| 30 |
+
body: JSON.stringify({ fen: board.fen() }),
|
| 31 |
+
})
|
| 32 |
+
.then((r) => r.json())
|
| 33 |
+
.then((data) => {
|
| 34 |
+
allResults = data.results;
|
| 35 |
+
window.searchTimeMs = data.time_ms;
|
| 36 |
+
currentPage = 0;
|
| 37 |
+
displayPage();
|
| 38 |
});
|
| 39 |
}
|
| 40 |
|
| 41 |
function displayPage() {
|
| 42 |
+
const start = currentPage * RESULTS_PER_PAGE,
|
| 43 |
+
end = start + RESULTS_PER_PAGE,
|
| 44 |
+
pageResults = allResults.slice(start, end),
|
| 45 |
+
totalPages = Math.ceil(allResults.length / RESULTS_PER_PAGE);
|
| 46 |
+
let html = `<p>Found ${allResults.length} puzzles${allResults.length > 0 ? ` in ${window.searchTimeMs.toFixed(0)}ms (page ${currentPage + 1} of ${totalPages})` : ""}</p>`;
|
| 47 |
+
if (allResults.length > 0)
|
| 48 |
+
html += `<div class="pagination"><button onclick="prevPage()" ${currentPage === 0 ? "disabled" : ""}>Previous</button><button onclick="nextPage()" ${end >= allResults.length ? "disabled" : ""}>Next</button></div>`;
|
| 49 |
+
html += `<div class="puzzle-grid">`;
|
| 50 |
+
pageResults.forEach((p, i) => {
|
| 51 |
+
const idx = start + i;
|
| 52 |
+
html += `<div class="puzzle"><div class="puzzle-board" id="result-board-${idx}"></div><div class="puzzle-info"><h3><a href="https://lichess.org/training/${p.PuzzleId}" target="_blank">${p.PuzzleId}</a></h3><p>Rating: ${p.Rating} | Pop: ${p.Popularity}%</p><div class="themes">${p.Themes.map((t) => `<span class="theme">${t}</span>`).join(" ")}</div><p>Move ${p.MatchedMove}</p></div></div>`;
|
| 53 |
+
});
|
| 54 |
+
html += `</div>`;
|
| 55 |
+
document.getElementById("results").innerHTML = html;
|
| 56 |
+
pageResults.forEach((p, i) =>
|
| 57 |
+
Chessboard(`result-board-${start + i}`, {
|
| 58 |
+
position: p.FEN,
|
| 59 |
+
pieceTheme: "/static/img/chesspieces/wikipedia/{piece}.png",
|
| 60 |
+
}),
|
| 61 |
+
);
|
| 62 |
}
|
| 63 |
|
| 64 |
function nextPage() {
|
| 65 |
+
if ((currentPage + 1) * RESULTS_PER_PAGE < allResults.length) {
|
| 66 |
+
currentPage++;
|
| 67 |
+
displayPage();
|
| 68 |
+
window.scrollTo(0, 0);
|
| 69 |
+
}
|
| 70 |
}
|
| 71 |
|
| 72 |
function prevPage() {
|
| 73 |
+
if (currentPage > 0) {
|
| 74 |
+
currentPage--;
|
| 75 |
+
displayPage();
|
| 76 |
+
window.scrollTo(0, 0);
|
| 77 |
+
}
|
| 78 |
}
|
| 79 |
|
| 80 |
+
updateInfo();
|