Spaces:
Running on Zero
Running on Zero
File size: 6,392 Bytes
a39d07d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | // 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();
|