Tune page: live updates that keep the SPSA panel interactive
live.js already polled /tune/<id>, but patching the page in place would have wiped the chart, reordered the parameter rows under their listeners, dropped pinned lines and closed any fold the reader had opened. - live.js: leave the chart, the parameter list and its empty notice alone, keep the 'open' attribute of folds, and fire a 'live:patched' event with the fresh page after every patch. - spsa_chart.js: on that event, hand the new history to the existing chart (pinned lines and the inspected point stay), or build it if the first results have just arrived. - spsa_panel.js: on that event, refresh the numbers, bars and limit markers of each row in place, and re-sort when sorted by movement without jumping the scroll position.
This commit is contained in:
1 parent
0167e19057
commit
22bb0b934d
3 files changed
+154
-35
No files matched your search
@@ -18,11 +18,13 @@
|
||||
var FULL = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
|
||||
var DAY = { month: 'short', day: '2-digit' };
|
||||
|
||||
// Never replaced: forms keep what is typed, banners can be dismissed, the graph loads itself
|
||||
var PROTECT = 'form, .banner, .llr-history-panel, [data-live-skip], script, style';
|
||||
// Never replaced: forms keep what is typed, banners can be dismissed, the graphs load themselves.
|
||||
// The SPSA chart and parameter list ( Tune pages ) are interactive, so their scripts refresh them
|
||||
// from the 'live:patched' event instead.
|
||||
var PROTECT = 'form, .banner, .llr-history-panel, .spsa-chart, #spsa-rows, #spsa-empty, [data-live-skip], script, style';
|
||||
|
||||
// Attributes that other scripts add after the page loads
|
||||
var KEEP = { 'data-href': true, 'data-epoch': true };
|
||||
// Attributes that other scripts, or the reader, add after the page loads ( 'open' is a fold the reader opened )
|
||||
var KEEP = { 'data-href': true, 'data-epoch': true, 'open': true };
|
||||
|
||||
/* ---------- Timestamps ----------
|
||||
The pages print raw epochs and the browser formats them. Remember the
|
||||
@@ -176,6 +178,10 @@
|
||||
stash(fresh);
|
||||
patchChildren(content, fresh);
|
||||
restoreRows();
|
||||
|
||||
// Scripts that own a part of the page that is never patched ( see PROTECT ) refresh it from here
|
||||
document.dispatchEvent(new CustomEvent('live:patched', { detail: { fresh: fresh } }));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,20 +73,7 @@
|
||||
this.active = -1;
|
||||
this.width = 0;
|
||||
|
||||
// Movement from the start value, as a share of the allowed range
|
||||
this.series = data.names.map(function (name, i) {
|
||||
var span = (data.max[i] - data.min[i]) || 1;
|
||||
return data.points.map(function (point) {
|
||||
return (point[1 + i] - data.start[i]) / span * 100;
|
||||
});
|
||||
});
|
||||
|
||||
// Without a choice, follow whichever parameters moved the most
|
||||
var last = this.series.map(function (line, i) {
|
||||
return { index: i, size: Math.abs(line[line.length - 1]) };
|
||||
});
|
||||
last.sort(function (a, b) { return b.size - a.size || a.index - b.index; });
|
||||
this.defaults = last.slice(0, 3).map(function (entry) { return entry.index; });
|
||||
this.load(data);
|
||||
|
||||
root.textContent = '';
|
||||
|
||||
@@ -102,6 +89,33 @@
|
||||
this.bind();
|
||||
}
|
||||
|
||||
// What is drawn follows from the server's data. Live updates call this again with newer data.
|
||||
Chart.prototype.load = function (data) {
|
||||
|
||||
this.data = data;
|
||||
|
||||
// Movement from the start value, as a share of the allowed range
|
||||
this.series = data.names.map(function (name, i) {
|
||||
var span = (data.max[i] - data.min[i]) || 1;
|
||||
return data.points.map(function (point) {
|
||||
return (point[1 + i] - data.start[i]) / span * 100;
|
||||
});
|
||||
});
|
||||
|
||||
// Without a choice, follow whichever parameters moved the most
|
||||
var last = this.series.map(function (line, i) {
|
||||
return { index: i, size: Math.abs(line[line.length - 1]) };
|
||||
});
|
||||
last.sort(function (a, b) { return b.size - a.size || a.index - b.index; });
|
||||
this.defaults = last.slice(0, 3).map(function (entry) { return entry.index; });
|
||||
};
|
||||
|
||||
// Newer data, same chart: pinned lines and the point being inspected stay as they are
|
||||
Chart.prototype.update = function (data) {
|
||||
this.load(data);
|
||||
this.render();
|
||||
};
|
||||
|
||||
Chart.prototype.shown = function () {
|
||||
|
||||
var list = this.pinned.slice();
|
||||
@@ -418,36 +432,77 @@
|
||||
html('p', 'spsa-note', root).textContent = text;
|
||||
}
|
||||
|
||||
function start() {
|
||||
var chart = null; // the chart on the page, once there is something to draw
|
||||
|
||||
var root = document.querySelector('.spsa-chart[data-chart]');
|
||||
if (!root)
|
||||
return;
|
||||
function mount(root, raw) {
|
||||
|
||||
var data;
|
||||
try {
|
||||
data = JSON.parse(root.getAttribute('data-chart'));
|
||||
data = JSON.parse(raw);
|
||||
} catch (error) {
|
||||
chart = null;
|
||||
return note(root, 'The history could not be read.');
|
||||
}
|
||||
|
||||
if (!data.names.length || data.points.length < 2 || !data.games)
|
||||
// The notice about missing history sits beside the chart, and is redone with it
|
||||
var parent = root.parentNode;
|
||||
Array.prototype.slice.call(parent.children).forEach(function (child) {
|
||||
if (child !== root && child.classList.contains('spsa-note'))
|
||||
parent.removeChild(child);
|
||||
});
|
||||
|
||||
if (!data.names.length || data.points.length < 2 || !data.games) {
|
||||
chart = null;
|
||||
return note(root, 'No games have been played yet. The chart appears with the first results.');
|
||||
}
|
||||
|
||||
var items = document.querySelectorAll('#spsa-rows .spsa-row');
|
||||
var rows = [];
|
||||
for (var k = 0; k < items.length; k++)
|
||||
rows[parseInt(items[k].getAttribute('data-index'), 10)] = items[k];
|
||||
if (chart && chart.root === root && root.querySelector('.sch-plot')) {
|
||||
|
||||
new Chart(root, data, rows);
|
||||
// Live update: the same chart, with newer data
|
||||
chart.update(data);
|
||||
|
||||
} else {
|
||||
|
||||
var items = document.querySelectorAll('#spsa-rows .spsa-row');
|
||||
var rows = [];
|
||||
for (var k = 0; k < items.length; k++)
|
||||
rows[parseInt(items[k].getAttribute('data-index'), 10)] = items[k];
|
||||
|
||||
chart = new Chart(root, data, rows);
|
||||
}
|
||||
|
||||
if (!data.recorded) {
|
||||
var message = html('p', 'spsa-note');
|
||||
message.setAttribute('data-live-skip', '');
|
||||
message.textContent = 'History starts recording with the next result. Until then the chart shows only the start and the current values.';
|
||||
root.parentNode.appendChild(message);
|
||||
parent.appendChild(message);
|
||||
}
|
||||
}
|
||||
|
||||
function start() {
|
||||
|
||||
var root = document.querySelector('.spsa-chart[data-chart]');
|
||||
if (root)
|
||||
mount(root, root.getAttribute('data-chart'));
|
||||
}
|
||||
|
||||
/* Live updates ( live.js ) never touch the chart itself. When a fresh page
|
||||
arrives with more history, the data is handed over here instead. */
|
||||
document.addEventListener('live:patched', function (event) {
|
||||
|
||||
var root = document.querySelector('.spsa-chart[data-chart]');
|
||||
var next = event.detail && event.detail.fresh && event.detail.fresh.querySelector('.spsa-chart[data-chart]');
|
||||
if (!root || !next)
|
||||
return;
|
||||
|
||||
var raw = next.getAttribute('data-chart');
|
||||
if (raw === root.getAttribute('data-chart'))
|
||||
return;
|
||||
|
||||
root.setAttribute('data-chart', raw);
|
||||
mount(root, raw);
|
||||
});
|
||||
|
||||
if (document.readyState === 'loading')
|
||||
document.addEventListener('DOMContentLoaded', start);
|
||||
else
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
/* SPSA parameter panel: filter by name, sort by how far a parameter moved. */
|
||||
/* SPSA parameter panel: filter by name, sort by how far a parameter moved.
|
||||
|
||||
Live updates ( live.js ) never replace the rows, so the order, the filter,
|
||||
the pinned lines and the hover all survive a refresh. The numbers in the
|
||||
rows are brought up to date here, from the page that was just fetched. */
|
||||
|
||||
(function () {
|
||||
|
||||
// The parts of a row that only show numbers ( nothing in them carries a listener )
|
||||
var CELLS = ['.spsa-track-wrap', '.spsa-value', '.spsa-delta'];
|
||||
|
||||
function flash(el) {
|
||||
el.classList.remove('live-flash');
|
||||
void el.offsetWidth;
|
||||
el.classList.add('live-flash');
|
||||
setTimeout(function () { el.classList.remove('live-flash'); }, 1500);
|
||||
}
|
||||
|
||||
function init() {
|
||||
|
||||
var list = document.getElementById('spsa-rows');
|
||||
@@ -14,7 +28,7 @@
|
||||
|
||||
var rows = Array.prototype.slice.call(list.children);
|
||||
|
||||
function apply() {
|
||||
function apply(keepScroll) {
|
||||
|
||||
var term = search.value.trim().toLowerCase();
|
||||
var shown = 0;
|
||||
@@ -34,14 +48,58 @@
|
||||
list.appendChild(row);
|
||||
});
|
||||
|
||||
list.scrollTop = 0;
|
||||
// A new order or filter starts at the top; a refresh leaves the reader where they are
|
||||
if (!keepScroll)
|
||||
list.scrollTop = 0;
|
||||
|
||||
if (empty)
|
||||
empty.hidden = shown !== 0;
|
||||
}
|
||||
|
||||
search.addEventListener('input', apply);
|
||||
order.addEventListener('change', apply);
|
||||
function refresh(fresh) {
|
||||
|
||||
var incoming = {};
|
||||
Array.prototype.forEach.call(fresh.querySelectorAll('#spsa-rows .spsa-row'), function (row) {
|
||||
incoming[row.getAttribute('data-index')] = row;
|
||||
});
|
||||
|
||||
var moved = false;
|
||||
|
||||
rows.forEach(function (row) {
|
||||
|
||||
var next = incoming[row.getAttribute('data-index')];
|
||||
if (!next)
|
||||
return;
|
||||
|
||||
if (row.getAttribute('data-drift') !== next.getAttribute('data-drift')) {
|
||||
row.setAttribute('data-drift', next.getAttribute('data-drift'));
|
||||
moved = true;
|
||||
}
|
||||
|
||||
row.classList.toggle('at-bound', next.classList.contains('at-bound'));
|
||||
|
||||
CELLS.forEach(function (selector) {
|
||||
var current = row.querySelector(selector), latest = next.querySelector(selector);
|
||||
if (!current || !latest || current.innerHTML === latest.innerHTML)
|
||||
return;
|
||||
current.innerHTML = latest.innerHTML;
|
||||
if (selector !== '.spsa-track-wrap')
|
||||
flash(current);
|
||||
});
|
||||
});
|
||||
|
||||
// Sorted by how far they moved: the order itself may have changed
|
||||
if (moved && order.value === 'drift')
|
||||
apply(true);
|
||||
}
|
||||
|
||||
search.addEventListener('input', function () { apply(false); });
|
||||
order.addEventListener('change', function () { apply(false); });
|
||||
|
||||
document.addEventListener('live:patched', function (event) {
|
||||
if (event.detail && event.detail.fresh)
|
||||
refresh(event.detail.fresh);
|
||||
});
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading')
|
||||
|
||||
Reference in new issue
Block a user