/** * GitHub 数据显示模块 * 负责在页面上显示 GitHub 统计信息 */ class GitHubDisplay { constructor() { this.githubAPI = githubAPI; this.containerSelectors = { stats: '.github-stats', commits: '.github-commits', contributors: '.github-contributors', languages: '.github-languages' }; } /** * 初始化 GitHub 数据显示 */ async init() { console.log('初始化 GitHub 数据显示...'); // 显示加载状态 this.showLoadingState(); try { // 获取统计数据 const stats = await this.githubAPI.getAllStats(); if (!stats) { this.showError(); return; } // 更新所有显示区域 this.updateStats(stats); this.updateRecentCommits(stats); this.updateContributors(stats); this.updateLanguages(stats); console.log('GitHub 数据显示完成'); } catch (error) { console.error('GitHub 数据显示失败:', error); this.showError(); } } /** * 显示加载状态 */ showLoadingState() { const containers = document.querySelectorAll('.github-stats-loading'); containers.forEach(container => { container.innerHTML = `
正在加载 GitHub 数据...
`; }); } /** * 显示错误信息 */ showError() { const containers = document.querySelectorAll('.github-stats-loading'); containers.forEach(container => { container.innerHTML = `
无法加载 GitHub 数据
`; }); } /** * 更新统计数据显示 */ updateStats(stats) { const container = document.querySelector(this.containerSelectors.stats); if (!container) return; const repo = stats.repository; container.innerHTML = `

GitHub 仓库统计

${this.githubAPI.formatNumber(repo.stars)}
Stars
${this.githubAPI.formatNumber(repo.forks)}
Forks
${this.githubAPI.formatNumber(repo.watchers)}
Watchers
${this.githubAPI.formatNumber(repo.issues)}
Issues

${repo.description}

创建于 ${this.githubAPI.formatDate(repo.createdAt)} 更新于 ${this.githubAPI.formatDate(repo.updatedAt)}
查看仓库
`; container.classList.remove('github-stats-loading'); } /** * 更新最近提交记录 */ updateRecentCommits(stats) { const container = document.querySelector(this.containerSelectors.commits); if (!container) return; const commits = stats.recentCommits; container.innerHTML = `

最近提交

${commits.map(commit => `
${commit.message}
${commit.author} ${this.githubAPI.formatDate(commit.date)}
`).join('')}
`; } /** * 更新贡献者显示 */ updateContributors(stats) { const container = document.querySelector(this.containerSelectors.contributors); if (!container) return; const contributors = stats.contributors.slice(0, 10); // 显示前10名 container.innerHTML = `

贡献者

${contributors.map(contributor => ` `).join('')}
`; } /** * 更新语言分布显示 */ updateLanguages(stats) { const container = document.querySelector(this.containerSelectors.languages); if (!container) return; const languages = stats.languages; const totalBytes = Object.values(languages).reduce((sum, bytes) => sum + bytes, 0); // 计算百分比并排序 const languageStats = Object.entries(languages) .map(([name, bytes]) => ({ name, bytes, percentage: ((bytes / totalBytes) * 100).toFixed(1) })) .sort((a, b) => b.percentage - a.percentage); container.innerHTML = `

编程语言

${languageStats.map(lang => `
`).join('')}
${languageStats.map(lang => `
${lang.name}
${lang.percentage}%
`).join('')}
`; } /** * 获取语言对应的颜色 */ getLanguageColor(language) { const colors = { 'JavaScript': '#f1e05a', 'TypeScript': '#2b7489', 'Python': '#3572A5', 'Java': '#b07219', 'C++': '#f34b7d', 'C': '#555555', 'C#': '#239120', 'Ruby': '#701516', 'PHP': '#4F5D95', 'Swift': '#ffac45', 'Go': '#00ADD8', 'Rust': '#dea584', 'HTML': '#e34c26', 'CSS': '#563d7c', 'Shell': '#89e051', 'Dockerfile': '#384d54', 'Markdown': '#083fa1' }; return colors[language] || '#999999'; } /** * 手动刷新数据 */ async refresh() { console.log('刷新 GitHub 数据...'); localStorage.removeItem('github-data-cache'); await this.init(); } } // 创建全局实例 const githubDisplay = new GitHubDisplay(); // 页面加载完成后自动初始化 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => githubDisplay.init()); } else { githubDisplay.init(); } // 导出供其他模块使用 if (typeof module !== 'undefined' && module.exports) { module.exports = GitHubDisplay; }