Skip to content

Commit f856218

Browse files
committed
fix: sync seasons and years for fair medals pages & correct the +50 medals problem of display
1 parent 6351fcc commit f856218

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

website/js/fair-distribution.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@
239239
flex-shrink: 0;
240240
}
241241
242+
.fair-medal-overflow {
243+
font-size: 12px;
244+
font-weight: 800;
245+
color: rgba(255,255,255,0.7);
246+
align-self: flex-end;
247+
margin-left: 2px;
248+
}
249+
242250
.fair-note {
243251
text-align: center;
244252
margin-top: 22px;
@@ -385,23 +393,34 @@
385393
function bindControls() {
386394
d3.select("#fair-season").on("change", function () {
387395
state.season = this.value;
396+
state.rankingSeason = this.value;
388397

389398
const years = getAvailableYears(state.season);
390399
if (!years.includes(state.year)) {
391400
state.year = years[years.length - 1] || null;
392401
}
402+
if (!years.includes(state.rankingYear)) {
403+
state.rankingYear = years[years.length - 1] || null;
404+
}
393405

394406
updateCountriesAfterSeasonOrYearChange();
395407
populateControls();
396408
render();
409+
410+
const rankingSeasonEl = document.getElementById("fair-ranking-season");
411+
if (rankingSeasonEl) rankingSeasonEl.value = this.value;
397412
});
398413

399414
d3.select("#fair-year").on("change", function () {
400415
state.year = Number(this.value);
416+
state.rankingYear = Number(this.value);
401417

402418
updateCountriesAfterSeasonOrYearChange();
403419
populateControls();
404420
render();
421+
422+
const rankingYearEl = document.getElementById("fair-ranking-year");
423+
if (rankingYearEl) rankingYearEl.value = this.value;
405424
});
406425

407426
d3.select("#fair-country-1").on("change", function () {
@@ -773,6 +792,7 @@
773792
grid.append("div").html(renderCountryCard(state.country1, data1, maxScale));
774793
grid.append("div").html(renderCountryCard(state.country2, data2, maxScale));
775794
renderRankingTable();
795+
requestAnimationFrame(measureIconOverflow);
776796
}
777797

778798
function renderMedalIcons(value, color) {
@@ -783,12 +803,44 @@
783803
const full = Array(fullCount).fill(0).map(() =>
784804
`<div class="fair-medal-icon" style="background:${color}"></div>`
785805
).join("");
786-
787806
const half = hasHalf
788807
? `<div class="fair-medal-half-wrap"><div class="fair-medal-icon" style="background:${color}"></div></div>`
789808
: "";
790809

791-
return `<div class="fair-medal-icons">${full}${half}</div>`;
810+
return `<div class="fair-medal-icons" data-total="${rounded}">${full}${half}</div>`;
811+
}
812+
813+
function measureIconOverflow() {
814+
document.querySelectorAll(".fair-medal-icons").forEach(container => {
815+
container.querySelectorAll(".fair-medal-overflow").forEach(el => el.remove());
816+
817+
const ROW_HEIGHT = 16;
818+
const GAP = 3;
819+
const MAX_ROWS = 2;
820+
const maxH = MAX_ROWS * ROW_HEIGHT + (MAX_ROWS - 1) * GAP;
821+
822+
if (container.scrollHeight <= maxH + 1) return;
823+
824+
const total = parseFloat(container.dataset.total) || 0;
825+
const iconW = ROW_HEIGHT + GAP;
826+
const containerW = container.clientWidth;
827+
const iconsPerRow = Math.max(1, Math.floor((containerW + GAP) / iconW));
828+
const maxVisible = iconsPerRow * MAX_ROWS;
829+
830+
const icons = Array.from(container.children).filter(
831+
el => el.classList.contains("fair-medal-icon") || el.classList.contains("fair-medal-half-wrap")
832+
);
833+
834+
icons.slice(maxVisible).forEach(el => el.style.display = "none");
835+
836+
const hidden = Math.ceil(total - maxVisible);
837+
if (hidden > 0) {
838+
const label = document.createElement("span");
839+
label.className = "fair-medal-overflow";
840+
label.textContent = `+${hidden}`;
841+
container.appendChild(label);
842+
}
843+
});
792844
}
793845

794846
function renderCountryCard(country, data, maxScale) {
@@ -951,16 +1003,40 @@
9511003

9521004
d3.select("#fair-ranking-season").on("change", function () {
9531005
state.rankingSeason = this.value;
1006+
state.season = this.value;
1007+
9541008
const newYears = getAvailableYears(state.rankingSeason);
9551009
state.rankingYear = newYears.includes(state.rankingYear)
9561010
? state.rankingYear
9571011
: newYears[newYears.length - 1];
1012+
if (!newYears.includes(state.year)) {
1013+
state.year = newYears[newYears.length - 1] || null;
1014+
}
1015+
9581016
renderRankingTable();
1017+
1018+
const fairSeasonEl = document.getElementById("fair-season");
1019+
if (fairSeasonEl) {
1020+
fairSeasonEl.value = this.value;
1021+
updateCountriesAfterSeasonOrYearChange();
1022+
populateControls();
1023+
render();
1024+
}
9591025
});
9601026

9611027
d3.select("#fair-ranking-year").on("change", function () {
9621028
state.rankingYear = Number(this.value);
1029+
state.year = Number(this.value);
1030+
9631031
renderRankingTable();
1032+
1033+
const fairYearEl = document.getElementById("fair-year");
1034+
if (fairYearEl) {
1035+
fairYearEl.value = this.value;
1036+
updateCountriesAfterSeasonOrYearChange();
1037+
populateControls();
1038+
render();
1039+
}
9641040
});
9651041

9661042
// Click highlight country

0 commit comments

Comments
 (0)