199 lines
8.1 KiB
JavaScript
199 lines
8.1 KiB
JavaScript
document.execCommand("defaultParagraphSeparator", false, "br");
|
|
var socket;
|
|
var tid = setInterval( function () {
|
|
if ( document.readyState !== 'complete' ) return;
|
|
clearInterval( tid );
|
|
|
|
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
|
|
socket = io.connect('https://' + document.domain + ':' + location.port);
|
|
socket.on('connect', function() {
|
|
socket.emit('joined', {room: room_id});
|
|
});
|
|
socket.on('message', function(data) {
|
|
let msg_str = '<div id="msg-' + data.message_id + '" class="message">';
|
|
msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> ';
|
|
msg_str += '<span class="messageDate">' + strftime(data.date) + '</span> ';
|
|
msg_str += '<span class="messageID">No.<a href="javascript:quote(' + data.message_id + ')">' + data.message_id + '</a></span></div>';
|
|
msg_str += '<div class="messageContent">' + data.message + '</div></div><hr>';
|
|
|
|
let mbox = document.getElementById('chatWindow');
|
|
mbox.innerHTML = mbox.innerHTML + msg_str;
|
|
mbox.scrollTop = mbox.scrollHeight;
|
|
});
|
|
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);
|
|
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="post-' + 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>';
|
|
}
|
|
post_str += '</div></div><br />';
|
|
qposts.innerHTML = qposts.innerHTML + post_str;
|
|
});
|
|
socket.on('update_post', function(data) {
|
|
let post_id = data.post_id;
|
|
let post = document.getElementById('questPostData-' + post_id);
|
|
if (data.post_type === 'text') {
|
|
post.innerHTML = data.post;
|
|
} 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) {
|
|
let post = document.getElementById('questPostData-' + data.post_id);
|
|
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
|
|
document.getElementById('close_post_id-' + data.post_id).style.display = 'none';
|
|
document.getElementById('open_post_id-' + data.post_id).style.display = 'initial';
|
|
if (post.parentElement.classList.contains('pollPost')) {
|
|
let table = document.getElementById('poll-' + data.post_id);
|
|
table.getElementsByTagName("col")[0].style.visibility = 'collapse';
|
|
let writein = document.getElementById('writeinContainer');
|
|
if (writein) {
|
|
writein.style.display = 'none';
|
|
}
|
|
}
|
|
});
|
|
socket.on('open_post', function(data) {
|
|
let post = document.getElementById('questPostData-' + data.post_id);
|
|
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
|
|
document.getElementById('close_post_id-' + data.post_id).style.display = 'initial';
|
|
document.getElementById('open_post_id-' + data.post_id).style.display = 'none';
|
|
if (post.parentElement.classList.contains('pollPost')) {
|
|
table = document.getElementById('poll-' + data.post_id);
|
|
table.getElementsByTagName("col")[0].style.visibility = '';
|
|
let writein = document.getElementById('writeinContainer');
|
|
if (writein) {
|
|
writein.style.display = 'initial';
|
|
}
|
|
}
|
|
});
|
|
socket.on('vote', function(data) {
|
|
let row = document.getElementById('optionRow-' + data.option_id);
|
|
if (data.polarity) {
|
|
row.cells[2].textContent = Number(row.cells[2].textContent) + 1;
|
|
} 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;
|
|
});
|
|
let mtarea = document.getElementById('messageTextArea');
|
|
mtarea.addEventListener('keypress', function(event) {
|
|
if (event.key == 'Enter' && !event.shiftKey) {
|
|
let text = mtarea.value.trim();
|
|
mtarea.value = '';
|
|
if (text == '') { return; }
|
|
socket.emit('message', {message: text, name: 'Anonymous', room: room_id});
|
|
}
|
|
});
|
|
}, 100 );
|
|
function padToTwo(number) {
|
|
if (number<=99) { number = ("0"+number).slice(-2); }
|
|
return number;
|
|
}
|
|
function strftime(date) {
|
|
date = new Date(date * 1000);
|
|
let date_str = date.getFullYear() + '-' + padToTwo(date.getMonth()+1) + '-' + padToTwo(date.getDate()) + ' ';
|
|
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});
|
|
}
|
|
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 quote(message_id) {
|
|
let textbox = document.getElementById('messageTextArea');
|
|
textbox.value += '>>' + message_id + '\n';
|
|
textbox.focus();
|
|
}
|
|
function scrollToMsg(message_id) {
|
|
let elem = document.getElementById('msg-' + message_id);
|
|
if (!elem) { return; }
|
|
elem.scrollIntoView();
|
|
}
|
|
function showPreview(event, message_id) {
|
|
let elem = document.getElementById('msg-' + message_id);
|
|
if (!elem) { return; }
|
|
let preview = document.getElementById('preview');
|
|
preview.innerHTML = elem.innerHTML;
|
|
preview.style.display = '';
|
|
let x = event.clientX + 20 + 'px';
|
|
let y = event.clientY + 20 + 'px';
|
|
let maxWidth = window.innerWidth - event.clientX - 80 + 'px';
|
|
preview.style.top = y;
|
|
preview.style.left = x;
|
|
preview.style.maxWidth = maxWidth;
|
|
}
|
|
function clearPreview() {
|
|
document.getElementById('preview').innerHTML = '';
|
|
document.getElementById('preview').style.display = 'none';
|
|
}
|
|
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");
|
|
let writein = document.getElementById('writeinContainer');
|
|
if (writein) {
|
|
writein.outerHTML = '';
|
|
}
|
|
}
|