Files
OpenBench/OpenBench/static/event_viewer.js
T

243 lines
8.0 KiB
JavaScript

/* Event viewer: move navigation for PGNs, filtering for plain logs, and the
copy / download buttons.
"index" is the number of moves played: 0 is the start position, and
moves.length is the position after the last move.
Whenever the selection changes, a CustomEvent named "ob:ply" is dispatched
on document with
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';
function $(id) { return document.getElementById(id); }
function readJSON(id, fallback) {
var element = $(id);
if (!element) return fallback;
try { return JSON.parse(element.textContent); } catch (error) { return fallback; }
}
var raw = readJSON('ev-raw', '');
var moves = readJSON('ev-moves', []);
var toastTimer = null;
function toast(message) {
var element = $('toast');
if (!element) return;
element.textContent = message;
element.classList.add('show');
window.clearTimeout(toastTimer);
toastTimer = window.setTimeout(function () { element.classList.remove('show'); }, 1800);
}
function copyText(text) {
if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text);
return new Promise(function (resolve, reject) {
var area = document.createElement('textarea');
area.value = text;
area.style.position = 'fixed';
area.style.opacity = '0';
document.body.appendChild(area);
area.select();
try { document.execCommand('copy') ? resolve() : reject(); }
catch (error) { reject(error); }
finally { document.body.removeChild(area); }
});
}
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' }));
var link = document.createElement('a');
link.href = url;
link.download = name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.setTimeout(function () { URL.revokeObjectURL(url); }, 1000);
}
function bindActions() {
var copyButton = $('ev-copy'), save = $('ev-download');
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);
});
}
function setText(id, text) {
var element = $(id);
if (element) element.textContent = text;
}
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) {
var ply = parseInt(button.getAttribute('data-ply'), 10);
buttons[ply] = button;
button.addEventListener('click', function () { select(ply + 1, false); });
});
function select(index, scroll) {
index = Math.max(0, Math.min(total, index));
var move = index ? moves[index - 1] : null;
if (buttons[current - 1]) {
buttons[current - 1].classList.remove('is-active');
buttons[current - 1].removeAttribute('aria-selected');
}
current = index;
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', 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 && move.tone ? move.tone : '');
var note = $('sel-note');
if (note) {
note.textContent = move && move.note ? move.note : '';
note.hidden = !(move && move.note);
}
document.dispatchEvent(new CustomEvent('ob:ply', {
detail: { index: index, ply: index - 1, move: move, moves: moves }
}));
}
document.addEventListener('keydown', function (event) {
if (event.altKey || event.ctrlKey || event.metaKey) return;
if (/^(INPUT|TEXTAREA|SELECT)$/.test((event.target && event.target.tagName) || '')) return;
var handled = true;
switch (event.key) {
case 'ArrowLeft': select(current - 1); break;
case 'ArrowRight': select(current + 1); break;
case 'Home': select(0); break;
case 'End': select(total); break;
default: handled = false;
}
if (handled) event.preventDefault();
});
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(total);
}
function initLog() {
var body = $('log-body');
if (!body) return;
var filter = $('log-filter'), wrap = $('log-wrap'), issues = $('log-issues');
var empty = $('log-empty');
var lines = Array.prototype.map.call(body.querySelectorAll('.ev-line'), function (line) {
return {
element : line,
level : line.getAttribute('data-level'),
text : line.querySelector('.ev-lt').textContent.toLowerCase()
};
});
function apply() {
var query = filter ? filter.value.trim().toLowerCase() : '';
var only = issues ? issues.checked : false;
var shown = 0;
lines.forEach(function (line) {
var visible = (!query || line.text.indexOf(query) !== -1) && (!only || line.level !== 'info');
line.element.hidden = !visible;
if (visible) shown++;
});
if (empty) empty.hidden = shown !== 0;
}
if (filter) filter.addEventListener('input', apply);
if (issues) issues.addEventListener('change', apply);
if (wrap) wrap.addEventListener('change', function () {
body.classList.toggle('ev-wrap', wrap.checked);
});
// Jump to the first problem, since that is what people came here for
var first = body.querySelector('.ev-line-bad');
if (first) body.scrollTop = Math.max(0, first.offsetTop - body.clientHeight / 3);
}
function ready(fn) {
if (document.readyState !== 'loading') fn();
else document.addEventListener('DOMContentLoaded', fn);
}
ready(function () {
bindActions();
initMoves();
initLog();
});
})();