add time format for chat box

This commit is contained in:
cuom1999 2020-03-20 15:34:33 -06:00
parent 112f2b57c3
commit dd34f474a5
4 changed files with 45 additions and 22 deletions

View file

@ -1,6 +1,7 @@
import json import json
from channels.generic.websocket import AsyncWebsocketConsumer from channels.generic.websocket import AsyncWebsocketConsumer
from .models import Message from .models import Message
from .views import format_time
from judge.models.profile import Profile from judge.models.profile import Profile
@ -60,5 +61,3 @@ def save_data_and_get_time(message):
return new_message.time return new_message.time
def format_time(time):
return time.strftime('%H:%M %p %d-%m-%Y')

View file

@ -4,14 +4,22 @@ from django.views.generic import ListView
from .models import Message from .models import Message
def format_time(time):
return time.strftime('%H:%M %p %d-%m-%Y')
class ChatView(ListView): class ChatView(ListView):
model = Message model = Message
context_object_name = 'message' context_object_name = 'message'
template_name = 'chat/chat.html' template_name = 'chat/chat.html'
title = _('Chat Box') title = _('Chat Box')
paginate_by = 10 paginate_by = 50
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['title'] = self.title context['title'] = self.title
for msg in context['message']:
msg.time = format_time(msg.time)
return context return context

View file

@ -1,5 +1,4 @@
#chat-log { #chat-box {
padding: 0;
border: 1px solid #ccc; border: 1px solid #ccc;
border-radius: 4px; border-radius: 4px;
height: 20em; height: 20em;
@ -8,6 +7,17 @@
overflow-wrap: break-word; overflow-wrap: break-word;
overflow-y: scroll; overflow-y: scroll;
} }
#loader {
display: block;
margin-left: auto;
margin-right: auto;
width: 4%;
}
#chat-log {
padding: 0;
width: 100%;
}
#chat-log li { #chat-log li {
list-style-type: none; list-style-type: none;

View file

@ -1,7 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block js_media %} {% block js_media %}
<script src="{{ static('libs/jquery.waypoints.min.js')}}"></script>
<script src="{{ static('libs/infinite.min.js') }}"></script>
<script type="text/javascript"> <script type="text/javascript">
var chatSocket = new WebSocket( var chatSocket = new WebSocket(
'ws://' + window.location.host + 'ws://' + window.location.host +
@ -9,6 +7,8 @@
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
$('#loader').hide();
chatSocket.onmessage = function(e) { chatSocket.onmessage = function(e) {
let data = JSON.parse(e.data) let data = JSON.parse(e.data)
data = data['message'] data = data['message']
@ -36,25 +36,31 @@
</li>` </li>`
ul = $('#chat-log') ul = $('#chat-log')
ul.append(li) ul.append(li)
$('#chat-log').scrollTop($('#chat-log')[0].scrollHeight); $('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
} }
(function init_chatlog() { (function init_chatlog() {
ul = $('#chat-log') ul = $('#chat-log')
{% for msg in message|reverse %} {% for msg in message|reverse %}
loadMessage('{{msg.body}}', '{{msg.author}}', '12:00:00', '{{gravatar(msg.author, 32)}}') loadMessage('{{msg.body}}', '{{msg.author}}', '{{msg.time}}', '{{gravatar(msg.author, 32)}}')
{% endfor %} {% endfor %}
})() })()
var infinite = new Waypoint.Infinite({ $('#chat-box').scroll(function() {
element: $('.infinite-container')[0], if ($('#chat-box').scrollTop() == 0) {
onBeforePageLoad: function () { console.log('Hi')
$('.loading').show(); $('#loader').show()
},
onAfterPageLoad: function ($items) {
$('.loading').hide();
} }
}); })
// var infinite = new Waypoint.Infinite({
// element: $('.infinite-container')[0],
// onBeforePageLoad: function () {
// $('.loading').show();
// },
// onAfterPageLoad: function ($items) {
// $('.loading').hide();
// }
// });
$("#chat-submit").click(function() { $("#chat-submit").click(function() {
if ($("#chat-input").val().trim()) { if ($("#chat-input").val().trim()) {
@ -118,14 +124,14 @@
{% block body %} {% block body %}
<div id="chat-area"> <div id="chat-area">
<ul id="chat-log" class="infinite-container"> <div id="chat-box">
</ul> <img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif" id="loader">
<ul id="chat-log" class="infinite-container waypoint">
</ul>
</div>
{{_('Your message')}} {{_('Your message')}}
<textarea rows="6" id="chat-input"></textarea> <textarea rows="6" id="chat-input"></textarea>
</div> </div>
<button id="chat-submit"> Send </button> <button id="chat-submit"> Send </button>
{% if page_obj.has_next %}
<a href="{{ request.path }}?page={{ message.next_page_number }}">next</a>
{% endif %}
{% endblock body %} {% endblock body %}