glarr's picture
UI Redesign
a39d07d
Raw History Blame Contribute Delete
6.39 kB
// Run explorer behaviour, loaded via gr.HTML(js_on_load=...). Markup comes from inversion/explorer.py.
// `element` is the component root; it survives re-renders, so one delegated listener handles everything.
//
// Navigation: stage panels are <section data-stage>; one is shown at a time.
// Live follow: while a run streams, the view follows the newest stage until the user navigates away;
// Play resumes following. Each re-render re-applies the user's stage (watch('value')).
// Replay: once the run is done, Replay steps through every stage with the design's 3.2 s dwell.
const DWELL_MS = 3200;
const S = { run: null, cur: -1, shown: -1, follow: true, replay: false, timer: null, copyTimer: null, busy: false };
const $ = (sel) => element.querySelector(sel);
const $$ = (sel) => Array.from(element.querySelectorAll(sel));
const root = () => $('.pre-x');
const panels = () => $$('.pre-x-stage');
const isDone = () => { const r = root(); return !!r && r.dataset.done === '1'; };
const liveIndex = () => { const r = root(); return r ? Number(r.dataset.live || 0) : 0; };
const usable = (i) => { const p = panels()[i]; return !!p && p.dataset.state !== 'pending'; };
function stopReplay() {
S.replay = false;
clearTimeout(S.timer);
S.timer = null;
}
function apply() {
const r = root();
if (!r) return;
S.busy = true;
const list = panels();
if (S.cur < 0 || S.cur >= list.length || !usable(S.cur)) S.cur = liveIndex();
list.forEach((p, i) => {
p.hidden = i !== S.cur;
if (i === S.cur && S.shown !== S.cur) {
p.classList.add('pre-x-enter');
p.addEventListener('animationend', () => p.classList.remove('pre-x-enter'), { once: true });
}
});
S.shown = S.cur;
r.classList.toggle('replaying', S.replay);
r.style.setProperty('--pre-x-dwell', DWELL_MS + 'ms');
$$('[data-go]').forEach((b) => {
const i = Number(b.dataset.go);
const on = i === S.cur;
b.setAttribute('aria-current', on ? 'step' : 'false');
b.classList.toggle('is-current', on);
b.classList.toggle('before', i < S.cur);
b.classList.toggle('is-timed', on && S.replay);
});
$$('.pre-x-group').forEach((g) => g.classList.toggle('active', !!g.querySelector('.is-current')));
const num = $('[data-role="stepnum"]');
if (num) num.textContent = String(S.cur + 1);
const prev = $('[data-act="prev"]');
const next = $('[data-act="next-top"]');
if (prev) prev.disabled = S.cur <= 0;
if (next) next.disabled = !usable(S.cur + 1);
const play = $('[data-act="play"]');
if (play) {
let state;
let label;
if (isDone()) { state = S.replay ? 'pause' : 'replay'; label = S.replay ? 'Pause' : 'Replay'; }
else { state = S.follow ? 'pause' : 'play'; label = S.follow ? 'Pause' : 'Play'; }
play.dataset.state = state;
play.setAttribute('aria-label', isDone() ? (S.replay ? 'Pause replay' : 'Replay the run')
: (S.follow ? 'Stop following the live stage' : 'Follow the live stage'));
const lbl = play.querySelector('[data-role="playlabel"]');
if (lbl) lbl.textContent = label;
}
S.busy = false;
}
function go(i) {
if (i < 0 || i >= panels().length || !usable(i)) return;
stopReplay();
S.cur = i;
S.follow = !isDone() && i === liveIndex();
apply();
}
function scheduleReplay() {
clearTimeout(S.timer);
if (!S.replay) return;
S.timer = setTimeout(() => {
if (!S.replay) return;
const last = panels().length - 1;
if (S.cur >= last) { stopReplay(); apply(); return; }
S.cur += 1;
apply();
scheduleReplay();
}, DWELL_MS);
}
function togglePlay() {
if (isDone()) {
if (S.replay) stopReplay();
else { S.replay = true; S.cur = 0; }
apply();
scheduleReplay();
return;
}
S.follow = !S.follow;
if (S.follow) S.cur = liveIndex();
apply();
}
function toggleTarget(btn) {
const id = btn.getAttribute('aria-controls');
const el = id ? element.querySelector('#' + id) : null;
if (!el) return;
el.hidden = !el.hidden;
$$('[aria-controls="' + id + '"]').forEach((b) => {
b.setAttribute('aria-expanded', String(!el.hidden));
const lbl = b.querySelector('[data-role="label"]');
if (lbl && b.dataset.on) lbl.textContent = el.hidden ? b.dataset.off : b.dataset.on;
});
}
function copy(btn) {
const src = btn.dataset.src ? element.querySelector('#' + btn.dataset.src) : null;
const text = src ? src.textContent : '';
const done = () => {
const lbl = btn.querySelector('[data-role="label"]');
if (!lbl) return;
lbl.textContent = 'Copied';
clearTimeout(S.copyTimer);
S.copyTimer = setTimeout(() => { lbl.textContent = 'Copy'; }, 1600);
};
const fallback = () => {
const ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
try { if (document.execCommand('copy')) done(); } catch (err) { /* clipboard unavailable */ }
ta.remove();
};
if (navigator.clipboard && window.isSecureContext) navigator.clipboard.writeText(text).then(done, fallback);
else fallback();
}
element.addEventListener('click', (ev) => {
const t = ev.target.closest('[data-act], [data-go]');
if (!t || !element.contains(t) || t.disabled) return;
if (t.hasAttribute('data-go')) { go(Number(t.dataset.go)); return; }
const act = t.dataset.act;
if (act === 'prev') go(S.cur - 1);
else if (act === 'next' || act === 'next-top') go(S.cur + 1);
else if (act === 'play') togglePlay();
else if (act === 'toggle') toggleTarget(t);
else if (act === 'copy') copy(t);
});
element.addEventListener('keydown', (ev) => {
if (ev.key !== 'Escape') return;
$$('.pre-x-pop:not([hidden])').forEach((pop) => {
const opener = element.querySelector('.pre-x-help[aria-controls="' + pop.id + '"]');
if (opener) { toggleTarget(opener); opener.focus(); }
});
});
function onValue() {
const r = root();
const id = r ? r.dataset.run : null;
if (id !== S.run) {
S.run = id;
S.cur = -1;
S.shown = -1;
S.follow = true;
stopReplay();
}
if (S.follow && !S.replay) S.cur = liveIndex();
apply();
}
if (typeof watch === 'function') {
watch('value', onValue);
} else {
new MutationObserver(() => { if (!S.busy) onValue(); }).observe(element, { childList: true });
}
onValue();