diff --git a/amadeus/static/js/mural.js b/amadeus/static/js/mural.js index 18d20e9..6118e26 100644 --- a/amadeus/static/js/mural.js +++ b/amadeus/static/js/mural.js @@ -1,4 +1,5 @@ var new_posts = []; +var new_comments = {}; // loadOnScroll handler var loadOnScroll = function() { // If the current scroll position is past out cutoff point... @@ -70,6 +71,12 @@ $(function () { frm.submit(); }); + + $(".comment-section").each(function () { + var height = $(this)[0].scrollHeight; + + $(this).animate({scrollTop: height}, 0); + }); }); function setPostFormSubmit(post = "") { @@ -196,7 +203,7 @@ function setPostDeleteSubmit (post) { function comment(field) { var url = field.find('h4').data('url'), - post = field.parent().parent(); + post = field.find('h4').data('post'); $.ajax({ url: url, @@ -212,8 +219,7 @@ function comment(field) { function editComment(btn) { var url = btn.data('url'), - post_id = btn.data('post'), - post = $("#post-" + post_id), + post = btn.data('post'), comment = btn.data('id'); $.ajax({ @@ -248,9 +254,13 @@ function setCommentFormSubmit(post, comment = "") { old.remove(); } else { - $(post).find(".comment-section").append(data.view); + $("#post-" + post).find(".comment-section").append(data.view); - //new_posts.push(data.new_id); + if (typeof(new_comments[post]) == 'undefined') { + new_comments[post] = []; + } + + new_comments[post].push(data.new_id); } $('#post-modal-form').modal('hide'); @@ -311,4 +321,37 @@ function setCommentDeleteSubmit (comment) { return false; }); +} + +function loadComments (btn) { + var url = btn.data('url'), + post = btn.data('post'), + page = btn.parent().data('page'), + loading = btn.parent().find('.loading'); + + page = parseInt(page); + page = page + 1; + + loading.show(); + btn.hide(); + + var showing; + + if (typeof(new_comments[post]) == 'undefined') { + showing = ""; + } else { + showing = new_comments[post].join(','); + } + + $.ajax({ + url: url, + data: {'page': page, 'showing': showing}, + dataType: 'json', + success: function (response) { + loading.hide(); + btn.show(); + + btn.after(response.loaded); + } + }); } \ No newline at end of file diff --git a/mural/templates/mural/_list_view_comment.html b/mural/templates/mural/_list_view_comment.html new file mode 100644 index 0000000..1f8b3c3 --- /dev/null +++ b/mural/templates/mural/_list_view_comment.html @@ -0,0 +1,18 @@ +{% for comment in comments reversed %} + {% include 'mural/_view_comment.html' %} +{% endfor %} + + \ No newline at end of file diff --git a/mural/templates/mural/_view.html b/mural/templates/mural/_view.html index a55129b..51f0532 100644 --- a/mural/templates/mural/_view.html +++ b/mural/templates/mural/_view.html @@ -1,4 +1,4 @@ -{% load i18n mural_filters %} +{% load i18n mural_filters comments_list %}
@@ -44,9 +44,9 @@ {% endif %}
- {% for comment in post.comment_post.all %} - {% include 'mural/_view_comment.html' %} - {% endfor %} + + + {% comments_list request post %}
@@ -56,7 +56,7 @@
-

{% trans 'Make a comment...' %}

+

{% trans 'Make a comment...' %}

diff --git a/mural/templatetags/comments_list.py b/mural/templatetags/comments_list.py new file mode 100644 index 0000000..f53a89f --- /dev/null +++ b/mural/templatetags/comments_list.py @@ -0,0 +1,36 @@ +from django import template + +from django.core.paginator import Paginator, EmptyPage +from django.http import Http404 + +from mural.models import Comment + +register = template.Library() + +@register.inclusion_tag('mural/_list_view_comment.html') +def comments_list(request, post): + context = { + 'request': request, + } + + comments = Comment.objects.filter(post = post).order_by('-last_update') + + paginator = Paginator(comments, 5) + + try: + page_number = int(request.GET.get('page', 1)) + except ValueError: + raise Http404 + + try: + page_obj = paginator.page(page_number) + except EmptyPage: + raise Http404 + + context['paginator'] = paginator + context['page_obj'] = page_obj + + context['comments'] = page_obj.object_list + context['post_id'] = post.id + + return context \ No newline at end of file diff --git a/mural/urls.py b/mural/urls.py index 935972a..b505c1f 100644 --- a/mural/urls.py +++ b/mural/urls.py @@ -14,5 +14,6 @@ urlpatterns = [ url(r'^deleted_comment/$', views.deleted_comment, name='deleted_comment'), url(r'^render_comment/([\w_-]+)/([\w_-]+)/$', views.render_comment, name='render_comment'), url(r'^render_post/([\w_-]+)/([\w_-]+)/$', views.render_gen_post, name='render_post_general'), + url(r'^load_comments/([\w_-]+)/([\w_-]+)/$', views.load_comments, name='load_comments'), url(r'^suggest_users/$', views.suggest_users, name='suggest_users'), ] \ No newline at end of file diff --git a/mural/views.py b/mural/views.py index 7ba5e51..2344dd2 100644 --- a/mural/views.py +++ b/mural/views.py @@ -1,4 +1,6 @@ from django.shortcuts import get_object_or_404, redirect, render +from django.core.paginator import Paginator, EmptyPage +from django.http import Http404 from django.views import generic from django.contrib import messages from django.http import JsonResponse @@ -389,4 +391,39 @@ def suggest_users(request): response = render_to_string("mural/_user_suggestions_list.html", context, request) - return JsonResponse({"search_result": response}) \ No newline at end of file + return JsonResponse({"search_result": response}) + +def load_comments(request, post, child_id): + context = { + 'request': request, + } + + showing = request.GET.get('showing', '') + + if showing == '': + comments = Comment.objects.filter(post__id = post).order_by('-last_update') + else: + showing = showing.split(',') + comments = Comment.objects.filter(post__id = post).exclude(id__in = showing).order_by('-last_update') + + paginator = Paginator(comments, 5) + + try: + page_number = int(request.GET.get('page', 1)) + except ValueError: + raise Http404 + + try: + page_obj = paginator.page(page_number) + except EmptyPage: + raise Http404 + + context['paginator'] = paginator + context['page_obj'] = page_obj + + context['comments'] = page_obj.object_list + context['post_id'] = child_id + + response = render_to_string("mural/_list_view_comment.html", context, request) + + return JsonResponse({"loaded": response}) \ No newline at end of file -- libgit2 0.21.2