优化白色主题渲染逻辑
This commit is contained in:
@@ -13,9 +13,18 @@
|
||||
// Prevent theme flicker
|
||||
(function() {
|
||||
const savedTheme = localStorage.getItem('theme');
|
||||
if (savedTheme === 'light') {
|
||||
const settings = window.SITE_SETTINGS || {};
|
||||
const defaultTheme = settings.default_theme || 'dark';
|
||||
const theme = savedTheme || defaultTheme;
|
||||
|
||||
if (theme === 'light') {
|
||||
document.documentElement.classList.add('light-theme');
|
||||
}
|
||||
|
||||
// Also apply title if available to prevent flicker
|
||||
if (settings.page_name) {
|
||||
document.title = settings.page_name;
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
|
||||
@@ -128,12 +128,24 @@
|
||||
// Start data fetching
|
||||
fetchMetrics();
|
||||
fetchNetworkHistory();
|
||||
|
||||
// Site settings
|
||||
if (window.SITE_SETTINGS) {
|
||||
applySiteSettings(window.SITE_SETTINGS);
|
||||
// Actual theme class already applied in head, just update icons and inputs
|
||||
const savedTheme = localStorage.getItem('theme');
|
||||
const currentTheme = savedTheme || window.SITE_SETTINGS.default_theme || 'dark';
|
||||
updateThemeIcons(currentTheme);
|
||||
|
||||
// Still populate inputs
|
||||
dom.pageNameInput.value = window.SITE_SETTINGS.page_name || '';
|
||||
dom.siteTitleInput.value = window.SITE_SETTINGS.title || '';
|
||||
dom.logoUrlInput.value = window.SITE_SETTINGS.logo_url || '';
|
||||
dom.defaultThemeInput.value = window.SITE_SETTINGS.default_theme || 'dark';
|
||||
}
|
||||
|
||||
loadSiteSettings();
|
||||
|
||||
// Initial icon check based on early head script
|
||||
const currentTheme = document.documentElement.classList.contains('light-theme') ? 'light' : 'dark';
|
||||
updateThemeIcons(currentTheme);
|
||||
|
||||
setInterval(fetchMetrics, REFRESH_INTERVAL);
|
||||
setInterval(fetchNetworkHistory, NETWORK_HISTORY_INTERVAL);
|
||||
}
|
||||
@@ -444,6 +456,8 @@
|
||||
const response = await fetch('/api/settings');
|
||||
const settings = await response.json();
|
||||
|
||||
window.SITE_SETTINGS = settings; // Cache it globally
|
||||
|
||||
// Update inputs
|
||||
dom.pageNameInput.value = settings.page_name || '';
|
||||
dom.siteTitleInput.value = settings.title || '';
|
||||
|
||||
@@ -276,7 +276,49 @@ app.use(async (req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(express.static(path.join(__dirname, '..', 'public')));
|
||||
// Helper to serve index.html with injected settings
|
||||
const serveIndex = async (req, res) => {
|
||||
try {
|
||||
const indexPath = path.join(__dirname, '..', 'public', 'index.html');
|
||||
if (!fs.existsSync(indexPath)) return res.status(404).send('Not found');
|
||||
|
||||
let html = fs.readFileSync(indexPath, 'utf8');
|
||||
|
||||
// Fetch settings
|
||||
let settings = {
|
||||
page_name: '数据可视化展示大屏',
|
||||
title: '数据可视化展示大屏',
|
||||
logo_url: null,
|
||||
default_theme: 'dark'
|
||||
};
|
||||
|
||||
if (isDbInitialized) {
|
||||
try {
|
||||
const [rows] = await db.query('SELECT * FROM site_settings WHERE id = 1');
|
||||
if (rows.length > 0) settings = rows[0];
|
||||
} catch (e) {
|
||||
// DB not ready or table missing
|
||||
}
|
||||
}
|
||||
|
||||
// Inject settings
|
||||
const settingsJson = JSON.stringify(settings);
|
||||
const injection = `<script>window.SITE_SETTINGS = ${settingsJson};</script>`;
|
||||
|
||||
// Replace <head> with <head> + injection
|
||||
html = html.replace('<head>', '<head>' + injection);
|
||||
|
||||
res.send(html);
|
||||
} catch (err) {
|
||||
console.error('Error serving index:', err);
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
};
|
||||
|
||||
app.get('/', serveIndex);
|
||||
app.get('/index.html', serveIndex);
|
||||
|
||||
app.use(express.static(path.join(__dirname, '..', 'public'), { index: false }));
|
||||
|
||||
// ==================== Prometheus Source CRUD ====================
|
||||
|
||||
@@ -553,8 +595,9 @@ app.get('/api/metrics/cpu-history', async (req, res) => {
|
||||
});
|
||||
|
||||
// SPA fallback
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
|
||||
app.get('*', (req, res, next) => {
|
||||
if (req.path.startsWith('/api/') || req.path.includes('.')) return next();
|
||||
serveIndex(req, res);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user