Skip to content

Commit 2ae4502

Browse files
Allow to ❤️ on mobile scoreboard
Fixes #2762.
1 parent 6a6ae10 commit 2ae4502

File tree

2 files changed

+126
-68
lines changed

2 files changed

+126
-68
lines changed

webapp/public/js/domjudge.js

Lines changed: 111 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,29 @@ function getSelectedTeams()
207207
return JSON.parse(cookieVal);
208208
}
209209

210-
function getScoreboard()
210+
function getScoreboards(mobile)
211211
{
212-
var scoreboard = document.getElementsByClassName("scoreboard");
213-
if (scoreboard === null || scoreboard[0] === null || scoreboard[0] === undefined) {
212+
const scoreboards = document.getElementsByClassName("scoreboard");
213+
if (scoreboards === null || scoreboards[0] === null || scoreboards[0] === undefined) {
214214
return null;
215215
}
216-
return scoreboard[0].rows;
216+
let scoreboardRows = {};
217+
const mobileScoreboardClass = 'mobile-scoreboard';
218+
const desktopScoreboardClass = 'desktop-scoreboard';
219+
for (let i = 0; i < scoreboards.length; i++) {
220+
if (scoreboards[i].classList.contains(mobileScoreboardClass)) {
221+
scoreboardRows.mobile = scoreboards[i].rows;
222+
} else if (scoreboards[i].classList.contains(desktopScoreboardClass)) {
223+
scoreboardRows.desktop = scoreboards[i].rows;
224+
}
225+
}
226+
if (mobile === undefined) {
227+
return scoreboardRows;
228+
} else if (mobile) {
229+
return scoreboardRows.mobile;
230+
} else {
231+
return scoreboardRows.desktop;
232+
}
217233
}
218234

219235
function getRank(row)
@@ -226,7 +242,7 @@ function getHeartCol(row) {
226242
var td = null;
227243
// search for td before the team name
228244
for (var i = 1; i < 4; i++) {
229-
if (tds[i].className == "scoretn") {
245+
if (tds[i].classList.contains("scoretn")) {
230246
td = tds[i - 1];
231247
break;
232248
}
@@ -249,16 +265,28 @@ function getTeamname(row)
249265
return row.getAttribute("data-team-id");
250266
}
251267

252-
function toggle(id, show)
268+
function toggle(id, show, mobile)
253269
{
254-
var scoreboard = getScoreboard();
270+
var scoreboard = getScoreboards(mobile);
255271
if (scoreboard === null) return;
256272

273+
// Filter out all rows that do not have a data-team-id attribute or have
274+
// the class `scoreheader`.
275+
// The mobile scoreboard has them, and we need to ignore them.
276+
scoreboard = Array.from(scoreboard)
277+
.filter(
278+
row => row.getAttribute("data-team-id")
279+
|| row.classList.contains("scoreheader")
280+
);
281+
257282
var favTeams = getSelectedTeams();
258283
// count visible favourite teams (if filtered)
259284
var visCnt = 0;
260285
for (var i = 0; i < favTeams.length; i++) {
261286
for (var j = 0; j < scoreboard.length; j++) {
287+
if (!scoreboard[j].getAttribute("data-team-id")) {
288+
continue;
289+
}
262290
var scoreTeamname = getTeamname(scoreboard[j]);
263291
if (scoreTeamname === null) {
264292
continue;
@@ -297,69 +325,99 @@ function toggle(id, show)
297325
});
298326
}
299327

300-
function addHeart(rank, row, id, isFav)
328+
function addHeart(rank, row, id, isFav, mobile)
301329
{
302330
var heartCol = getHeartCol(row);
303331
var iconClass = isFav ? "fas fa-heart" : "far fa-heart";
304-
return heartCol.innerHTML + "<span class=\"heart " + iconClass + "\" onclick=\"toggle(" + id + "," + (isFav ? "false" : "true") + ")\"></span>";
332+
return heartCol.innerHTML + "<span class=\"heart " + iconClass + "\" onclick=\"toggle(" + id + "," + (isFav ? "false" : "true") + "," + mobile + ")\"></span>";
305333
}
306334

307335
function initFavouriteTeams()
308336
{
309-
var scoreboard = getScoreboard();
310-
if (scoreboard === null) {
337+
const scoreboards = getScoreboards();
338+
if (scoreboards === null) {
311339
return;
312340
}
313341

314342
var favTeams = getSelectedTeams();
315-
var toAdd = new Array();
316-
var cntFound = 0;
317-
var lastRank = 0;
318-
for (var j = 0; j < scoreboard.length; j++) {
319-
var found = false;
320-
var teamname = getTeamname(scoreboard[j]);
321-
if (teamname === null) {
322-
continue;
323-
}
324-
var firstCol = getRank(scoreboard[j]);
325-
var heartCol = getHeartCol(scoreboard[j]);
326-
var rank = firstCol.innerHTML;
327-
for (var i = 0; i < favTeams.length; i++) {
328-
if (teamname === favTeams[i]) {
329-
found = true;
330-
heartCol.innerHTML = addHeart(rank, scoreboard[j], j, found);
331-
toAdd[cntFound] = scoreboard[j].cloneNode(true);
332-
if (rank.trim().length === 0) {
333-
// make rank explicit in case of tie
334-
getRank(toAdd[cntFound]).innerHTML += lastRank;
343+
Object.keys(scoreboards).forEach(function(key) {
344+
var toAdd = new Array();
345+
var toAddMobile = new Array();
346+
var cntFound = 0;
347+
var lastRank = 0;
348+
const scoreboard = scoreboards[key];
349+
const mobile = key === 'mobile';
350+
let teamIndex = 1;
351+
for (var j = 0; j < scoreboard.length; j++) {
352+
var found = false;
353+
var teamname = getTeamname(scoreboard[j]);
354+
if (teamname === null) {
355+
continue;
356+
}
357+
var firstCol = getRank(scoreboard[j]);
358+
var heartCol = getHeartCol(scoreboard[j]);
359+
var rank = firstCol.innerHTML;
360+
for (var i = 0; i < favTeams.length; i++) {
361+
if (teamname === favTeams[i]) {
362+
found = true;
363+
heartCol.innerHTML = addHeart(rank, scoreboard[j], teamIndex, found, mobile);
364+
toAdd[cntFound] = scoreboard[j].cloneNode(true);
365+
if (mobile) {
366+
toAddMobile[cntFound] = scoreboard[j + 1].cloneNode(true);
367+
}
368+
if (rank.trim().length === 0) {
369+
// make rank explicit in case of tie
370+
getRank(toAdd[cntFound]).innerHTML += lastRank;
371+
}
372+
scoreboard[j].style.background = "lightyellow";
373+
const scoretn = scoreboard[j].querySelector('.scoretn');
374+
if (scoretn && scoretn.classList.contains('cl_FFFFFF')) {
375+
scoretn.classList.remove('cl_FFFFFF');
376+
scoretn.classList.add('cl_FFFFE0');
377+
}
378+
if (mobile) {
379+
scoreboard[j + 1].style.background = "lightyellow";
380+
}
381+
cntFound++;
382+
break;
335383
}
336-
scoreboard[j].style.background = "lightyellow";
337-
cntFound++;
338-
break;
339384
}
385+
if (!found) {
386+
heartCol.innerHTML = addHeart(rank, scoreboard[j], teamIndex, found, mobile);
387+
}
388+
if (rank !== "") {
389+
lastRank = rank;
390+
}
391+
392+
teamIndex++;
340393
}
341-
if (!found) {
342-
heartCol.innerHTML = addHeart(rank, scoreboard[j], j, found);
343-
}
344-
if (rank !== "") {
345-
lastRank = rank;
346-
}
347-
}
348394

349-
// copy favourite teams to the top of the scoreboard
350-
for (var i = 0; i < cntFound; i++) {
351-
var copy = toAdd[i];
352-
var style = "";
353-
if (i === 0) {
354-
style += "border-top: 2px solid black;";
395+
let addCounter = 1;
396+
const copyRow = function (i, copy, addTopBorder, addBottomBorder, noMiddleBorder) {
397+
let style = "";
398+
if (noMiddleBorder) {
399+
style += "border-bottom-width: 0;";
400+
}
401+
if (addTopBorder && i === 0) {
402+
style += "border-top: 2px solid black;";
403+
}
404+
if (addBottomBorder && i === cntFound - 1) {
405+
style += "border-bottom: thick solid black;";
406+
}
407+
copy.setAttribute("style", style);
408+
const tbody = scoreboard[1].parentNode;
409+
tbody.insertBefore(copy, scoreboard[addCounter]);
410+
addCounter++;
355411
}
356-
if (i === cntFound - 1) {
357-
style += "border-bottom: thick solid black;";
412+
413+
// copy favourite teams to the top of the scoreboard
414+
for (let i = 0; i < cntFound; i++) {
415+
copyRow(i, toAdd[i], true, !mobile, mobile);
416+
if (mobile) {
417+
copyRow(i, toAddMobile[i], false, true, false);
418+
}
358419
}
359-
copy.setAttribute("style", style);
360-
var tbody = scoreboard[1].parentNode;
361-
tbody.insertBefore(copy, scoreboard[i + 1]);
362-
}
420+
});
363421
}
364422

365423
// This function is a specific addition for using DOMjudge within a

webapp/templates/partials/scoreboard_table.html.twig

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</style>
2929
{% endif %}
3030
31-
<table class="d-none d-md-table scoreboard center {% if jury %}scoreboard_jury{% endif %}">
31+
<table class="d-none d-md-table scoreboard desktop-scoreboard center {% if jury %}scoreboard_jury{% endif %}">
3232
3333
{% set teamColspan = 2 %}
3434
{% if showAffiliationLogos %}
@@ -38,22 +38,22 @@
3838
{# output table column groups (for the styles) #}
3939
<colgroup>
4040
{% if enable_ranking %}
41-
<col id="scorerankmobile"/>
41+
<col id="scorerank"/>
4242
{% endif %}
4343
{% if showFlags %}
44-
<col id="scoreflagsmobile"/>
44+
<col id="scoreflags"/>
4545
{% else %}
4646
<col/>
4747
{% endif %}
4848
{% if showAffiliationLogos %}
49-
<col id="scorelogosmobile"/>
49+
<col id="scorelogos"/>
5050
{% endif %}
51-
<col id="scoreteamnamemobile"/>
51+
<col id="scoreteamname"/>
5252
</colgroup>
5353
{% if enable_ranking %}
5454
<colgroup>
55-
<col id="scoresolvmobile"/>
56-
<col id="scoretotalmobile"/>
55+
<col id="scoresolv"/>
56+
<col id="scoretotal"/>
5757
</colgroup>
5858
{% endif %}
5959
<colgroup>
@@ -106,7 +106,7 @@
106106
<tbody>
107107
{% set previousSortOrder = -1 %}
108108
{% set previousTeam = null %}
109-
{% set backgroundColors = {"#FFFFFF": 1} %}
109+
{% set backgroundColors = {"#FFFFFF": 1, '#FFFFE0': 1} %}
110110
{% set medalCount = 0 %}
111111
{% for score in scores %}
112112
{% set classes = [] %}
@@ -315,25 +315,25 @@
315315
</tbody>
316316
</table>
317317
318-
<table class="d-md-none scoreboard center {% if jury %}scoreboard_jury{% endif %}">
318+
<table class="d-md-none scoreboard mobile-scoreboard center {% if jury %}scoreboard_jury{% endif %}">
319319
{# output table column groups (for the styles) #}
320320
<colgroup>
321321
{% if enable_ranking %}
322-
<col id="scorerank"/>
322+
<col id="scorerankmobile"/>
323323
{% endif %}
324324
{% if showFlags %}
325-
<col id="scoreflags"/>
325+
<col id="scoreflagsmobile"/>
326326
{% else %}
327327
<col/>
328328
{% endif %}
329329
{% if showAffiliationLogos %}
330-
<col id="scorelogos"/>
330+
<col id="scorelogosmobile"/>
331331
{% endif %}
332-
<col id="scoreteamname"/>
332+
<col id="scoreteamnamemobile"/>
333333
</colgroup>
334334
{% if enable_ranking %}
335335
<colgroup>
336-
<col id="scoresolv"/>
336+
<col id="scoresolvmobile"/>
337337
</colgroup>
338338
{% endif %}
339339
<thead>
@@ -356,7 +356,7 @@
356356
<tbody>
357357
{% set previousSortOrder = -1 %}
358358
{% set previousTeam = null %}
359-
{% set backgroundColors = {"#FFFFFF": 1} %}
359+
{% set backgroundColors = {"#FFFFFF": 1, '#FFFFE0': 1} %}
360360
{% set medalCount = 0 %}
361361
{% for score in scores %}
362362
{% set classes = [] %}

0 commit comments

Comments
 (0)