Commit 000a745c by Tom Giannattasio

Merge branch 'feature/tomg/new-discussions' of github.com:MITx/mitx into…

Merge branch 'feature/tomg/new-discussions' of github.com:MITx/mitx into feature/tomg/new-discussions
parents a7281341 31f7d6f4
......@@ -6,6 +6,7 @@ if Backbone?
DiscussionUtil.addDiscussion @id, @
@bind "add", (item) =>
item.discussion = @
@comparator = @sortByDate
find: (id) ->
_.first @where(id: id)
......@@ -16,6 +17,19 @@ if Backbone?
@add model
model
sortByDate: (thread) ->
thread.get("created_at")
sortByVotes: (thread1, thread2) ->
thread1_count = parseInt(thread1.get("votes")['up_count'])
thread2_count = parseInt(thread2.get("votes")['up_count'])
thread2_count - thread1_count
sortByComments: (thread1, thread2) ->
thread1_count = parseInt(thread1.get("comments_count"))
thread2_count = parseInt(thread2.get("comments_count"))
thread2_count - thread1_count
class @DiscussionView extends Backbone.View
$: (selector) ->
......
......@@ -7,16 +7,23 @@ class @DiscussionRouter extends Backbone.Router
@discussion = options['discussion']
@nav = new DiscussionThreadListView(collection: @discussion, el: $(".sidebar"))
@nav.on "thread:selected", @navigateToThread
@nav.on "threads:rendered", @setActiveThread
@nav.render()
@main = new DiscussionThreadView(el: $(".discussion-column"))
allThreads: ->
true
setActiveThread: =>
if @thread
@nav.setActiveThread(@thread.get("id"))
showThread: (forum_name, thread_id) ->
@nav.setActiveThread(thread_id)
thread = @discussion.get(thread_id)
@main.model = thread
@thread = @discussion.get(thread_id)
@setActiveThread()
if(@main)
@main.undelegateEvents()
@main = new DiscussionThreadView(el: $(".discussion-column"), model: @thread)
@main.render()
navigateToThread: (thread_id) =>
......
......@@ -3,6 +3,7 @@ class @DiscussionThreadListView extends Backbone.View
events:
"click .search": "showSearch"
"keyup .post-search-field": "performSearch"
"click .sort-bar a": "sortThreads"
render: ->
@timer = 0;
......@@ -14,6 +15,7 @@ class @DiscussionThreadListView extends Backbone.View
renderThreads: =>
@$(".post-list").html("")
@collection.each @renderThreadListItem
@trigger "threads:rendered"
renderThreadListItem: (thread) =>
view = new ThreadListItemView(model: thread)
......@@ -26,7 +28,7 @@ class @DiscussionThreadListView extends Backbone.View
@trigger("thread:selected", thread_id)
setActiveThread: (thread_id) ->
@$("a").removeClass("active")
@$(".post-list a").removeClass("active")
@$("a[data-id='#{thread_id}']").addClass("active")
showSearch: ->
......@@ -34,6 +36,20 @@ class @DiscussionThreadListView extends Backbone.View
@$(".browse").removeClass('is-open');
setTimeout (-> @$(".post-search-field").focus()), 200
sortThreads: (event) ->
@$(".sort-bar a").removeClass("active")
$(event.target).addClass("active")
sortBy = $(event.target).data("sort")
if sortBy == "date"
@collection.comparator = @collection.sortByDate
else if sortBy == "votes"
@collection.comparator = @collection.sortByVotes
else if sortBy == "comments"
@collection.comparator = @collection.sortByComments
@collection.sort()
delay: (callback, ms) =>
clearTimeout(@timer)
@timer = setTimeout(callback, ms)
......
......@@ -18,12 +18,17 @@
$$contents = {};
$$discussions = {};
$$course_id = "${course_id}";
$(document).ready(function() {
DiscussionApp = {
start: function() {
window.user = new DiscussionUser(JSON.parse("${user_info | escapejs}"));
var discussion = new Discussion(JSON.parse("${threads | escapejs}"));
new DiscussionRouter({discussion: discussion});
Backbone.history.start({pushState: true, root: "/courses/${course_id}/discussion/forum/"});
}
}
$(document).ready(function() {
DiscussionApp.start();
});
</script>
</%block>
......
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