Replace hardcoded updateAllText() calls with languageChanged event pattern so feature modules subscribe independently. Guard all API calls behind apiKey checks to prevent unauthorized requests when not logged in. Fix login modal localization, hide tabs when logged out, clear all panels on logout, and treat profiles with no conditions as always-true. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
57 lines
2.6 KiB
JavaScript
57 lines
2.6 KiB
JavaScript
/**
|
|
* Tab switching — switchTab, initTabs, startAutoRefresh.
|
|
*/
|
|
|
|
import { apiKey, refreshInterval, setRefreshInterval, dashboardPollInterval } from '../core/state.js';
|
|
|
|
export function switchTab(name) {
|
|
document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.toggle('active', btn.dataset.tab === name));
|
|
document.querySelectorAll('.tab-panel').forEach(panel => panel.classList.toggle('active', panel.id === `tab-${name}`));
|
|
localStorage.setItem('activeTab', name);
|
|
if (name === 'dashboard') {
|
|
// Use window.* to avoid circular imports with feature modules
|
|
if (apiKey && typeof window.loadDashboard === 'function') window.loadDashboard();
|
|
if (apiKey && typeof window.startDashboardWS === 'function') window.startDashboardWS();
|
|
} else {
|
|
if (typeof window.stopDashboardWS === 'function') window.stopDashboardWS();
|
|
if (typeof window.stopPerfPolling === 'function') window.stopPerfPolling();
|
|
if (typeof window.stopUptimeTimer === 'function') window.stopUptimeTimer();
|
|
if (!apiKey) return;
|
|
if (name === 'streams') {
|
|
if (typeof window.loadPictureSources === 'function') window.loadPictureSources();
|
|
} else if (name === 'targets') {
|
|
if (typeof window.loadTargetsTab === 'function') window.loadTargetsTab();
|
|
} else if (name === 'profiles') {
|
|
if (typeof window.loadProfiles === 'function') window.loadProfiles();
|
|
}
|
|
}
|
|
}
|
|
|
|
export function initTabs() {
|
|
let saved = localStorage.getItem('activeTab');
|
|
// Migrate legacy 'devices' tab to 'targets'
|
|
if (saved === 'devices') saved = 'targets';
|
|
if (!saved || !document.getElementById(`tab-${saved}`)) saved = 'dashboard';
|
|
switchTab(saved);
|
|
}
|
|
|
|
export function startAutoRefresh() {
|
|
if (refreshInterval) {
|
|
clearInterval(refreshInterval);
|
|
}
|
|
|
|
setRefreshInterval(setInterval(() => {
|
|
if (apiKey) {
|
|
const activeTab = localStorage.getItem('activeTab') || 'dashboard';
|
|
if (activeTab === 'targets') {
|
|
// Skip refresh while user interacts with a picker or slider
|
|
const panel = document.getElementById('targets-panel-content');
|
|
if (panel && panel.contains(document.activeElement) && document.activeElement.matches('input')) return;
|
|
if (typeof window.loadTargetsTab === 'function') window.loadTargetsTab();
|
|
} else if (activeTab === 'dashboard') {
|
|
if (typeof window.loadDashboard === 'function') window.loadDashboard();
|
|
}
|
|
}
|
|
}, dashboardPollInterval));
|
|
}
|