FIX: Jump to post was not respecting gaps

This commit is contained in:
Robin Ward 2015-01-12 18:10:16 -05:00
parent da7c9e6447
commit 1e6dea46dd
4 changed files with 38 additions and 10 deletions

View File

@ -1,11 +1,3 @@
/**
Handles a gap between posts with a click to load more
@class PostGapComponent
@extends Ember.Component
@namespace Discourse
@module Discourse
**/
export default Ember.Component.extend({
classNameBindings: [':gap', 'gap::hidden'],

View File

@ -29,8 +29,7 @@ export default Ember.ObjectController.extend({
}
this.set('toPostIndex', postIndex);
var stream = this.get('postStream'),
idStream = stream.get('stream'),
postId = idStream[postIndex - 1];
postId = stream.findPostIdForPostNumber(postIndex);
if (!postId) {
Em.Logger.warn("jump-post code broken - requested an index outside the stream array");

View File

@ -667,6 +667,32 @@ Discourse.PostStream = Em.Object.extend({
return closest;
},
// Find a postId for a postNumber, respecting gaps
findPostIdForPostNumber: function(postNumber) {
var count = 1,
stream = this.get('stream'),
beforeLookup = this.get('gaps.before'),
streamLength = stream.length;
for (var i=0; i<streamLength; i++) {
var pid = stream[i];
// See if there are posts before this post
if (beforeLookup) {
var before = beforeLookup[pid];
if (before) {
for (var j=0; j<before.length; j++) {
if (count === postNumber) { return pid; }
count++;
}
}
}
if (count === postNumber) { return pid; }
count++;
}
},
/**
@private

View File

@ -121,6 +121,17 @@ test("cancelFilter", function() {
blank(postStream.get('userFilters'), "cancelling the filters clears the userFilters");
});
test("findPostIdForPostNumber", function() {
var postStream = buildStream(1234, [10, 20, 30, 40, 50, 60, 70]);
postStream.set('gaps', { before: { 60: [55, 58] } });
equal(postStream.findPostIdForPostNumber(500), null, 'it returns null when the post cannot be found');
equal(postStream.findPostIdForPostNumber(1), 10, 'it finds the postId at the beginning');
equal(postStream.findPostIdForPostNumber(5), 50, 'it finds the postId in the middle');
equal(postStream.findPostIdForPostNumber(8), 60, 'it respects gaps');
});
test("toggleParticipant", function() {
var postStream = buildStream(1236);
sandbox.stub(postStream, "refresh");