From 3e015e75d9c792b9aaa5fd926f366abb709e2f19 Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 24 Aug 2018 18:24:25 -0400 Subject: [PATCH] only one dicecall may be open a time --- quest/events.py | 7 +++++++ quest/static/quest.js | 10 ++++++++-- quest/static/questQM.js | 7 ++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/quest/events.py b/quest/events.py index be3b9f2..3d101e2 100644 --- a/quest/events.py +++ b/quest/events.py @@ -191,9 +191,16 @@ def open_post(socket, data): """ Called when the QM opens a closed post. """ + # TODO: only posts on last page can be opened post_id = data.get('post_id') p = Post.objects.get(id=post_id) if data.get('post_type') == 'dice': + posts = Post.objects.filter(post_type='dice', dicecall__open=True) + for post in posts: + post.dicecall.open = False + post.dicecall.save() + socket.send('close_all_posts', {'post_type': 'dice'}) + p.dicecall.open = True p.dicecall.save() elif data.get('post_type') == 'poll': diff --git a/quest/static/quest.js b/quest/static/quest.js index 9d8335c..b7393f6 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -86,9 +86,13 @@ socket.events['open_post'] = function(data) { open_post(data.post_id); } socket.events['close_all_posts'] = function(data) { - let posts = document.getElementsByClassName('activePost'); + let class_set = ''; + if (data.post_type == 'dice') { class_set = 'dicePost activePost'; } + else if (data.post_type == 'poll') { class_set = 'pollPost activePost'; } + else { class_set = 'activePost'; } + let posts = document.getElementsByClassName(class_set); for (let i = 0; i < posts.length; i++) { - close_post(post.children[1].id.slice(14)); // retreive the number at the end + close_post(posts[i].children[1].id.slice(14)); // retreive the id number at the end } } @@ -150,6 +154,8 @@ function close_post(post_id) { function open_post(post_id) { let post = document.getElementById('questPostData-' + post_id); post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open'); + post.parentElement.className += ' activePost'; + console.log(post.parentElement.className); if (post.parentElement.classList.contains('pollPost')) { let table = document.getElementById('poll-' + post_id); let boxes = table.getElementsByTagName('input'); diff --git a/quest/static/questQM.js b/quest/static/questQM.js index e93d957..dc2bd3d 100644 --- a/quest/static/questQM.js +++ b/quest/static/questQM.js @@ -47,7 +47,7 @@ socket.events['new_post'] = function(data) { qposts.innerHTML = qposts.innerHTML + post_str; }; -/* socket send */ +/* Websocket send */ function makePost() { let qparea = document.getElementById('postTextArea'); let text = qparea.value.trim(); @@ -55,7 +55,7 @@ function makePost() { if (text == '') { return; } socket.send('text_post', {text: text, page_num: page_num, quest_id: quest_id}); } -function form_post(form_id, emit_msg) { +function form_post(form_id, event) { let formData = new FormData(document.getElementById(form_id)); let obj = {}; formData.forEach(function(value, key) { @@ -63,7 +63,7 @@ function form_post(form_id, emit_msg) { }); obj.quest_id = quest_id; obj.page_num = page_num; - socket.send(emit_msg, obj); + socket.send(event, obj); document.getElementById(form_id).reset(); } function close_post_send(post_id) { @@ -120,6 +120,7 @@ function close_post(post_id) { function open_post(post_id) { let post = document.getElementById('questPostData-' + post_id); post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open'); + post.parentElement.className += ' activePost'; /* QM only */ document.getElementById('closePost-' + post_id).style.display = 'initial'; document.getElementById('openPost-' + post_id).style.display = 'none';