-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
299 lines (262 loc) · 10.2 KB
/
script.js
File metadata and controls
299 lines (262 loc) · 10.2 KB
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
// ==============================================
// 全局强制捕获 下一页 按钮
// ==============================================
document.addEventListener('click', function(e) {
if (e.target && e.target.classList.contains('next-page')) {
e.preventDefault();
e.stopPropagation();
const totalPages = Math.ceil(filteredVersions.length / pageSize);
if (currentPage < totalPages) {
currentPage++;
updateUrlParams();
renderVersions();
}
}
}, true);
// 全局变量
let minecraftVersions = [];
let currentFilters = [];
let currentSearch = '';
// 分页相关变量
let currentPage = 1;
let pageSize = 50;
let filteredVersions = [];
// 标签分组配置
const TAG_GROUPS = {
main: ['正式版', '发布候选版', '预发布版', '快照版', '实验性快照版', '愚人节版'],
ancient: ['远古版', 'Beta', 'Alpha', 'Indev', 'Classic', 'pre-Classic']
};
// DOM元素
const versionsContainer = document.getElementById('versions-container');
const tagsContainer = document.getElementById('tags-container');
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
// 分页组件类
class Pagination {
constructor() {
this.pageSizeSelects = document.querySelectorAll('select[id^="page-size"]');
this.prevPageBtns = document.querySelectorAll('.prev-page');
this.nextPageBtns = document.querySelectorAll('.next-page');
this.pageInfos = document.querySelectorAll('.page-info');
this.bindEvents();
}
bindEvents() {
this.pageSizeSelects.forEach(sel => sel.replaceWith(sel.cloneNode(true)));
this.prevPageBtns.forEach(btn => btn.replaceWith(btn.cloneNode(true)));
this.nextPageBtns.forEach(btn => btn.replaceWith(btn.cloneNode(true)));
this.pageSizeSelects = document.querySelectorAll('select[id^="page-size"]');
this.prevPageBtns = document.querySelectorAll('.prev-page');
this.nextPageBtns = document.querySelectorAll('.next-page');
this.nextPageBtns.forEach((btn) => {
btn.onclick = () => {
const totalPages = Math.ceil(filteredVersions.length / pageSize);
if (currentPage < totalPages) {
currentPage++;
updateUrlParams();
renderVersions();
}
};
});
this.prevPageBtns.forEach(btn => {
btn.onclick = () => {
if (currentPage > 1) {
currentPage--;
updateUrlParams();
renderVersions();
}
};
});
this.pageSizeSelects.forEach(select => {
select.addEventListener('change', (e) => {
pageSize = parseInt(e.target.value) || 50;
currentPage = 1;
updateUrlParams();
renderVersions();
});
});
}
update() {
const totalItems = filteredVersions.length;
const totalPages = totalItems === 0 ? 0 : Math.ceil(totalItems / pageSize);
this.pageInfos.forEach(info => {
info.textContent = totalItems === 0 ? '无数据' : `第 ${currentPage} 页,共 ${totalPages} 页`;
});
this.prevPageBtns.forEach(btn => btn.disabled = currentPage <= 1 || totalItems === 0);
this.nextPageBtns.forEach(btn => btn.disabled = currentPage >= totalPages);
this.pageSizeSelects.forEach(select => select.value = pageSize);
}
}
let pagination;
// 初始化
async function init() {
try {
const response = await fetch('versions.json');
minecraftVersions = await response.json();
loadUrlParams();
filterVersions(false);
renderTags();
bindEvents();
pagination = new Pagination();
renderVersions();
} catch (error) {
console.error('加载版本数据失败:', error);
const tbody = versionsContainer?.querySelector('tbody');
if (tbody) tbody.innerHTML = '<tr><td colspan="5">加载失败</td></tr>';
}
}
// 加载URL参数
function loadUrlParams() {
try {
const urlParams = new URLSearchParams(window.location.search);
const tagsParam = urlParams.get('tags');
if (tagsParam) currentFilters = tagsParam.split(',');
const searchParam = urlParams.get('search');
if (searchParam) {
currentSearch = searchParam;
searchInput.value = searchParam;
}
const pageParam = urlParams.get('page');
if (pageParam && !isNaN(parseInt(pageParam))) currentPage = parseInt(pageParam);
const sizeParam = urlParams.get('size');
if (sizeParam && !isNaN(parseInt(sizeParam))) pageSize = parseInt(sizeParam);
} catch (e) {}
}
// 更新URL参数
function updateUrlParams() {
try {
const urlParams = new URLSearchParams();
if (currentFilters.length > 0) urlParams.set('tags', currentFilters.join(','));
if (currentSearch) urlParams.set('search', currentSearch);
urlParams.set('page', currentPage);
urlParams.set('size', pageSize);
window.history.replaceState({}, '', `${window.location.pathname}?${urlParams}`);
} catch (e) {}
}
// 获取所有唯一标签
function getAllTags() {
const tagsSet = new Set();
minecraftVersions.forEach(v => {
if (v.tags) v.tags.forEach(t => tagsSet.add(t));
});
return Array.from(tagsSet);
}
// 渲染标签
function renderTags() {
if (!tagsContainer) return;
tagsContainer.innerHTML = '';
getAllTags().forEach(tag => {
const el = document.createElement('div');
el.className = `tag ${currentFilters.includes(tag) ? 'active' : ''}`;
el.textContent = tag;
el.addEventListener('click', () => toggleTag(tag));
tagsContainer.appendChild(el);
});
}
// 标签切换
function toggleTag(clickedTag) {
const getGroup = (tag) => {
if (TAG_GROUPS.main.includes(tag)) return 'main';
if (TAG_GROUPS.ancient.includes(tag)) return 'ancient';
return null;
};
const group = getGroup(clickedTag);
const isActive = currentFilters.includes(clickedTag);
if (isActive) {
currentFilters = currentFilters.filter(t => t !== clickedTag);
} else {
if (group) currentFilters = currentFilters.filter(t => !TAG_GROUPS.main.includes(t) && !TAG_GROUPS.ancient.includes(t));
currentFilters.push(clickedTag);
}
updateAndRender();
}
function updateAndRender() {
updateUrlParams();
renderTags();
filterVersions(true);
renderVersions();
}
// 过滤版本(彻底移除“跳过”)
function filterVersions(resetPage = true) {
filteredVersions = minecraftVersions.filter(version => {
if (version.name === "跳过") return false;
const matchTags = currentFilters.length === 0 || (version.tags && currentFilters.every(t => version.tags.includes(t)));
const kw = currentSearch.toLowerCase().trim();
const matchSearch = !kw || (version.name || "").toLowerCase().includes(kw) || (version.description || "").toLowerCase().includes(kw);
return matchTags && matchSearch;
});
if (resetPage) currentPage = 1;
return filteredVersions;
}
// 渲染版本
function renderVersions() {
if (!versionsContainer) return;
const tbody = versionsContainer.querySelector('tbody');
if (!tbody) return;
tbody.innerHTML = '';
if (filteredVersions.length === 0) {
tbody.innerHTML = '<tr><td colspan="5">无数据</td></tr>';
pagination?.update();
return;
}
const start = (currentPage - 1) * pageSize;
const end = start + pageSize;
const list = filteredVersions.slice(start, end);
list.forEach(v => tbody.appendChild(createVersionRow(v)));
pagination?.update();
}
// 创建行(移除跳过逻辑)
function createVersionRow(version) {
const row = document.createElement('tr');
const desc = (version.description || '').length > 30 ? (version.description.substring(0,30)+"...") : (version.description || '');
row.innerHTML = `
<td><strong>${version.name}</strong></td>
<td>${version.date || '-'}</td>
<td><span>${desc}</span> <button class="description-btn">详情</button></td>
<td>${(version.tags || []).map(t=>`<span class="version-tag">${t}</span>`).join('')}</td>
<td><button class="download-btn">查看</button></td>
`;
row.querySelector('.download-btn').onclick = () => openDownloadModal(version);
row.querySelector('.description-btn').onclick = () => openDescriptionModal(version);
return row;
}
// 弹窗
const downloadModal = document.getElementById('download-modal');
const descriptionModal = document.getElementById('description-modal');
function openDownloadModal(v) {
document.getElementById('modal-body').innerHTML = v.downloads
? Object.entries(v.downloads).map(([key, url]) => {
// 自动把 key 翻译成中文名称
const nameMap = {
client: "客户端",
server: "服务端",
json: "JSON文件",
wiki: "Wiki 页面",
minecraft: "官方文章"
};
const name = nameMap[key] || key;
return `<a href="${url}" target="_blank" class="download-link">${name}</a>`;
}).join('')
: "无链接";
downloadModal.classList.add('active');
}
function openDescriptionModal(v) {
document.getElementById('description-modal-body').textContent = v.description || "无描述";
descriptionModal.classList.add('active');
}
function closeDownloadModal() { downloadModal.classList.remove('active'); }
function closeDescriptionModalFunc() { descriptionModal.classList.remove('active'); }
// 绑定事件
function bindEvents() {
searchButton?.addEventListener('click', performSearch);
searchInput?.addEventListener('keypress', e=>e.key==='Enter'&&performSearch());
document.getElementById('close-modal')?.addEventListener('click', closeDownloadModal);
document.getElementById('close-description-modal')?.addEventListener('click', closeDescriptionModalFunc);
}
function performSearch() {
currentSearch = searchInput.value.trim();
updateUrlParams();
filterVersions(true);
renderVersions();
}
// 启动
init();