Commit aa099162fac9c10052e20fb6220d37d4eb599bbd

Authored by Zambom
1 parent 2f64da62

Adding comments load more

amadeus/static/js/mural.js
1 1 var new_posts = [];
  2 +var new_comments = {};
2 3 // loadOnScroll handler
3 4 var loadOnScroll = function() {
4 5 // If the current scroll position is past out cutoff point...
... ... @@ -70,6 +71,12 @@ $(function () {
70 71  
71 72 frm.submit();
72 73 });
  74 +
  75 + $(".comment-section").each(function () {
  76 + var height = $(this)[0].scrollHeight;
  77 +
  78 + $(this).animate({scrollTop: height}, 0);
  79 + });
73 80 });
74 81  
75 82 function setPostFormSubmit(post = "") {
... ... @@ -196,7 +203,7 @@ function setPostDeleteSubmit (post) {
196 203  
197 204 function comment(field) {
198 205 var url = field.find('h4').data('url'),
199   - post = field.parent().parent();
  206 + post = field.find('h4').data('post');
200 207  
201 208 $.ajax({
202 209 url: url,
... ... @@ -212,8 +219,7 @@ function comment(field) {
212 219  
213 220 function editComment(btn) {
214 221 var url = btn.data('url'),
215   - post_id = btn.data('post'),
216   - post = $("#post-" + post_id),
  222 + post = btn.data('post'),
217 223 comment = btn.data('id');
218 224  
219 225 $.ajax({
... ... @@ -248,9 +254,13 @@ function setCommentFormSubmit(post, comment = "") {
248 254  
249 255 old.remove();
250 256 } else {
251   - $(post).find(".comment-section").append(data.view);
  257 + $("#post-" + post).find(".comment-section").append(data.view);
252 258  
253   - //new_posts.push(data.new_id);
  259 + if (typeof(new_comments[post]) == 'undefined') {
  260 + new_comments[post] = [];
  261 + }
  262 +
  263 + new_comments[post].push(data.new_id);
254 264 }
255 265  
256 266 $('#post-modal-form').modal('hide');
... ... @@ -311,4 +321,37 @@ function setCommentDeleteSubmit (comment) {
311 321  
312 322 return false;
313 323 });
  324 +}
  325 +
  326 +function loadComments (btn) {
  327 + var url = btn.data('url'),
  328 + post = btn.data('post'),
  329 + page = btn.parent().data('page'),
  330 + loading = btn.parent().find('.loading');
  331 +
  332 + page = parseInt(page);
  333 + page = page + 1;
  334 +
  335 + loading.show();
  336 + btn.hide();
  337 +
  338 + var showing;
  339 +
  340 + if (typeof(new_comments[post]) == 'undefined') {
  341 + showing = "";
  342 + } else {
  343 + showing = new_comments[post].join(',');
  344 + }
  345 +
  346 + $.ajax({
  347 + url: url,
  348 + data: {'page': page, 'showing': showing},
  349 + dataType: 'json',
  350 + success: function (response) {
  351 + loading.hide();
  352 + btn.show();
  353 +
  354 + btn.after(response.loaded);
  355 + }
  356 + });
314 357 }
315 358 \ No newline at end of file
... ...
mural/templates/mural/_list_view_comment.html 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +{% for comment in comments reversed %}
  2 + {% include 'mural/_view_comment.html' %}
  3 +{% endfor %}
  4 +
  5 +<script type="text/javascript">
  6 + $(function () {
  7 + var section = $("#post-{{ post_id }}").find('.comment-section'),
  8 + pageNumber = "{{ page_obj.number }}",
  9 + totalPages = "{{ paginator.num_pages }}";
  10 +
  11 + section.data('page', pageNumber);
  12 + section.data('pages', totalPages);
  13 +
  14 + if (pageNumber == totalPages) {
  15 + section.find('.load-btn').hide();
  16 + }
  17 + });
  18 +</script>
0 19 \ No newline at end of file
... ...
mural/templates/mural/_view.html
1   -{% load i18n mural_filters %}
  1 +{% load i18n mural_filters comments_list %}
2 2  
3 3 <div id="post-{{ post.id }}" class="panel panel-default">
4 4 <div class="panel-body post">
... ... @@ -44,9 +44,9 @@
44 44 {% endif %}
45 45 </div>
46 46 <div class="col-md-12 comment-section">
47   - {% for comment in post.comment_post.all %}
48   - {% include 'mural/_view_comment.html' %}
49   - {% endfor %}
  47 + <button type="button" class="btn btn-block btn-default btn-sm loading" style="display:none"><span class="fa fa-spin fa-circle-o-notch"></span></button>
  48 + <button type="button" onclick="loadComments($(this))" data-url="{% url 'mural:load_comments' post.mural_ptr_id post.id %}" data-post="{{ post.id }}" class="btn btn-block btn-default btn-sm load-btn">{% trans 'See more comments...' %}</button>
  49 + {% comments_list request post %}
50 50 </div>
51 51 <div class="col-md-12 post-comment">
52 52 <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 user-img">
... ... @@ -56,7 +56,7 @@
56 56 </div>
57 57 <div class="col-lg-11 col-md-11 col-sm-11 col-xs-11 comment-field" onclick="comment($(this))">
58 58 <div>
59   - <h4 data-url="{% url 'mural:create_comment' post.mural_ptr_id %}">{% trans 'Make a comment...' %}</h4>
  59 + <h4 data-url="{% url 'mural:create_comment' post.mural_ptr_id %}" data-post="{{ post.id }}">{% trans 'Make a comment...' %}</h4>
60 60 </div>
61 61 </div>
62 62 </div>
... ...
mural/templatetags/comments_list.py 0 → 100644
... ... @@ -0,0 +1,36 @@
  1 +from django import template
  2 +
  3 +from django.core.paginator import Paginator, EmptyPage
  4 +from django.http import Http404
  5 +
  6 +from mural.models import Comment
  7 +
  8 +register = template.Library()
  9 +
  10 +@register.inclusion_tag('mural/_list_view_comment.html')
  11 +def comments_list(request, post):
  12 + context = {
  13 + 'request': request,
  14 + }
  15 +
  16 + comments = Comment.objects.filter(post = post).order_by('-last_update')
  17 +
  18 + paginator = Paginator(comments, 5)
  19 +
  20 + try:
  21 + page_number = int(request.GET.get('page', 1))
  22 + except ValueError:
  23 + raise Http404
  24 +
  25 + try:
  26 + page_obj = paginator.page(page_number)
  27 + except EmptyPage:
  28 + raise Http404
  29 +
  30 + context['paginator'] = paginator
  31 + context['page_obj'] = page_obj
  32 +
  33 + context['comments'] = page_obj.object_list
  34 + context['post_id'] = post.id
  35 +
  36 + return context
0 37 \ No newline at end of file
... ...
mural/urls.py
... ... @@ -14,5 +14,6 @@ urlpatterns = [
14 14 url(r'^deleted_comment/$', views.deleted_comment, name='deleted_comment'),
15 15 url(r'^render_comment/([\w_-]+)/([\w_-]+)/$', views.render_comment, name='render_comment'),
16 16 url(r'^render_post/([\w_-]+)/([\w_-]+)/$', views.render_gen_post, name='render_post_general'),
  17 + url(r'^load_comments/([\w_-]+)/([\w_-]+)/$', views.load_comments, name='load_comments'),
17 18 url(r'^suggest_users/$', views.suggest_users, name='suggest_users'),
18 19 ]
19 20 \ No newline at end of file
... ...
mural/views.py
1 1 from django.shortcuts import get_object_or_404, redirect, render
  2 +from django.core.paginator import Paginator, EmptyPage
  3 +from django.http import Http404
2 4 from django.views import generic
3 5 from django.contrib import messages
4 6 from django.http import JsonResponse
... ... @@ -389,4 +391,39 @@ def suggest_users(request):
389 391  
390 392 response = render_to_string("mural/_user_suggestions_list.html", context, request)
391 393  
392   - return JsonResponse({"search_result": response})
393 394 \ No newline at end of file
  395 + return JsonResponse({"search_result": response})
  396 +
  397 +def load_comments(request, post, child_id):
  398 + context = {
  399 + 'request': request,
  400 + }
  401 +
  402 + showing = request.GET.get('showing', '')
  403 +
  404 + if showing == '':
  405 + comments = Comment.objects.filter(post__id = post).order_by('-last_update')
  406 + else:
  407 + showing = showing.split(',')
  408 + comments = Comment.objects.filter(post__id = post).exclude(id__in = showing).order_by('-last_update')
  409 +
  410 + paginator = Paginator(comments, 5)
  411 +
  412 + try:
  413 + page_number = int(request.GET.get('page', 1))
  414 + except ValueError:
  415 + raise Http404
  416 +
  417 + try:
  418 + page_obj = paginator.page(page_number)
  419 + except EmptyPage:
  420 + raise Http404
  421 +
  422 + context['paginator'] = paginator
  423 + context['page_obj'] = page_obj
  424 +
  425 + context['comments'] = page_obj.object_list
  426 + context['post_id'] = child_id
  427 +
  428 + response = render_to_string("mural/_list_view_comment.html", context, request)
  429 +
  430 + return JsonResponse({"loaded": response})
394 431 \ No newline at end of file
... ...