Skip to content

Commit

Permalink
js to jump to last message
Browse files Browse the repository at this point in the history
  • Loading branch information
Jpadilla1 committed Apr 30, 2014
1 parent c4190bd commit d09386e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apps/chats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def get_context_data(self, **kwargs):
context['form'] = self.get_form(self.get_form_class())
context['room'] = get_object_or_404(ChatRoom, slug=self.kwargs['slug'])
context['room_messages'] = Message.objects.filter(room=context['room'])
context['room_message_last'] = context['room_messages'].last().id - 1
users = User.objects.filter(
last_login__gt=self.request.user.last_logged_out,
is_active__exact=1, ).order_by('-last_login')
Expand Down Expand Up @@ -84,6 +85,7 @@ def get_ajax(self, request, *args, **kwargs):
m.append(i['fields'])
data = {
'messages': json.dumps(m),
'last_message_id': messages.last().id - 1,
'online_users': json.dumps(list(ChatRoom.objects.get(
slug=kwargs['slug']).members.filter(
last_login__gt=self.request.user.last_logged_out,
Expand Down
8 changes: 6 additions & 2 deletions templates/chats/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<div class="panel-heading"><strong>{{ room.name }}</strong></div>
<div class="panel-body messages-bx" id="messages">
{% for m in room_messages %}
<h4>{{ m.created_by.username }} | {{ m.created_at|naturaltime }}</h4>
<h4 id="message_{{ forloop.counter0 }}">{{ m.created_by.username }} | {{ m.created_at|naturaltime }}</h4>
<p class="message">{{ m }}</p>
<hr>
{% endfor %}
Expand Down Expand Up @@ -57,14 +57,18 @@ <h4>{{ m.created_by.username }} | {{ m.created_at|naturaltime }}</h4>
var html = "";
d = $.parseJSON(messages);
$.each(d, function(i, item) {
html += "<h4>" + item.created_by + " | " + item.created_at +" </h4>" + "<p class='message'>" + item.body + "</p>" + "<hr>";
html += "<h4 id='message_" + i + "'>" + item.created_by + " | " + item.created_at +" </h4>" + "<p class='message'>" + item.body + "</p>" + "<hr>";
});
$('#messages').html(html);

location.href = '#message_' + data['last_message_id'];
},

});
};

location.href = '#message_' + {{ room_message_last }};

var interval = 1000 * 10;

setInterval(ajax_call, interval);
Expand Down
2 changes: 1 addition & 1 deletion templates/users/myrooms.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
{% if myrooms|length > 0 %}
<h1>My Rooms</h1>
<h2>My Rooms</h2>
<hr>
{% for room in myrooms %}
<p class="lead"><a href="{% url 'chats:room' room.slug %}">{{ room }}</a> - <span class="text-success">{{ room.members.count }} members</span></p>
Expand Down

0 comments on commit d09386e

Please sign in to comment.