inf scroll changes

dont load main post if start > 0
This commit is contained in:
barisusakli
2015-09-24 21:14:34 -04:00
parent 137f2097db
commit c617a3f008
9 changed files with 127 additions and 153 deletions

View File

@@ -22,28 +22,20 @@ define('forum/topic/posts', [
return;
}
updatePostCounts(data.posts);
if (config.usePagination) {
return onNewPostPagination(data);
onNewPostPagination(data);
} else {
onNewPostInfiniteScroll(data);
}
for (var i=0; i<data.posts.length; ++i) {
var cmp = components.get('user/postcount', data.posts[i].uid);
cmp.html(parseInt(cmp.attr('data-postcount'), 10) + 1);
utils.addCommasToNumbers(cmp);
}
createNewPosts(data, components.get('post').not('[data-index=0]'), function(html) {
if (html) {
html.addClass('new');
}
scrollToPostIfSelf(data.posts[0]);
});
};
function scrollToPostIfSelf(post) {
var isSelfPost = parseInt(post.uid, 10) === parseInt(app.user.uid, 10);
if (isSelfPost) {
navigator.scrollBottom(post.index);
function updatePostCounts(posts) {
for (var i=0; i<posts.length; ++i) {
var cmp = components.get('user/postcount', posts[i].uid);
cmp.html(parseInt(cmp.attr('data-postcount'), 10) + 1);
utils.addCommasToNumbers(cmp);
}
}
@@ -55,17 +47,38 @@ define('forum/topic/posts', [
var posts = data.posts;
pagination.pageCount = Math.max(1, Math.ceil((posts[0].topic.postcount - 1) / config.postsPerPage));
var direction = config.topicPostSort === 'oldest_to_newest' || config.topicPostSort === 'most_votes' ? 1 : -1;
if (pagination.currentPage === pagination.pageCount) {
createNewPosts(data, components.get('post').not('[data-index=0]'), scrollToPost);
var isPostVisible = (pagination.currentPage === pagination.pageCount && direction === 1) || (pagination.currentPage === 1 && direction === -1);
if (isPostVisible) {
createNewPosts(data, components.get('post').not('[data-index=0]'), direction, scrollToPost);
} else if (parseInt(posts[0].uid, 10) === parseInt(app.user.uid, 10)) {
pagination.loadPage(pagination.pageCount, scrollToPost);
}
}
function createNewPosts(data, repliesSelector, callback) {
function onNewPostInfiniteScroll(data) {
var direction = config.topicPostSort === 'oldest_to_newest' || config.topicPostSort === 'most_votes' ? 1 : -1;
createNewPosts(data, components.get('post').not('[data-index=0]'), direction, function(html) {
if (html) {
html.addClass('new');
}
scrollToPostIfSelf(data.posts[0]);
});
}
function scrollToPostIfSelf(post) {
var isSelfPost = parseInt(post.uid, 10) === parseInt(app.user.uid, 10);
if (isSelfPost) {
navigator.scrollBottom(post.index);
}
}
function createNewPosts(data, repliesSelector, direction, callback) {
callback = callback || function() {};
if(!data || (data.posts && !data.posts.length)) {
if (!data || (data.posts && !data.posts.length)) {
return callback();
}
@@ -91,59 +104,30 @@ define('forum/topic/posts', [
if (newPosts.length && data.posts.length > 1) {
data.posts.forEach(function(post) {
components.get('post', 'pid', post.pid).remove();
});
} else {
data.posts = data.posts.filter(function(post) {
return components.get('post', 'pid', post.pid).length === 0;
});
}
}
var after = null,
before = null;
function findInsertionPoint() {
var firstPostTimestamp = parseInt(data.posts[0].timestamp, 10);
var firstPostVotes = parseInt(data.posts[0].votes, 10);
var firstPostIndex = parseInt(data.posts[0].index, 10);
var replies = $(repliesSelector);
var firstReply = replies.first();
var lastReply = replies.last();
if (config.topicPostSort === 'oldest_to_newest') {
if (firstPostTimestamp < parseInt(firstReply.attr('data-timestamp'), 10)) {
before = firstReply;
} else if(firstPostTimestamp >= parseInt(lastReply.attr('data-timestamp'), 10)) {
after = lastReply;
}
} else if(config.topicPostSort === 'newest_to_oldest') {
if (firstPostTimestamp > parseInt(firstReply.attr('data-timestamp'), 10)) {
before = firstReply;
} else if(firstPostTimestamp <= parseInt(lastReply.attr('data-timestamp'), 10)) {
after = lastReply;
}
} else if(config.topicPostSort === 'most_votes') {
if (firstPostVotes > parseInt(firstReply.attr('data-votes'), 10)) {
before = firstReply;
} else if(firstPostVotes < parseInt(firstReply.attr('data-votes'), 10)) {
after = lastReply;
} else {
if (firstPostIndex > parseInt(firstReply.attr('data-index'), 10)) {
before = firstReply;
} else if(firstPostIndex <= parseInt(firstReply.attr('data-index'), 10)) {
after = lastReply;
var p = components.get('post', 'pid', post.pid);
if (p.hasClass('new')) {
p.remove()
}
}
});
}
data.posts = data.posts.filter(function(post) {
return components.get('post', 'pid', post.pid).length === 0;
});
}
removeAlreadyAddedPosts();
if (!data.posts.length) {
return callback();
}
findInsertionPoint();
var after, before;
if (direction === 1) {
after = repliesSelector.last();
} else if (direction === -1) {
before = repliesSelector.first();
}
data.title = $('<div></div>').text(ajaxify.data.title).html();
data.slug = ajaxify.data.slug;
@@ -157,17 +141,13 @@ define('forum/topic/posts', [
} else if (before) {
// Save document height and position for future reference (about 5 lines down)
var height = $(document).height(),
scrollTop = $(document).scrollTop(),
originalPostEl = components.get('post', 'index', 0);
scrollTop = $(document).scrollTop();
// Insert the new post
html.insertBefore(before);
// If the user is not at the top of the page... (or reasonably so...)
if (scrollTop > originalPostEl.offset().top) {
// Now restore the relative position the user was on prior to new post insertion
$(document).scrollTop(scrollTop + ($(document).height() - height));
}
// Now restore the relative position the user was on prior to new post insertion
$(document).scrollTop(scrollTop + ($(document).height() - height));
} else {
components.get('topic').append(html);
}
@@ -225,16 +205,12 @@ define('forum/topic/posts', [
return;
}
var reverse = config.topicPostSort === 'newest_to_oldest' || config.topicPostSort === 'most_votes';
var replies = components.get('post').not('[data-index=0]').not('.new');
var afterEl = direction > 0 ? replies.last() : replies.first();
var after = parseInt(afterEl.attr('data-index'), 10) || 0;
infinitescroll.calculateAfter(direction, components.get('topic').find('[data-index][data-index!="0"]:not(.new)'), config.postsPerPage, reverse, function(after, offset, el) {
loadPostsAfter(after);
});
};
function loadPostsAfter(after) {
var tid = ajaxify.data.tid;
if (!utils.isNumber(tid) || !utils.isNumber(after) || (after === 0 && components.get('post', 'index', 1).length)) {
if (!utils.isNumber(tid) || !utils.isNumber(after) || (direction < 0 && components.get('post', 'index', 0).length)) {
return;
}
@@ -245,13 +221,14 @@ define('forum/topic/posts', [
infinitescroll.loadMore('topics.loadMore', {
tid: tid,
after: after
after: after,
direction: direction
}, function (data, done) {
indicatorEl.fadeOut();
if (data && data.posts && data.posts.length) {
createNewPosts(data, components.get('post').not('[data-index=0]').not('.new'), done);
createNewPosts(data, components.get('post').not('[data-index=0]').not('.new'), direction, done);
} else {
if (app.user.uid) {
socket.emit('topics.markAsRead', [tid]);
@@ -260,7 +237,7 @@ define('forum/topic/posts', [
done();
}
});
}
};
Posts.processPage = function(posts) {
app.createUserTooltips();