document.addEventListener('DOMContentLoaded', async () => {
window.nodeSizeMode = null;
window.drillGroup = null;
window.dig = 0; window.drillDig = 0;
window.tier = null; window.cycleOnly = false;
window.BASELINE = readEmbeddedSnapshot('cs-baseline');
window.CURRENT = readEmbeddedSnapshot('cs-current');
const EMPTY = { graphs: {} };
window.DIFF = computeDiff(window.BASELINE ?? window.CURRENT ?? EMPTY, window.CURRENT ?? window.BASELINE ?? EMPTY);
window.CYCLES = computeCycles(window.BASELINE ?? window.CURRENT ?? EMPTY, window.CURRENT ?? window.BASELINE ?? EMPTY);
window.META = computeMeta(window.BASELINE, window.CURRENT);
const urlSide = getNavParams().side;
window.viewSide = (urlSide === 'baseline' && window.BASELINE) ? 'baseline'
: window.CURRENT ? 'current'
: 'baseline';
const urlStat = getNavParams().stat;
if (urlStat && window.isSummaryStat?.(urlStat)) window._summaryStat = urlStat;
window._statsOpen = getNavParams().panel === 'stats';
const epState = (typeof epReadUrl === 'function') ? epReadUrl() : null;
if (epState?.sel?.length) {
if (!window._ntSelected) window._ntSelected = {};
window._ntSelected[epState.level] = new Set(epState.sel);
}
document.querySelectorAll('.view').forEach(sec => setupNodeTable(sec, sec.dataset.view));
setupSnapPopup();
setupModeToggle();
setupFileControls();
setupTooltip();
buildSummary();
setupSummaryStatControl();
setupSummaryPopup();
updateFilesTab();
updateHeader();
if (window._statsOpen) openSummaryPopup(false);
const active = document.querySelector('.view.active');
const loading = active?.querySelector('.loading-indicator');
if (loading) { loading.textContent = 'Loading Graphviz…'; loading.classList.add('on'); }
window.gv = await window['@hpcc-js/wasm/graphviz'].Graphviz.load();
const np = getNavParams();
const freshLoad = !np.group && !np.tier && !np.node && !np.mode && !np.depth && !np.level;
if (freshLoad) {
const lvl = currentLevel();
window.tier = 'file';
const segs = window.autoFocusSegs?.(lvl) ?? [];
if (segs.length) {
window.drillGroup = segs.join('/');
window.drillDig = window.digOfKeyForTier(lvl, window.drillGroup, 'file');
window.focusDig = window.landingFocusDig(lvl);
} else {
window.dig = window.overviewBaseDig(lvl);
}
}
renderView(active);
const { level: urlLevel, node: urlNode, group: urlGroup, mode: urlMode, depth: urlDepth, tier: urlTier, stat: urlStat2 } = np;
if (urlLevel && urlLevel !== currentLevel()) switchToLevel(urlLevel);
if (!freshLoad) applyViewState({ level: urlLevel, group: urlGroup, mode: urlMode, depth: urlDepth, tier: urlTier, stat: urlStat2 }, { rerender: !!(urlGroup || urlMode || urlDepth || urlTier) });
if (urlNode) openModalForNode(urlNode, urlLevel ?? currentLevel());
history.replaceState(
{ level: currentLevel(), node: urlNode ?? null, group: window.drillGroup ?? null, mode: window.nodeSizeMode ?? null, depth: window.navDepth?.() ?? 0, tier: window.tier || null, side: window.viewSide, stat: window.navStat?.() ?? null, panel: window._statsOpen ? 'stats' : null },
'', location.href
);
if (epState) {
if (epState.level && epState.level !== currentLevel()) switchToLevel(epState.level);
openExportPopup(epState.level, epState);
}
window.addEventListener('popstate', e => {
const st = e.state || getNavParams();
const lvl = st.level;
const nid = st.node;
const side = st.side;
if (window.CURRENT && (side === 'baseline' || side === 'current')) setViewSide(side);
if (lvl && lvl !== currentLevel()) switchToLevel(lvl);
applyViewState({ level: lvl ?? currentLevel(), group: st.group, mode: st.mode, depth: st.depth, tier: st.tier, stat: st.stat }, { rerender: true });
if (nid) {
openModalForNode(nid, lvl ?? currentLevel());
} else {
closeModalSilent();
}
if (st.panel === 'stats') openSummaryPopup(false); else closeSummaryPopup(false);
});
});