enabling infinite post loader (re: issue #42), fixes #42

v1.18.x
Julian Lam 12 years ago
parent 9df0b39a5d
commit 936a284397

@ -8,7 +8,8 @@
deleted: templates.get('deleted'),
pinned: templates.get('pinned')
},
topic_name = templates.get('topic_name');
topic_name = templates.get('topic_name'),
infiniteLoaderActive = false;
function addCommasToNumbers() {
$('.formatted-number').each(function(index, element) {
@ -204,6 +205,20 @@
followEl[0].addEventListener('click', function() {
socket.emit('api:topic.follow', tid);
}, false);
// Infinite scrolling of posts
window.addEventListener('scroll', function() {
var windowHeight = document.body.offsetHeight - $(window).height(),
half = windowHeight / 2;
if (document.body.scrollTop > half && !infiniteLoaderActive) {
infiniteLoaderActive = true;
socket.emit('api:topic.loadMore', {
tid: tid,
after: document.querySelectorAll('#post-container li[data-pid]').length
});
}
});
});
@ -304,18 +319,20 @@
socket.on('event:new_post', function(data) {
data.posts[0].display_moderator_tools = 'none';
var html = templates.prepare(templates['topic'].blocks['posts']).parse(data),
uniqueid = new Date().getTime();
jQuery('<div id="' + uniqueid + '"></div>')
.appendTo("#post-container")
.hide()
.append(html)
.fadeIn('slow');
uniqueid = new Date().getTime(),
tempContainer = jQuery('<div id="' + uniqueid + '"></div>')
.appendTo("#post-container")
.hide()
.append(html)
.fadeIn('slow');
for(var x=0,numPosts=data.posts.length;x<numPosts;x++) {
socket.emit('api:post.privileges', data.posts[x].pid);
}
tempContainer.replaceWith(tempContainer.contents());
infiniteLoaderActive = false;
set_up_posts(uniqueid);
addCommasToNumbers();

@ -17,16 +17,17 @@ marked.setOptions({
Posts.getPostsByTid = function(tid, current_user, start, end, callback) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
RDB.handle(err);
if (pids.length === 0 ) {
throw new Error('Topic should never have 0 posts. tid: ' + tid);
}
topics.markAsRead(tid, current_user);
Posts.getPostsByPids(pids, current_user, function(posts) {
callback(posts);
});
if (pids.length) {
Posts.getPostsByPids(pids, current_user, function(posts) {
callback(posts);
});
} else {
callback({
error: 'no-posts'
});
}
});
}

@ -410,11 +410,13 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
end = start + 10;
posts.getPostsByTid(data.tid, uid, start, end, function(posts){
postTools.constructPostObject(posts, data.tid, uid, null, function(postObj) {
io.sockets.in('topic_' + data.tid).emit('event:new_post', {
posts: postObj
if (!posts.error) {
postTools.constructPostObject(posts, data.tid, uid, null, function(postObj) {
io.sockets.in('topic_' + data.tid).emit('event:new_post', {
posts: postObj
});
});
});
}
});
});

Loading…
Cancel
Save