139 lines
5.7 KiB
JavaScript
139 lines
5.7 KiB
JavaScript
/* Websocket receive */
|
|
socket.events['new_post'] = function(data) {
|
|
//deactivate_post();
|
|
let qposts = document.getElementById('questPosts');
|
|
let post_str = '<div class="questPost ';
|
|
if (data.post_type == 'text') {
|
|
post_str += 'textPost">';
|
|
} else if (data.post_type == 'dice') {
|
|
post_str += 'dicePost active_post">';
|
|
} else if (data.post_type == 'poll') {
|
|
post_str += 'pollPost active_post">';
|
|
}
|
|
post_str += '<div class="questPostMeta">' + 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>';
|
|
} else if (data.post_type == 'dice' || data.post_type == 'poll') {
|
|
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_text;
|
|
} else if (data.post_type == 'dice') {
|
|
post_str += '<h3>' + data.post_text + ' - Open</h3>';
|
|
} else if (data.post_type == 'poll') {
|
|
post_str += '<h3>' + data.post_text + ' - Open</h3>';
|
|
post_str += '<table class="poll" id="poll-' + data.post_id + '">';
|
|
post_str += '<col/><col/><col/>';
|
|
for (i = 0; i < data.options.length; i++) {
|
|
post_str += '<tr id="optionRow-' + data.options[i][0] + '">';
|
|
post_str += '<td class="pollCheckBox"><input type="checkbox" id="pollInput-' + data.options[i][0] + '" onchange="pollVote(' + data.post_id + ',' + data.options[i][0] + ')"/>';
|
|
post_str += '<label for="pollInput-' + data.options[i][0] + '"></label></td>';
|
|
post_str += '<td class="option_text">' + data.options[i][1] + '</td>';
|
|
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;
|
|
};
|
|
|
|
/* socket send */
|
|
function makePost() {
|
|
let qparea = document.getElementById('postTextArea');
|
|
let text = qparea.value.trim();
|
|
qparea.value = '';
|
|
if (text == '') { return; }
|
|
socket.send('text_post', {text: text, page_num: page_num, quest_id: quest_id});
|
|
}
|
|
function form_post(form_id, emit_msg) {
|
|
let formData = new FormData(document.getElementById(form_id));
|
|
let obj = {};
|
|
formData.forEach(function(value, key) {
|
|
obj[key] = value;
|
|
});
|
|
obj.quest_id = quest_id;
|
|
obj.page_num = page_num;
|
|
socket.send(emit_msg, obj);
|
|
document.getElementById(form_id).reset();
|
|
}
|
|
function close_post_send(post_id) {
|
|
let post = document.getElementById('questPostData-' + post_id);
|
|
if (post.parentElement.classList.contains('dicePost')) {
|
|
data = {post_type: 'dice', post_id: post_id};
|
|
} else if (post.parentElement.classList.contains('pollPost')) {
|
|
data = {post_type: 'poll', post_id: post_id};
|
|
}
|
|
socket.send('close_post', data);
|
|
}
|
|
function open_post_send(post_id) {
|
|
let post = document.getElementById('questPostData-' + post_id);
|
|
if (post.parentElement.classList.contains('dicePost')) {
|
|
data = {post_type: 'dice', post_id: post_id};
|
|
} else if (post.parentElement.classList.contains('pollPost')) {
|
|
data = {post_type: 'poll', post_id: post_id};
|
|
}
|
|
socket.send('open_post', data);
|
|
}
|
|
|
|
/* DOM editing */
|
|
function openPostTab(event, modeName) {
|
|
let QMPostTabContent = document.getElementsByClassName("QMPostTabContent");
|
|
for (let i = 0; i < QMPostTabContent.length; i++) {
|
|
QMPostTabContent[i].style.display = "none";
|
|
}
|
|
let QMPostTab = document.getElementsByClassName("QMPostTab");
|
|
for (let i = 0; i < QMPostTab.length; i++) {
|
|
QMPostTab[i].className = QMPostTab[i].className.replace(" active", "");
|
|
}
|
|
document.getElementById(modeName).style.display = "block";
|
|
event.currentTarget.className += " active";
|
|
}
|
|
function close_post(post_id) {
|
|
let post = document.getElementById('questPostData-' + post_id);
|
|
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
|
|
/* QM only */
|
|
document.getElementById('closePost-' + post_id).style.display = 'none';
|
|
document.getElementById('openPost-' + post_id).style.display = 'initial';
|
|
/* end QM only */
|
|
if (post.parentElement.classList.contains('pollPost')) {
|
|
let table = document.getElementById('poll-' + post_id);
|
|
let boxes = table.getElementsByTagName('input');
|
|
for (let i = 0; i < boxes.length; i++) {
|
|
boxes[i].disabled = true;
|
|
}
|
|
let writein = document.getElementById('writeinContainer');
|
|
if (writein) {
|
|
writein.style.display = 'none';
|
|
}
|
|
}
|
|
}
|
|
function open_post(post_id) {
|
|
let post = document.getElementById('questPostData-' + post_id);
|
|
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
|
|
/* QM only */
|
|
document.getElementById('closePost-' + post_id).style.display = 'initial';
|
|
document.getElementById('openPost-' + post_id).style.display = 'none';
|
|
/* end QM only */
|
|
if (post.parentElement.classList.contains('pollPost')) {
|
|
let table = document.getElementById('poll-' + post_id);
|
|
let boxes = table.getElementsByTagName('input');
|
|
for (let i = 0; i < boxes.length; i++) {
|
|
boxes[i].disabled = false;
|
|
}
|
|
let writein = document.getElementById('writeinContainer');
|
|
if (writein) {
|
|
writein.style.display = 'initial';
|
|
}
|
|
}
|
|
}
|