File size: 3,924 Bytes
31226fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const TAB_META = {
  overview: { kicker: "SYSTEM", title: "Overview", context: "Live system state, health and scientific status" },
  network: { kicker: "OBSERVE", title: "Network", context: "Neural dynamics, topology and data inspection" },
  control: { kicker: "OPERATE", title: "Control", context: "Runtime, pacing, experiments and approvals" },
  research: { kicker: "EVIDENCE", title: "Research", context: "Experiments, provenance, files and scientific evidence" },
  gate: { kicker: "VERIFY", title: "Release", context: "Scientific gate, CI evidence and release readiness" },
  settings: { kicker: "CONFIGURE", title: "Settings", context: "Runtime-visible configuration and operator preferences" },
  embodiment: { kicker: "EMBODY", title: "Embodiment", context: "Observed body, devices, interoception and feedback" },
};

function ensureStylesheet() {
  // The canonical stylesheet imports the shell layer once.
}

function currentTab() {
  const active = document.querySelector(".tab-content.active[id^='tab-']");
  return active?.id?.replace("tab-", "") || "overview";
}

function fitCanvas(canvas) {
  if (!(canvas instanceof HTMLCanvasElement)) return;
  const ratio = canvas.height / Math.max(canvas.width, 1);
  canvas.style.width = "100%";
  canvas.style.height = `${Math.max(160, canvas.clientWidth * ratio)}px`;
}

function fitVisibleCanvases(root = document) {
  root.querySelectorAll("canvas").forEach(fitCanvas);
}

function ensureWorkspaceHeaders() {
  // Workspace headers removed — sidebar provides all navigation context.
}

function addUtilityBars() {
  // Utility bars removed — sidebar provides all navigation and context.
}

function refreshReadOnlyData() {
  window.dispatchEvent(new CustomEvent("brain5d:dashboard-refresh", { detail: { tab: currentTab() } }));
  document.querySelector(`.tab-btn[data-tab="${currentTab()}"]`)?.click();
}

function bindUtilityActions() {
  document.addEventListener("click", (event) => {
    const button = event.target.closest("[data-dashboard-action]");
    if (!button) return;
    const action = button.dataset.dashboardAction;
    if (action === "top") {
      window.scrollTo({ top: 0, behavior: "smooth" });
      return;
    }
    if (action === "density") {
      const compact = document.body.dataset.dashboardDensity === "compact";
      document.body.dataset.dashboardDensity = compact ? "comfortable" : "compact";
      document.querySelectorAll('[data-dashboard-action="density"]').forEach((item) => {
        item.textContent = compact ? "Compact" : "Comfortable";
      });
      localStorage.setItem("brain5d-dashboard-density", document.body.dataset.dashboardDensity);
      fitVisibleCanvases(document.querySelector(".tab-content.active") || document);
      return;
    }
    if (action === "refresh") refreshReadOnlyData();
  });
}

function bindTabResponsiveness() {
  document.querySelectorAll(".tab-btn[data-tab]").forEach((button) => {
    button.addEventListener("click", () => {
      requestAnimationFrame(() => {
        const tab = document.getElementById(`tab-${button.dataset.tab}`);
        if (tab) fitVisibleCanvases(tab);
      });
    });
  });

  let resizeTimer = null;
  window.addEventListener("resize", () => {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(() => fitVisibleCanvases(document.querySelector(".tab-content.active") || document), 80);
  });
}

function restorePreferences() {
  document.body.dataset.dashboardDensity = localStorage.getItem("brain5d-dashboard-density") || "comfortable";
}

function init() {
  ensureStylesheet();
  restorePreferences();
  ensureWorkspaceHeaders();
  addUtilityBars();
  bindUtilityActions();
  bindTabResponsiveness();
  fitVisibleCanvases(document.querySelector(".tab-content.active") || document);
}

ensureStylesheet();
if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", init, { once: true });
} else {
  init();
}