Skip to content

Commit 500d276

Browse files
Fix hearts on mobile
1 parent 371b975 commit 500d276

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

webapp/public/js/domjudge.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -301,27 +301,20 @@ function getScoreboards(mobile)
301301

302302
function getRank(row)
303303
{
304-
return row.getElementsByTagName("td")[0];
304+
return row.querySelector('.rank');
305305
}
306306

307307
function getHeartCol(row) {
308-
var tds = row.getElementsByTagName("td");
309-
var td = null;
310-
// search for td before the team name
311-
for (var i = 1; i < 4; i++) {
312-
if (tds[i].classList.contains("scoretn")) {
313-
td = tds[i - 1];
308+
const tds = row.getElementsByTagName("td");
309+
let td = null;
310+
// search for td to store the hearts
311+
for (let i = 1; i < 5; i++) {
312+
if (tds[i] && tds[i].classList.contains("heart")) {
313+
td = tds[i];
314314
break;
315315
}
316316
}
317-
if (td === null) {
318-
td = tds[1];
319-
}
320-
if (td !== null) {
321-
return td;
322-
}
323-
324-
return null;
317+
return td;
325318
}
326319

327320
function getTeamname(row)
@@ -394,10 +387,10 @@ function toggle(id, show, mobile)
394387
});
395388
}
396389

397-
function getHeartTd(rank, row, id, isFav, mobile)
390+
function getHeart(rank, row, id, isFav, mobile)
398391
{
399392
var iconClass = isFav ? "fas fa-heart" : "far fa-heart";
400-
return "<td class=\"scoreaf heart\"><span class=\"heart " + iconClass + "\" onclick=\"toggle(" + id + "," + (isFav ? "false" : "true") + "," + mobile + ")\"></span></td>";
393+
return "<span class=\"heart " + iconClass + "\" onclick=\"toggle(" + id + "," + (isFav ? "false" : "true") + "," + mobile + ")\"></span>";
401394
}
402395

403396
function initFavouriteTeams()
@@ -422,22 +415,32 @@ function initFavouriteTeams()
422415
if (teamname === null) {
423416
continue;
424417
}
425-
var firstCol = getRank(scoreboard[j]);
418+
let rankElement;
419+
if (mobile) {
420+
rankElement = getRank(scoreboard[j + 1]);
421+
} else {
422+
rankElement = getRank(scoreboard[j]);
423+
}
426424
var heartCol = getHeartCol(scoreboard[j]);
427-
var rank = firstCol.innerHTML;
425+
if (!heartCol) {
426+
continue;
427+
}
428+
var rank = rankElement.innerHTML.trim();
428429
for (var i = 0; i < favTeams.length; i++) {
429430
if (teamname === favTeams[i]) {
430431
found = true;
431-
let heartTd = getHeartTd(rank, scoreboard[j], teamIndex, found, mobile);
432-
// Insert td after heartCol
433-
heartCol.insertAdjacentHTML('afterend', heartTd);
432+
heartCol.innerHTML = getHeart(rank, scoreboard[j], teamIndex, found, mobile);
434433
toAdd[cntFound] = scoreboard[j].cloneNode(true);
435434
if (mobile) {
436435
toAddMobile[cntFound] = scoreboard[j + 1].cloneNode(true);
437436
}
438-
if (rank.trim().length === 0) {
437+
if (rank.length === 0) {
439438
// make rank explicit in case of tie
440-
getRank(toAdd[cntFound]).innerHTML += lastRank;
439+
if (mobile) {
440+
getRank(toAddMobile[cntFound]).innerHTML += lastRank;
441+
} else {
442+
getRank(toAdd[cntFound]).innerHTML += lastRank;
443+
}
441444
}
442445
scoreboard[j].style.background = "lightyellow";
443446
const scoretn = scoreboard[j].querySelector('.scoretn');
@@ -453,8 +456,7 @@ function initFavouriteTeams()
453456
}
454457
}
455458
if (!found) {
456-
let heartTd = getHeartTd(rank, scoreboard[j], teamIndex, found, mobile);
457-
heartCol.insertAdjacentHTML('afterend', heartTd);
459+
heartCol.innerHTML = getHeart(rank, scoreboard[j], teamIndex, found, mobile);
458460
}
459461
if (rank !== "") {
460462
lastRank = rank;

webapp/templates/partials/scoreboard_table.html.twig

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
{% else %}
5252
<col/>
5353
{% endif %}
54-
{% if public %}
55-
<col id="scorehearts"/>
56-
{% endif %}
5754
{% if showAffiliationLogos %}
5855
<col id="scorelogos"/>
5956
{% endif %}
57+
{% if public %}
58+
<col id="scorehearts"/>
59+
{% endif %}
6060
<col id="scoreteamname"/>
6161
</colgroup>
6262
{% if enable_ranking %}
@@ -196,6 +196,9 @@
196196
{% endif %}
197197
</td>
198198
{% endif %}
199+
{% if public %}
200+
<td class="scoreaf heart"></td>
201+
{% endif %}
199202
{% if color is null %}
200203
{% set color = "#FFFFFF" %}
201204
{% set colorClass = "_FFFFFF" %}
@@ -345,6 +348,9 @@
345348
{% if showAffiliationLogos %}
346349
<col id="scorelogosmobile"/>
347350
{% endif %}
351+
{% if public %}
352+
<col id="scoreheartmobile"/>
353+
{% endif %}
348354
<col id="scoreteamnamemobile"/>
349355
</colgroup>
350356
{% if enable_ranking %}
@@ -358,6 +364,9 @@
358364
{% if showAffiliationLogos %}
359365
{% set teamColspan = teamColspan + 1 %}
360366
{% endif %}
367+
{% if public %}
368+
{% set teamColspan = teamColspan + 1 %}
369+
{% endif %}
361370
362371
<tr class="scoreheader" data-static="{{ static }}" style="font-size: 75%;">
363372
{% if enable_ranking %}
@@ -436,6 +445,9 @@
436445
{% endif %}
437446
</td>
438447
{% endif %}
448+
{% if public %}
449+
<td class="scoreaf heart"></td>
450+
{% endif %}
439451
{% if color is null %}
440452
{% set color = "#FFFFFF" %}
441453
{% set colorClass = "_FFFFFF" %}
@@ -489,7 +501,7 @@
489501
<tr style="height: 32px;">
490502
<td>
491503
{# Only print rank when score is different from the previous team #}
492-
<span class="d-block me-2">
504+
<span class="d-block me-2 rank">
493505
{% if not displayRank %}
494506
?
495507
{% elseif previousTeam is null or scoreboard.scores[previousTeam.teamid].rank != score.rank %}
@@ -502,7 +514,10 @@
502514
{% if showAffiliationLogos %}
503515
{% set problemSpan = 3 %}
504516
{% else %}
505-
{% set problemSpan = 2 %}
517+
{% set problemSpan = 2 %}
518+
{% endif %}
519+
{% if public %}
520+
{% set problemSpan = problemSpan + 1 %}
506521
{% endif %}
507522
<td colspan="{{ problemSpan }}">
508523
{% for problem in problems %}

0 commit comments

Comments
 (0)