2021-03-28 11:15:54 +00:00
|
|
|
window.addEventListener('DOMContentLoaded', () => {
|
2021-03-27 19:37:17 +00:00
|
|
|
displayLocalTimestamps();
|
2021-03-28 11:15:54 +00:00
|
|
|
for (const el of document.querySelectorAll('.option input[type=checkbox]')) {
|
|
|
|
el.onclick = updateVotesRemaining;
|
|
|
|
}
|
2021-03-27 19:37:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function displayLocalTimestamps() {
|
|
|
|
for (const el of document.getElementsByTagName('time')) {
|
2021-03-28 11:15:54 +00:00
|
|
|
const dateObj = new Date(el.getAttribute('datetime'));
|
|
|
|
const relDate = compareDateToToday(dateObj);
|
|
|
|
el.innerText = relDate === 0
|
|
|
|
? `Today, ${dateObj.toLocaleTimeString()}`
|
|
|
|
: dateObj.toLocaleDateString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareDateToToday(date) {
|
|
|
|
const today = new Date();
|
|
|
|
if (today.getDate() === date.getDate()
|
|
|
|
&& today.getMonth() === date.getMonth()
|
|
|
|
&& date.getFullYear() === today.getFullYear()) {
|
|
|
|
return 0;
|
2021-03-27 19:37:17 +00:00
|
|
|
}
|
2021-03-28 11:15:54 +00:00
|
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
return today > date ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateVotesRemaining() {
|
|
|
|
const numChecked = document.querySelectorAll('.option input[type=checkbox]:checked').length;
|
|
|
|
const votesRemainingElement = document.querySelector('.votes-remaining');
|
|
|
|
const total = votesRemainingElement.dataset.maxVotes;
|
|
|
|
const remaining = Math.max(0, total - numChecked);
|
|
|
|
const plural = remaining === 1 ? '' : 's';
|
|
|
|
votesRemainingElement.innerText = `${remaining} Vote${plural} remaining`;
|
|
|
|
const voteButton = document.querySelector('button[name=submit_vote]');
|
|
|
|
const canVoteInitial = voteButton.dataset.canVote === 'true';
|
|
|
|
voteButton.disabled = !canVoteInitial || (total - numChecked) < 0 || numChecked === 0;
|
2021-03-27 19:37:17 +00:00
|
|
|
}
|