Event viewer: index-based move selection, new board analyser layout

This commit is contained in:
claude committed 2026-10-10 16:01:57 +02:00
1 parent 9fba5f41b7
commit 309754bb08
2 files changed
+143 -86

No files matched your search

+52 -47
View File
@@ -1,12 +1,16 @@
/* Event viewer: move navigation for PGNs, filtering for plain logs, and the /* Event viewer: move navigation for PGNs, filtering for plain logs, and the
copy / download buttons. copy / download buttons.
Hook for the board analyser: whenever the selected move changes, a "index" is the number of moves played: 0 is the start position, and
CustomEvent named "ob:ply" is dispatched on document, with moves.length is the position after the last move.
event.detail = { ply: <index>, move: <move>, moves: <all moves> } Whenever the selection changes, a CustomEvent named "ob:ply" is dispatched
on document with
and window.OpenBenchViewer exposes { moves, select(ply), current() }. */ event.detail = { index: <moves played>, ply: <index - 1>, move: <move or null>, moves: <all moves> }
board.js draws the board from it. window.OpenBenchViewer exposes
{ moves, total, select(index), current(), copy(text, message), toast(message) }. */
(function () { (function () {
'use strict'; 'use strict';
@@ -54,6 +58,10 @@
}); });
} }
function copy(text, message) {
return copyText(text).then(function () { toast(message || 'Copied to clipboard'); }, function () { toast('Copy failed'); });
}
function download(name, text) { function download(name, text) {
var url = URL.createObjectURL(new Blob([text], { type: 'text/plain;charset=utf-8' })); var url = URL.createObjectURL(new Blob([text], { type: 'text/plain;charset=utf-8' }));
@@ -70,11 +78,9 @@
function bindActions() { function bindActions() {
var copy = $('ev-copy'), save = $('ev-download'); var copyButton = $('ev-copy'), save = $('ev-download');
if (copy) copy.addEventListener('click', function () { if (copyButton) copyButton.addEventListener('click', function () { copy(raw, 'Copied to clipboard'); });
copyText(raw).then(function () { toast('Copied to clipboard'); }, function () { toast('Copy failed'); });
});
if (save) save.addEventListener('click', function () { if (save) save.addEventListener('click', function () {
download(save.getAttribute('data-filename') || 'event.log', raw); download(save.getAttribute('data-filename') || 'event.log', raw);
@@ -89,69 +95,71 @@
function initMoves() { function initMoves() {
var list = $('ev-movelist'); var list = $('ev-movelist');
window.OpenBenchViewer = {
moves : moves,
total : moves.length,
copy : copy,
toast : toast
};
if (!list || !moves.length) return; if (!list || !moves.length) return;
var total = moves.length;
var buttons = {}; var buttons = {};
var current = -1; var current = -1;
Array.prototype.forEach.call(list.querySelectorAll('.ev-move[data-ply]'), function (button) { Array.prototype.forEach.call(list.querySelectorAll('.ev-move[data-ply]'), function (button) {
buttons[button.getAttribute('data-ply')] = button; var ply = parseInt(button.getAttribute('data-ply'), 10);
button.addEventListener('click', function () { select(parseInt(button.getAttribute('data-ply'), 10), false); }); buttons[ply] = button;
button.addEventListener('click', function () { select(ply + 1, false); });
}); });
function select(ply, scroll) { function select(index, scroll) {
ply = Math.max(0, Math.min(moves.length - 1, ply)); index = Math.max(0, Math.min(total, index));
var move = moves[ply]; var move = index ? moves[index - 1] : null;
if (buttons[current]) { if (buttons[current - 1]) {
buttons[current].classList.remove('is-active'); buttons[current - 1].classList.remove('is-active');
buttons[current].removeAttribute('aria-selected'); buttons[current - 1].removeAttribute('aria-selected');
} }
current = ply; current = index;
var button = buttons[ply]; var button = buttons[index - 1];
if (button) { if (button) {
button.classList.add('is-active'); button.classList.add('is-active');
button.setAttribute('aria-selected', 'true'); button.setAttribute('aria-selected', 'true');
if (scroll !== false) { if (scroll !== false) {
list.scrollTop = Math.max(0, button.offsetTop - list.clientHeight / 2 + button.offsetHeight / 2); list.scrollTop = Math.max(0, button.offsetTop - list.clientHeight / 2 + button.offsetHeight / 2);
} }
} else if (scroll !== false) {
list.scrollTop = 0;
} }
setText('sel-counter', 'Ply ' + (ply + 1) + ' / ' + moves.length); setText('sel-counter', index + ' / ' + total);
setText('sel-label', move.num + (move.color === 'w' ? '.' : '\u2026')); setText('sel-label', move ? move.num + (move.color === 'w' ? '.' : '\u2026') : '');
setText('sel-san', move.san); setText('sel-san', move ? move.san : 'Start position');
setText('sel-eval', move.eval || '\u2013'); setText('sel-eval', move && move.eval ? move.eval : '\u2013');
setText('sel-depth', move.depth || '\u2013'); setText('sel-depth', move && move.depth ? move.depth : '\u2013');
setText('sel-time', move.time || '\u2013'); setText('sel-time', move && move.time ? move.time : '\u2013');
var evalElement = $('sel-eval'); var evalElement = $('sel-eval');
if (evalElement) evalElement.className = 'mono ' + (move.tone || ''); if (evalElement) evalElement.className = 'mono ' + (move && move.tone ? move.tone : '');
var note = $('sel-note'); var note = $('sel-note');
if (note) { if (note) {
note.textContent = move.note || ''; note.textContent = move && move.note ? move.note : '';
note.hidden = !move.note; note.hidden = !(move && move.note);
} }
['ev-first', 'ev-prev'].forEach(function (id) { if ($(id)) $(id).disabled = ply === 0; }); document.dispatchEvent(new CustomEvent('ob:ply', {
['ev-next', 'ev-last'].forEach(function (id) { if ($(id)) $(id).disabled = ply === moves.length - 1; }); detail: { index: index, ply: index - 1, move: move, moves: moves }
}));
document.dispatchEvent(new CustomEvent('ob:ply', { detail: { ply: ply, move: move, moves: moves } }));
} }
function bind(id, handler) {
if ($(id)) $(id).addEventListener('click', handler);
}
bind('ev-first', function () { select(0); });
bind('ev-prev', function () { select(current - 1); });
bind('ev-next', function () { select(current + 1); });
bind('ev-last', function () { select(moves.length - 1); });
document.addEventListener('keydown', function (event) { document.addEventListener('keydown', function (event) {
if (event.altKey || event.ctrlKey || event.metaKey) return; if (event.altKey || event.ctrlKey || event.metaKey) return;
@@ -163,21 +171,18 @@
case 'ArrowLeft': select(current - 1); break; case 'ArrowLeft': select(current - 1); break;
case 'ArrowRight': select(current + 1); break; case 'ArrowRight': select(current + 1); break;
case 'Home': select(0); break; case 'Home': select(0); break;
case 'End': select(moves.length - 1); break; case 'End': select(total); break;
default: handled = false; default: handled = false;
} }
if (handled) event.preventDefault(); if (handled) event.preventDefault();
}); });
window.OpenBenchViewer = { window.OpenBenchViewer.select = select;
moves : moves, window.OpenBenchViewer.current = function () { return current; };
select : select,
current : function () { return current; }
};
// Start on the last move, which is usually the one that ended the game // Start on the last move, which is usually the one that ended the game
select(moves.length - 1); select(total);
} }
function initLog() { function initLog() {
+91 -39
View File
@@ -1,9 +1,10 @@
{% extends "OpenBench/base.html" %} {% extends "OpenBench/base.html" %}
{% load static %} {% load static %}
{% load uitags %}
{% block scripts %} {% block scripts %}
<script src="{% static 'chess.js' %}?{{ static_version }}" defer></script>
<script src="{% static 'board.js' %}?{{ static_version }}" defer></script>
<script src="{% static 'event_viewer.js' %}?{{ static_version }}" defer></script> <script src="{% static 'event_viewer.js' %}?{{ static_version }}" defer></script>
{% endblock %} {% endblock %}
@@ -81,6 +82,7 @@
{% if game.time_control %}<div><dt>Time control</dt><dd class="mono">{{ game.time_control }}</dd></div>{% endif %} {% if game.time_control %}<div><dt>Time control</dt><dd class="mono">{{ game.time_control }}</dd></div>{% endif %}
{% if game.date %}<div><dt>Date</dt><dd class="mono">{{ game.date }}</dd></div>{% endif %} {% if game.date %}<div><dt>Date</dt><dd class="mono">{{ game.date }}</dd></div>{% endif %}
{% if game.duration %}<div><dt>Duration</dt><dd class="mono">{{ game.duration }}</dd></div>{% endif %} {% if game.duration %}<div><dt>Duration</dt><dd class="mono">{{ game.duration }}</dd></div>{% endif %}
{% if game.chess960 %}<div><dt>Variant</dt><dd class="mono">Chess960</dd></div>{% endif %}
{% if game.fen %}<div><dt>Start FEN</dt><dd class="mono">{{ game.fen }}</dd></div>{% endif %} {% if game.fen %}<div><dt>Start FEN</dt><dd class="mono">{{ game.fen }}</dd></div>{% endif %}
</dl> </dl>
@@ -99,14 +101,87 @@
{% if game.rows %} {% if game.rows %}
<div class="ev-grid"> <div class="bd-layout">
<section class="card bd-card">
<header class="card-head">
<h2 class="card-title">Board</h2>
{% if game.chess960 %}<span class="badge badge-info">Chess960</span>{% endif %}
</header>
<div class="bd-body">
<div class="bd-grid">
<div class="bd-player is-black" id="bd-top">
<i class="ev-piece"></i>
<strong class="bd-name">{{ game.black }}</strong>
<span class="bd-caps"></span>
<span class="bd-mat"></span>
</div>
<div class="bd-eval" id="bd-eval" aria-hidden="true">
<div class="bd-eval-fill"></div>
<span class="bd-eval-label on-white">&ndash;</span>
</div>
<div class="bd-board" id="bd-board"><noscript><p class="bd-nojs">The board needs JavaScript.</p></noscript></div>
<div class="bd-player is-white" id="bd-bottom">
<i class="ev-piece"></i>
<strong class="bd-name">{{ game.white }}</strong>
<span class="bd-caps"></span>
<span class="bd-mat"></span>
</div>
</div>
<p class="bd-status" id="bd-status">&nbsp;</p>
<div class="bd-controls">
<button type="button" class="bd-btn" id="bd-first" title="First position (Home)" aria-label="First position"><i class="fa-solid fa-backward-step"></i></button>
<button type="button" class="bd-btn" id="bd-prev" title="Previous move (Left arrow)" aria-label="Previous move"><i class="fa-solid fa-chevron-left"></i></button>
<button type="button" class="bd-btn bd-btn-main" id="bd-play" title="Play or pause (Space)" aria-label="Play"><i class="fa-solid fa-play"></i></button>
<button type="button" class="bd-btn" id="bd-next" title="Next move (Right arrow)" aria-label="Next move"><i class="fa-solid fa-chevron-right"></i></button>
<button type="button" class="bd-btn" id="bd-last" title="Last position (End)" aria-label="Last position"><i class="fa-solid fa-forward-step"></i></button>
<button type="button" class="bd-btn bd-btn-flip" id="bd-flip" title="Flip the board (F)" aria-label="Flip the board"><i class="fa-solid fa-retweet"></i></button>
</div>
<div class="bd-fen">
<span class="bd-fen-label">FEN</span>
<code id="bd-fen"></code>
<button type="button" class="bd-btn bd-btn-icon" id="bd-copy" title="Copy FEN" aria-label="Copy FEN"><i class="fa-regular fa-copy"></i></button>
</div>
</div>
</section>
<div class="ev-right">
<div class="ev-right-inner">
<section class="card ev-moves-card">
<section class="card">
<header class="card-head"> <header class="card-head">
<h2 class="card-title">Moves</h2> <h2 class="card-title">Moves</h2>
<span class="count">{{ game.ply_count }}</span> <span class="count">{{ game.ply_count }}</span>
</header> </header>
<div class="ev-sel">
<div class="ev-sel-main">
<span class="ev-sel-label mono" id="sel-label"></span>
<span class="ev-sel-san mono" id="sel-san">&ndash;</span>
<span class="ev-sel-counter mono" id="sel-counter"></span>
</div>
<div class="ev-sel-stats">
<div><span>Eval</span><strong class="mono" id="sel-eval">&ndash;</strong></div>
<div><span>Depth</span><strong class="mono" id="sel-depth">&ndash;</strong></div>
<div><span>Time</span><strong class="mono" id="sel-time">&ndash;</strong></div>
</div>
<p class="ev-sel-note" id="sel-note" hidden></p>
</div>
<div class="ev-movelist" id="ev-movelist" role="listbox" aria-label="Moves"> <div class="ev-movelist" id="ev-movelist" role="listbox" aria-label="Moves">
{% for row in game.rows %} {% for row in game.rows %}
<div class="ev-row"> <div class="ev-row">
@@ -130,51 +205,27 @@
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<footer class="ev-moves-foot">
<span><kbd>&larr;</kbd> <kbd>&rarr;</kbd> step</span>
<span><kbd>Space</kbd> play</span>
<span><kbd>F</kbd> flip</span>
</footer>
</section> </section>
<aside class="ev-side"> <section class="card bd-graph-card" id="bd-graph-card">
<section class="card">
<header class="card-head"> <header class="card-head">
<h2 class="card-title">Selected move</h2> <h2 class="card-title">Evaluation</h2>
<span class="mono muted" id="sel-counter">&ndash;</span> <span class="mono muted" id="bd-graph-label"></span>
</header> </header>
<div class="ev-sel"> <div class="bd-graph-wrap">
<div class="ev-sel-main"> <div class="bd-graph" id="bd-graph"></div>
<span class="ev-sel-label mono" id="sel-label">&ndash;</span>
<span class="ev-sel-san mono" id="sel-san">&ndash;</span>
</div>
<div class="ev-sel-stats">
<div><span>Eval</span><strong class="mono" id="sel-eval">&ndash;</strong></div>
<div><span>Depth</span><strong class="mono" id="sel-depth">&ndash;</strong></div>
<div><span>Time</span><strong class="mono" id="sel-time">&ndash;</strong></div>
</div>
<p class="ev-sel-note" id="sel-note" hidden></p>
<div class="ev-nav">
<button type="button" class="anchorbutton btn-start" id="ev-first" title="First move (Home)" aria-label="First move"><i class="fa-solid fa-backward-step"></i></button>
<button type="button" class="anchorbutton btn-start" id="ev-prev" title="Previous move (Left arrow)" aria-label="Previous move"><i class="fa-solid fa-chevron-left"></i></button>
<button type="button" class="anchorbutton btn-start" id="ev-next" title="Next move (Right arrow)" aria-label="Next move"><i class="fa-solid fa-chevron-right"></i></button>
<button type="button" class="anchorbutton btn-start" id="ev-last" title="Last move (End)" aria-label="Last move"><i class="fa-solid fa-forward-step"></i></button>
</div>
<p class="ev-hint"><kbd>&larr;</kbd> <kbd>&rarr;</kbd> step through the game, <kbd>Home</kbd> <kbd>End</kbd> jump to the ends</p>
</div> </div>
</section> </section>
<section class="card ev-board-card" id="ev-board-slot">
<header class="card-head">
<h2 class="card-title">Board</h2>
<span class="badge badge-muted">Coming soon</span>
</header>
<div class="ev-board" aria-hidden="true">
<div class="ev-board-msg">
<i class="fa-solid fa-chess-board"></i>
<strong>Board analyser</strong>
<span>Follow the game position by position.</span>
</div> </div>
</div> </div>
</section>
</aside>
</div> </div>
@@ -190,6 +241,7 @@
{% endif %} {% endif %}
{{ moves_json|json_script:"ev-moves" }} {{ moves_json|json_script:"ev-moves" }}
{{ game_json|json_script:"ev-game" }}
{% elif kind == 'log' %} {% elif kind == 'log' %}