| const RESULTS_PER_PAGE = 20; |
| let allResults = []; |
| let currentPage = 0; |
|
|
| var board = Chessboard("board", { |
| draggable: true, |
| dropOffBoard: "trash", |
| sparePieces: true, |
| pieceTheme: "/static/img/chesspieces/wikipedia/{piece}.png", |
| onSnapEnd: updateInfo, |
| }); |
|
|
| function textSearch() { |
| const query = document.getElementById("text-query").value.trim(); |
| if (!query) return; |
| document.getElementById("results").innerHTML = "<p>Searching...</p>"; |
| fetch("/text-search", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ query: query }), |
| }) |
| .then((r) => r.json()) |
| .then((data) => { |
| allResults = data.results; |
| window.searchTimeMs = data.time_ms; |
| currentPage = 0; |
| displayPage(); |
| }); |
| } |
|
|
| function updateInfo() { |
| document.getElementById('fen').value = board.fen(); |
| } |
|
|
| function search() { |
| let pos = board.position(); |
| if (Object.keys(pos).length < 3) { |
| document.getElementById("results").innerHTML = |
| "<p>Add at least 3 pieces to search.</p>"; |
| return; |
| } |
| document.getElementById("results").innerHTML = "<p>Searching...</p>"; |
| fetch("/search", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ fen: board.fen() }), |
| }) |
| .then((r) => r.json()) |
| .then((data) => { |
| allResults = data.results; |
| window.searchTimeMs = data.time_ms; |
| currentPage = 0; |
| displayPage(); |
| }); |
| } |
|
|
| function displayPage() { |
| const start = currentPage * RESULTS_PER_PAGE, |
| end = start + RESULTS_PER_PAGE, |
| pageResults = allResults.slice(start, end), |
| totalPages = Math.ceil(allResults.length / RESULTS_PER_PAGE); |
| const paginationHtml = allResults.length > 0 ? `<div class="pagination"><button onclick="prevPage()" ${currentPage === 0 ? "disabled" : ""}><</button><button onclick="nextPage()" ${end >= allResults.length ? "disabled" : ""}>></button></div>` : ""; |
| let html = `<p>Found ${allResults.length} games${allResults.length > 0 ? ` in ${window.searchTimeMs.toFixed(0)}ms (page ${currentPage + 1} of ${totalPages})` : ""}</p>`; |
| html += paginationHtml; |
| html += `<div class="puzzle-grid">`; |
| pageResults.forEach((p, i) => { |
| const idx = start + i; |
| html += `<div class="puzzle"><div class="puzzle-board" id="result-board-${idx}"></div><div class="puzzle-info"><p><strong>${p.White} vs ${p.Black}</strong></p><p>${p.Event}</p><p>${p.Date} · ${p.Result}</p><p>Move ${p.MatchedMove}</p></div></div>`; |
| }); |
| html += `</div>`; |
| html += paginationHtml; |
| document.getElementById("results").innerHTML = html; |
| pageResults.forEach((p, i) => |
| Chessboard(`result-board-${start + i}`, { |
| position: p.FEN, |
| pieceTheme: "/static/img/chesspieces/wikipedia/{piece}.png", |
| }) |
| ); |
| } |
|
|
| function nextPage() { |
| if ((currentPage + 1) * RESULTS_PER_PAGE < allResults.length) { |
| currentPage++; |
| displayPage(); |
| } |
| } |
|
|
| function prevPage() { |
| if (currentPage > 0) { |
| currentPage--; |
| displayPage(); |
| } |
| } |
|
|
| updateInfo(); |