-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added js controls logic - control prepend or append for comments #15
base: master
Are you sure you want to change the base?
Changes from 6 commits
0a8247a
840c469
d4c3271
a005c85
e69bb10
8a828df
6a4eeb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,14 @@ | |
var COMMENT_SCROLL_TOP_OFFSET = 40; | ||
var PREVIEW_SCROLL_TOP_OFFSET = 20; | ||
|
||
|
||
$.fn.ready(function() | ||
{ | ||
|
||
var COMMENT_CONTROLS = (window.COMMENT_CONTROLS !== undefined) ? window.COMMENT_CONTROLS : { | ||
'is_reversed': false, | ||
'scroll_to_comment': true, | ||
}; | ||
|
||
var commentform = $('form.js-comments-form'); | ||
if( commentform.length > 0 ) | ||
{ | ||
|
@@ -93,7 +98,7 @@ | |
|
||
function scrollToElement( $element, speed, offset ) | ||
{ | ||
if( $element.length ) | ||
if( $element.length && COMMENT_CONTROLS.scroll_to_comment === true ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why the explicit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was working on the assumption that if the user enters "false" it will evaluate as true. |
||
$(scrollElement).animate( {scrollTop: $element.offset().top - (offset || 0) }, speed || 1000 ); | ||
} | ||
|
||
|
@@ -222,22 +227,26 @@ | |
function addComment(data) | ||
{ | ||
// data contains the server-side response. | ||
var html = data['html'] | ||
var html = $(data['html']) // create the domElement and thus fire appropriate events | ||
var parent_id = data['parent_id']; | ||
|
||
var $new_comment; | ||
|
||
// define the action by which the comment is inserted at the top of the list or the bottom | ||
var insert_action = (COMMENT_CONTROLS.is_reversed === true) ? 'prepend' : 'append' ; | ||
|
||
if(parent_id) | ||
{ | ||
var $parentLi = $("#c" + parseInt(parent_id)).parent('li.comment-wrapper'); | ||
var $commentUl = $parentLi.children('ul'); | ||
if( $commentUl.length == 0 ) | ||
$commentUl = $parentLi.append('<ul class="comment-list-wrapper"></ul>').children('ul.comment-list-wrapper'); | ||
$commentUl.append('<li class="comment-wrapper">' + html + '</li>'); | ||
$commentUl[insert_action]('<li class="comment-wrapper">' + html.prop('outerHTML') + '</li>'); | ||
} | ||
else | ||
{ | ||
var $comments = getCommentsDiv(); | ||
$comments.append(html).removeClass('empty'); | ||
$comments[insert_action](html.prop('outerHTML')).removeClass('empty'); | ||
} | ||
|
||
return $("#c" + parseInt(data.comment_id)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from django.conf import settings | ||
from django.template import Library | ||
from django.core import context_processors | ||
from django.template.loader import get_template | ||
from django.template.loader import get_template, render_to_string | ||
from django.contrib.comments.templatetags.comments import RenderCommentListNode | ||
from fluent_comments import appsettings | ||
from fluent_comments.models import get_comments_for_model | ||
from fluent_comments.moderation import comments_are_open, comments_are_moderated | ||
|
||
|
||
register = Library() | ||
|
||
@register.inclusion_tag("fluent_comments/templatetags/ajax_comment_tags.html", takes_context=True) | ||
|
@@ -56,3 +58,45 @@ def fluent_comments_list(context): | |
|
||
context['USE_THREADEDCOMMENTS'] = appsettings.USE_THREADEDCOMMENTS | ||
return template.render(context) | ||
|
||
|
||
class RenderCommentListReversedNode(RenderCommentListNode): | ||
"""Render the comment list directly in reverse """ | ||
|
||
def render(self, context): | ||
ctype, object_pk = self.get_target_ctype_pk(context) | ||
if object_pk: | ||
template_search_list = [ | ||
"comments/%s/%s/list.html" % (ctype.app_label, ctype.model), | ||
"comments/%s/list.html" % ctype.app_label, | ||
"comments/list.html" | ||
] | ||
qs = self.get_query_set(context).prefetch_related('user').order_by('-id') | ||
context.push() | ||
liststr = render_to_string(template_search_list, { | ||
"comment_list" : self.get_context_value_from_queryset(context, qs) | ||
}, context) | ||
context.pop() | ||
return liststr | ||
else: | ||
return '' | ||
|
||
@register.tag | ||
def render_comment_list_reversed(parser, token): | ||
""" | ||
Render the comment list (as returned by ``{% get_comment_list %}``) | ||
through the ``comments/list.html`` template | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this app is Ajax-sugar coating (and opinionated moderation) over django.contrib.comments (soon to be https://github.com/django/django-contrib-comments) this makes me wonder whether this template tag should be included here. Shouldn't this be part of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that is def true (and I probably overlooked that), but I'm a fan of overriding classes and not messy templates? my subjective view.. but your point is valid. |
||
|
||
but in reverse order | ||
|
||
Syntax:: | ||
|
||
{% render_comment_list_reversed for [object] %} | ||
{% render_comment_list_reversed for [app].[model] [object_id] %} | ||
|
||
Example usage:: | ||
|
||
{% render_comment_list_reversed for event %} | ||
|
||
""" | ||
return RenderCommentListReversedNode.handle_token(parser, token) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra suggestion: I'd vote for using
$.extend
here, so any future settings will have defaults too, regardless of whetherwindow.COMMENT_CONTROLS
lists them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, it bases on the assumption that jquery is a given.. but a godo point yes