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
+168 -111

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
copy / download buttons.
Hook for the board analyser: whenever the selected move changes, a
CustomEvent named "ob:ply" is dispatched on document, with
"index" is the number of moves played: 0 is the start position, and
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 () {
'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) {
var url = URL.createObjectURL(new Blob([text], { type: 'text/plain;charset=utf-8' }));
@@ -70,11 +78,9 @@
function bindActions() {
var copy = $('ev-copy'), save = $('ev-download');
var copyButton = $('ev-copy'), save = $('ev-download');
if (copy) copy.addEventListener('click', function () {
copyText(raw).then(function () { toast('Copied to clipboard'); }, function () { toast('Copy failed'); });
});
if (copyButton) copyButton.addEventListener('click', function () { copy(raw, 'Copied to clipboard'); });
if (save) save.addEventListener('click', function () {
download(save.getAttribute('data-filename') || 'event.log', raw);
@@ -89,69 +95,71 @@
function initMoves() {
var list = $('ev-movelist');
window.OpenBenchViewer = {
moves : moves,
total : moves.length,
copy : copy,
toast : toast
};
if (!list || !moves.length) return;
var total = moves.length;
var buttons = {};
var current = -1;
Array.prototype.forEach.call(list.querySelectorAll('.ev-move[data-ply]'), function (button) {
buttons[button.getAttribute('data-ply')] = button;
button.addEventListener('click', function () { select(parseInt(button.getAttribute('data-ply'), 10), false); });
var ply = parseInt(button.getAttribute('data-ply'), 10);
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]) {
buttons[current].classList.remove('is-active');
buttons[current].removeAttribute('aria-selected');
if (buttons[current - 1]) {
buttons[current - 1].classList.remove('is-active');
buttons[current - 1].removeAttribute('aria-selected');
}
current = ply;
current = index;
var button = buttons[ply];
var button = buttons[index - 1];
if (button) {
button.classList.add('is-active');
button.setAttribute('aria-selected', 'true');
if (scroll !== false) {
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-label', move.num + (move.color === 'w' ? '.' : '\u2026'));
setText('sel-san', move.san);
setText('sel-eval', move.eval || '\u2013');
setText('sel-depth', move.depth || '\u2013');
setText('sel-time', move.time || '\u2013');
setText('sel-counter', index + ' / ' + total);
setText('sel-label', move ? move.num + (move.color === 'w' ? '.' : '\u2026') : '');
setText('sel-san', move ? move.san : 'Start position');
setText('sel-eval', move && move.eval ? move.eval : '\u2013');
setText('sel-depth', move && move.depth ? move.depth : '\u2013');
setText('sel-time', move && move.time ? move.time : '\u2013');
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');
if (note) {
note.textContent = move.note || '';
note.hidden = !move.note;
note.textContent = move && move.note ? move.note : '';
note.hidden = !(move && move.note);
}
['ev-first', 'ev-prev'].forEach(function (id) { if ($(id)) $(id).disabled = ply === 0; });
['ev-next', 'ev-last'].forEach(function (id) { if ($(id)) $(id).disabled = ply === moves.length - 1; });
document.dispatchEvent(new CustomEvent('ob:ply', { detail: { ply: ply, move: move, moves: moves } }));
document.dispatchEvent(new CustomEvent('ob:ply', {
detail: { index: index, ply: index - 1, 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) {
if (event.altKey || event.ctrlKey || event.metaKey) return;
@@ -163,21 +171,18 @@
case 'ArrowLeft': select(current - 1); break;
case 'ArrowRight': select(current + 1); break;
case 'Home': select(0); break;
case 'End': select(moves.length - 1); break;
case 'End': select(total); break;
default: handled = false;
}
if (handled) event.preventDefault();
});
window.OpenBenchViewer = {
moves : moves,
select : select,
current : function () { return current; }
};
window.OpenBenchViewer.select = select;
window.OpenBenchViewer.current = function () { return current; };
// Start on the last move, which is usually the one that ended the game
select(moves.length - 1);
select(total);
}
function initLog() {