christopher commited on
Commit
f794d93
·
1 Parent(s): edd78c3

Update static/app.js

Browse files
Files changed (1) hide show
  1. static/app.js +59 -42
static/app.js CHANGED
@@ -1,63 +1,80 @@
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
  }
18
 
19
  function search() {
20
- let pos = board.position();
21
- if (Object.keys(pos).length < 3) { document.getElementById('results').innerHTML = '<p>Add at least 3 pieces to search.</p>'; return; }
22
- document.getElementById('results').innerHTML = '<p>Searching...</p>';
23
- fetch('/search', {
24
- method: 'POST',
25
- headers: {'Content-Type': 'application/json'},
26
- body: JSON.stringify({fen: board.fen()})
27
- })
28
- .then(r => r.json())
29
- .then(data => {
30
- allResults = data.results;
31
- window.searchTimeMs = data.time_ms;
32
- currentPage = 0;
33
- displayPage();
 
 
 
 
34
  });
35
  }
36
 
37
  function displayPage() {
38
- const start = currentPage * RESULTS_PER_PAGE, end = start + RESULTS_PER_PAGE, pageResults = allResults.slice(start, end), totalPages = Math.ceil(allResults.length / RESULTS_PER_PAGE);
39
- let html = `<p>Found ${allResults.length} puzzles${allResults.length > 0 ? ` in ${window.searchTimeMs.toFixed(0)}ms (page ${currentPage + 1} of ${totalPages})` : ''}</p>`;
40
- if (allResults.length > 0) html += `<div class="pagination"><button onclick="prevPage()" ${currentPage === 0 ? 'disabled' : ''}>Previous</button><button onclick="nextPage()" ${end >= allResults.length ? 'disabled' : ''}>Next</button></div>`; html += `<div class="puzzle-grid">`;
41
- 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"><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>`; });
42
- html += `</div>`;
43
- document.getElementById('results').innerHTML = html;
44
- pageResults.forEach((p, i) => Chessboard(`result-board-${start + i}`, { position: p.FEN, pieceTheme: '/static/img/chesspieces/wikipedia/{piece}.png' }));
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
 
47
  function nextPage() {
48
- if ((currentPage + 1) * RESULTS_PER_PAGE < allResults.length) {
49
- currentPage++;
50
- displayPage();
51
- window.scrollTo(0, 0);
52
- }
53
  }
54
 
55
  function prevPage() {
56
- if (currentPage > 0) {
57
- currentPage--;
58
- displayPage();
59
- window.scrollTo(0, 0);
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();