From 21f762385936b326dc0ce60da81bb96d0a0d9e49 Mon Sep 17 00:00:00 2001 From: aymm Date: Sun, 28 Mar 2021 13:15:54 +0200 Subject: [PATCH] Added vote button en/disabling, remaining vote count --- scripts/poll.js | 35 ++++++++++++++++++++++++++++++++--- styles/style.css | 30 +++++++++++++++++++++++++++++- view_poll.php | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/scripts/poll.js b/scripts/poll.js index 09ee8a1..750cfb7 100644 --- a/scripts/poll.js +++ b/scripts/poll.js @@ -1,10 +1,39 @@ -window.addEventListener('DOMContentLoaded', (event) => { +window.addEventListener('DOMContentLoaded', () => { displayLocalTimestamps(); + for (const el of document.querySelectorAll('.option input[type=checkbox]')) { + el.onclick = updateVotesRemaining; + } }); function displayLocalTimestamps() { for (const el of document.getElementsByTagName('time')) { - let dateObj = new Date(el.getAttribute('datetime')); - el.innerText = dateObj.toLocaleString(); + 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; + } + 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; } \ No newline at end of file diff --git a/styles/style.css b/styles/style.css index 9dc67ca..8acb4d8 100644 --- a/styles/style.css +++ b/styles/style.css @@ -3,7 +3,8 @@ --main-bg-color: rgb(48, 48, 48); --secondary-bg-color: rgba(10, 10, 10, 0.7); --main-fg-color: white; - --main-accent-color: rgb(253, 122, 0) + --main-accent-color: rgb(253, 122, 0); + --disabled-color: gray; } @supports (color: color(display-p3 1 1 1)) { :root { @@ -28,6 +29,29 @@ body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Cantarell, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; } +button { + background-color: var(--main-accent-color); + outline: 1px solid var(--main-fg-color); + color: var(--main-fg-color); + cursor: pointer; + border: none; + transition: all 0.2s; +} + +button:hover:not(:disabled) { + box-shadow: 4px 6px 3px 0 var(--secondary-bg-color); + transform: translate(-2px, -2px); +} + +button:disabled { + background-color: var(--disabled-color); + cursor: not-allowed; +} + +.hidden { + display: none; +} + /* Header */ header { display: flex; @@ -93,3 +117,7 @@ datewrapper time { grid-template-columns: auto 1fr; grid-column-gap: 8px; } + +.votes-remaining { + font-size: small; +} diff --git a/view_poll.php b/view_poll.php index d4b4e39..8538636 100644 --- a/view_poll.php +++ b/view_poll.php @@ -42,6 +42,15 @@ $disabled = $poll->canVote() ? '' : 'disabled'; $user_name = $poll->user->name ?? ''; $created_at = $poll->created_at; $closed_at = $poll->closed_at; +$user_votes = $poll->getMyVotes(); + +$votes_remaining = $poll->max_options - count($user_votes); +$votes_remaining_plural = $votes_remaining === 1 ? '' : 's'; +$votes_remaining_text = "$votes_remaining Vote$votes_remaining_plural remaining"; +$votes_remaining_hidden = $poll->canVote() ? '' : ' hidden'; +$data_can_vote = $poll->canVote() ? 'true' : 'false'; +$disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled'; + ?>
@@ -64,9 +73,16 @@ $closed_at = $poll->closed_at;
- prompt ?> + + prompt ?>
+ + +
+
closed_at; foreach ($poll->options as $option) { $checked = $option->is_your_response ? 'checked' : ''; ?>
- /> - text . ' (' . $option->respondents . ')'?> -
-
+ /> + text . ' (' . $option->respondents . ')'?> +
+
respondent_ids as $res_id) { $u = $api->getUser($res_id, $user_args); ?> - + -
+
+ +