Commit 5b9967e5 by Matthew Mongeau

Paging wip

parent 031a7d4e
...@@ -22,7 +22,7 @@ import django_comment_client.utils as utils ...@@ -22,7 +22,7 @@ import django_comment_client.utils as utils
import comment_client as cc import comment_client as cc
import xml.sax.saxutils as saxutils import xml.sax.saxutils as saxutils
THREADS_PER_PAGE = 200 THREADS_PER_PAGE = 2
INLINE_THREADS_PER_PAGE = 5 INLINE_THREADS_PER_PAGE = 5
PAGES_NEARBY_DELTA = 2 PAGES_NEARBY_DELTA = 2
escapedict = {'"': '"'} escapedict = {'"': '"'}
...@@ -224,6 +224,7 @@ def forum_form_discussion(request, course_id): ...@@ -224,6 +224,7 @@ def forum_form_discussion(request, course_id):
#'trending_tags': trending_tags, #'trending_tags': trending_tags,
'staff_access' : has_access(request.user, course, 'staff'), 'staff_access' : has_access(request.user, course, 'staff'),
'threads': saxutils.escape(json.dumps(threads),escapedict), 'threads': saxutils.escape(json.dumps(threads),escapedict),
'thread_pages': query_params['num_pages'],
'user_info': saxutils.escape(json.dumps(user_info),escapedict), 'user_info': saxutils.escape(json.dumps(user_info),escapedict),
'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info),escapedict), 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info),escapedict),
'course_id': course.id, 'course_id': course.id,
......
...@@ -2,7 +2,9 @@ if Backbone? ...@@ -2,7 +2,9 @@ if Backbone?
class @Discussion extends Backbone.Collection class @Discussion extends Backbone.Collection
model: Thread model: Thread
initialize: -> initialize: (models, options)->
@pages = options['pages'] || 1
@current_page = 1
@bind "add", (item) => @bind "add", (item) =>
item.discussion = @ item.discussion = @
@comparator = @sortByDateRecentFirst @comparator = @sortByDateRecentFirst
...@@ -12,12 +14,30 @@ if Backbone? ...@@ -12,12 +14,30 @@ if Backbone?
find: (id) -> find: (id) ->
_.first @where(id: id) _.first @where(id: id)
hasMorePages: ->
@current_page < @pages
addThread: (thread, options) -> addThread: (thread, options) ->
options ||= {} options ||= {}
model = new Thread thread model = new Thread thread
@add model @add model
model model
retrieveAnotherPage: ->
@current_page += 1
url = DiscussionUtil.urlFor 'threads'
data = { page: @current_page }
DiscussionUtil.safeAjax
$elem: @$el
url: url
data: data
dataType: 'json'
success: (response, textStatus) =>
models = @models
new_threads = [new Thread(data) for data in response.discussion_data][0]
new_collection = _.union(models, new_threads)
@reset new_collection
sortByDate: (thread) -> sortByDate: (thread) ->
thread.get("created_at") thread.get("created_at")
......
...@@ -7,10 +7,11 @@ if Backbone? ...@@ -7,10 +7,11 @@ if Backbone?
window.$$course_id = element.data("course-id") window.$$course_id = element.data("course-id")
user_info = element.data("user-info") user_info = element.data("user-info")
threads = element.data("threads") threads = element.data("threads")
thread_pages = element.data("thread-pages")
content_info = element.data("content-info") content_info = element.data("content-info")
window.user = new DiscussionUser(user_info) window.user = new DiscussionUser(user_info)
Content.loadContentInfos(content_info) Content.loadContentInfos(content_info)
discussion = new Discussion(threads) discussion = new Discussion(threads, pages: thread_pages)
new DiscussionRouter({discussion: discussion}) new DiscussionRouter({discussion: discussion})
Backbone.history.start({pushState: true, root: "/courses/#{$$course_id}/discussion/forum/"}) Backbone.history.start({pushState: true, root: "/courses/#{$$course_id}/discussion/forum/"})
DiscussionProfileApp = DiscussionProfileApp =
......
...@@ -66,6 +66,7 @@ class @DiscussionUtil ...@@ -66,6 +66,7 @@ class @DiscussionUtil
permanent_link_thread : "/courses/#{$$course_id}/discussion/forum/#{param}/threads/#{param1}" permanent_link_thread : "/courses/#{$$course_id}/discussion/forum/#{param}/threads/#{param1}"
permanent_link_comment : "/courses/#{$$course_id}/discussion/forum/#{param}/threads/#{param1}##{param2}" permanent_link_comment : "/courses/#{$$course_id}/discussion/forum/#{param}/threads/#{param1}##{param2}"
user_profile : "/courses/#{$$course_id}/discussion/forum/users/#{param}" user_profile : "/courses/#{$$course_id}/discussion/forum/users/#{param}"
threads : "/courses/#{$$course_id}/discussion/forum"
}[name] }[name]
@safeAjax: (params) -> @safeAjax: (params) ->
......
...@@ -7,11 +7,20 @@ if Backbone? ...@@ -7,11 +7,20 @@ if Backbone?
"click .sort-bar a": "sortThreads" "click .sort-bar a": "sortThreads"
"click .browse-topic-drop-menu": "filterTopic" "click .browse-topic-drop-menu": "filterTopic"
"click .browse-topic-drop-search-input": "ignoreClick" "click .browse-topic-drop-search-input": "ignoreClick"
"click .post-list a": "threadSelected" "click .post-list .list-item a": "threadSelected"
"click .post-list .more-pages a": "loadMorePages"
initialize: -> initialize: ->
@displayedCollection = new Discussion(@collection.models) @displayedCollection = new Discussion(@collection.models, pages: @collection.pages)
@collection.on "change", @reloadDisplayedCollection @collection.on "change", @reloadDisplayedCollection
@collection.on "reset", (discussion) =>
board = $(".current-board").html()
@displayedCollection.current_page = discussion.current_page
@displayedCollection.reset discussion.models
# TODO: filter correctly
# target = _.filter($("a.topic:contains('#{board}')"), (el) -> el.innerText == "General" || el.innerHTML == "General")
# if target.length > 0
# @filterTopic($.Event("filter", {'target': target[0]}))
@collection.on "add", @addAndSelectThread @collection.on "add", @addAndSelectThread
@sidebar_padding = 10 @sidebar_padding = 10
@sidebar_header_height = 87 @sidebar_header_height = 87
...@@ -96,11 +105,21 @@ if Backbone? ...@@ -96,11 +105,21 @@ if Backbone?
for thread in @displayedCollection.models for thread in @displayedCollection.models
content = @renderThread(thread) content = @renderThread(thread)
rendered.append content rendered.append content
content.wrap("<li data-id='#{thread.get('id')}' />") content.wrap("<li class='list-item' data-id='#{thread.get('id')}' />")
@$(".post-list").html(rendered.html()) @$(".post-list").html(rendered.html())
@renderMorePages()
@trigger "threads:rendered" @trigger "threads:rendered"
renderMorePages: ->
if @displayedCollection.hasMorePages()
@$(".post-list").append("<li class='more-pages'><a href='#'>Load more</a></li>")
loadMorePages: ->
@$(".more-pages").html('<div class="loading-animation"></div>')
@$(".more-pages").addClass("loading")
@collection.retrieveAnotherPage()
renderThread: (thread) => renderThread: (thread) =>
content = $(_.template($("#thread-list-item-template").html())(thread.toJSON())) content = $(_.template($("#thread-list-item-template").html())(thread.toJSON()))
if thread.get('subscribed') if thread.get('subscribed')
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
<script type="text/javascript" src="${static.url('js/discussions-temp.js')}"></script> <script type="text/javascript" src="${static.url('js/discussions-temp.js')}"></script>
<section class="discussion container" id="discussion-container" data-roles="${roles}" data-course-id="${course_id}" data-user-info="${user_info}" data-threads="${threads}"> <section class="discussion container" id="discussion-container" data-roles="${roles}" data-course-id="${course_id}" data-user-info="${user_info}" data-threads="${threads}" data-thread-pages="${thread_pages}">
<div class="discussion-body"> <div class="discussion-body">
<div class="sidebar"></div> <div class="sidebar"></div>
<div class="discussion-column"> <div class="discussion-column">
......
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