Commit cae338a4 by Rocky Duan

dropdown filter

parent fd31ab30
...@@ -5,18 +5,23 @@ class @DiscussionThreadListView extends Backbone.View ...@@ -5,18 +5,23 @@ class @DiscussionThreadListView extends Backbone.View
"click .browse": "toggleTopicDrop" "click .browse": "toggleTopicDrop"
"keyup .post-search-field": "performSearch" "keyup .post-search-field": "performSearch"
"click .sort-bar a": "sortThreads" "click .sort-bar a": "sortThreads"
"click .board-drop-menu": "setTopic" "click .browse-topic-drop-menu": "filterTopic"
initialize: ->
@displayedCollection = new Discussion(@collection.models)
console.log @displayedCollection
render: -> render: ->
@timer = 0; @timer = 0
@$el.html(@template()) @$el.html(@template())
@collection.on "reset", @renderThreads @displayedCollection.on "reset", @renderThreads
@renderThreads() @renderThreads()
@ @
renderThreads: => renderThreads: =>
console.log "rendering"
@$(".post-list").html("") @$(".post-list").html("")
@collection.each @renderThreadListItem @displayedCollection.each @renderThreadListItem
@trigger "threads:rendered" @trigger "threads:rendered"
renderThreadListItem: (thread) => renderThreadListItem: (thread) =>
...@@ -41,44 +46,49 @@ class @DiscussionThreadListView extends Backbone.View ...@@ -41,44 +46,49 @@ class @DiscussionThreadListView extends Backbone.View
toggleTopicDrop: => toggleTopicDrop: =>
@$(".browse").toggleClass('is-dropped') @$(".browse").toggleClass('is-dropped')
if @$(".browse").hasClass('is-dropped') if @$(".browse").hasClass('is-dropped')
@$(".board-drop-menu").show() @$(".browse-topic-drop-menu-wrapper").show()
setTimeout((=> setTimeout((=>
$("body").bind("click", @toggleTopicDrop) $("body").bind("click", @toggleTopicDrop)
), 0) ), 0)
else else
@$(".board-drop-menu").hide() @$(".browse-topic-drop-menu-wrapper").hide()
$("body").unbind("click", @toggleTopicDrop) $("body").unbind("click", @toggleTopicDrop)
setTopic: (e) -> setTopic: (event) ->
item = $(e.target).closest('a') item = $(event.target).closest('a')
boardName = item.find(".board-name").html() boardName = item.find(".board-name").html()
_.each item.parents('ul').not('.board-drop-menu'), (parent) -> _.each item.parents('ul').not('.browse-topic-drop-menu'), (parent) ->
console.log(parent)
boardName = $(parent).siblings('a').find('.board-name').html() + ' / ' + boardName boardName = $(parent).siblings('a').find('.board-name').html() + ' / ' + boardName
@$(".current-board").html(boardName) @$(".current-board").html(boardName)
fontSize = 16; fontSize = 16
@$(".current-board").css('font-size', '16px'); @$(".current-board").css('font-size', '16px')
while @$(".current-board").width() > (@$el.width() * .8) - 40 while @$(".current-board").width() > (@$el.width() * .8) - 40
fontSize--; fontSize--
if fontSize < 11 if fontSize < 11
break; break
@$(".current-board").css('font-size', fontSize + 'px'); @$(".current-board").css('font-size', fontSize + 'px')
filterTopic: (event) ->
@setTopic(event)
item = $(event.target).closest('li')
discussionIds = _.compact _.map item.find("span.board-name"), (board) -> $(board).data("discussion_id")
discussionIds = _.map discussionIds, (info) -> info.id
filtered = @collection.filter (thread) =>
_.include(discussionIds, thread.get('commentable_id'))
@displayedCollection.reset filtered
sortThreads: (event) -> sortThreads: (event) ->
@$(".sort-bar a").removeClass("active") @$(".sort-bar a").removeClass("active")
$(event.target).addClass("active") $(event.target).addClass("active")
sortBy = $(event.target).data("sort") sortBy = $(event.target).data("sort")
if sortBy == "date" if sortBy == "date"
@collection.comparator = @collection.sortByDate @displayedCollection.comparator = @displayedCollection.sortByDate
else if sortBy == "votes" else if sortBy == "votes"
@collection.comparator = @collection.sortByVotes @displayedCollection.comparator = @displayedCollection.sortByVotes
else if sortBy == "comments" else if sortBy == "comments"
@collection.comparator = @collection.sortByComments @displayedCollection.comparator = @displayedCollection.sortByComments
@collection.sort() @displayedCollection.sort()
delay: (callback, ms) => delay: (callback, ms) =>
clearTimeout(@timer) clearTimeout(@timer)
...@@ -96,5 +106,6 @@ class @DiscussionThreadListView extends Backbone.View ...@@ -96,5 +106,6 @@ class @DiscussionThreadListView extends Backbone.View
success: (response, textStatus) => success: (response, textStatus) =>
if textStatus == 'success' if textStatus == 'success'
@collection.reset(response.discussion_data) @collection.reset(response.discussion_data)
@displayedCollection.reset(@collection)
@delay(callback, 300) @delay(callback, 300)
<%! import json %>
<%def name="render_dropdown(map)"> <%def name="render_dropdown(map)">
% for child in map["children"]: % for child in map["children"]:
% if child in map["entries"]: % if child in map["entries"]:
...@@ -9,7 +11,7 @@ ...@@ -9,7 +11,7 @@
</%def> </%def>
<%def name="render_entry(entries, entry)"> <%def name="render_entry(entries, entry)">
<li><a href="#"><span class="board-name" data-discussion_id="${entries[entry]}">${entry}</span> <span class="unread">1,248</span></a></li> <li><a href="#"><span class="board-name" data-discussion_id='${json.dumps(entries[entry])}'>${entry}</span> <span class="unread">1,248</span></a></li>
</%def> </%def>
<%def name="render_category(categories, category)"> <%def name="render_category(categories, category)">
......
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