Commit 3b7d6671 by Rocky Duan

update comment count based on operations

parent 641c5ae5
...@@ -6,9 +6,9 @@ import inspect ...@@ -6,9 +6,9 @@ import inspect
def pluralize(content, text): def pluralize(content, text):
num, word = text.split(' ') num, word = text.split(' ')
if int(num or '0') >= 2: if int(num or '0') >= 2:
return num + ' ' + word + 's' return word + 's'
else: else:
return num + ' ' + word return word
def url_for_user(content, user_id): def url_for_user(content, user_id):
return urlresolvers.reverse('django_comment_client.forum.views.user_profile', args=[content['course_id'], user_id]) return urlresolvers.reverse('django_comment_client.forum.views.user_profile', args=[content['course_id'], user_id])
......
...@@ -22,17 +22,27 @@ class @Content extends Backbone.Model ...@@ -22,17 +22,27 @@ class @Content extends Backbone.Model
@set('voted', info.voted) @set('voted', info.voted)
@set('subscribed', info.subscribed) @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 @get('children').push comment
model = new Comment $.extend {}, comment, { thread: @get('thread') } model = new Comment $.extend {}, comment, { thread: @get('thread') }
@get('comments').add model @get('comments').add model
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) -> resetComments: (children) ->
@set 'children', [] @set 'children', []
@set 'comments', new Comments() @set 'comments', new Comments()
for comment in (children || []) for comment in (children || [])
@addComment comment @addComment comment, { silent: true }
initialize: -> initialize: ->
DiscussionUtil.addContent @id, @ DiscussionUtil.addContent @id, @
...@@ -66,6 +76,9 @@ class @ContentView extends Backbone.View ...@@ -66,6 +76,9 @@ class @ContentView extends Backbone.View
votes_point: (votes_point) -> votes_point: (votes_point) ->
@$(".discussion-votes-point").html(votes_point) @$(".discussion-votes-point").html(votes_point)
comments_count: (comments_count) ->
@$(".comments-count").html(comments_count)
subscribed: (subscribed) -> #later subscribed: (subscribed) -> #later
...@@ -270,8 +283,9 @@ class @ContentView extends Backbone.View ...@@ -270,8 +283,9 @@ class @ContentView extends Backbone.View
if not c if not c
return return
$elem = $(event.target) $elem = $(event.target)
DiscussionUtil.post $elem, url, {}, (response, textStatus) => @$el.remove() DiscussionUtil.post $elem, url, {}, (response, textStatus) =>
#TODO also do data-wise operation @$el.remove()
@model.get('thread').removeComment(@model)
events: events:
"click .thread-title": "toggleSingleThread" "click .thread-title": "toggleSingleThread"
...@@ -354,6 +368,12 @@ class @Comment extends @Content ...@@ -354,6 +368,12 @@ class @Comment extends @Content
discussion_id = @get('thread').get('commentable_id') discussion_id = @get('thread').get('commentable_id')
return Discussion.urlFor("permanent_link_comment", discussion_id, thread_id, @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 @CommentView extends @ContentView
class @Comments extends Backbone.Collection class @Comments extends Backbone.Collection
......
...@@ -340,7 +340,7 @@ $tag-text-color: #5b614f; ...@@ -340,7 +340,7 @@ $tag-text-color: #5b614f;
margin-right: 1em; margin-right: 1em;
} }
.comment-count { .show-comments-wrapper {
display: inline; display: inline;
margin-right: 20px; margin-right: 20px;
} }
......
...@@ -40,13 +40,13 @@ ...@@ -40,13 +40,13 @@
<a href="{{##url_for_user}}{{content.user_id}}{{/url_for_user}}">{{content.username}}</a> <a href="{{##url_for_user}}{{content.user_id}}{{/url_for_user}}">{{content.username}}</a>
{{/content.anonymous}} {{/content.anonymous}}
</div> </div>
<div class="comment-count"> <div class="show-comments-wrapper">
{{#thread}} {{#thread}}
{{#partial_comments}} {{#partial_comments}}
<a href="javascript:void(0)" class="discussion-show-comments first-time">Show all comments ({{content.comments_count}} total)</a> <a href="javascript:void(0)" class="discussion-show-comments first-time">Show all comments (<span class="comments-count">{{content.comments_count}}</span> total)</a>
{{/partial_comments}} {{/partial_comments}}
{{^partial_comments}} {{^partial_comments}}
<a href="javascript:void(0)" class="discussion-show-comments">Show {{##pluralize}}{{content.comments_count}} comment{{/pluralize}}</a> <a href="javascript:void(0)" class="discussion-show-comments">Show <span class="comments-count">{{content.comments_count}}</span> {{##pluralize}}{{content.comments_count}} comment{{/pluralize}}</a>
{{/partial_comments}} {{/partial_comments}}
{{/thread}} {{/thread}}
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment