const PageDashboard = { _el: null, _currentCategory: 'all', init() { this._el = document.getElementById('page-dashboard'); }, async render() { this._el.innerHTML = this._template(); await this._loadCategories(); await this._loadThemes(); this._bindEvents(); }, async _loadCategories() { try { const cats = await API.getCategories(); const listEl = document.getElementById('dash-category-list'); if (!listEl) return; const allItem = document.createElement('div'); allItem.className = 'category-item active'; allItem.textContent = '全部'; allItem.dataset.cat = 'all'; listEl.appendChild(allItem); cats.forEach(cat => { const item = document.createElement('div'); item.className = 'category-item'; item.textContent = cat; item.dataset.cat = cat; listEl.appendChild(item); }); } catch (e) { console.error('Failed to load categories:', e); } }, async _loadThemes() { const gridEl = document.getElementById('dash-theme-grid'); if (!gridEl) return; gridEl.innerHTML = '
加载中...
'; try { const themes = await API.getDashboards(this._currentCategory); if (!themes || themes.length === 0) { gridEl.innerHTML = `

暂无仪表盘主题

创建或导入你的第一个仪表盘主题

`; return; } gridEl.innerHTML = themes.map(t => this._themeCardHtml(t)).join(''); } catch (e) { console.error('Failed to load themes:', e); gridEl.innerHTML = '
加载失败
'; } }, _bindEvents() { const categoryList = document.getElementById('dash-category-list'); if (categoryList) { categoryList.addEventListener('click', (e) => { const item = e.target.closest('.category-item'); if (!item) return; categoryList.querySelectorAll('.category-item').forEach(el => el.classList.remove('active')); item.classList.add('active'); this._currentCategory = item.dataset.cat; this._loadThemes(); }); } const gridEl = document.getElementById('dash-theme-grid'); if (gridEl) { gridEl.addEventListener('click', (e) => { const card = e.target.closest('.theme-card'); if (!card) return; const themeId = card.dataset.themeId; this._openDashboard(themeId); }); } }, _openDashboard(themeId) { const url = `${location.origin}/dashboard/${themeId}`; window.open(url, '_blank'); navigator.clipboard.writeText(url).then(() => { Toast.show('链接已复制,可在局域网设备访问', 'success'); }).catch(() => { Toast.show(`打开地址: ${url}`, 'info'); }); }, _themeCardHtml(theme) { const icon = theme.config?.icon || '📊'; const aspect = theme.config?.aspect_ratio || 'auto'; return `
${icon}
${theme.name || 'Unnamed'}
${theme.category || 'basic'} ${aspect}
${theme.author || ''} ${theme.is_builtin ? '内置' : '自定义'}
`; }, _template() { return `
`; } }; window.PageDashboard = PageDashboard;