added writeins

This commit is contained in:
iou1name 2018-09-05 08:41:53 -04:00
parent 1652b23590
commit 6117fbe1e7
5 changed files with 54 additions and 21 deletions

View File

@ -353,6 +353,36 @@ def vote(socket, data):
socket.send('vote', data)
def write_in(socket, data):
"""
Called when a player creates a new write-in.
"""
post_id = data.get('post_id')
option_text = data.get('option_text', '')
user = socket.scope['user']
option_text = option_text.strip()
if not option_text:
return
option_text = "Write in: " + bleach.clean(option_text)
if len(option_text) > 200:
# error message?
return
p = Poll.objects.get(post_id=post_id)
o = PollOption(
poll=p,
text=option_text
)
o.save()
data = {}
data['post_id'] = post_id
data['post_type'] = 'poll'
data['option_id'] = o.id
data['option_text'] = option_text
socket.send('update_post', data)
events = {}
for obj in dir():
if type(locals()[obj]) == types.FunctionType:

View File

@ -79,12 +79,12 @@
</tr>
{% endfor %}
</table>
{# if post.id == quest.open_post_id and post.id|is_write_in %}
<div id="writeinContainer">
Write-in: <input type="text" id="writeinInput" placeholder="Custom choice..." maxlength="200" /><br />
<input type="submit" id="writeinSubmit" value="Submit" onclick="submitWritein({{ post.id }});"/>
{% if post.poll.open and post.poll.allow_writein %}
<div id="writeinContainer-{{ post.id }}">
Write-in: <input type="text" id="writeinInput-{{ post.id }}" placeholder="Custom choice..." maxlength="200" /><br />
<input type="submit" id="writeinSubmit-{{ post.id }}" value="Submit" onclick="submitWritein({{ post.id }});"/>
</div>
{% endif #}
{% endif %}
{% endif %}
</div>
</div><br />

View File

@ -70,8 +70,8 @@ socket.events['new_post'] = function(data) {
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 += 'Write-in: <input type="text" id="writeinInput-' + data.post_id + '" placeholder="Custom choice..." maxlength="200" /><br />';
post_str += '<input type="submit" id="writeinSubmit-' + data.post_id + '" value="Submit" onclick="submitWritein(' + data.post_id + ');"/></div>';
}
}
post_str += '</div></div><br />';
@ -95,16 +95,17 @@ socket.events['close_all_posts'] = function(data) {
}
socket.events['update_post'] = function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
if (data.post_type === 'text') {
if (data.post_type === 'text') { // edit post
post.innerHTML = data.post_text;
} else if (data.post_type === 'dice') {
} else if (data.post_type === 'dice') { // diceroll
post.innerHTML += '<b>' + data.post_text + '</b><br>';
} else if (data.post_type === 'poll') {
} else if (data.post_type === 'poll') { // write-in
let row = post.children[1].insertRow(-1);
row.id = 'optionRow-' + data.option_id;
let cell = row.insertCell(0);
cell.className = 'pollCheckBox';
cell.innerHTML = '<input type="checkbox" id="pollInput-' + data.options_id + '" onchange="vote(' + data.post_id + ',' + data.options_id + ')"/>';
cell.innerHTML += '<label for="pollInput-' + data.options_id + '"></label>';
cell.innerHTML = '<input type="checkbox" id="pollInput-' + data.option_id + '" onchange="vote(' + data.post_id + ',' + data.option_id + ')"/>';
cell.innerHTML += '<label for="pollInput-' + data.option_id + '"></label>';
cell = row.insertCell(1);
cell.className = 'option_text';
@ -141,12 +142,12 @@ function vote(post_id, option_id) {
socket.send('vote', {post_id: post_id, option_id: option_id, polarity: polarity, quest_id: quest_id});
}
function submitWritein(post_id) {
let writeinInput = document.getElementById('writeinInput');
let writeinInput = document.getElementById('writeinInput-' + post_id);
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});
socket.send('write_in', {option_text: option_text, post_id: post_id});
}
/* Helpers */
@ -201,7 +202,7 @@ function close_post(post_id) {
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = true;
}
let writein = document.getElementById('writeinContainer');
let writein = document.getElementById('writeinContainer-' + post_id);
if (writein) {
writein.style.display = 'none';
}
@ -217,7 +218,7 @@ function open_post(post_id) {
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = false;
}
let writein = document.getElementById('writeinContainer');
let writein = document.getElementById('writeinContainer-' + post_id);
if (writein) {
writein.style.display = 'initial';
}

View File

@ -37,8 +37,8 @@ socket.events['new_post'] = function(data) {
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 += 'Write-in: <input type="text" id="writeinInput-' + data.post_id + '" placeholder="Custom choice..." maxlength="200" /><br />';
post_str += '<input type="submit" id="writeinSubmit-' + data.post_id + '" value="Submit" onclick="submitWritein(' + data.post_id + ');"/></div>';
}
}
post_str += '</div></div><br />';
@ -111,7 +111,7 @@ function close_post(post_id) {
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = true;
}
let writein = document.getElementById('writeinContainer');
let writein = document.getElementById('writeinContainer-' + post_id);
if (writein) {
writein.style.display = 'none';
}
@ -131,7 +131,7 @@ function open_post(post_id) {
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = false;
}
let writein = document.getElementById('writeinContainer');
let writein = document.getElementById('writeinContainer-' + post_id);
if (writein) {
writein.style.display = 'initial';
}

4
todo
View File

@ -31,4 +31,6 @@ Adjust quote preview postioning
Port from old code:
Edit post
Writein
Edit quest
Pages
Images