-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
341 lines (293 loc) · 10.9 KB
/
script.js
File metadata and controls
341 lines (293 loc) · 10.9 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
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
// 全局变量
let products = [];
let filteredProducts = [];
let currentLang = 'en';
let translations = {};
let currentSort = {
column: 'capacity',
direction: 'desc'
};
// DOM 元素
const typeFilter = document.getElementById('typeFilter');
const capacityFilter = document.getElementById('capacityFilter');
const sortBy = document.getElementById('sortBy');
const priceTableBody = document.getElementById('priceTableBody');
const langButtons = document.querySelectorAll('.lang-btn');
const tableHeaders = document.querySelectorAll('#priceTable th');
const lastUpdateElement = document.getElementById('lastUpdate');
// 初始化
async function init() {
try {
// 加载数据
const [productsResponse, translationsResponse] = await Promise.all([
fetch('data/products.json'),
fetch('data/languages.json')
]);
const productsData = await productsResponse.json();
translations = await translationsResponse.json();
products = productsData.products;
filteredProducts = [...products];
// 更新最后更新时间
updateLastUpdateTime(productsData.lastUpdate);
// 设置语言切换事件监听器
langButtons.forEach(btn => {
btn.addEventListener('click', () => {
const lang = btn.dataset.lang;
switchLanguage(lang);
});
});
// 设置表头排序事件监听器
setupTableSorting();
// 渲染表格
renderTable();
// 添加事件监听器
typeFilter.addEventListener('change', filterProducts);
capacityFilter.addEventListener('change', filterProducts);
sortBy.addEventListener('change', handleDropdownSort);
// 添加产品结构化数据
addProductStructuredData();
} catch (error) {
console.error('加载数据失败:', error);
}
}
// 渲染表格
function renderTable() {
priceTableBody.innerHTML = '';
filteredProducts.forEach(product => {
const row = document.createElement('tr');
// 添加所有列,优化容量显示格式
row.innerHTML = `
<td><a href="${product.url}" class="product-link" target="_blank" rel="noopener noreferrer" title="${product.title}">${product.title}</a></td>
<td class="price">$${product.pricePerTB.toFixed(2)}</td>
<td class="price">$${product.price.toFixed(2)}</td>
<td class="capacity">${product.capacity}</td>
<td>${product.warranty}</td>
<td>${product.formFactor}</td>
<td>${product.technology}</td>
<td>${product.condition}</td>
`;
priceTableBody.appendChild(row);
});
}
// 更新最后更新时间
function updateLastUpdateTime(timestamp) {
const date = new Date(timestamp);
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZoneName: 'short'
};
lastUpdateElement.textContent = date.toLocaleDateString(currentLang === 'zh' ? 'zh-CN' : 'en-US', options);
}
// 添加产品结构化数据
function addProductStructuredData() {
const productStructuredData = {
"@context": "https://schema.org",
"@type": "ItemList",
"itemListElement": filteredProducts.map((product, index) => ({
"@type": "ListItem",
"position": index + 1,
"item": {
"@type": "Product",
"name": product.title,
"description": `${product.capacity} ${product.type} ${product.technology}`,
"brand": {
"@type": "Brand",
"name": product.title.split(' ')[0]
},
"offers": {
"@type": "Offer",
"price": product.price,
"priceCurrency": "USD",
"itemCondition": `https://schema.org/${product.condition}`,
"availability": "https://schema.org/InStock"
}
}
}))
};
// 添加结构化数据到页面
const script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(productStructuredData);
document.head.appendChild(script);
}
// 设置表头排序
function setupTableSorting() {
const sortableColumns = {
0: 'title',
1: 'pricePerTB',
2: 'price',
3: 'capacity',
4: 'warranty',
5: 'formFactor',
6: 'technology',
7: 'condition'
};
tableHeaders.forEach((header, index) => {
if (index in sortableColumns) {
header.addEventListener('click', () => {
const column = sortableColumns[index];
handleHeaderSort(column, header);
});
}
});
}
// 处理表头排序
function handleHeaderSort(column, header) {
// 清除其他表头的排序状态
tableHeaders.forEach(h => {
h.classList.remove('sorted-asc', 'sorted-desc');
});
// 切换排序方向
if (currentSort.column === column) {
currentSort.direction = currentSort.direction === 'asc' ? 'desc' : 'asc';
} else {
currentSort.column = column;
currentSort.direction = 'asc';
}
// 更新表头样式
header.classList.add(`sorted-${currentSort.direction}`);
// 更新下拉框选项
updateSortDropdown(column, currentSort.direction);
// 排序并重新渲染
sortProducts();
}
// 更新排序下拉框
function updateSortDropdown(column, direction) {
const sortMap = {
'price': {'asc': 'price-asc', 'desc': 'price-desc'},
'pricePerTB': {'asc': 'pricePerTB-asc', 'desc': 'pricePerTB-desc'},
'capacity': {'asc': 'capacity-asc', 'desc': 'capacity-desc'}
};
if (column in sortMap) {
sortBy.value = sortMap[column][direction];
}
}
// 处理下拉框排序
function handleDropdownSort() {
const [column, direction] = sortBy.value.split('-');
currentSort.column = column;
currentSort.direction = direction;
// 更新表头样式
tableHeaders.forEach(header => {
header.classList.remove('sorted-asc', 'sorted-desc');
if (header.textContent === translations[currentLang].table[column]) {
header.classList.add(`sorted-${direction}`);
}
});
sortProducts();
}
// 切换语言
function switchLanguage(lang) {
currentLang = lang;
document.documentElement.lang = lang;
// 更新语言按钮状态
langButtons.forEach(btn => {
btn.classList.toggle('active', btn.dataset.lang === lang);
});
// 更新界面文本
updateUIText();
// 重新渲染表格以更新"查看详情"按钮文本
renderTable();
}
// 更新界面文本
function updateUIText() {
// 更新标题
document.querySelector('h1').textContent = translations[currentLang].title;
// 更新筛选器
if (currentLang === 'zh') {
typeFilter.innerHTML = `
<option value="">所有类型</option>
<option value="Tape">磁带</option>
<option value="LTO-9">LTO-9</option>
<option value="LTO-8">LTO-8</option>
<option value="LTO-6">LTO-6</option>
`;
capacityFilter.innerHTML = `
<option value="">所有容量</option>
<option value="18 TB">18 TB</option>
<option value="12 TB">12 TB</option>
<option value="2.5 TB">2.5 TB</option>
<option value="1.4 GB">1.4 GB</option>
<option value="1 GB">1 GB</option>
<option value="650 MB">650 MB</option>
`;
sortBy.innerHTML = `
<option value="price-asc">价格 (低 → 高)</option>
<option value="price-desc">价格 (高 → 低)</option>
<option value="pricePerTB-asc">每TB价格 (低 → 高)</option>
<option value="pricePerTB-desc">每TB价格 (高 → 低)</option>
<option value="capacity-asc">容量 (低 → 高)</option>
<option value="capacity-desc">容量 (高 → 低)</option>
`;
} else {
typeFilter.innerHTML = `
<option value="">All Types</option>
<option value="Tape">Tape</option>
<option value="LTO-9">LTO-9</option>
<option value="LTO-8">LTO-8</option>
<option value="LTO-6">LTO-6</option>
`;
capacityFilter.innerHTML = `
<option value="">All Capacities</option>
<option value="18 TB">18 TB</option>
<option value="12 TB">12 TB</option>
<option value="2.5 TB">2.5 TB</option>
<option value="1.4 GB">1.4 GB</option>
<option value="1 GB">1 GB</option>
<option value="650 MB">650 MB</option>
`;
sortBy.innerHTML = `
<option value="price-asc">Price (Low to High)</option>
<option value="price-desc">Price (High to Low)</option>
<option value="pricePerTB-asc">Price per TB (Low to High)</option>
<option value="pricePerTB-desc">Price per TB (High to Low)</option>
<option value="capacity-asc">Capacity (Low to High)</option>
<option value="capacity-desc">Capacity (High to Low)</option>
`;
}
// 更新表头
tableHeaders.forEach((header, index) => {
const columns = ['productName', 'pricePerTB', 'price', 'capacity', 'warranty', 'formFactor', 'technology', 'condition'];
if (index < columns.length) {
header.textContent = translations[currentLang].table[columns[index]];
}
});
}
// 筛选产品
function filterProducts() {
const selectedType = typeFilter.value;
const selectedCapacity = capacityFilter.value;
filteredProducts = products.filter(product => {
const typeMatch = !selectedType ||
(selectedType === 'Tape' ? product.formFactor === 'Tape' : product.technology === selectedType);
const capacityMatch = !selectedCapacity || product.capacity === selectedCapacity;
return typeMatch && capacityMatch;
});
sortProducts();
}
// 排序产品
function sortProducts() {
const { column, direction } = currentSort;
filteredProducts.sort((a, b) => {
let comparison = 0;
switch (column) {
case 'price':
case 'pricePerTB':
comparison = a[column] - b[column];
break;
case 'capacity':
comparison = parseFloat(a.capacity) - parseFloat(b.capacity);
break;
default:
comparison = String(a[column]).localeCompare(String(b[column]));
}
return direction === 'asc' ? comparison : -comparison;
});
renderTable();
}
// 初始化应用
init();