diff --git a/lms/static/coffee/src/discussion/content.coffee b/lms/static/coffee/src/discussion/content.coffee index ee6c50ecf7..3e175d3634 100644 --- a/lms/static/coffee/src/discussion/content.coffee +++ b/lms/static/coffee/src/discussion/content.coffee @@ -36,12 +36,14 @@ if Backbone? @get('children').push comment model = new Comment $.extend {}, comment, { thread: @get('thread') } @get('comments').add model + @trigger "comment:add" model removeComment: (comment) -> thread = @get('thread') comments_count = parseInt(thread.get('comments_count')) thread.set('comments_count', comments_count - 1 - comment.getCommentsCount()) + @trigger "comment:remove" resetComments: (children) -> @set 'children', [] @@ -56,6 +58,14 @@ if Backbone? @set 'user_url', DiscussionUtil.urlFor('user_profile', @get('user_id')) @resetComments(@get('children')) + remove: -> + + if @get('type') == 'comment' + @get('thread').removeComment(@) + @get('thread').trigger "comment:remove", @ + else + @trigger "thread:remove", @ + @addContent: (id, content) -> @contents[id] = content @getContent: (id) -> @contents[id] diff --git a/lms/static/coffee/src/discussion/discussion.coffee b/lms/static/coffee/src/discussion/discussion.coffee index 148ef64891..002593855c 100644 --- a/lms/static/coffee/src/discussion/discussion.coffee +++ b/lms/static/coffee/src/discussion/discussion.coffee @@ -6,6 +6,9 @@ if Backbone? @bind "add", (item) => item.discussion = @ @comparator = @sortByDateRecentFirst + @on "thread:remove", (thread) => + console.log "remove triggered" + @remove(thread) find: (id) -> _.first @where(id: id) diff --git a/lms/static/coffee/src/discussion/discussion_router.coffee b/lms/static/coffee/src/discussion/discussion_router.coffee index 4dc599898a..3a183e4d3b 100644 --- a/lms/static/coffee/src/discussion/discussion_router.coffee +++ b/lms/static/coffee/src/discussion/discussion_router.coffee @@ -7,6 +7,7 @@ class @DiscussionRouter extends Backbone.Router @discussion = options['discussion'] @nav = new DiscussionThreadListView(collection: @discussion, el: $(".sidebar")) @nav.on "thread:selected", @navigateToThread + @nav.on "thread:removed", @navigateToAllThreads @nav.on "threads:rendered", @setActiveThread @nav.render() @@ -33,3 +34,7 @@ class @DiscussionRouter extends Backbone.Router navigateToThread: (thread_id) => thread = @discussion.get(thread_id) @navigate("#{thread.get("commentable_id")}/threads/#{thread_id}", trigger: true) + + navigateToAllThreads: => + console.log "navigating" + @navigate("", trigger: true) diff --git a/lms/static/coffee/src/discussion/views/discussion_thread_list_view.coffee b/lms/static/coffee/src/discussion/views/discussion_thread_list_view.coffee index e485fbfb60..fd7f7d0017 100644 --- a/lms/static/coffee/src/discussion/views/discussion_thread_list_view.coffee +++ b/lms/static/coffee/src/discussion/views/discussion_thread_list_view.coffee @@ -21,6 +21,7 @@ class @DiscussionThreadListView extends Backbone.View @timer = 0 @$el.html(@template()) @displayedCollection.on "reset", @renderThreads + @displayedCollection.on "thread:remove", @renderThreads @renderThreads() @ @@ -30,8 +31,10 @@ class @DiscussionThreadListView extends Backbone.View @trigger "threads:rendered" renderThreadListItem: (thread) => + console.log "rendering" view = new ThreadListItemView(model: thread) view.on "thread:selected", @threadSelected + view.on "thread:removed", @threadRemoved view.render() @$(".post-list").append(view.el) @@ -39,6 +42,9 @@ class @DiscussionThreadListView extends Backbone.View @setActiveThread(thread_id) @trigger("thread:selected", thread_id) + threadRemoved: (thread_id) => + @trigger("thread:removed", thread_id) + setActiveThread: (thread_id) -> @$(".post-list a").removeClass("active") @$("a[data-id='#{thread_id}']").addClass("active") diff --git a/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee b/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee index 3dddb7d901..52a663153d 100644 --- a/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee +++ b/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee @@ -60,7 +60,7 @@ class @DiscussionThreadView extends DiscussionContentView @$(".responses").append(view.el) addComment: => - @model.trigger "comment:add" + toggleVote: (event) -> event.preventDefault() @@ -110,8 +110,9 @@ class @DiscussionThreadView extends DiscussionContentView url = @model.urlFor('reply') body = @$("#wmd-input").val() response = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), votes: { up_count: 0 }, endorsed: false, user_id: window.user.get("id")) + response.set('thread', @model.get('thread')) @renderResponse(response) - @addComment() + @model.addComment() DiscussionUtil.safeAjax $elem: $(event.target) @@ -123,8 +124,18 @@ class @DiscussionThreadView extends DiscussionContentView edit: -> - delete: -> - + delete: (event) -> + url = @model.urlFor('delete') + if not confirm "Are you sure to delete thread \"#{@model.get('title')}\"?" + return + @model.remove() + $elem = $(event.target) + DiscussionUtil.safeAjax + $elem: $elem + url: url + type: "POST" + success: (response, textStatus) => + toggleClosed: (event) -> $elem = $(event.target) url = @model.urlFor('close') diff --git a/lms/static/coffee/src/discussion/views/thread_list_item_view.coffee b/lms/static/coffee/src/discussion/views/thread_list_item_view.coffee index 5cf2954357..a437dbd593 100644 --- a/lms/static/coffee/src/discussion/views/thread_list_item_view.coffee +++ b/lms/static/coffee/src/discussion/views/thread_list_item_view.coffee @@ -5,23 +5,32 @@ class @ThreadListItemView extends Backbone.View "click a": "threadSelected" initialize: -> @model.on "change", @render + @model.on "thread:remove", @threadRemoved @model.on "thread:follow", @follow @model.on "thread:unfollow", @unfollow @model.on "comment:add", @addComment + @model.on "comment:remove", @removeComment render: => @$el.html(@template(@model.toJSON())) if window.user.following(@model) @follow() @ + threadSelected: (event) -> event.preventDefault() @trigger("thread:selected", @model.id) + threadRemoved: => + @trigger("thread:removed", @model.id) + follow: => @$("a").addClass("followed") unfollow: => @$("a").removeClass("followed") - addComment: => - @$(".comments-count").html(parseInt(@$(".comments-count").html()) + 1) + addComment: (comment) => + @$(".comments-count").html(@model.get('comments_count')) + + removeComment: (comment) => + @$(".comments-count").html(@model.get('comments_count')) diff --git a/lms/static/coffee/src/discussion/views/thread_response_view.coffee b/lms/static/coffee/src/discussion/views/thread_response_view.coffee index ceb604994c..f2dd24730b 100644 --- a/lms/static/coffee/src/discussion/views/thread_response_view.coffee +++ b/lms/static/coffee/src/discussion/views/thread_response_view.coffee @@ -68,7 +68,7 @@ class @ThreadResponseView extends DiscussionContentView return comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), user_id: window.user.get("id")) @renderComment(comment) - @trigger "comment:add" + @trigger "comment:add", comment @$(".comment-form-input").val("") DiscussionUtil.safeAjax