Event viewer: index-based move selection, new board analyser layout
This commit is contained in:
1 parent
9fba5f41b7
commit
309754bb08
2 files changed
+168
-111
No files matched your search
@@ -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() {
|
||||
|
||||
+116
-64
@@ -1,9 +1,10 @@
|
||||
{% extends "OpenBench/base.html" %}
|
||||
|
||||
{% load static %}
|
||||
{% load uitags %}
|
||||
|
||||
{% 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>
|
||||
{% 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.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.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 %}
|
||||
</dl>
|
||||
|
||||
@@ -99,82 +101,131 @@
|
||||
|
||||
{% if game.rows %}
|
||||
|
||||
<div class="ev-grid">
|
||||
<div class="bd-layout">
|
||||
|
||||
<section class="card bd-card">
|
||||
|
||||
<section class="card">
|
||||
<header class="card-head">
|
||||
<h2 class="card-title">Moves</h2>
|
||||
<span class="count">{{ game.ply_count }}</span>
|
||||
<h2 class="card-title">Board</h2>
|
||||
{% if game.chess960 %}<span class="badge badge-info">Chess960</span>{% endif %}
|
||||
</header>
|
||||
|
||||
<div class="ev-movelist" id="ev-movelist" role="listbox" aria-label="Moves">
|
||||
{% for row in game.rows %}
|
||||
<div class="ev-row">
|
||||
<span class="ev-num">{{ row.num }}.</span>
|
||||
<div class="bd-body">
|
||||
|
||||
{% if row.w %}
|
||||
<button type="button" class="ev-move{% if row.w.final %} is-final{% endif %}" role="option" data-ply="{{ row.w.ply }}">
|
||||
<span class="ev-san">{{ row.w.san }}</span>{% if row.w.eval %}<em class="ev-eval {{ row.w.tone }}">{{ row.w.eval }}</em>{% endif %}
|
||||
</button>
|
||||
{% else %}
|
||||
<span class="ev-move ev-empty">…</span>
|
||||
{% endif %}
|
||||
<div class="bd-grid">
|
||||
|
||||
{% if row.b %}
|
||||
<button type="button" class="ev-move{% if row.b.final %} is-final{% endif %}" role="option" data-ply="{{ row.b.ply }}">
|
||||
<span class="ev-san">{{ row.b.san }}</span>{% if row.b.eval %}<em class="ev-eval {{ row.b.tone }}">{{ row.b.eval }}</em>{% endif %}
|
||||
</button>
|
||||
{% else %}
|
||||
<span class="ev-move ev-empty"></span>
|
||||
{% endif %}
|
||||
<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>
|
||||
{% endfor %}
|
||||
|
||||
<div class="bd-eval" id="bd-eval" aria-hidden="true">
|
||||
<div class="bd-eval-fill"></div>
|
||||
<span class="bd-eval-label on-white">–</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"> </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>
|
||||
|
||||
<aside class="ev-side">
|
||||
<div class="ev-right">
|
||||
<div class="ev-right-inner">
|
||||
|
||||
<section class="card">
|
||||
<header class="card-head">
|
||||
<h2 class="card-title">Selected move</h2>
|
||||
<span class="mono muted" id="sel-counter">–</span>
|
||||
</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">–</span>
|
||||
</div>
|
||||
<div class="ev-sel-stats">
|
||||
<div><span>Eval</span><strong class="mono" id="sel-eval">–</strong></div>
|
||||
<div><span>Depth</span><strong class="mono" id="sel-depth">–</strong></div>
|
||||
<div><span>Time</span><strong class="mono" id="sel-time">–</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>←</kbd> <kbd>→</kbd> step through the game, <kbd>Home</kbd> <kbd>End</kbd> jump to the ends</p>
|
||||
</div>
|
||||
</section>
|
||||
<section class="card ev-moves-card">
|
||||
|
||||
<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>
|
||||
</section>
|
||||
<header class="card-head">
|
||||
<h2 class="card-title">Moves</h2>
|
||||
<span class="count">{{ game.ply_count }}</span>
|
||||
</header>
|
||||
|
||||
</aside>
|
||||
<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">–</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">–</strong></div>
|
||||
<div><span>Depth</span><strong class="mono" id="sel-depth">–</strong></div>
|
||||
<div><span>Time</span><strong class="mono" id="sel-time">–</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">
|
||||
{% for row in game.rows %}
|
||||
<div class="ev-row">
|
||||
<span class="ev-num">{{ row.num }}.</span>
|
||||
|
||||
{% if row.w %}
|
||||
<button type="button" class="ev-move{% if row.w.final %} is-final{% endif %}" role="option" data-ply="{{ row.w.ply }}">
|
||||
<span class="ev-san">{{ row.w.san }}</span>{% if row.w.eval %}<em class="ev-eval {{ row.w.tone }}">{{ row.w.eval }}</em>{% endif %}
|
||||
</button>
|
||||
{% else %}
|
||||
<span class="ev-move ev-empty">…</span>
|
||||
{% endif %}
|
||||
|
||||
{% if row.b %}
|
||||
<button type="button" class="ev-move{% if row.b.final %} is-final{% endif %}" role="option" data-ply="{{ row.b.ply }}">
|
||||
<span class="ev-san">{{ row.b.san }}</span>{% if row.b.eval %}<em class="ev-eval {{ row.b.tone }}">{{ row.b.eval }}</em>{% endif %}
|
||||
</button>
|
||||
{% else %}
|
||||
<span class="ev-move ev-empty"></span>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<footer class="ev-moves-foot">
|
||||
<span><kbd>←</kbd> <kbd>→</kbd> step</span>
|
||||
<span><kbd>Space</kbd> play</span>
|
||||
<span><kbd>F</kbd> flip</span>
|
||||
</footer>
|
||||
|
||||
</section>
|
||||
|
||||
<section class="card bd-graph-card" id="bd-graph-card">
|
||||
<header class="card-head">
|
||||
<h2 class="card-title">Evaluation</h2>
|
||||
<span class="mono muted" id="bd-graph-label"></span>
|
||||
</header>
|
||||
<div class="bd-graph-wrap">
|
||||
<div class="bd-graph" id="bd-graph"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -190,6 +241,7 @@
|
||||
{% endif %}
|
||||
|
||||
{{ moves_json|json_script:"ev-moves" }}
|
||||
{{ game_json|json_script:"ev-game" }}
|
||||
|
||||
{% elif kind == 'log' %}
|
||||
|
||||
|
||||
Reference in new issue
Block a user