From 11e3276e807cc01d1b981e86afeda4fd5b4d0cd7 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Tue, 21 Aug 2012 17:43:20 -0700 Subject: [PATCH] update comment count based on operations --- .../django_comment_client/mustache_helpers.py | 4 +-- .../src/backbone_discussion/content.coffee | 28 ++++++++++++++++--- lms/static/sass/_discussion.scss | 2 +- .../discussion/mustache/_content.mustache | 6 ++-- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lms/djangoapps/django_comment_client/mustache_helpers.py b/lms/djangoapps/django_comment_client/mustache_helpers.py index b3b5daaa01..c8e8e90c0a 100644 --- a/lms/djangoapps/django_comment_client/mustache_helpers.py +++ b/lms/djangoapps/django_comment_client/mustache_helpers.py @@ -6,9 +6,9 @@ import inspect def pluralize(content, text): num, word = text.split(' ') if int(num or '0') >= 2: - return num + ' ' + word + 's' + return word + 's' else: - return num + ' ' + word + return word def url_for_user(content, user_id): return urlresolvers.reverse('django_comment_client.forum.views.user_profile', args=[content['course_id'], user_id]) diff --git a/lms/static/coffee/src/backbone_discussion/content.coffee b/lms/static/coffee/src/backbone_discussion/content.coffee index 626623e08c..431c97a7aa 100644 --- a/lms/static/coffee/src/backbone_discussion/content.coffee +++ b/lms/static/coffee/src/backbone_discussion/content.coffee @@ -22,17 +22,27 @@ class @Content extends Backbone.Model @set('voted', info.voted) @set('subscribed', info.subscribed) - addComment: (comment) -> + addComment: (comment, options) -> + options ||= {} + if not options.silent + thread = @get('thread') + comments_count = parseInt(thread.get('comments_count')) + thread.set('comments_count', comments_count + 1) @get('children').push comment model = new Comment $.extend {}, comment, { thread: @get('thread') } @get('comments').add model model + removeComment: (comment) -> + thread = @get('thread') + comments_count = parseInt(thread.get('comments_count')) + thread.set('comments_count', comments_count - 1 - comment.getCommentsCount()) + resetComments: (children) -> @set 'children', [] @set 'comments', new Comments() for comment in (children || []) - @addComment comment + @addComment comment, { silent: true } initialize: -> DiscussionUtil.addContent @id, @ @@ -66,6 +76,9 @@ class @ContentView extends Backbone.View votes_point: (votes_point) -> @$(".discussion-votes-point").html(votes_point) + + comments_count: (comments_count) -> + @$(".comments-count").html(comments_count) subscribed: (subscribed) -> #later @@ -270,8 +283,9 @@ class @ContentView extends Backbone.View if not c return $elem = $(event.target) - DiscussionUtil.post $elem, url, {}, (response, textStatus) => @$el.remove() - #TODO also do data-wise operation + DiscussionUtil.post $elem, url, {}, (response, textStatus) => + @$el.remove() + @model.get('thread').removeComment(@model) events: "click .thread-title": "toggleSingleThread" @@ -354,6 +368,12 @@ class @Comment extends @Content discussion_id = @get('thread').get('commentable_id') return Discussion.urlFor("permanent_link_comment", discussion_id, thread_id, @id) + getCommentsCount: -> + count = 0 + @get('comments').each (comment) -> + count += comment.getCommentsCount() + 1 + count + class @CommentView extends @ContentView class @Comments extends Backbone.Collection diff --git a/lms/static/sass/_discussion.scss b/lms/static/sass/_discussion.scss index 93c435a040..dd71966927 100644 --- a/lms/static/sass/_discussion.scss +++ b/lms/static/sass/_discussion.scss @@ -340,7 +340,7 @@ $tag-text-color: #5b614f; margin-right: 1em; } - .comment-count { + .show-comments-wrapper { display: inline; margin-right: 20px; } diff --git a/lms/templates/discussion/mustache/_content.mustache b/lms/templates/discussion/mustache/_content.mustache index 24be67ec91..63ef2b41ab 100644 --- a/lms/templates/discussion/mustache/_content.mustache +++ b/lms/templates/discussion/mustache/_content.mustache @@ -40,13 +40,13 @@ {{content.username}} {{/content.anonymous}} -
+