anonkun/static/anonkunQM.js

174 lines
7.3 KiB
JavaScript

socket.removeListener('new_post');
socket.on('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">' + 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>';
} 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;
} else if (data.post_type == 'dice') {
post_str += '<h3>' + data.post + ' - Open</h3>';
} else if (data.post_type == 'poll') {
post_str += '<h3>' + data.post + ' - 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.removeListener('close_post');
socket.on('close_post', function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
/* QM only */
document.getElementById('close_post_id-' + data.post_id).style.display = 'none';
document.getElementById('open_post_id-' + data.post_id).style.display = 'initial';
/* end QM only */
if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + data.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';
}
}
});
socket.removeListener('open_post');
socket.on('open_post', function(data) {
let post = document.getElementById('questPostData-' + data.post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
/* QM only */
document.getElementById('close_post_id-' + data.post_id).style.display = 'initial';
document.getElementById('open_post_id-' + data.post_id).style.display = 'none';
/* end QM only */
if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + data.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';
}
}
});
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();
qparea.value = '';
if (text == '') { return; }
socket.emit('new_post', {post: text, page_num: page_num, room: room_id});
}
function edit_post(post_id) {
document.getElementById('questPostData-' + post_id).contentEditable = 'true';
document.execCommand("defaultParagraphSeparator", false, "br");
document.getElementById('questPostData-' + post_id).style.border = '1px solid #ccc';
document.getElementById('savePost-' + post_id).style.display = 'initial';
document.getElementById('editPost-' + post_id).style.display = 'none';
}
function save_post(post_id) {
document.getElementById('questPostData-' + post_id).contentEditable = 'false';
document.getElementById('questPostData-' + post_id).style.border = null;
let text = document.getElementById('questPostData-' + post_id).innerHTML;
socket.emit('update_post', {post_id: post_id, post: text, room: room_id});
document.getElementById('savePost-' + post_id).style.display = 'none';
document.getElementById('editPost-' + post_id).style.display = 'initial';
}
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.room = room_id;
obj.page_num = page_num;
socket.emit(emit_msg, obj);
document.getElementById(form_id).reset();
}
function close_post(post_id) {
socket.emit('close_post', {post_id: post_id, room: room_id});
}
function open_post(post_id) {
socket.emit('open_post', {post_id: post_id, room: room_id});
}
function insertPollOption() {
let opts = document.getElementById('pollOptions');
let num = opts.children.length+1;
let temp = document.createElement('template');
temp.innerHTML = '<div><input type="text" name="pollOption-' + num + '" class="pollOption" placeholder="Option ' + num + '" maxlength="200" /></div>';
opts.appendChild(temp.content);
}
function removePollOption() {
let opts = document.getElementById('pollOptions');
if (opts.children.length == 0) { return; }
opts.children[opts.children.length-1].outerHTML = '';
}
function openPostTab(event, modeName) {
let i, QMPostTabContent, QMPostTab;
QMPostTabContent = document.getElementsByClassName("QMPostTabContent");
for (i = 0; i < QMPostTabContent.length; i++) {
QMPostTabContent[i].style.display = "none";
}
QMPostTab = document.getElementsByClassName("QMPostTab");
for (i = 0; i < QMPostTab.length; i++) {
QMPostTab[i].className = QMPostTab[i].className.replace(" active", "");
}
document.getElementById(modeName).style.display = "block";
event.currentTarget.className += " active";
}
/* end QM only */