bep40 commited on
Commit
fe9bf66
·
verified ·
1 Parent(s): aa67f6b

Frontend: render fixtures from JSON (group tables, stadium, VN time, live scores)"

Browse files
Files changed (1) hide show
  1. static/wc2026_v2.js +59 -34
static/wc2026_v2.js CHANGED
@@ -1,4 +1,4 @@
1
- // === WORLD CUP 2026 - Render + LIVE ===
2
  let _wc2026Data = null;
3
  let _wcRefreshInterval = null;
4
 
@@ -20,47 +20,71 @@ function switchWCTab(tab) {
20
  else if (tab === 'stats') renderWCStats(el);
21
  }
22
 
23
- function _disableLinks(root){ root.querySelectorAll('a').forEach(a => a.addEventListener('click', e => e.preventDefault())); }
24
- function _cleanupBox(root){
25
- root.querySelectorAll('[style]').forEach(x => { if (x.tagName !== 'IMG') x.removeAttribute('style'); });
26
- root.querySelectorAll('[bgcolor]').forEach(x => x.removeAttribute('bgcolor'));
27
- root.querySelectorAll('[color]').forEach(x => x.removeAttribute('color'));
28
- root.querySelectorAll('img').forEach(img => { img.onerror = function(){ this.style.display='none'; }; });
29
- _disableLinks(root);
30
- }
31
-
32
  function renderWCFixtures(el) {
33
  const d = _wc2026Data.fixtures || {};
34
- if (d.html && d.html.length > 100) {
35
- el.innerHTML = `<div class="wc-source-note">📅 Lịch thi đấu World Cup 2026 - Nguồn: ${esc(d.source || 'Tiền Phong')}</div><div class="wc-data-box">${d.html}</div>`;
36
- const box = el.querySelector('.wc-data-box'); if (box) _cleanupBox(box);
37
  return;
38
  }
39
- if (d.images && d.images.length) {
40
- let h = `<div class="wc-source-note">📅 Lịch thi đấu World Cup 2026 - Nguồn: ${esc(d.source || 'Tiền Phong')}</div><div class="wc-fixture-images">`;
41
- d.images.forEach(img => {
42
- h += `<img src="${esc(img.src)}" alt="${esc(img.alt||'Lịch thi đấu World Cup 2026')}" loading="lazy" onerror="this.style.display='none'">`;
43
- if (img.alt) h += `<div class="wc-img-caption">${esc(img.alt)}</div>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  });
45
- h += '</div>';
46
- el.innerHTML = h;
47
- return;
 
 
 
 
 
 
 
 
 
 
 
48
  }
49
- el.innerHTML = '<div class="loading">Chưa tải được lịch thi đấu World Cup 2026</div>';
50
  }
51
 
52
- function _wcRenderHTML(el, data, emptyMsg) {
53
- const html = data?.html || '';
54
- if (html && html.length > 100) {
55
- el.innerHTML = '<div class="wc-data-box">' + html + '</div>';
56
- const box = el.querySelector('.wc-data-box'); if (box) _cleanupBox(box);
 
57
  } else {
58
- el.innerHTML = `<div class="loading">${emptyMsg}</div>`;
59
  }
60
  }
61
- function renderWCStandings(el) { _wcRenderHTML(el, _wc2026Data.standings, 'Chưa tải được BXH World Cup 2026'); }
62
- function renderWCStats(el) { _wcRenderHTML(el, _wc2026Data.stats, 'Chưa có thống kê'); }
63
 
 
64
  function renderWCNews(el) {
65
  const news = _wc2026Data.news || [];
66
  const road = _wc2026Data.road || [];
@@ -70,18 +94,19 @@ function renderWCNews(el) {
70
  if (ni < news.length) combined.push({...news[ni++], _type: 'news'});
71
  if (ri < road.length) combined.push({...road[ri++], _type: 'road'});
72
  }
73
- if (!combined.length) { el.innerHTML = '<div class="loading">Đang cập nhật tin tức...</div>'; return; }
74
  let h = '<div class="wc-news-grid">';
75
- combined.slice(0, 24).forEach(a => {
76
  const badge = a._type === 'road' ? '<span class="badge badge-wc">Đường tới WC</span>' : '<span class="badge badge-fpt">Tin WC</span>';
77
  h += `<div class="wc-news-item" onclick="readArticle('${esc(a.link)}')">`;
78
- h += `<div class="wc-news-img">${a.img ? `<img src="${esc(a.img)}" onerror="this.parentElement.style.display='none'">` : ''}</div>`;
79
  h += `<div class="wc-news-text">${badge}<div class="wc-news-title">${esc(a.title)}</div><div class="wc-news-via">${esc(a.source || '')}</div></div></div>`;
80
  });
81
  h += '</div>';
82
  el.innerHTML = h;
83
  }
84
 
 
85
  function startWCLiveRefresh() {
86
  if (_wcRefreshInterval) clearInterval(_wcRefreshInterval);
87
  _wcRefreshInterval = setInterval(async () => {
@@ -90,6 +115,6 @@ function startWCLiveRefresh() {
90
  const data = await fetch('/api/wc2026').then(r => r.json()).catch(() => null);
91
  if (data) { _wc2026Data = data; }
92
  } catch(e) {}
93
- }, 90000);
94
  }
95
  setTimeout(startWCLiveRefresh, 5000);
 
1
+ // === WORLD CUP 2026 - Full render ===
2
  let _wc2026Data = null;
3
  let _wcRefreshInterval = null;
4
 
 
20
  else if (tab === 'stats') renderWCStats(el);
21
  }
22
 
23
+ // === FIXTURES (JSON beautiful table by group) ===
 
 
 
 
 
 
 
 
24
  function renderWCFixtures(el) {
25
  const d = _wc2026Data.fixtures || {};
26
+ const matches = d.matches || [];
27
+ if (!matches.length) {
28
+ el.innerHTML = '<div class="loading">Đang tải lịch thi đấu...</div>';
29
  return;
30
  }
31
+ // Group by 'group' field
32
+ const groups = {};
33
+ matches.forEach(m => {
34
+ const g = m.group || 'Vòng loại trực tiếp';
35
+ if (!groups[g]) groups[g] = [];
36
+ groups[g].push(m);
37
+ });
38
+
39
+ let h = '<div class="wc-fix-container">';
40
+ h += `<div class="wc-fix-header">📅 Lịch thi đấu World Cup 2026 <span>(${d.total || matches.length} trận · Giờ Việt Nam)</span></div>`;
41
+
42
+ for (const [group, gMatches] of Object.entries(groups)) {
43
+ h += `<div class="wc-fix-group">${esc(group)}</div>`;
44
+ h += '<table class="wc-fix-table"><thead><tr><th>#</th><th>Giờ VN</th><th>Đội nhà</th><th>Tỉ số</th><th>Đội khách</th><th>Sân</th></tr></thead><tbody>';
45
+ gMatches.forEach(m => {
46
+ const score = (m.home_score !== null && m.away_score !== null)
47
+ ? `<span class="wc-score-live">${m.home_score} - ${m.away_score}</span>`
48
+ : '<span class="wc-score-vs">vs</span>';
49
+ const isLive = m.home_score !== null && m.away_score !== null;
50
+ h += `<tr class="${isLive ? 'wc-row-played' : ''}">`;
51
+ h += `<td class="wc-td-num">${m.match_number}</td>`;
52
+ h += `<td class="wc-td-time">${esc(m.date_vn)}</td>`;
53
+ h += `<td class="wc-td-team wc-td-home">${esc(m.home)}</td>`;
54
+ h += `<td class="wc-td-score">${score}</td>`;
55
+ h += `<td class="wc-td-team">${esc(m.away)}</td>`;
56
+ h += `<td class="wc-td-venue">${esc(m.location)}</td>`;
57
+ h += '</tr>';
58
  });
59
+ h += '</tbody></table>';
60
+ }
61
+ h += '</div>';
62
+ el.innerHTML = h;
63
+ }
64
+
65
+ // === STANDINGS (bongda.com.vn HTML) ===
66
+ function renderWCStandings(el) {
67
+ const d = _wc2026Data.standings || {};
68
+ if (d.html && d.html.length > 100) {
69
+ el.innerHTML = '<div class="wc-data-box">' + d.html + '</div>';
70
+ el.querySelectorAll('a').forEach(a => a.addEventListener('click', e => e.preventDefault()));
71
+ } else {
72
+ el.innerHTML = '<div class="loading">Chưa có BXH (giải chưa bắt đầu)</div>';
73
  }
 
74
  }
75
 
76
+ // === STATS ===
77
+ function renderWCStats(el) {
78
+ const d = _wc2026Data.stats || {};
79
+ if (d.html && d.html.length > 100) {
80
+ el.innerHTML = '<div class="wc-data-box">' + d.html + '</div>';
81
+ el.querySelectorAll('a').forEach(a => a.addEventListener('click', e => e.preventDefault()));
82
  } else {
83
+ el.innerHTML = '<div class="loading">Chưa có thống kê (giải chưa bắt đầu)</div>';
84
  }
85
  }
 
 
86
 
87
+ // === NEWS (with images) ===
88
  function renderWCNews(el) {
89
  const news = _wc2026Data.news || [];
90
  const road = _wc2026Data.road || [];
 
94
  if (ni < news.length) combined.push({...news[ni++], _type: 'news'});
95
  if (ri < road.length) combined.push({...road[ri++], _type: 'road'});
96
  }
97
+ if (!combined.length) { el.innerHTML = '<div class="loading">Đang cập nhật...</div>'; return; }
98
  let h = '<div class="wc-news-grid">';
99
+ combined.slice(0, 20).forEach(a => {
100
  const badge = a._type === 'road' ? '<span class="badge badge-wc">Đường tới WC</span>' : '<span class="badge badge-fpt">Tin WC</span>';
101
  h += `<div class="wc-news-item" onclick="readArticle('${esc(a.link)}')">`;
102
+ if (a.img) h += `<div class="wc-news-img"><img src="${esc(a.img)}" onerror="this.style.display='none'"></div>`;
103
  h += `<div class="wc-news-text">${badge}<div class="wc-news-title">${esc(a.title)}</div><div class="wc-news-via">${esc(a.source || '')}</div></div></div>`;
104
  });
105
  h += '</div>';
106
  el.innerHTML = h;
107
  }
108
 
109
+ // === LIVE REFRESH ===
110
  function startWCLiveRefresh() {
111
  if (_wcRefreshInterval) clearInterval(_wcRefreshInterval);
112
  _wcRefreshInterval = setInterval(async () => {
 
115
  const data = await fetch('/api/wc2026').then(r => r.json()).catch(() => null);
116
  if (data) { _wc2026Data = data; }
117
  } catch(e) {}
118
+ }, 60000); // 60s for live score updates
119
  }
120
  setTimeout(startWCLiveRefresh, 5000);