Fix assignment of post number to progress position

This commit is contained in:
riking 2014-08-14 17:31:34 -07:00
parent ec4e268703
commit 8539c08d35
4 changed files with 58 additions and 7 deletions

View File

@ -629,10 +629,9 @@ export default ObjectController.extend(Discourse.SelectedPostsCount, {
if (!post) { return; }
var postStream = this.get('postStream'),
lastLoadedPost = postStream.get('lastLoadedPost'),
index = postStream.get('stream').indexOf(post.get('id'))+1;
lastLoadedPost = postStream.get('lastLoadedPost');
this.set('controllers.topic-progress.progressPosition', index);
this.set('controllers.topic-progress.progressPosition', postStream.progressIndexOfPost(post));
if (lastLoadedPost && lastLoadedPost === post) {
postStream.appendMore();

View File

@ -220,7 +220,9 @@ Discourse.URL = Em.Object.createWithMixins({
highlightOnInsert: closest,
enteredAt: new Date().getTime().toString()
});
topicProgressController.set('progressPosition', closest);
var closestPost = postStream.closestPostForPostNumber(closest),
progress = postStream.progressIndexOfPost(closestPost);
topicProgressController.set('progressPosition', progress);
Discourse.PostView.considerHighlighting(topicController, closest);
}).then(function() {
Discourse.URL.jumpToPost(closest);

View File

@ -635,13 +635,61 @@ Discourse.PostStream = Em.Object.extend({
});
},
/**
Returns the closest post given a postNumber that may not exist in the stream.
For example, if the user asks for a post that's deleted or otherwise outside the range.
This allows us to set the progress bar with the correct number.
@method closestPostForPostNumber
@param {Number} postNumber the post number we're looking for
@return {Post} the closest post
@see PostStream.closestPostNumberFor
**/
closestPostForPostNumber: function(postNumber) {
if (!this.get('hasPosts')) { return; }
var closest = null;
this.get('posts').forEach(function (p) {
if (closest === postNumber) { return; }
if (!closest) { closest = p; }
if (Math.abs(postNumber - p.get('post_number')) < Math.abs(closest.get('post_number') - postNumber)) {
closest = p;
}
});
return closest;
},
/**
Get the index of a post in the stream. (Use this for the topic progress bar.)
@param post the post to get the index of
@returns {Number} 1-starting index of the post, or 0 if not found
@see PostStream.progressIndexOfPostId
**/
progressIndexOfPost: function(post) {
return this.progressIndexOfPostId(post.get('id'));
},
/**
Get the index in the stream of a post id. (Use this for the topic progress bar.)
@param post_id - post id to search for
@returns {Number} 1-starting index of the post, or 0 if not found
**/
progressIndexOfPostId: function(post_id) {
return this.get('stream').indexOf(post_id) + 1;
},
/**
Returns the closest post number given a postNumber that may not exist in the stream.
For example, if the user asks for a post that's deleted or otherwise outside the range.
This allows us to set the progress bar with the correct number.
@method closestPostNumberFor
@param {Integer} postNumber the post number we're looking for
@param {Number} postNumber the post number we're looking for
@return {Number} a close post number
**/
closestPostNumberFor: function(postNumber) {
if (!this.get('hasPosts')) { return; }

View File

@ -23,7 +23,9 @@ Discourse.TopicFromParamsRoute = Discourse.Route.extend({
postStream.refresh(params).then(function () {
// The post we requested might not exist. Let's find the closest post
var closest = postStream.closestPostNumberFor(params.nearPost) || 1;
var closestPost = postStream.closestPostForPostNumber(params.nearPost || 1),
closest = closestPost.get('post_number'),
progress = postStream.progressIndexOfPost(closestPost);
topicController.setProperties({
currentPost: closest,
@ -32,7 +34,7 @@ Discourse.TopicFromParamsRoute = Discourse.Route.extend({
});
topicProgressController.setProperties({
progressPosition: closest,
progressPosition: progress,
expanded: false
});
Discourse.URL.jumpToPost(closest);