-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
393 lines (362 loc) · 17.9 KB
/
script.js
File metadata and controls
393 lines (362 loc) · 17.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
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', async function() {
// Load data from JSON files
const [textContent, timelineData, halvingSchedule, halvingEventsForChart, yearlyPriceData] = await Promise.all([
fetch('data/textContent.json').then(r => r.json()),
fetch('data/timelineData.json').then(r => r.json()),
fetch('data/halvingSchedule.json').then(r => r.json()),
fetch('data/halvingEvents.json').then(r => r.json()),
fetch('data/yearlyPriceData.json').then(r => r.json())
]);
let currentLang = 'th';
let activeFilter = 'all';
let athChart, halvingChart;
let countdownInterval;
// ประมาณการวันที่ Halving ครั้งถัดไป (ครั้งที่ 5)
// โดยประมาณ 4 ปีหลังจาก Halving ครั้งที่ 4
const nextHalvingDate = new Date('2028-04-20T00:00:00Z'); // ใช้เวลา UTC เพื่อความแม่นยำ
const langThBtn = document.getElementById('lang-th');
const langEnBtn = document.getElementById('lang-en');
const timelineContainer = document.getElementById('timeline-container');
const timelineFiltersContainer = document.getElementById('timeline-filters');
const bannerImage = document.getElementById('banner'); // อ้างอิงถึง element รูปภาพแบนเนอร์
const currentRewardEl = document.getElementById('current-reward');
const nextHalvingNumberEl = document.getElementById('next-halving-number');
const nextRewardEl = document.getElementById('next-reward');
const daysEl = document.getElementById('days');
const hoursEl = document.getElementById('hours');
const minutesEl = document.getElementById('minutes');
const secondsEl = document.getElementById('seconds');
const halvingSummaryEl = document.getElementById('halving-summary'); // New element for summary
function updateTextContent() {
document.querySelectorAll('[data-key]').forEach(el => {
const key = el.getAttribute('data-key');
if (textContent[currentLang][key]) {
el.innerHTML = el.dataset.key === 'halving_chart_title' ? `<span class="mr-2">⚒</span>${textContent[currentLang][key]}` : textContent[currentLang][key];
el.classList.toggle('font-th', currentLang === 'th');
el.classList.toggle('font-en', currentLang === 'en');
}
});
document.documentElement.lang = currentLang;
document.title = currentLang === 'th' ? 'Bitcoin และการเดินทาง | ไทม์ไลน์ประวัติศาสตร์ Bitcoin' : 'Bitcoin Odyssey | Bitcoin History Timeline';
// อัปเดต src ของรูปภาพแบนเนอร์ตามภาษา
if (bannerImage) {
bannerImage.src = textContent[currentLang].banner_image;
}
// อัปเดตข้อความสำหรับ Halving Countdown
updateHalvingInfo();
updateHalvingSummary(); // Call new function to update summary
}
function updateHalvingInfo() {
const latestHalving = halvingSchedule[halvingSchedule.length - 1];
const currentReward = latestHalving.reward;
const nextHalvingNum = latestHalving.number + 1;
const nextReward = currentReward / 2;
if (currentRewardEl) currentRewardEl.textContent = `${currentReward} BTC`;
if (nextHalvingNumberEl) nextHalvingNumberEl.textContent = `${nextHalvingNum}`;
if (nextRewardEl) nextRewardEl.textContent = `${nextReward} BTC`;
}
function updateHalvingSummary() {
if (!halvingSummaryEl) return;
let summaryHtml = `<p class="text-lg font-semibold mb-2">${textContent[currentLang].halving_summary_title}</p>`;
summaryHtml += `<p class="mb-2">${textContent[currentLang].halving_summary_total}</p>`;
summaryHtml += `<p class="mb-1">${textContent[currentLang].halving_summary_details_prefix}</p>`;
summaryHtml += `<ul class="list-disc list-inside ml-4">`;
// Clone halvingEventsForChart and add the next halving (5th) for summary
const halvingEvents = [...halvingEventsForChart];
const latestHalving = halvingSchedule[halvingSchedule.length - 1];
const nextHalvingNum = latestHalving.number + 1;
const nextReward = latestHalving.reward / 2;
halvingEvents.push({
label: `${nextHalvingNum}${nextHalvingNum === 1 ? 'st' : nextHalvingNum === 2 ? 'nd' : nextHalvingNum === 3 ? 'rd' : 'th'} Halving (${nextHalvingDate.getFullYear()}) (คาดการณ์)`,
reward: nextReward
});
halvingEvents.forEach(event => {
summaryHtml += `<li class="text-sm md:text-base">${event.label}: ${event.reward} BTC</li>`;
});
summaryHtml += `</ul>`;
halvingSummaryEl.innerHTML = summaryHtml;
}
function updateCountdown() {
const now = new Date().getTime();
const distance = nextHalvingDate.getTime() - now;
if (distance < 0) {
clearInterval(countdownInterval);
if (daysEl) daysEl.textContent = '00';
if (hoursEl) hoursEl.textContent = '00';
if (minutesEl) minutesEl.textContent = '00';
if (secondsEl) secondsEl.textContent = '00';
return;
}
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (daysEl) daysEl.textContent = String(days).padStart(2, '0');
if (hoursEl) hoursEl.textContent = String(hours).padStart(2, '0');
if (minutesEl) minutesEl.textContent = String(minutes).padStart(2, '0');
if (secondsEl) secondsEl.textContent = String(seconds).padStart(2, '0');
}
function createTimeline() {
timelineContainer.innerHTML = '<div class="timeline-line"></div>'; // Reset
timelineData.sort((a,b) => new Date(a.date) - new Date(b.date)).forEach((item, index) => {
const isLeft = index % 2 === 0;
const itemDiv = document.createElement('div');
itemDiv.className = `timeline-item mb-8 flex justify-between items-center w-full ${isLeft ? 'flex-row-reverse left-timeline' : 'right-timeline'}`;
itemDiv.dataset.type = item.type;
const contentClass = `w-5/12 p-4 md:p-6 bg-white rounded-xl shadow-md border border-gray-100 transition-transform transform hover:scale-105`;
itemDiv.innerHTML = `
<div class="order-1 ${contentClass}">
<p class="mb-2 text-sm font-bold ${currentLang === 'th' ? 'font-th' : 'font-en'}" style="color: #E59500;">${new Date(item.date).toLocaleDateString(currentLang === 'th' ? 'th-TH' : 'en-US', { year: 'numeric', month: 'long', day: 'numeric' })}</p>
<h3 class="mb-2 font-bold text-lg md:text-xl text-gray-800 ${currentLang === 'th' ? 'font-th' : 'font-en'}">${item.title[currentLang]}</h3>
<p class="text-sm md:text-base text-gray-600 ${currentLang === 'th' ? 'font-th' : 'font-en'}">${item.description[currentLang]}</p>
</div>
<div class="order-1 w-1/12 flex justify-center">
<div class="timeline-point w-4 h-4 rounded-full"></div>
</div>
<div class="order-1 w-5/12"></div>
`;
timelineContainer.appendChild(itemDiv);
});
applyFilter();
}
function createFilters() {
const filters = {
all: { th: 'ทั้งหมด', en: 'All' },
milestone: { th: 'เหตุการณ์สำคัญ', en: 'Milestones' },
halving: { th: 'Halving', en: 'Halvings' },
ath: { th: 'ราคาสูงสุด', en: 'ATHs' },
crisis: { th: 'วิกฤต', en: 'Crises' },
adoption: { th: 'การยอมรับ', en: 'Adoption' },
fork: { th: 'Fork', en: 'Forks' },
upgrade: { th: 'อัปเกรด', en: 'Upgrades' }
};
timelineFiltersContainer.innerHTML = '';
Object.keys(filters).forEach(key => {
const button = document.createElement('button');
button.dataset.filter = key;
button.textContent = filters[key][currentLang];
button.className = `btn-filter px-4 py-2 rounded-full text-sm md:text-base font-semibold transition-all duration-300 shadow-sm ${currentLang === 'th' ? 'font-th' : 'font-en'} ${activeFilter === key ? 'active' : 'bg-white text-gray-600 hover:bg-gray-100'}`;
button.addEventListener('click', () => {
activeFilter = key;
document.querySelectorAll('.btn-filter').forEach(btn => {
btn.classList.remove('active');
btn.classList.add('bg-white', 'text-gray-600', 'hover:bg-gray-100');
});
button.classList.add('active');
button.classList.remove('bg-white', 'text-gray-600', 'hover:bg-gray-100');
applyFilter();
});
timelineFiltersContainer.appendChild(button);
});
}
function applyFilter() {
document.querySelectorAll('.timeline-item').forEach(item => {
if (activeFilter === 'all' || item.dataset.type === activeFilter) {
item.classList.remove('filtered-out');
} else {
item.classList.add('filtered-out');
}
});
}
function createAthChart() {
const ctx = document.getElementById('athChart').getContext('2d');
const chartData = yearlyPriceData;
const data = {
labels: chartData.map(d => d.year), // Show year on x-axis
datasets: [{
label: textContent[currentLang].price_label,
data: chartData.map(d => d.price), // Use price data
borderColor: '#E59500',
backgroundColor: 'rgba(229, 149, 0, 0.1)',
fill: true,
tension: 0.4,
pointBackgroundColor: '#E59500',
pointBorderColor: '#fff',
pointHoverRadius: 7,
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: '#E59500',
pointRadius: 5
}]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: false,
type: 'logarithmic',
ticks: {
callback: function(value, index, values) {
return '$' + value.toLocaleString();
},
font: { family: "'Inter', sans-serif" }
},
grid: {
color: 'rgba(0, 0, 0, 0.05)'
}
},
x: {
display: true,
ticks: {
font: { family: "'Inter', sans-serif" }
},
grid: {
display: false
}
}
},
plugins: {
legend: { display: false },
tooltip: {
callbacks: {
title: function(context) {
// Show the year as the title of the tooltip
return textContent[currentLang].year_label + ': ' + context[0].label;
},
label: function(context) {
const dataPoint = yearlyPriceData[context.dataIndex];
let labels = [];
labels.push(`${context.dataset.label}: $${context.parsed.y.toLocaleString()}`);
if (dataPoint.key_price_description && dataPoint.key_price_description[currentLang]) {
labels.push(`${textContent[currentLang].key_price_label}: ${dataPoint.key_price_description[currentLang]}`);
}
return labels;
}
},
titleFont: { family: "'Inter', sans-serif" },
bodyFont: { family: "'Inter', sans-serif" },
backgroundColor: '#3D3D3D',
titleMarginBottom: 10,
padding: 15,
cornerRadius: 8
}
}
}
};
if (athChart) athChart.destroy();
athChart = new Chart(ctx, config);
}
function createHalvingChart() {
const ctx = document.getElementById('halvingChart').getContext('2d');
const now = new Date(); // Get current date for comparison
// Clone halvingEventsForChart and add the next halving (5th)
const halvingEvents = [...halvingEventsForChart];
const latestHalving = halvingSchedule[halvingSchedule.length - 1];
const nextHalvingNum = latestHalving.number + 1;
const nextReward = latestHalving.reward / 2;
halvingEvents.push({
label: `${nextHalvingNum}${nextHalvingNum === 1 ? 'st' : nextHalvingNum === 2 ? 'nd' : nextHalvingNum === 3 ? 'rd' : 'th'} Halving (${nextHalvingDate.getFullYear()})`,
reward: nextReward,
date: nextHalvingDate.toISOString()
});
const backgroundColors = halvingEvents.map(d => {
return new Date(d.date) <= now ? 'rgba(229, 149, 0, 0.8)' : 'rgba(150, 150, 150, 0.5)';
});
const borderColors = halvingEvents.map(d => {
return new Date(d.date) <= now ? 'rgb(229, 149, 0)' : 'rgb(150, 150, 150)';
});
const data = {
labels: halvingEvents.map(d => d.label),
datasets: [{
label: textContent[currentLang].block_reward_label,
data: halvingEvents.map(d => d.reward),
backgroundColor: backgroundColors,
borderColor: borderColors,
borderWidth: 2,
borderRadius: 8,
hoverBorderWidth: 3,
}]
};
const config = {
type: 'bar',
data: data,
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: textContent[currentLang].block_reward_label,
font: { family: "'Inter', sans-serif", size: 14 }
},
ticks: { font: { family: "'Inter', sans-serif" } },
grid: { color: 'rgba(0, 0, 0, 0.05)' }
},
x: {
ticks: { font: { family: "'Inter', sans-serif" } },
grid: { display: false }
}
},
plugins: {
legend: { display: false },
tooltip: {
callbacks: {
label: function(context) {
return `${context.dataset.label}: ${context.parsed.y} BTC`;
}
},
titleFont: { family: "'Inter', sans-serif" },
bodyFont: { family: "'Inter', sans-serif" },
backgroundColor: '#3D3D3D',
titleMarginBottom: 10,
padding: 15,
cornerRadius: 8
}
}
}
};
if(halvingChart) halvingChart.destroy();
halvingChart = new Chart(ctx, config);
}
function initializeApp() {
updateTextContent();
createTimeline();
createFilters();
createAthChart();
createHalvingChart();
// Clear any existing interval before setting a new one
if (countdownInterval) {
clearInterval(countdownInterval);
}
updateCountdown(); // Call once immediately to avoid delay
countdownInterval = setInterval(updateCountdown, 1000); // Update every second
}
// Move language switcher to the right and update color logic
const langSwitcher = document.querySelector('nav .flex.items-center');
if (langSwitcher) {
langSwitcher.classList.add('justify-end', 'w-full');
langSwitcher.classList.remove('space-x-2');
}
// Initial color set
function updateLangBtnColor() {
// Always reset both buttons first
langThBtn.classList.remove('text-[#E59500]', 'text-gray-400');
langEnBtn.classList.remove('text-[#E59500]', 'text-gray-400');
if (currentLang === 'th') {
langThBtn.classList.add('text-[#E59500]');
langEnBtn.classList.add('text-gray-400');
} else {
langEnBtn.classList.add('text-[#E59500]');
langThBtn.classList.add('text-gray-400');
}
}
// Add new language switch events
langThBtn.addEventListener('click', () => {
currentLang = 'th';
initializeApp();
updateLangBtnColor();
});
langEnBtn.addEventListener('click', () => {
currentLang = 'en';
initializeApp();
updateLangBtnColor();
});
// Set initial color on load
updateLangBtnColor();
initializeApp();
});