View raw

1 import { initialiseGraphControls } from "./dashboard/graph-controls.js"; 2 import { createProbeCharts } from "./dashboard/charts.js"; 3 import { createInsightsCharts } from "./dashboard/insights.js"; 4 import { createWeatherCharts } from "./dashboard/weather-page.js"; 5 import { loadWeatherForecast } from "./dashboard/weather-forecast.js"; 6 import { DASHBOARD_REFRESH_MS } from "./dashboard/constants.js"; 7 import { loadQuickReadings } from "./dashboard/quick-readings.js"; 8 import { createStatusLoader } from "./dashboard/status.js"; 9 import { createTimeframeState } from "./dashboard/timeframe.js"; 10 11 // Global chart defaults: incline x-axis labels for better mobile readability 12 Chart.defaults.scales.category.ticks.maxRotation = 45; 13 Chart.defaults.scales.category.ticks.minRotation = 45; 14 15 const timeframe = createTimeframeState(); 16 const probes = createProbeCharts(timeframe); 17 const insights = createInsightsCharts(); 18 const weatherPage = createWeatherCharts(); 19 const loadStatuses = createStatusLoader(); 20 21 document.querySelectorAll(".tab-button").forEach(button => { 22 button.addEventListener("click", () => probes.activateTab(button.dataset.probe)); 23 }); 24 25 initialiseGraphControls(timeframe, () => { 26 probes.reloadDisplayed(); 27 }); 28 29 window.addEventListener("hashchange", () => { 30 const probe = probes.probeFromLocation(); 31 if (probe) probes.activateTab(probe); 32 }); 33 34 const initialHashProbe = probes.probeFromHash(); 35 const initialProbe = probes.probeFromLocation(); 36 37 if (initialProbe) { 38 probes.activateTab(initialProbe); 39 40 if (initialHashProbe) { 41 document.getElementById(`probe-${initialHashProbe}`)?.scrollIntoView(); 42 } 43 } else if (probes.activeProbe()) { 44 probes.loadDisplayed(); 45 } 46 47 let refreshInFlight = null; 48 49 function refreshDashboard() { 50 if (refreshInFlight) return refreshInFlight; 51 52 refreshInFlight = Promise.allSettled([ 53 probes.loadDisplayed(), 54 insights.reload(), 55 weatherPage.reload(), 56 loadWeatherForecast(), 57 loadQuickReadings(), 58 loadStatuses() 59 ]).finally(() => { 60 refreshInFlight = null; 61 }); 62 return refreshInFlight; 63 } 64 65 function refreshVisibleDashboard() { 66 return document.hidden ? null : refreshDashboard(); 67 } 68 69 document.addEventListener("visibilitychange", () => { 70 if (!document.hidden) refreshDashboard(); 71 }); 72 73 refreshVisibleDashboard(); 74 setInterval(refreshVisibleDashboard, DASHBOARD_REFRESH_MS); 75