2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-06 10:50:21 +08:00

Display correct post counts, even with a filter active

This commit is contained in:
Robin Ward 2013-03-26 14:18:35 -04:00
parent 5dc47c2d82
commit f1e2923a91
6 changed files with 98 additions and 61 deletions

View file

@ -62,11 +62,9 @@ Discourse.TopicController = Discourse.ObjectController.extend({
hideProgress: function() { hideProgress: function() {
if (!this.get('content.loaded')) return true; if (!this.get('content.loaded')) return true;
if (!this.get('currentPost')) return true; if (!this.get('currentPost')) return true;
if (this.get('content.highest_post_number') < 2) return true; if (this.get('content.filtered_posts_count') < 2) return true;
if (this.get('bestOf')) return true;
if (this.get('userFilters.length')) return true;
return false; return false;
}.property('content.loaded', 'currentPost', 'bestOf', 'userFilters.length'), }.property('content.loaded', 'currentPost', 'content.filtered_posts_count'),
selectPost: function(post) { selectPost: function(post) {
post.toggleProperty('selected'); post.toggleProperty('selected');
@ -215,6 +213,7 @@ Discourse.TopicController = Discourse.ObjectController.extend({
}); });
topicController.updateBottomBar(); topicController.updateBottomBar();
topicController.set('filtered_posts_count', result.filtered_posts_count);
topicController.set('loadingBelow', false); topicController.set('loadingBelow', false);
topicController.set('seenBottom', false); topicController.set('seenBottom', false);
}); });

View file

