diff --git a/public/src/forum/admin/topics.js b/public/src/forum/admin/topics.js index 4294f140a4..322cc75b33 100644 --- a/public/src/forum/admin/topics.js +++ b/public/src/forum/admin/topics.js @@ -1,5 +1,6 @@ $(document).ready(function() { - var topicsListEl = document.querySelector('.topics'); + var topicsListEl = document.querySelector('.topics'), + loadMoreEl = document.getElementById('topics_loadmore'); $(topicsListEl).on('click', '[data-action]', function() { var $this = $(this), @@ -22,6 +23,16 @@ $(document).ready(function() { } }); + loadMoreEl.addEventListener('click', function() { + var topics = document.querySelectorAll('.topics li[data-tid]'), + lastTid = parseInt(topics[topics.length - 1].getAttribute('data-tid')); + + socket.emit('api:admin.topics.getMore', { + limit: 10, + after: lastTid + }); + }, false); + // Resolve proper button state for all topics var topicEls = topicsListEl.querySelectorAll('li'), numTopics = topicEls.length; @@ -81,4 +92,9 @@ socket.on('api:topic.restore', function(response) { $(btnEl).removeClass('active'); } +}); + +socket.on('api:admin.topics.getMore', function(topics) { + var html = templates.prepare(templates['admin/topics'].blocks['topics']).parse(topics); + console.log(html); }); \ No newline at end of file diff --git a/public/templates/admin/topics.tpl b/public/templates/admin/topics.tpl index d3fef7b6b5..68d58e2c19 100644 --- a/public/templates/admin/topics.tpl +++ b/public/templates/admin/topics.tpl @@ -19,4 +19,8 @@ +
+ +
+ \ No newline at end of file diff --git a/src/routes/admin.js b/src/routes/admin.js index 76bd9d8fb9..615a412cfb 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -57,10 +57,7 @@ var user = require('./../user.js'), } break; case 'topics': - topics.getAllTopics(function(topics) { - topics.sort(function(a, b) { - return b.timestamp - a.timestamp; - }); + topics.getAllTopics(10, null, function(topics) { res.json({ topics: topics }); diff --git a/src/topics.js b/src/topics.js index c14bd425f2..bc24eca30b 100644 --- a/src/topics.js +++ b/src/topics.js @@ -184,9 +184,33 @@ marked.setOptions({ }); } - Topics.getAllTopics = function(callback) { + Topics.getAllTopics = function(limit, after, callback) { RDB.smembers('topics:tid', function(err, tids) { - var topics = []; + var topics = [], + numTids, x; + + // Sort into ascending order + tids.sort(function(a, b) { return a - b; }); + + // Eliminate everything after the "after" tid + if (after) { + for(x=0,numTids=tids.length;x= after) { + tids = tids.slice(0, x); + break; + } + } + } + + if (limit) { + if (limit > 0 && limit < tids.length) { + tids = tids.slice(tids.length - limit); + } + } + + // Sort into descending order + tids.sort(function(a, b) { return b - a; }); + async.each(tids, function(tid, next) { Topics.get_topic(tid, 0, function(topicData) { topics.push(topicData); diff --git a/src/websockets.js b/src/websockets.js index 6d161313f2..6530896f9d 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -121,7 +121,6 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); // BEGIN: API calls (todo: organize) - // julian: :^) socket.on('api:updateHeader', function(data) { if(uid) { @@ -391,6 +390,12 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); } }); + + socket.on('api:admin.topics.getMore', function(data) { + topics.getAllTopics(data.limit, data.after, function(topics) { + socket.emit('api:admin.topics.getMore', JSON.stringify(topics)); + }); + }); }); }(SocketIO));