diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6 index 11baa77c9a5..dc194d3fdad 100644 --- a/app/assets/javascripts/discourse/controllers/topic.js.es6 +++ b/app/assets/javascripts/discourse/controllers/topic.js.es6 @@ -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(); diff --git a/app/assets/javascripts/discourse/lib/url.js b/app/assets/javascripts/discourse/lib/url.js index d9062be681a..a22caaaca1d 100644 --- a/app/assets/javascripts/discourse/lib/url.js +++ b/app/assets/javascripts/discourse/lib/url.js @@ -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); diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js index d4223da2a8c..7451ff69fba 100644 --- a/app/assets/javascripts/discourse/models/post_stream.js +++ b/app/assets/javascripts/discourse/models/post_stream.js @@ -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; } diff --git a/app/assets/javascripts/discourse/routes/topic_from_params_route.js b/app/assets/javascripts/discourse/routes/topic_from_params_route.js index e6fa7c41b5e..f45bdf3d2e1 100644 --- a/app/assets/javascripts/discourse/routes/topic_from_params_route.js +++ b/app/assets/javascripts/discourse/routes/topic_from_params_route.js @@ -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);