@ -6,7 +6,7 @@
<div class='container'> <div class='container'>
<div class='inner'> <div class='inner'>
{{#if view.showFavoriteButton}} {{#if view.showFavoriteButton}}
<a {{bindAttr class=":star view.topic.starred:starred"}} {{action toggleStar target="controller"}} href='#' {{bindAttr title="favoriteTooltip"}}></a> <a {{bindAttr class=":star view.topic.starred:starred"}} {{action toggleStar}} href='#' {{bindAttr title="favoriteTooltip"}}></a>
{{/if}} {{/if}}
{{#if view.editingTopic}} {{#if view.editingTopic}}
<input id='edit-title' type='text' {{bindAttr value="view.topic.title"}} autofocus> <input id='edit-title' type='text' {{bindAttr value="view.topic.title"}} autofocus>
@ -41,15 +41,15 @@
{{view Discourse.SelectedPostsView}} {{view Discourse.SelectedPostsView}}
<div class="row"> <div class="row">
<section class="topic-area" id='topic' data-topic-id='{{unbound content.id}}'> <section class="topic-area" id='topic' data-topic-id='{{unbound id}}'>
<div class='posts-wrapper'> <div class='posts-wrapper'>
<div id='topic-progress-wrapper'> <div id='topic-progress-wrapper'>
<nav id='topic-progress' title="{{i18n topic.progress.title}}" {{bindAttr class="controller.hideProgress:hidden"}}> <nav id='topic-progress' title="{{i18n topic.progress.title}}" {{bindAttr class="hideProgress:hidden"}}>
<button id='jump-top' title="{{i18n topic.progress.jump_top}}" {{action jumpTop target="controller"}}><i class="icon-circle-arrow-up"></i></button> <button id='jump-top' title="{{i18n topic.progress.jump_top}}" {{action jumpTop}}><i class="icon-circle-arrow-up"></i></button>
<div class='nums'> <div class='nums'>
<h4 title="{{i18n topic.progress.current}}">{{view.progressPosition}}</h4> <span>{{i18n of_value}}</span> <h4>{{content.highest_post_number}}</h4> <h4 title="{{i18n topic.progress.current}}">{{view.progressPosition}}</h4> <span>{{i18n of_value}}</span> <h4>{{filtered_posts_count}}</h4>
</div> </div>
<button id='jump-bottom' title="{{i18n topic.progress.jump_bottom}}" {{action jumpBottom target="controller"}}><i class="icon-circle-arrow-down"></i></button> <button id='jump-bottom' title="{{i18n topic.progress.jump_bottom}}" {{action jumpBottom}}><i class="icon-circle-arrow-down"></i></button>
<div class='bg'>&nbsp;</div> <div class='bg'>&nbsp;</div>
</nav> </nav>
</div> </div>
@ -128,12 +128,12 @@
<div id='topic-filter' style='display: none'> <div id='topic-filter' style='display: none'>
{{filterDesc}} {{filterDesc}}
<a href='#' {{action cancelFilter target="controller"}}>{{i18n topic.filters.cancel}}</a> <a href='#' {{action cancelFilter}}>{{i18n topic.filters.cancel}}</a>
</div> </div>
{{render share}} {{render share}}
{{render quoteButton}} {{render quoteButton}}
{{#if Discourse.currentUser.admin}} {{#if Discourse.currentUser.admin}}
{{render topicAdminMenu controller.content}} {{render topicAdminMenu content}}
{{/if}} {{/if}}

View file

@ -18,20 +18,13 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
SHORT_POST: 1200, SHORT_POST: 1200,
// Update the progress bar using sweet animations // Update the progress bar using sweet animations
updateBar: (function() { updateBar: function() {
var $topicProgress, bg, currentWidth, progressWidth, ratio, totalWidth; var $topicProgress, bg, currentWidth, progressWidth, ratio, totalWidth;
if (!this.get('topic.loaded')) return; if (!this.get('topic.loaded')) return;
$topicProgress = $('#topic-progress'); $topicProgress = $('#topic-progress');
if (!$topicProgress.length) return; if (!$topicProgress.length) return;
// Don't show progress when there is only one post ratio = this.get('progressPosition') / this.get('topic.filtered_posts_count');
if (this.get('topic.highest_post_number') === 1) {
$topicProgress.hide();
} else {
$topicProgress.show();
}
ratio = this.get('progressPosition') / this.get('topic.highest_post_number');
totalWidth = $topicProgress.width(); totalWidth = $topicProgress.width();
progressWidth = ratio * totalWidth; progressWidth = ratio * totalWidth;
bg = $topicProgress.find('.bg'); bg = $topicProgress.find('.bg');
@ -53,15 +46,15 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
} else { } else {
bg.animate({ width: progressWidth }, 400); bg.animate({ width: progressWidth }, 400);
} }
}).observes('progressPosition', 'topic.highest_post_number', 'topic.loaded'), }.observes('progressPosition', 'topic.filtered_posts_count', 'topic.loaded'),
updateTitle: (function() { updateTitle: function() {
var title; var title;
title = this.get('topic.title'); title = this.get('topic.title');
if (title) return Discourse.set('title', title); if (title) return Discourse.set('title', title);
}).observes('topic.loaded', 'topic.title'), }.observes('topic.loaded', 'topic.title'),
currentPostChanged: (function() { currentPostChanged: function() {
var current = this.get('controller.currentPost'); var current = this.get('controller.currentPost');
var topic = this.get('topic'); var topic = this.get('topic');
@ -93,13 +86,13 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
} else { } else {
$('#jump-bottom').attr('disabled', false); $('#jump-bottom').attr('disabled', false);
} }
}).observes('controller.currentPost', 'controller.bestOf', 'topic.highest_post_number'), }.observes('controller.currentPost', 'controller.bestOf', 'topic.highest_post_number'),
composeChanged: (function() { composeChanged: function() {
var composerController = Discourse.get('router.composerController'); var composerController = Discourse.get('router.composerController');
composerController.clearState(); composerController.clearState();
return composerController.set('topic', this.get('topic')); composerController.set('topic', this.get('topic'));
}).observes('composer'), }.observes('composer'),
// This view is being removed. Shut down operations // This view is being removed. Shut down operations
willDestroyElement: function() { willDestroyElement: function() {
@ -267,18 +260,18 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
return this.loadMore(this.getPost($post)); return this.loadMore(this.getPost($post));
}, },
postCountChanged: (function() { postCountChanged: function() {
this.set('controller.seenBottom', false); this.set('controller.seenBottom', false);
}).observes('topic.highest_post_number'), }.observes('topic.highest_post_number'),
loadMore: function(post) { loadMore: function(post) {
if (!post) { return; } if (!post) return;
if (this.get('controller.loading')) { return; } if (this.get('controller.loading')) return;
// Don't load if we know we're at the bottom // Don't load if we know we're at the bottom
if (this.get('topic.highest_post_number') === post.get('post_number')) { return; } if (this.get('topic.highest_post_number') === post.get('post_number')) return;
if (this.get('controller.seenBottom')) { return; } if (this.get('controller.seenBottom')) return;
// Don't double load ever // Don't double load ever
if (this.topic.posts.last().post_number !== post.post_number) return; if (this.topic.posts.last().post_number !== post.post_number) return;
@ -298,7 +291,7 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
if (result.suggested_topics) { if (result.suggested_topics) {
var suggested = Em.A(); var suggested = Em.A();
result.suggested_topics.each(function(st) { result.suggested_topics.each(function(st) {
return suggested.pushObject(Discourse.Topic.create(st)); suggested.pushObject(Discourse.Topic.create(st));
}); });
topicView.set('topic.suggested_topics', suggested); topicView.set('topic.suggested_topics', suggested);
} }
@ -350,11 +343,10 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
}, },
updateDock: function(postView) { updateDock: function(postView) {
var post;
if (!postView) return; if (!postView) return;
post = postView.get('post'); var post = postView.get('post');
if (!post) return; if (!post) return;
this.set('progressPosition', post.get('post_number')); this.set('progressPosition', post.get('index'));
}, },
nonUrgentPositionUpdate: Discourse.debounce(function(opts){ nonUrgentPositionUpdate: Discourse.debounce(function(opts){

View file

@ -44,7 +44,8 @@ class TopicViewSerializer < ApplicationSerializer
:posts, :posts,
:at_bottom, :at_bottom,
:highest_post_number, :highest_post_number,
:pinned :pinned,
:filtered_posts_count
has_one :created_by, serializer: BasicUserSerializer, embed: :objects has_one :created_by, serializer: BasicUserSerializer, embed: :objects
has_one :last_poster, serializer: BasicUserSerializer, embed: :objects has_one :last_poster, serializer: BasicUserSerializer, embed: :objects
@ -98,6 +99,10 @@ class TopicViewSerializer < ApplicationSerializer
object.post_action_visibility.present? object.post_action_visibility.present?
end end
def filtered_posts_count
object.filtered_posts_count
end
def voted_in_topic def voted_in_topic
object.voted_in_topic? object.voted_in_topic?
end end
@ -204,13 +209,22 @@ class TopicViewSerializer < ApplicationSerializer
@posts = [] @posts = []
@highest_number_in_posts = 0 @highest_number_in_posts = 0
if object.posts.present? if object.posts.present?
object.posts.each do |p| object.posts.each_with_index do |p, idx|
@highest_number_in_posts = p.post_number if p.post_number > @highest_number_in_posts @highest_number_in_posts = p.post_number if p.post_number > @highest_number_in_posts
ps = PostSerializer.new(p, scope: scope, root: false) ps = PostSerializer.new(p, scope: scope, root: false)
ps.topic_slug = object.topic.slug ps.topic_slug = object.topic.slug
ps.topic_view = object ps.topic_view = object
p.topic = object.topic p.topic = object.topic
@posts << ps.as_json
post_json = ps.as_json
if object.index_reverse
post_json[:index] = object.index_offset - idx
else
post_json[:index] = object.index_offset + idx + 1
end
@posts << post_json
end end
end end
@posts @posts

View file

@ -4,11 +4,8 @@ require_dependency 'summarize'
class TopicView class TopicView
attr_accessor :topic, attr_reader :topic, :posts, :index_offset, :index_reverse
:draft, attr_accessor :draft, :draft_key, :draft_sequence
:draft_key,
:draft_sequence,
:posts
def initialize(topic_id, user=nil, options={}) def initialize(topic_id, user=nil, options={})
@topic = find_topic(topic_id) @topic = find_topic(topic_id)
@ -34,6 +31,7 @@ class TopicView
@user = user @user = user
@initial_load = true @initial_load = true
@index_reverse = false
filter_posts(options) filter_posts(options)
@ -75,6 +73,10 @@ class TopicView
@topic.title @topic.title
end end
def filtered_posts_count
@filtered_posts_count ||= @filtered_posts.count
end
def summary def summary
return nil if posts.blank? return nil if posts.blank?
Summarize.new(posts.first.cooked).summary Summarize.new(posts.first.cooked).summary
@ -146,8 +148,13 @@ class TopicView
sort_order = sort_order_for_post_number(post_number) sort_order = sort_order_for_post_number(post_number)
return nil unless sort_order return nil unless sort_order
# Find posts before the `sort_order` # Find posts before the `sort_order`
@posts = @filtered_posts.order('sort_order desc').where("sort_order < ?", sort_order) @posts = @filtered_posts.order('sort_order desc').where("sort_order < ?", sort_order)
@index_offset = @posts.count
@index_reverse = true
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(SiteSetting.posts_per_page) @posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(SiteSetting.posts_per_page)
end end
@ -158,6 +165,7 @@ class TopicView
sort_order = sort_order_for_post_number(post_number) sort_order = sort_order_for_post_number(post_number)
return nil unless sort_order return nil unless sort_order
@index_offset = @filtered_posts.where("sort_order <= ?", sort_order).count
@posts = @filtered_posts.order('sort_order').where("sort_order > ?", sort_order) @posts = @filtered_posts.order('sort_order').where("sort_order > ?", sort_order)
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(SiteSetting.posts_per_page) @posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(SiteSetting.posts_per_page)
end end
@ -263,6 +271,7 @@ class TopicView
private private
def filter_posts_in_range(min, max) def filter_posts_in_range(min, max)
max_index = (filtered_post_ids.length - 1) max_index = (filtered_post_ids.length - 1)
# If we're off the charts, return nil # If we're off the charts, return nil
@ -272,6 +281,8 @@ class TopicView
max = max_index if max > max_index max = max_index if max > max_index
min = 0 if min < 0 min = 0 if min < 0
@index_offset = min
# TODO: Sort might be off # TODO: Sort might be off
@posts = Post.where(id: filtered_post_ids[min..max]) @posts = Post.where(id: filtered_post_ids[min..max])
.includes(:user) .includes(:user)

View file

@ -7,7 +7,6 @@ describe TopicView do
let(:coding_horror) { Fabricate(:coding_horror) } let(:coding_horror) { Fabricate(:coding_horror) }
let(:first_poster) { topic.user } let(:first_poster) { topic.user }
let(:topic_view) { TopicView.new(topic.id, coding_horror) } let(:topic_view) { TopicView.new(topic.id, coding_horror) }
it "raises a not found error if the topic doesn't exist" do it "raises a not found error if the topic doesn't exist" do
@ -192,10 +191,14 @@ describe TopicView do
it "returns undeleted posts after a post" do it "returns undeleted posts after a post" do
topic_view.filter_posts_after(p1.post_number).should == [p2, p3, p5] topic_view.filter_posts_after(p1.post_number).should == [p2, p3, p5]
topic_view.should_not be_initial_load topic_view.should_not be_initial_load
topic_view.index_offset.should == 1
topic_view.index_reverse.should be_false
end end
it "clips to the end boundary" do it "clips to the end boundary" do
topic_view.filter_posts_after(p2.post_number).should == [p3, p5] topic_view.filter_posts_after(p2.post_number).should == [p3, p5]
topic_view.index_offset.should == 2
topic_view.index_reverse.should be_false
end end
it "returns nothing after the last post" do it "returns nothing after the last post" do
@ -209,6 +212,8 @@ describe TopicView do
it "returns deleted posts to an admin" do it "returns deleted posts to an admin" do
coding_horror.admin = true coding_horror.admin = true
topic_view.filter_posts_after(p1.post_number).should == [p2, p3, p4] topic_view.filter_posts_after(p1.post_number).should == [p2, p3, p4]
topic_view.index_offset.should == 1
topic_view.index_reverse.should be_false
end end
end end
@ -216,10 +221,14 @@ describe TopicView do
it "returns undeleted posts before a post" do it "returns undeleted posts before a post" do
topic_view.filter_posts_before(p5.post_number).should == [p3, p2, p1] topic_view.filter_posts_before(p5.post_number).should == [p3, p2, p1]
topic_view.should_not be_initial_load topic_view.should_not be_initial_load
topic_view.index_offset.should == 3
topic_view.index_reverse.should be_true
end end
it "clips to the beginning boundary" do it "clips to the beginning boundary" do
topic_view.filter_posts_before(p3.post_number).should == [p2, p1] topic_view.filter_posts_before(p3.post_number).should == [p2, p1]
topic_view.index_offset.should == 2
topic_view.index_reverse.should be_true
end end
it "returns nothing before the first post" do it "returns nothing before the first post" do
@ -234,35 +243,47 @@ describe TopicView do
it "returns deleted posts to an admin" do it "returns deleted posts to an admin" do
coding_horror.admin = true coding_horror.admin = true
topic_view.filter_posts_before(p5.post_number).should == [p4, p3, p2] topic_view.filter_posts_before(p5.post_number).should == [p4, p3, p2]
topic_view.index_offset.should == 4
topic_view.index_reverse.should be_true
end end
end end
describe "filter_posts_near" do describe "filter_posts_near" do
def topic_view_posts_near(post) def topic_view_near(post)
TopicView.new(topic.id, coding_horror, post_number: post.post_number).posts TopicView.new(topic.id, coding_horror, post_number: post.post_number)
end end
it "snaps to the lower boundary" do it "snaps to the lower boundary" do
topic_view_posts_near(p1).should == [p1, p2, p3] near_view = topic_view_near(p1)
near_view.posts.should == [p1, p2, p3]
near_view.index_offset.should == 0
near_view.index_reverse.should be_false
end end
it "snaps to the upper boundary" do it "snaps to the upper boundary" do
topic_view_posts_near(p5).should == [p2, p3, p5] near_view = topic_view_near(p5)
near_view.posts.should == [p2, p3, p5]
near_view.index_offset.should == 1
near_view.index_reverse.should be_false
end end
it "returns the posts in the middle" do it "returns the posts in the middle" do
topic_view_posts_near(p2).should == [p1, p2, p3] near_view = topic_view_near(p2)
near_view.posts.should == [p1, p2, p3]
near_view.index_offset.should == 0
near_view.index_reverse.should be_false
end end
it "returns deleted posts to an admin" do it "returns deleted posts to an admin" do
coding_horror.admin = true coding_horror.admin = true
topic_view_posts_near(p3).should == [p2, p3, p4] near_view = topic_view_near(p3)
near_view.posts.should == [p2, p3, p4]
near_view.index_offset.should == 1
near_view.index_reverse.should be_false
end end
end end
end end
end end