Commit eaa93bf6 by Greg Price

Hide comments in marked answers in forum questions

A link to show the comments appears in their place.
parent ec482c0d
describe 'ThreadResponseView', -> describe 'ThreadResponseView', ->
beforeEach -> beforeEach ->
DiscussionSpecHelper.setUpGlobals()
setFixtures """ setFixtures """
<script id="thread-response-template" type="text/template"> <script id="thread-response-template" type="text/template">
<div/> <a href="#" class="action-show-comments">Show comments</a>
<ol class="comments"></ol>
</script> </script>
<div id="thread-response-fixture"/> <div id="thread-response-fixture"/>
""" """
...@@ -14,6 +16,33 @@ describe 'ThreadResponseView', -> ...@@ -14,6 +16,33 @@ describe 'ThreadResponseView', ->
spyOn(ResponseCommentView.prototype, "render") spyOn(ResponseCommentView.prototype, "render")
describe 'renderComments', -> describe 'renderComments', ->
it 'hides "show comments" link if collapseComments is not set', ->
@view.render()
expect(@view.$(".comments")).toBeVisible()
expect(@view.$(".action-show-comments")).not.toBeVisible()
it 'hides "show comments" link if collapseComments is set but response has no comments', ->
@response = new Comment { children: [] }
@view = new ThreadResponseView({
model: @response, el: $("#thread-response-fixture"),
collapseComments: true
})
@view.render()
expect(@view.$(".comments")).toBeVisible()
expect(@view.$(".action-show-comments")).not.toBeVisible()
it 'hides comments if collapseComments is set and shows them when "show comments" link is clicked', ->
@view = new ThreadResponseView({
model: @response, el: $("#thread-response-fixture"),
collapseComments: true
})
@view.render()
expect(@view.$(".comments")).not.toBeVisible()
expect(@view.$(".action-show-comments")).toBeVisible()
@view.$(".action-show-comments").click()
expect(@view.$(".comments")).toBeVisible()
expect(@view.$(".action-show-comments")).not.toBeVisible()
it 'populates commentViews and binds events', -> it 'populates commentViews and binds events', ->
# Ensure that edit view is set to test invocation of cancelEdit # Ensure that edit view is set to test invocation of cancelEdit
@view.createEditView() @view.createEditView()
......
...@@ -41,9 +41,9 @@ if Backbone? ...@@ -41,9 +41,9 @@ if Backbone?
@$("span.timeago").timeago() @$("span.timeago").timeago()
@makeWmdEditor "reply-body" @makeWmdEditor "reply-body"
@renderAddResponseButton() @renderAddResponseButton()
@responses.on("add", (response) => @renderResponseToList(response, ".js-response-list")) @responses.on("add", (response) => @renderResponseToList(response, ".js-response-list", {}))
if @isQuestion() if @isQuestion()
@markedAnswers.on("add", (response) => @renderResponseToList(response, ".js-marked-answer-list")) @markedAnswers.on("add", (response) => @renderResponseToList(response, ".js-marked-answer-list", {collapseComments: true}))
if @mode == "tab" if @mode == "tab"
# Without a delay, jQuery doesn't add the loading extension defined in # Without a delay, jQuery doesn't add the loading extension defined in
# utils.coffee before safeAjax is invoked, which results in an error # utils.coffee before safeAjax is invoked, which results in an error
...@@ -191,9 +191,9 @@ if Backbone? ...@@ -191,9 +191,9 @@ if Backbone?
loadMoreButton.click((event) => @loadResponses(responseLimit, loadMoreButton)) loadMoreButton.click((event) => @loadResponses(responseLimit, loadMoreButton))
responsePagination.append(loadMoreButton) responsePagination.append(loadMoreButton)
renderResponseToList: (response, listSelector) => renderResponseToList: (response, listSelector, options) =>
response.set('thread', @model) response.set('thread', @model)
view = new ThreadResponseView(model: response) view = new ThreadResponseView($.extend({model: response}, options))
view.on "comment:add", @addComment view.on "comment:add", @addComment
view.on "comment:endorse", @endorseThread view.on "comment:endorse", @endorseThread
view.render() view.render()
......
if Backbone? if Backbone?
class @ThreadResponseView extends DiscussionContentView class @ThreadResponseView extends DiscussionContentView
tagName: "li" tagName: "li"
className: "forum-response"
events: events:
"click .discussion-submit-comment": "submitComment" "click .discussion-submit-comment": "submitComment"
...@@ -9,7 +10,8 @@ if Backbone? ...@@ -9,7 +10,8 @@ if Backbone?
$: (selector) -> $: (selector) ->
@$el.find(selector) @$el.find(selector)
initialize: -> initialize: (options) ->
@collapseComments = options.collapseComments
@createShowView() @createShowView()
renderTemplate: -> renderTemplate: ->
...@@ -65,6 +67,15 @@ if Backbone? ...@@ -65,6 +67,15 @@ if Backbone?
collectComments(child) collectComments(child)
@model.get('comments').each collectComments @model.get('comments').each collectComments
comments.each (comment) => @renderComment(comment, false, null) comments.each (comment) => @renderComment(comment, false, null)
if @collapseComments && comments.length
@$(".comments").hide()
@$(".action-show-comments").on("click", (event) =>
event.preventDefault()
@$(".action-show-comments").hide()
@$(".comments").show()
)
else
@$(".action-show-comments").hide()
renderComment: (comment) => renderComment: (comment) =>
comment.set('thread', @model.get('thread')) comment.set('thread', @model.get('thread'))
......
...@@ -88,7 +88,13 @@ class SingleThreadViewFixture(DiscussionContentFixture): ...@@ -88,7 +88,13 @@ class SingleThreadViewFixture(DiscussionContentFixture):
def addResponse(self, response, comments=[]): def addResponse(self, response, comments=[]):
response['children'] = comments response['children'] = comments
self.thread.setdefault('children', []).append(response) if self.thread["thread_type"] == "discussion":
responseListAttr = "children"
elif response["endorsed"]:
responseListAttr = "endorsed_responses"
else:
responseListAttr = "non_endorsed_responses"
self.thread.setdefault(responseListAttr, []).append(response)
self.thread['comments_count'] += len(comments) + 1 self.thread['comments_count'] += len(comments) + 1
def _get_comment_map(self): def _get_comment_map(self):
......
...@@ -95,6 +95,18 @@ class DiscussionThreadPage(PageObject, DiscussionPageMixin): ...@@ -95,6 +95,18 @@ class DiscussionThreadPage(PageObject, DiscussionPageMixin):
"Response edit started" "Response edit started"
).fulfill() ).fulfill()
def is_show_comments_visible(self, response_id):
"""Returns true if the "show comments" link is visible for a response"""
return self._is_element_visible(".response_{} .action-show-comments".format(response_id))
def show_comments(self, response_id):
"""Click the "show comments" link for a response"""
self._find_within(".response_{} .action-show-comments".format(response_id)).first.click()
EmptyPromise(
lambda: self._is_element_visible(".response_{} .comments".format(response_id)),
"Comments shown"
).fulfill()
def is_add_comment_visible(self, response_id): def is_add_comment_visible(self, response_id):
"""Returns true if the "add comment" form is visible for a response""" """Returns true if the "add comment" form is visible for a response"""
return self._is_element_visible("#wmd-input-comment-body-{}".format(response_id)) return self._is_element_visible("#wmd-input-comment-body-{}".format(response_id))
......
...@@ -144,6 +144,27 @@ class DiscussionTabSingleThreadTest(UniqueCourseTest, DiscussionResponsePaginati ...@@ -144,6 +144,27 @@ class DiscussionTabSingleThreadTest(UniqueCourseTest, DiscussionResponsePaginati
self.thread_page = DiscussionTabSingleThreadPage(self.browser, self.course_id, thread_id) # pylint:disable=W0201 self.thread_page = DiscussionTabSingleThreadPage(self.browser, self.course_id, thread_id) # pylint:disable=W0201
self.thread_page.visit() self.thread_page.visit()
def test_marked_answer_comments(self):
thread_id = "test_thread_{}".format(uuid4().hex)
response_id = "test_response_{}".format(uuid4().hex)
comment_id = "test_comment_{}".format(uuid4().hex)
thread_fixture = SingleThreadViewFixture(
Thread(id=thread_id, commentable_id=self.discussion_id, thread_type="question")
)
thread_fixture.addResponse(
Response(id=response_id, endorsed=True),
[Comment(id=comment_id)]
)
thread_fixture.push()
self.setup_thread_page(thread_id)
self.assertFalse(self.thread_page.is_comment_visible(comment_id))
self.assertFalse(self.thread_page.is_add_comment_visible(response_id))
self.assertTrue(self.thread_page.is_show_comments_visible(response_id))
self.thread_page.show_comments(response_id)
self.assertTrue(self.thread_page.is_comment_visible(comment_id))
self.assertTrue(self.thread_page.is_add_comment_visible(response_id))
self.assertFalse(self.thread_page.is_show_comments_visible(response_id))
@attr('shard_1') @attr('shard_1')
class DiscussionCommentDeletionTest(UniqueCourseTest): class DiscussionCommentDeletionTest(UniqueCourseTest):
......
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
@import "discussion/elements/editor"; @import "discussion/elements/editor";
@import "discussion/elements/navigation"; @import "discussion/elements/navigation";
@import "discussion/views/thread"; @import "discussion/views/thread";
@import "discussion/views/response";
@import 'discussion/utilities/developer'; @import 'discussion/utilities/developer';
@import 'discussion/utilities/shame'; @import 'discussion/utilities/shame';
......
.forum-response .action-show-comments {
@include box-sizing(border-box);
@include font-size(13);
display: block;
padding: ($baseline/2) $baseline;
width: 100%;
background: $gray-l6;
box-shadow: 0 1px 3px -1px $shadow inset;
}
...@@ -133,6 +133,10 @@ ...@@ -133,6 +133,10 @@
<script aria-hidden="true" type="text/template" id="thread-response-template"> <script aria-hidden="true" type="text/template" id="thread-response-template">
<div class="discussion-response"></div> <div class="discussion-response"></div>
<a href="#" class="action-show-comments">
${u"<%- interpolate('{}', {{num_comments: comments.length}}, true) %>".format(escapejs(_("Show Comments (%(num_comments)s)")))}
<i class="icon icon-caret-down"></i>
</a>
<ol class="comments"> <ol class="comments">
<li class="new-comment"> <li class="new-comment">
% if course is UNDEFINED or has_permission(user, 'create_sub_comment', course.id): % if course is UNDEFINED or has_permission(user, 'create_sub_comment', course.id):
......
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