Commit 11e3276e by Rocky Duan

update comment count based on operations

parent 273af4a5
......@@ -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])
......
......@@ -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
......
......@@ -340,7 +340,7 @@ $tag-text-color: #5b614f;
margin-right: 1em;
}
.comment-count {
.show-comments-wrapper {
display: inline;
margin-right: 20px;
}
......
......@@ -40,13 +40,13 @@
<a href="{{##url_for_user}}{{content.user_id}}{{/url_for_user}}">{{content.username}}</a>
{{/content.anonymous}}
</div>
<div class="comment-count">
<div class="show-comments-wrapper">
{{#thread}}
{{#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}}
<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}}
{{/thread}}
</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