diff --git a/common/static/common/js/discussion/views/discussion_thread_list_view.js b/common/static/common/js/discussion/views/discussion_thread_list_view.js index 7ab85715d6..c6a8c379d0 100644 --- a/common/static/common/js/discussion/views/discussion_thread_list_view.js +++ b/common/static/common/js/discussion/views/discussion_thread_list_view.js @@ -107,6 +107,7 @@ this.boardName = null; this.current_search = ''; this.mode = 'all'; + this.showThreadPreview = options.showThreadPreview; this.searchAlertCollection = new Backbone.Collection([], { model: Backbone.Model }); @@ -316,14 +317,26 @@ }, error); }; + DiscussionThreadListView.prototype.containsMarkup = function(threadBody) { + var imagePostSearchString = '![', + mathJaxSearchString = /\$/g, + containsImages = threadBody.indexOf(imagePostSearchString) !== -1, + // mathJax has to have at least 2 dollar signs + containsMathJax = (threadBody.match(mathJaxSearchString) || []).length > 1; + return containsImages || containsMathJax; + }; + DiscussionThreadListView.prototype.renderThread = function(thread) { var threadCommentCount = thread.get('comments_count'), threadUnreadCommentCount = thread.get('unread_comments_count'), neverRead = !thread.get('read') && threadUnreadCommentCount === threadCommentCount, + threadPreview = this.containsMarkup(thread.get('body')) ? '' : thread.get('body'), context = _.extend( { neverRead: neverRead, - threadUrl: thread.urlFor('retrieve') + threadUrl: thread.urlFor('retrieve'), + threadPreview: threadPreview, + showThreadPreview: this.showThreadPreview }, thread.toJSON() ); diff --git a/common/static/common/js/spec/discussion/view/discussion_thread_list_view_spec.js b/common/static/common/js/spec/discussion/view/discussion_thread_list_view_spec.js index b142c6b784..ae33cba5fc 100644 --- a/common/static/common/js/spec/discussion/view/discussion_thread_list_view_spec.js +++ b/common/static/common/js/spec/discussion/view/discussion_thread_list_view_spec.js @@ -83,10 +83,12 @@ renderSingleThreadWithProps = function(props) { return makeView(new Discussion([new Thread(DiscussionViewSpecHelper.makeThreadWithProps(props))])).render(); }; - makeView = function(discussion) { + makeView = function(discussion, options) { + var opts = options || {}; return new DiscussionThreadListView({ el: $('#fixture-element'), collection: discussion, + showThreadPreview: opts.showThreadPreview || true, courseSettings: new DiscussionCourseSettings({ is_cohorted: true }) @@ -542,5 +544,40 @@ }); }); + describe('thread preview body', function() { + it('should be shown when showThreadPreview is true', function() { + renderSingleThreadWithProps({ + thread_type: 'discussion' + }); + expect($('.thread-preview-body').length).toEqual(1); + }); + + it('should not show image when showThreadPreview is true', function() { + renderSingleThreadWithProps({ + thread_type: 'discussion', + body: '![customizedImageAltTitle].png' + }); + expect($('.thread-preview-body').text()).toEqual(''); + }); + + it('should not show MathJax when showThreadPreview is true', function() { + renderSingleThreadWithProps({ + thread_type: 'discussion', + body: '$$x^2 + sqrt(y)$$' + }); + expect($('.thread-preview-body').text()).toEqual(''); + }); + + it('should not be shown when showThreadPreview is false', function() { + var view, + discussion = new Discussion([]), + options = { + showThreadPreview: false + }; + view = makeView(discussion, options); + view.render(); + expect(view.$el.find('.thread-preview-body').length).toEqual(0); + }); + }); }); }).call(this); diff --git a/common/static/common/templates/discussion/thread-list-item.underscore b/common/static/common/templates/discussion/thread-list-item.underscore index dfc578382d..d57aec8630 100644 --- a/common/static/common/templates/discussion/thread-list-item.underscore +++ b/common/static/common/templates/discussion/thread-list-item.underscore @@ -21,6 +21,9 @@
<%- title %> + <% if (showThreadPreview){ %> +
<%- threadPreview %>
+ <% } %> <% if(typeof(subscribed) === "undefined") { var subscribed = null; } %> <% if(pinned || subscribed || staff_authored || community_ta_authored) { %>