adding 'load more topics' button to topic admin

v1.18.x
Julian Lam 12 years ago
parent 3ef32ba7fb
commit 89e852f692

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

@ -19,4 +19,8 @@
<!-- END topics -->
</ul>
<div class="text-center">
<button id="topics_loadmore" class="btn btn-large">Load More Topics</button>
</div>
<script type="text/javascript" src="../../src/forum/admin/topics.js"></script>

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

@ -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<numTids;x++) {
if (tids[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);

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

Loading…
Cancel
Save