Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ The templates and admin interface adapt themselves automatically
to show the threaded comments.


Javascript Controls
-------------------

You are able to control various output settings via javascript.

1. is_reversed: Will use prepend instead of append to insert the ajax created comment


Insert the following code, that does not wait for document.ready

```
<script>
var COMMENT_CONTROLS = {
'is_reversed': true,
}
</script>
```

Contributing
------------

Expand Down
4 changes: 2 additions & 2 deletions fluent_comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def on_comment_posted(sender, comment, request, **kwargs):
'content_object': content_object
}

message = render(request, "comments/comment_notification_email.txt", context)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
message = unicode(render(request, "comments/comment_notification_email.txt", context))
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=False)


def get_comments_for_model(content_object, include_moderated=False):
Expand Down
19 changes: 14 additions & 5 deletions fluent_comments/static/fluent_comments/js/ajaxcomments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Copy link
Contributor

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 whether window.COMMENT_CONTROLS lists them.

Copy link
Author

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


var commentform = $('form.js-comments-form');
if( commentform.length > 0 )
{
Expand Down Expand Up @@ -93,7 +98,7 @@

function scrollToElement( $element, speed, offset )
{
if( $element.length )
if( $element.length && COMMENT_CONTROLS.scroll_to_comment === true )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why the explicit === true ?

Copy link
Author

Choose a reason for hiding this comment

The 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.
var test = {};
test.item = 'false'
"false"
if (test.item) { console.log('yay') }

$(scrollElement).animate( {scrollTop: $element.offset().top - (offset || 0) }, speed || 1000 );
}

Expand Down Expand Up @@ -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));
Expand Down
46 changes: 45 additions & 1 deletion fluent_comments/templatetags/fluent_comments_tags.py
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)
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 comments module itself,
or be an overwritten comments/list.html template that does something like {% for comment in comments|reversed %}?

Copy link
Author

Choose a reason for hiding this comment

The 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)