Merge pull request #2659 from riking/wrong-progress-pos

Hopefully fix once and for all the 172/160 problem
This commit is contained in:
Robin Ward 2014-08-18 11:30:09 -04:00
commit 05193deb4d
6 changed files with 87 additions and 17 deletions

View File

@ -7,7 +7,7 @@ export default Ember.ObjectController.extend({
toggleExpansion: function(opts) {
this.toggleProperty('expanded');
if (this.get('expanded')) {
this.set('toPostNumber', this.get('progressPosition'));
this.set('toPostIndex', this.get('progressPosition'));
if(opts && opts.highlight){
// TODO: somehow move to view?
Em.run.next(function(){
@ -18,17 +18,36 @@ export default Ember.ObjectController.extend({
},
jumpPost: function() {
var postNumber = parseInt(this.get('toPostNumber'), 10);
var postIndex = parseInt(this.get('toPostIndex'), 10);
// Validate the post number first
if (isNaN(postNumber) || postNumber < 1) {
postNumber = 1;
// Validate the post index first
if (isNaN(postIndex) || postIndex < 1) {
postIndex = 1;
}
if (postNumber > this.get('highest_post_number')) {
postNumber = this.get('highest_post_number');
if (postIndex > this.get('postStream.filteredPostsCount')) {
postIndex = this.get('postStream.filteredPostsCount');
}
this.set('toPostIndex', postIndex);
var stream = this.get('postStream'),
idStream = stream.get('stream'),
postId = idStream[postIndex - 1];
if (!postId) {
Em.Logger.warn("jump-post code broken - requested an index outside the stream array");
return;
}
var post = stream.findLoadedPost(postId);
if (post) {
this.jumpTo(this.get('model').urlForPostNumber(post.get('post_number')));
} else {
var self = this;
// need to load it
stream.findPostsByIds([postId]).then(function(arr) {
post = arr[0];
self.jumpTo(self.get('model').urlForPostNumber(post.get('post_number')));
});
}
this.set('toPostNumber', postNumber);
this.jumpTo(this.get('model').urlForPostNumber(postNumber));
},
jumpTop: function() {

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);

View File

@ -5,7 +5,7 @@
{{i18n topic.progress.go_top}}
</button>
<div class='jump-form'>
{{input value=toPostNumber}} <button {{action jumpPost}} class='btn'>{{i18n topic.progress.go}}</button>
{{input value=toPostIndex}} <button {{action jumpPost}} class='btn'>{{i18n topic.progress.go}}</button>
</div>
<button {{action jumpBottom}} {{bind-attr disabled=jumpBottomDisabled}} class='btn full no-text jump-bottom'>
{{i18n topic.progress.go_bottom}}