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

This commit is contained in:
Julian Lam
2013-06-26 13:38:05 -04:00
parent 9df0b39a5d
commit 936a284397
3 changed files with 40 additions and 20 deletions

View File

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