diff --git a/public/src/forum/category.js b/public/src/forum/category.js index eb3b34c450..4d88d475e6 100644 --- a/public/src/forum/category.js +++ b/public/src/forum/category.js @@ -6,7 +6,8 @@ googleEl = document.getElementById('google-share'), twitter_url = templates.get('twitter-intent-url'), facebook_url = templates.get('facebook-share-url'), - google_url = templates.get('google-share-url'); + google_url = templates.get('google-share-url'), + loadingMoreTopics = false; app.enter_room(room); @@ -26,7 +27,7 @@ var new_post = document.getElementById('new_post'); new_post.onclick = function() { require(['composer'], function(cmp) { - cmp.push(0, cid); + cmp.push(0, cid); }); } @@ -34,7 +35,8 @@ 'event:new_topic' ]); - socket.on('event:new_topic', function(data) { + function onNewTopic(data) { + var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: [data] }), topic = document.createElement('div'), container = document.getElementById('topics-container'), @@ -59,8 +61,9 @@ container.insertBefore(topic, null); $(topic).hide().fadeIn('slow'); } - }); + } + socket.on('event:new_topic', onNewTopic); socket.emit('api:categories.getRecentReplies', cid); socket.on('api:categories.getRecentReplies', function(posts) { @@ -91,5 +94,41 @@ recent_replies.appendChild(frag); } }); + + function onTopicsLoaded(topics) { + + var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: topics }), + container = $('#topics-container'); + + jQuery('#topics-container, .category-sidebar').removeClass('hidden'); + jQuery('#category-no-topics').remove(); + + container.append(html); + } + + + + function loadMoreTopics(cid) { + loadingMoreTopics = true; + socket.emit('api:category.loadMore', { + cid: cid, + after: $('#topics-container').children().length + }, function(data) { + if(data.topics.length) { + onTopicsLoaded(data.topics); + loadingMoreTopics = false; + } + }); + } + + $(window).off('scroll').on('scroll', function(ev) { + var windowHeight = document.body.offsetHeight - $(window).height(), + half = windowHeight / 2; + + if (document.body.scrollTop > half && !loadingMoreTopics) { + loadMoreTopics(cid); + } + }); + })(); \ No newline at end of file diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 7701522f45..2b04f4a2d3 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -201,9 +201,8 @@ }, false); // Infinite scrolling of posts - $(window).off('scroll'); - $(window).on('scroll', function() { - var windowHeight = document.body.offsetHeight - $(window).height(), + $(window).off('scroll').on('scroll', function() { + var windowHeight = document.body.offsetHeight - $(window).height(), half = windowHeight / 2; if (document.body.scrollTop > half && !app.infiniteLoaderActive && $('#post-container').children().length) { diff --git a/src/categories.js b/src/categories.js index 4f584b077a..96e3552eea 100644 --- a/src/categories.js +++ b/src/categories.js @@ -17,8 +17,9 @@ var RDB = require('./redis.js'), category_description = categoryData.description; function getTopicIds(next) { - Categories.getTopicIds(category_id, next); + Categories.getTopicIds(category_id, 0, 19, next); } + function getActiveUsers(next) { Categories.getActiveUsers(category_id, next); } @@ -44,15 +45,7 @@ var RDB = require('./redis.js'), function getTopics(next) { topics.getTopicsByTids(tids, current_user, function(topicsData) { - // Float pinned topics to the top - topicsData = topicsData.sort(function(a, b) { - if (a.pinned !== b.pinned) return b.pinned - a.pinned; - else { - return b.lastposttime - a.lastposttime; - } - }); next(null, topicsData); - }, category_id); } @@ -89,8 +82,16 @@ var RDB = require('./redis.js'), }); } - Categories.getTopicIds = function(cid, callback) { - RDB.smembers('categories:' + cid + ':tid', callback); + Categories.getCategoryTopics = function(cid, start, stop, uid, callback) { + Categories.getTopicIds(cid, start, stop, function(err, tids) { + topics.getTopicsByTids(tids, uid, function(topicsData) { + callback(topicsData); + }, cid); + }); + } + + Categories.getTopicIds = function(cid, start, stop, callback) { + RDB.zrevrange('categories:' + cid + ':tid', start, stop, callback); } Categories.getActiveUsers = function(cid, callback) { @@ -144,7 +145,7 @@ var RDB = require('./redis.js'), } Categories.isTopicsRead = function(cid, uid, callback) { - RDB.smembers('categories:' + cid + ':tid', function(err, tids) { + RDB.zrange('categories:' + cid + ':tid', 0, -1, function(err, tids) { topics.hasReadTopics(tids, uid, function(hasRead) { diff --git a/src/posts.js b/src/posts.js index 146ff06825..0a2bcd1cab 100644 --- a/src/posts.js +++ b/src/posts.js @@ -108,6 +108,20 @@ var RDB = require('./redis.js'), }); } + Posts.getPostField = function(pid, field, callback) { + RDB.hget('post:' + pid, field, function(err, data) { + if(err === null) + callback(data); + else + console.log(err); + }); + } + + Posts.setPostField = function(pid, field, value) { + RDB.hset('post:' + pid, field, value); + } + + Posts.getPostsByPids = function(pids, callback) { var posts = []; @@ -141,35 +155,6 @@ var RDB = require('./redis.js'), }); } - Posts.getPostField = function(pid, field, callback) { - RDB.hget('post:' + pid, field, function(err, data) { - if(err === null) - callback(data); - else - console.log(err); - }); - } - - Posts.setPostField = function(pid, field, value) { - RDB.hset('post:' + pid, field, value); - } - - Posts.getPostFields = function(pid, fields, callback) { - RDB.hmget('post:' + pid, fields, function(err, data) { - if(err === null) { - var returnData = {}; - - for(var i=0, ii=fields.length; i