From 7f94b5d04577a821ef77b1712ee02d26e8af9f5c Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 20 Jul 2018 21:21:54 -0400 Subject: [PATCH] poll tables sort by most votes when new vote is received --- events.py | 2 ++ static/anonkunQM.js | 16 ++++++++++++++++ static/anonkunUser.js | 11 +++++++++++ 3 files changed, 29 insertions(+) diff --git a/events.py b/events.py index ab45b52..7d48893 100644 --- a/events.py +++ b/events.py @@ -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") diff --git a/static/anonkunQM.js b/static/anonkunQM.js index 4874ae1..567e45d 100644 --- a/static/anonkunQM.js +++ b/static/anonkunQM.js @@ -56,6 +56,11 @@ var tid = setInterval( function () { post_str += '0'; } post_str += ''; + if (data.allow_writein) { + post_str += '
'; + post_str += 'Write-in:
'; + post_str += '
'; + } } post_str += '
'; 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}); diff --git a/static/anonkunUser.js b/static/anonkunUser.js index c375b84..f1080c4 100644 --- a/static/anonkunUser.js +++ b/static/anonkunUser.js @@ -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});