poll tables sort by most votes when new vote is received

This commit is contained in:
iou1name 2018-07-20 21:21:54 -04:00
parent d63ef1c484
commit 7f94b5d045
3 changed files with 29 additions and 0 deletions

View File

@ -308,6 +308,7 @@ def poll_post(data):
data["post_type"] = "poll"
data["date"] = int(time.time())
data["options"] = new_options
data["allow_writein"] = allow_writein
emit("new_post", data, room=room)
@ -316,6 +317,7 @@ def vote(data):
"""
Called when a user changes their vote on a poll.
"""
# TODO: ensure poll is open before counting vote
room = data.get("room")
option_id = data.get("option_id")
post_id = data.get("post_id")

View File

@ -56,6 +56,11 @@ var tid = setInterval( function () {
post_str += '<td class="optionVotes">0</td></tr>';
}
post_str += '</table>';
if (data.allow_writein) {
post_str += '<div id="writeinContainer">';
post_str += 'Write-in: <input type="text" id="writeinInput" placeholder="Custom choice..." maxlength="200" /><br />';
post_str += '<input type="submit" id="writeinSubmit" value="Submit" onclick="submitWritein({{ quest_post[0] }});"/></div>';
}
}
post_str += '</div></div><br />';
qposts.innerHTML = qposts.innerHTML + post_str;
@ -118,6 +123,14 @@ var tid = setInterval( function () {
} else {
row.cells[2].textContent = Number(row.cells[2].textContent) - 1;
}
let table = row.parentElement.parentElement;
arr = Array.prototype.slice.call(table.rows);
arr.sort(sort_by_votes);
let new_tbody = document.createElement('tbody');
for (let i = 0; i < arr.length; i++) {
new_tbody.appendChild(arr[i]);
}
table.replaceChild(new_tbody, table.children[1]);
});
socket.on('toggle_option_box', function(data) {
document.getElementById('pollInput-' + data.option_id).checked = data.polarity;
@ -142,6 +155,9 @@ function strftime(date) {
date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds());
return date_str;
}
function sort_by_votes(a, b) {
return b.cells[2].textContent.localeCompare(a.cells[2].textContent);
}
function pollVote(post_id, option_id) {
let polarity = document.getElementById('pollInput-' + option_id).checked;
socket.emit('vote', {post_id: post_id, option_id: option_id, polarity: polarity, room: room_id});

View File

@ -109,6 +109,14 @@ var tid = setInterval( function () {
} else {
row.cells[2].textContent = Number(row.cells[2].textContent) - 1;
}
let table = row.parentElement.parentElement;
arr = Array.prototype.slice.call(table.rows);
arr.sort(sort_by_votes);
let new_tbody = document.createElement('tbody');
for (let i = 0; i < arr.length; i++) {
new_tbody.appendChild(arr[i]);
}
table.replaceChild(new_tbody, table.children[1]);
});
socket.on('toggle_option_box', function(data) {
document.getElementById('pollInput-' + data.option_id).checked = data.polarity;
@ -133,6 +141,9 @@ function strftime(date) {
date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds());
return date_str;
}
function sort_by_votes(a, b) {
return b.cells[2].textContent.localeCompare(a.cells[2].textContent);
}
function pollVote(post_id, option_id) {
let polarity = document.getElementById('pollInput-' + option_id).checked;
socket.emit('vote', {post_id: post_id, option_id: option_id, polarity: polarity, room: room_id});