diff --git a/OpenBench/static/event_viewer.js b/OpenBench/static/event_viewer.js index f2080d1..0ff02e3 100644 --- a/OpenBench/static/event_viewer.js +++ b/OpenBench/static/event_viewer.js @@ -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: , move: , 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: , ply: , move: , 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() { diff --git a/Templates/OpenBench/event.html b/Templates/OpenBench/event.html index ed206f4..7af99aa 100644 --- a/Templates/OpenBench/event.html +++ b/Templates/OpenBench/event.html @@ -1,9 +1,10 @@ {% extends "OpenBench/base.html" %} {% load static %} -{% load uitags %} {% block scripts %} + + {% endblock %} @@ -81,6 +82,7 @@ {% if game.time_control %}
Time control
{{ game.time_control }}
{% endif %} {% if game.date %}
Date
{{ game.date }}
{% endif %} {% if game.duration %}
Duration
{{ game.duration }}
{% endif %} + {% if game.chess960 %}
Variant
Chess960
{% endif %} {% if game.fen %}
Start FEN
{{ game.fen }}
{% endif %} @@ -99,82 +101,131 @@ {% if game.rows %} -
+
+ +
-
-

Moves

- {{ game.ply_count }} +

Board

+ {% if game.chess960 %}Chess960{% endif %}
-
- {% for row in game.rows %} -
- {{ row.num }}. +
- {% if row.w %} - - {% else %} - … - {% endif %} +
- {% if row.b %} - - {% else %} - - {% endif %} +
+ + {{ game.black }} + +
- {% endfor %} + + + +
+ +
+ + {{ game.white }} + + +
+ +
+ +

 

+ +
+ + + + + + +
+ +
+ FEN + + +
+
+
- +
+
+ + – + +
+
+
Eval–
+
Depth–
+
Time–
+
+ +
+ +
+ {% for row in game.rows %} +
+ {{ row.num }}. + + {% if row.w %} + + {% else %} + … + {% endif %} + + {% if row.b %} + + {% else %} + + {% endif %} +
+ {% endfor %} +
+ +
+ ← → step + Space play + F flip +
+ +
+ +
+
+

Evaluation

+ +
+
+
+
+
+ +
+
@@ -190,6 +241,7 @@ {% endif %} {{ moves_json|json_script:"ev-moves" }} + {{ game_json|json_script:"ev-game" }} {% elif kind == 'log' %}