Skip to content

Commit 82405b1

Browse files
Move scoreboard javascript to JS file so browser can cache it.
1 parent 3b907ea commit 82405b1

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

webapp/public/js/domjudge.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,3 +824,52 @@ function setupPreviewClarification($input, $previewDiv, previewInitial) {
824824
$(function () {
825825
$('[data-toggle="tooltip"]').tooltip();
826826
});
827+
828+
// Make sure the items in the desktop scoreboard fit
829+
document.querySelectorAll(".desktop-scoreboard .forceWidth:not(.toolong)").forEach(el => {
830+
if (el instanceof Element && el.scrollWidth > el.offsetWidth) {
831+
el.classList.add("toolong");
832+
}
833+
});
834+
835+
/**
836+
* Helper method to resize mobile team names and problem badges
837+
*/
838+
function resizeMobileTeamNamesAndProblemBadges() {
839+
// Make team names fit on the screen, but only when the mobile
840+
// scoreboard is visible
841+
const mobileScoreboard = document.querySelector('.mobile-scoreboard');
842+
if (mobileScoreboard.offsetWidth === 0) {
843+
return;
844+
}
845+
const windowWidth = document.body.offsetWidth;
846+
const teamNameMaxWidth = Math.max(10, windowWidth - 150);
847+
const problemBadgesMaxWidth = Math.max(10, windowWidth - 78);
848+
document.querySelectorAll(".mobile-scoreboard .forceWidth:not(.toolong)").forEach(el => {
849+
el.classList.remove("toolong");
850+
el.style.maxWidth = teamNameMaxWidth + 'px';
851+
if (el instanceof Element && el.scrollWidth > el.offsetWidth) {
852+
el.classList.add("toolong");
853+
} else {
854+
el.classList.remove("toolong");
855+
}
856+
});
857+
document.querySelectorAll(".mobile-scoreboard .mobile-problem-badges:not(.toolong)").forEach(el => {
858+
el.classList.remove("toolong");
859+
el.style.maxWidth = problemBadgesMaxWidth + 'px';
860+
if (el instanceof Element && el.scrollWidth > el.offsetWidth) {
861+
el.classList.add("toolong");
862+
const scale = el.offsetWidth / el.scrollWidth;
863+
const offset = -1 * (el.scrollWidth - el.offsetWidth) / 2;
864+
el.style.transform = `scale(${scale}) translateX(${offset}px)`;
865+
} else {
866+
el.classList.remove("toolong");
867+
el.style.transform = null;
868+
}
869+
});
870+
}
871+
872+
if (document.querySelector('.mobile-scoreboard')) {
873+
window.addEventListener('resize', resizeMobileTeamNamesAndProblemBadges);
874+
resizeMobileTeamNamesAndProblemBadges();
875+
}

webapp/templates/partials/scoreboard_table.html.twig

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -587,47 +587,3 @@
587587
}
588588
{% endfor %}
589589
</style>
590-
<script>
591-
document.querySelectorAll(".desktop-scoreboard .forceWidth:not(.toolong)").forEach(el => {
592-
if (el instanceof Element && el.scrollWidth > el.offsetWidth) {
593-
el.classList.add("toolong");
594-
}
595-
});
596-
window.resizeMobileTeamNames = function() {
597-
// Make team names fit on the screen, but only when the mobile
598-
// scoreboard is visible
599-
const mobileScoreboard = document.querySelector('.mobile-scoreboard');
600-
if (mobileScoreboard.offsetWidth === 0) {
601-
return;
602-
}
603-
const windowWidth = document.body.offsetWidth;
604-
const teamNameMaxWidth = Math.max(10, windowWidth - 150);
605-
const problemBadgesMaxWidth = Math.max(10, windowWidth - 78);
606-
document.querySelectorAll(".mobile-scoreboard .forceWidth:not(.toolong)").forEach(el => {
607-
el.classList.remove("toolong");
608-
el.style.maxWidth = teamNameMaxWidth + 'px';
609-
if (el instanceof Element && el.scrollWidth > el.offsetWidth) {
610-
el.classList.add("toolong");
611-
} else {
612-
el.classList.remove("toolong");
613-
}
614-
});
615-
document.querySelectorAll(".mobile-scoreboard .mobile-problem-badges:not(.toolong)").forEach(el => {
616-
el.classList.remove("toolong");
617-
el.style.maxWidth = problemBadgesMaxWidth + 'px';
618-
if (el instanceof Element && el.scrollWidth > el.offsetWidth) {
619-
el.classList.add("toolong");
620-
const scale = el.offsetWidth / el.scrollWidth;
621-
const offset = -1 * (el.scrollWidth - el.offsetWidth) / 2;
622-
el.style.transform = `scale(${scale}) translateX(${offset}px)`;
623-
} else {
624-
el.classList.remove("toolong");
625-
el.style.transform = null;
626-
}
627-
});
628-
};
629-
window.addEventListener('resize', () => {
630-
resizeMobileTeamNames();
631-
});
632-
resizeMobileTeamNames();
633-
</script>

webapp/templates/public/scoreboard.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
{% if static and refresh is defined %}
5757
disableRefreshOnModal();
5858
{% endif %}
59-
resizeMobileTeamNames();
59+
resizeMobileTeamNamesAndProblemBadges();
6060
};
6161
6262
{% if static and refresh is defined %}

0 commit comments

Comments
 (0)