Spaces:
Sleeping
Sleeping
Commit
·
2ba5ab4
1
Parent(s):
962af3e
Create app.js
Browse files- static/app.js +94 -0
static/app.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const RESULTS_PER_PAGE = 10;
|
| 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 |
+
document.getElementById('position-info').textContent = `Pieces on board: ${pieces}`;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
function search() {
|
| 21 |
+
document.getElementById('results').innerHTML = '<p>Searching...</p>';
|
| 22 |
+
fetch('/search', {
|
| 23 |
+
method: 'POST',
|
| 24 |
+
headers: {'Content-Type': 'application/json'},
|
| 25 |
+
body: JSON.stringify({fen: board.fen()})
|
| 26 |
+
})
|
| 27 |
+
.then(r => r.json())
|
| 28 |
+
.then(data => {
|
| 29 |
+
allResults = data.results;
|
| 30 |
+
currentPage = 0;
|
| 31 |
+
displayPage();
|
| 32 |
+
});
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
function displayPage() {
|
| 36 |
+
const start = currentPage * RESULTS_PER_PAGE;
|
| 37 |
+
const end = start + RESULTS_PER_PAGE;
|
| 38 |
+
const pageResults = allResults.slice(start, end);
|
| 39 |
+
const totalPages = Math.ceil(allResults.length / RESULTS_PER_PAGE);
|
| 40 |
+
|
| 41 |
+
let html = `<h2>Found ${allResults.length} puzzles (page ${currentPage + 1} of ${totalPages})</h2>`;
|
| 42 |
+
html += `<div class="pagination">
|
| 43 |
+
<button onclick="prevPage()" ${currentPage === 0 ? 'disabled' : ''}>Previous</button>
|
| 44 |
+
<button onclick="nextPage()" ${end >= allResults.length ? 'disabled' : ''}>Next</button>
|
| 45 |
+
</div>`;
|
| 46 |
+
|
| 47 |
+
pageResults.forEach((p, i) => {
|
| 48 |
+
const globalIdx = start + i;
|
| 49 |
+
html += `
|
| 50 |
+
<div class="puzzle">
|
| 51 |
+
<div class="puzzle-board" id="result-board-${globalIdx}"></div>
|
| 52 |
+
<div class="puzzle-info">
|
| 53 |
+
<h3><a href="${p.PuzzleUrl}" target="_blank">${p.PuzzleId}</a></h3>
|
| 54 |
+
<p>Rating: ${p.Rating} | Popularity: ${p.Popularity}%</p>
|
| 55 |
+
<div class="themes">${p.Themes.map(t => `<span class="theme">${t}</span>`).join('')}</div>
|
| 56 |
+
<p>Moves: ${p.Moves}</p>
|
| 57 |
+
<p><a href="${p.GameUrl}" target="_blank">View game</a></p>
|
| 58 |
+
</div>
|
| 59 |
+
</div>`;
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
html += `<div class="pagination">
|
| 63 |
+
<button onclick="prevPage()" ${currentPage === 0 ? 'disabled' : ''}>Previous</button>
|
| 64 |
+
<button onclick="nextPage()" ${end >= allResults.length ? 'disabled' : ''}>Next</button>
|
| 65 |
+
</div>`;
|
| 66 |
+
|
| 67 |
+
document.getElementById('results').innerHTML = html;
|
| 68 |
+
|
| 69 |
+
pageResults.forEach((p, i) => {
|
| 70 |
+
const globalIdx = start + i;
|
| 71 |
+
Chessboard(`result-board-${globalIdx}`, {
|
| 72 |
+
position: p.FEN,
|
| 73 |
+
pieceTheme: '/static/img/chesspieces/wikipedia/{piece}.png'
|
| 74 |
+
});
|
| 75 |
+
});
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
function nextPage() {
|
| 79 |
+
if ((currentPage + 1) * RESULTS_PER_PAGE < allResults.length) {
|
| 80 |
+
currentPage++;
|
| 81 |
+
displayPage();
|
| 82 |
+
window.scrollTo(0, 0);
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
function prevPage() {
|
| 87 |
+
if (currentPage > 0) {
|
| 88 |
+
currentPage--;
|
| 89 |
+
displayPage();
|
| 90 |
+
window.scrollTo(0, 0);
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
updateInfo();
|