write-ins fully functioning

This commit is contained in:
iou1name 2018-07-20 20:20:14 -04:00
parent 6ae8adebad
commit d63ef1c484
4 changed files with 92 additions and 20 deletions

View File

@ -346,3 +346,26 @@ def vote(data):
data["option_id"] = option_id
data["polarity"] = polarity
emit("vote", data, room=room)
@socketio.on("write_in")
def write_in(data):
"""
Called when a user submits a write-in option.
"""
room = data.get("room")
post_id = data.get("post_id")
option_text = data.get("option_text")
if len(option_text) > 200:
return
option_text = bleach.clean(option_text)
option_text = "Write-in: " + option_text
option_id = db.insert_poll_option(post_id, option_text)
data = {}
data["post_id"] = post_id
data["post_type"] = "poll"
data["option_id"] = option_id
data["option_text"] = option_text
emit("update_post", data, room=room)

View File

@ -30,6 +30,7 @@ var tid = setInterval( function () {
post_str += 'pollPost active_post">';
}
post_str += '<div class="questPostMeta">' + strftime(data.date);
/* QM only */
if (data.post_type == 'text') {
post_str += '<br /><a href="javascript:void(0);" onclick="edit_post(\'' + data.post_id + '\')">Edit</a>';
post_str += '<a href="javascript:void(0);" id="savePost-' + data.post_id + '" onclick="save_post(\'' + data.post_id + '\')" style="display:none;">Save</a>';
@ -37,6 +38,7 @@ var tid = setInterval( function () {
post_str += '<br /><a href="javascript:void(0);" id="close_post_id-' + data.post_id + '" onclick="close_post(' + data.post_id + ')">Close</a>';
post_str += '<a href="javascript:void(0);" id="open_post_id-' + data.post_id + '" onclick="open_post(' + data.post_id + ')" style="display:none;">Open</a>'
}
/* end QM only */
post_str += '</div><div class="questPostData" id="questPostData-' + data.post_id + '">';
if (data.post_type == 'text') {
post_str += data.post;
@ -61,11 +63,24 @@ var tid = setInterval( function () {
socket.on('update_post', function(data) {
let post_id = data.post_id;
let post = document.getElementById('questPostData-' + post_id);
if (data.post_type == 'text') {
if (data.post_type === 'text') {
post.innerHTML = data.post;
}
else if (data.post_type == 'dice') {
} else if (data.post_type === 'dice') {
post.innerHTML += '<b>' + data.post + '</b><br />';
} else if (data.post_type === 'poll') {
let row = post.children[1].insertRow(-1);
let cell = row.insertCell(0);
cell.className = 'pollCheckBox';
cell.innerHTML = '<input type="checkbox" id="pollInput-' + data.options_id + '" onchange="pollVote(' + data.post_id + ',' + data.options_id + ')"/>';
cell.innerHTML += '<label for="pollInput-' + data.options_id + '"></label>';
cell = row.insertCell(1);
cell.className = 'option_text';
cell.innerHTML = data.option_text;
cell = row.insertCell(2);
cell.className = "optionVotes";
cell.innerHTML = "0";
}
});
socket.on('close_post', function(data) {
@ -131,6 +146,30 @@ 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});
}
function submitWritein(post_id) {
let writeinInput = document.getElementById('writeinInput');
if (!writeinInput) { return; }
let option_text = writeinInput.value;
writeinInput.value = '';
if (!option_text) { return; }
socket.emit('write_in', {option_text: option_text, post_id: post_id, room: room_id});
}
function deactivate_post() {
let post = document.getElementsByClassName('active_post');
if (post.length == 0) { return; }
post = post[0];
post.children[1].children[0].textContent = post.children[1].children[0].textContent.replace('Open', 'Closed');
post.classList.remove("active_post");
/* QM only */
post.children[0].children[2].outerHTML = "";
post.children[0].children[1].outerHTML = "";
/* end QM only */
let writein = document.getElementById('writeinContainer');
if (writein) {
writein.outerHTML = '';
}
}
/* QM only */
function makePost() {
let qparea = document.getElementById('postTextArea');
let text = qparea.value.trim();
@ -169,19 +208,6 @@ function close_post(post_id) {
function open_post(post_id) {
socket.emit('open_post', {post_id: post_id, room: room_id});
}
function deactivate_post() {
let post = document.getElementsByClassName('active_post');
if (post.length == 0) { return; }
post = post[0];
post.children[1].children[0].textContent = post.children[1].children[0].textContent.replace('Open', 'Closed');
post.classList.remove("active_post");
post.children[0].children[2].outerHTML = "";
post.children[0].children[1].outerHTML = "";
let writein = document.getElementById('writeinContainer');
if (writein) {
writein.outerHTML = '';
}
}
function insertPollOption() {
let opts = document.getElementById('pollOptions');
let num = opts.children.length+1;
@ -210,3 +236,4 @@ function openPostTab(event, modeName) {
document.getElementById(modeName).style.display = "block";
event.currentTarget.className += " active";
}
/* end QM only */

View File

@ -54,11 +54,24 @@ var tid = setInterval( function () {
socket.on('update_post', function(data) {
let post_id = data.post_id;
let post = document.getElementById('questPostData-' + post_id);
if (data.post_type == 'text') {
if (data.post_type === 'text') {
post.innerHTML = data.post;
}
else if (data.post_type == 'dice') {
} else if (data.post_type === 'dice') {
post.innerHTML += '<b>' + data.post + '</b><br />';
} else if (data.post_type === 'poll') {
let row = post.children[1].insertRow(-1);
let cell = row.insertCell(0);
cell.className = 'pollCheckBox';
cell.innerHTML = '<input type="checkbox" id="pollInput-' + data.options_id + '" onchange="pollVote(' + data.post_id + ',' + data.options_id + ')"/>';
cell.innerHTML += '<label for="pollInput-' + data.options_id + '"></label>';
cell = row.insertCell(1);
cell.className = 'option_text';
cell.innerHTML = data.option_text;
cell = row.insertCell(2);
cell.className = "optionVotes";
cell.innerHTML = "0";
}
});
socket.on('close_post', function(data) {
@ -124,6 +137,14 @@ 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});
}
function submitWritein(post_id) {
let writeinInput = document.getElementById('writeinInput');
if (!writeinInput) { return; }
let option_text = writeinInput.value;
writeinInput.value = '';
if (!option_text) { return; }
socket.emit('write_in', {option_text: option_text, post_id: post_id, room: room_id});
}
function deactivate_post() {
let post = document.getElementsByClassName('active_post');
if (post.length == 0) { return; }

View File

@ -75,7 +75,8 @@
</table>
{% if quest_post[0] == open_post_id and quest_post[0]|is_write_in %}
<div id="writeinContainer">
Write-in: <input type="text" id="writeinInput" placeholder="Custom choice..." maxlength="200" />
Write-in: <input type="text" id="writeinInput" placeholder="Custom choice..." maxlength="200" /><br />
<input type="submit" id="writeinSubmit" value="Submit" onclick="submitWritein({{ quest_post[0] }});"/>
</div>
{% endif %}
{% endif %}