-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
393 lines (338 loc) · 14.2 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
document.addEventListener('DOMContentLoaded', () => {
const wall = document.querySelector('.contributors-wall');
const categoriesList = document.querySelector('.categories-list');
const searchInput = document.querySelector('#search');
const searchType = document.querySelector('#searchType');
const contributorInfo = document.querySelector('#contributorInfo');
const prevPageBtn = document.querySelector('#prevPage');
const nextPageBtn = document.querySelector('#nextPage');
const pageInfo = document.querySelector('#pageInfo');
const REPOS_PER_PAGE = 5;
const BATCH_SIZE = 100; // 每批加载100个
let currentPage = 1;
let currentCategory = 'all';
let allCategories = [];
let intersectionObserver;
let loadMoreObserver;
let allContributorsCache = []; // 缓存所有贡献者
let currentIndex = 0; // 当前加载的索引
let isLoading = false;
// 初始化交叉观察器
function initIntersectionObserver() {
intersectionObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target.querySelector('img');
if (img && img.dataset.src) {
img.src = img.dataset.src;
delete img.dataset.src;
}
}
});
}, {
root: null,
rootMargin: '50px',
threshold: 0.1
});
}
// 初始化加载更多观察器
function initLoadMoreObserver() {
loadMoreObserver = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && !isLoading && currentIndex < allContributorsCache.length) {
loadMoreContributors();
}
}, {
root: null,
rootMargin: '100px',
threshold: 0.1
});
}
// 添加加载更多触发器
function addLoadMoreTrigger() {
const trigger = document.createElement('div');
trigger.className = 'load-more-trigger';
trigger.style.height = '20px';
wall.appendChild(trigger);
loadMoreObserver.observe(trigger);
}
// 加载更多贡献者
async function loadMoreContributors() {
if (isLoading || currentIndex >= allContributorsCache.length) return;
isLoading = true;
const fragment = document.createDocumentFragment();
const endIndex = Math.min(currentIndex + BATCH_SIZE, allContributorsCache.length);
for (let i = currentIndex; i < endIndex; i++) {
fragment.appendChild(allContributorsCache[i]);
}
// 移除旧的触发器
const oldTrigger = wall.querySelector('.load-more-trigger');
if (oldTrigger) {
oldTrigger.remove();
}
wall.appendChild(fragment);
currentIndex = endIndex;
// 如果还有更多数据,添加新的触发器
if (currentIndex < allContributorsCache.length) {
addLoadMoreTrigger();
}
isLoading = false;
}
// 获取排序后的仓库列表
function getSortedRepos() {
return Object.entries(contributorsData)
.map(([name, contributors]) => ({
name,
count: contributors.length
}))
.sort((a, b) => b.count - a.count); // 按贡献者数量降序排序
}
// 创建分类标签
function createCategories() {
categoriesList.innerHTML = '';
// 始终显示 "All" 选项
const allCategory = document.createElement('div');
allCategory.className = 'category';
if (currentCategory === 'all') {
allCategory.classList.add('active');
}
allCategory.textContent = 'All';
allCategory.dataset.category = 'all';
categoriesList.appendChild(allCategory);
// 获取排序后的仓库列表
const sortedRepos = getSortedRepos();
allCategories = sortedRepos.map(repo => repo.name);
// 显示当前页的仓库
const startIdx = (currentPage - 1) * REPOS_PER_PAGE;
const endIdx = startIdx + REPOS_PER_PAGE;
const pageCategories = sortedRepos.slice(startIdx, endIdx);
pageCategories.forEach(repo => {
const categoryElement = document.createElement('div');
categoryElement.className = 'category';
if (repo.name === currentCategory) {
categoryElement.classList.add('active');
}
categoryElement.textContent = `${repo.name} (${repo.count})`;
categoryElement.dataset.category = repo.name;
categoryElement.title = `${repo.count} contributors`;
categoriesList.appendChild(categoryElement);
});
updatePaginationControls();
}
// 更新分页控件
function updatePaginationControls() {
const totalPages = Math.ceil(allCategories.length / REPOS_PER_PAGE);
pageInfo.textContent = `${currentPage}/${totalPages}`;
prevPageBtn.disabled = currentPage === 1;
nextPageBtn.disabled = currentPage === totalPages;
// 添加总数显示
const totalContributors = Object.values(contributorsData)
.reduce((sum, contributors) => sum + contributors.length, 0);
const uniqueContributors = new Set(
Object.values(contributorsData)
.flat()
.map(c => c.username)
).size;
pageInfo.title = `Total: ${totalContributors} contributions, ${uniqueContributors} unique contributors`;
}
// 创建贡献者元素
function createContributorElement(contributor, index, category) {
const div = document.createElement('div');
div.className = 'contributor';
div.dataset.category = category;
div.dataset.username = contributor.username;
div.style.setProperty('--delay', (index % 20) * 0.1);
const img = document.createElement('img');
img.dataset.src = contributor.avatar;
img.alt = contributor.username;
img.loading = 'lazy';
div.appendChild(img);
intersectionObserver.observe(div);
// 添加悬浮事件
div.addEventListener('mouseenter', (e) => {
showContributorInfo(contributor, e);
wall.classList.add('dimmed');
});
div.addEventListener('mouseleave', () => {
hideContributorInfo();
wall.classList.remove('dimmed');
});
// 添加点击事件(仅用于跳转到个人主页)
div.addEventListener('click', () => {
if (contributor.profile) {
window.open(contributor.profile, '_blank');
}
});
return div;
}
// 显示贡献者详细信息
function showContributorInfo(contributor, event) {
const infoElement = document.querySelector('#contributorInfo');
const avatar = infoElement.querySelector('.large-avatar');
const name = infoElement.querySelector('.contributor-name');
const followers = infoElement.querySelector('.followers');
const following = infoElement.querySelector('.following');
const stars = infoElement.querySelector('.stars');
avatar.src = contributor.avatar;
avatar.alt = contributor.username;
name.textContent = `@${contributor.username}`;
followers.textContent = contributor.followers;
following.textContent = contributor.following;
stars.textContent = contributor.stars;
const rect = event.target.getBoundingClientRect();
const infoWidth = 300;
const infoHeight = 250;
let left = rect.right + 20;
let top = rect.top;
if (left + infoWidth > window.innerWidth) {
left = rect.left - infoWidth - 20;
}
if (top + infoHeight > window.innerHeight) {
top = window.innerHeight - infoHeight - 20;
}
infoElement.style.left = `${left}px`;
infoElement.style.top = `${top}px`;
infoElement.style.transform = 'none';
infoElement.classList.add('active');
}
// 隐藏贡献者详细信息
function hideContributorInfo() {
const infoElement = document.querySelector('#contributorInfo');
infoElement.classList.remove('active');
}
// 准备贡献者数据
function prepareContributors(filter = '') {
const contributors = [];
const searchLower = filter.toLowerCase();
const isRepoSearch = searchType.value === 'repo';
if (currentCategory === 'all') {
const allContributors = new Map();
Object.entries(contributorsData).forEach(([category, categoryContributors]) => {
categoryContributors.forEach(contributor => {
if (!allContributors.has(contributor.username)) {
allContributors.set(contributor.username, {
...contributor,
categories: [category]
});
} else {
allContributors.get(contributor.username).categories.push(category);
}
});
});
allContributors.forEach((contributor, username) => {
if (!filter ||
(isRepoSearch && contributor.categories.some(cat => cat.toLowerCase().includes(searchLower))) ||
(!isRepoSearch && username.toLowerCase().includes(searchLower))) {
contributors.push(createContributorElement(contributor, contributors.length, 'all'));
}
});
} else {
const categoryContributors = contributorsData[currentCategory] || [];
categoryContributors.forEach((contributor, index) => {
if (!filter ||
(isRepoSearch && currentCategory.toLowerCase().includes(searchLower)) ||
(!isRepoSearch && contributor.username.toLowerCase().includes(searchLower))) {
contributors.push(createContributorElement(contributor, index, currentCategory));
}
});
}
// 随机排序
for (let i = contributors.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[contributors[i], contributors[j]] = [contributors[j], contributors[i]];
}
return contributors;
}
// 渲染贡献者墙
async function renderContributors(filter = '') {
const startTime = performance.now();
wall.innerHTML = '';
currentIndex = 0;
isLoading = false;
// 准备所有贡献者数据
allContributorsCache = prepareContributors(filter);
console.log(`Prepared ${allContributorsCache.length} contributors`);
// 加载第一批
await loadMoreContributors();
console.log(`Initial rendering completed in ${performance.now() - startTime}ms`);
}
// 初始化
initIntersectionObserver();
initLoadMoreObserver();
createCategories();
renderContributors();
// 搜索功能
let searchTimeout;
searchInput.addEventListener('input', (e) => {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
renderContributors(e.target.value);
}, 300);
});
// 搜索类型切换
searchType.addEventListener('change', () => {
if (searchInput.value) {
renderContributors(searchInput.value);
}
});
// 分类切换
categoriesList.addEventListener('click', (e) => {
if (e.target.classList.contains('category')) {
document.querySelectorAll('.category').forEach(cat => cat.classList.remove('active'));
e.target.classList.add('active');
currentCategory = e.target.dataset.category;
renderContributors(searchInput.value);
}
});
// 分页控制 - 只影响仓库列表
prevPageBtn.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
const sortedRepos = getSortedRepos();
const startIdx = (currentPage - 1) * REPOS_PER_PAGE;
const pageCategories = sortedRepos.slice(startIdx, startIdx + REPOS_PER_PAGE);
if (currentCategory !== 'all' && !pageCategories.find(repo => repo.name === currentCategory)) {
currentCategory = 'all';
}
createCategories();
renderContributors(searchInput.value);
}
});
nextPageBtn.addEventListener('click', () => {
const totalPages = Math.ceil(allCategories.length / REPOS_PER_PAGE);
if (currentPage < totalPages) {
currentPage++;
const sortedRepos = getSortedRepos();
const startIdx = (currentPage - 1) * REPOS_PER_PAGE;
const pageCategories = sortedRepos.slice(startIdx, startIdx + REPOS_PER_PAGE);
if (currentCategory !== 'all' && !pageCategories.find(repo => repo.name === currentCategory)) {
currentCategory = 'all';
}
createCategories();
renderContributors(searchInput.value);
}
});
// 优化滚动性能
let scrollRAF;
window.addEventListener('scroll', () => {
if (scrollRAF) {
cancelAnimationFrame(scrollRAF);
}
scrollRAF = requestAnimationFrame(() => {
const scrolled = window.pageYOffset;
const viewportHeight = window.innerHeight;
const elements = document.elementsFromPoint(
window.innerWidth / 2,
viewportHeight / 2
);
elements.forEach(element => {
if (element.classList.contains('contributor')) {
const rect = element.getBoundingClientRect();
const centerY = rect.top + rect.height / 2;
const distanceFromCenter = Math.abs(viewportHeight / 2 - centerY);
const parallaxAmount = Math.min(20, 20 * (1 - distanceFromCenter / (viewportHeight / 2)));
element.style.transform = `translateY(${-scrolled * 0.1}px) translateZ(${parallaxAmount}px)`;
}
});
});
});
});