Files
NodeBB/public/src/forum/category.js

141 lines
4.1 KiB
JavaScript
Raw Normal View History

(function () {
var cid = templates.get('category_id'),
room = 'category_' + cid,
twitterEl = document.getElementById('twitter-intent'),
facebookEl = document.getElementById('facebook-share'),
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'),
loadingMoreTopics = false;
2013-08-20 12:11:17 -04:00
app.enter_room(room);
twitterEl.addEventListener('click', function () {
window.open(twitter_url, '_blank', 'width=550,height=420,scrollbars=no,status=no');
2013-07-16 20:30:46 -04:00
return false;
}, false);
facebookEl.addEventListener('click', function () {
window.open(facebook_url, '_blank', 'width=626,height=436,scrollbars=no,status=no');
2013-07-16 20:30:46 -04:00
return false;
}, false);
googleEl.addEventListener('click', function () {
window.open(google_url, '_blank', 'width=500,height=570,scrollbars=no,status=no');
2013-07-16 20:30:46 -04:00
return false;
}, false);
var new_post = document.getElementById('new_post');
new_post.onclick = function () {
require(['composer'], function (cmp) {
cmp.push(0, cid);
});
}
ajaxify.register_events([
'event:new_topic'
]);
function onNewTopic(data) {
2013-09-17 13:05:54 -04:00
var html = templates.prepare(templates['category'].blocks['topics']).parse({
topics: [data]
}),
topic = document.createElement('div'),
container = document.getElementById('topics-container'),
topics = document.querySelectorAll('#topics-container a'),
numTopics = topics.length,
x;
jQuery('#topics-container, .category-sidebar').removeClass('hidden');
jQuery('#category-no-topics').remove();
topic.innerHTML = html;
topic = topic.querySelector('a');
2013-08-20 12:11:17 -04:00
if (numTopics > 0) {
2013-09-17 13:05:54 -04:00
for (x = 0; x < numTopics; x++) {
if (topics[x].querySelector('.icon-pushpin')) continue;
container.insertBefore(topic, topics[x]);
$(topic).hide().fadeIn('slow');
break;
}
} else {
container.insertBefore(topic, null);
$(topic).hide().fadeIn('slow');
}
2013-08-19 14:43:37 -04:00
socket.emit('api:categories.getRecentReplies', cid);
2013-09-19 15:10:03 -04:00
$('#topics-container span.timeago').timeago();
}
socket.on('event:new_topic', onNewTopic);
socket.emit('api:categories.getRecentReplies', cid);
socket.on('api:categories.getRecentReplies', function (posts) {
2013-07-02 19:46:58 -04:00
if (!posts || posts.length === 0) {
return;
}
2013-07-15 16:19:27 -04:00
2013-07-02 19:46:58 -04:00
var recent_replies = document.getElementById('category_recent_replies');
recent_replies.innerHTML = '';
2013-08-20 12:11:17 -04:00
2013-09-17 13:05:54 -04:00
var frag = document.createDocumentFragment(),
2013-07-15 16:19:27 -04:00
li = document.createElement('li');
2013-09-17 13:05:54 -04:00
for (var i = 0, numPosts = posts.length; i < numPosts; i++) {
2013-09-19 15:02:35 -04:00
2013-07-15 16:19:27 -04:00
li.setAttribute('data-pid', posts[i].pid);
2013-07-19 10:36:42 -04:00
2013-09-18 16:01:54 -04:00
li.innerHTML = '<a href="/user/' + posts[i].userslug + '"><img title="' + posts[i].username + '" style="width: 48px; height: 48px; /*temporary*/" class="img-rounded" src="' + posts[i].picture + '" class="" /></a>' +
2013-09-17 13:05:54 -04:00
'<a href="/topic/' + posts[i].topicSlug + '#' + posts[i].pid + '">' +
'<p>' +
posts[i].content +
'</p>' +
'<p class="meta"><strong>' + posts[i].username + '</strong></span> -<span class="timeago" title="' + posts[i].relativeTime + '"></span></p>' +
2013-09-17 13:05:54 -04:00
'</a>';
2013-07-15 16:19:27 -04:00
frag.appendChild(li.cloneNode(true));
recent_replies.appendChild(frag);
}
2013-09-19 15:02:35 -04:00
$('#category_recent_replies span.timeago').timeago();
});
2013-08-20 12:11:17 -04:00
function onTopicsLoaded(topics) {
2013-09-17 13:05:54 -04:00
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);
2013-08-20 12:11:17 -04:00
2013-09-19 15:02:35 -04:00
$('#topics-container span.timeago').timeago();
}
function loadMoreTopics(cid) {
loadingMoreTopics = true;
socket.emit('api:category.loadMore', {
2013-08-20 12:11:17 -04:00
cid: cid,
after: $('#topics-container').children().length
}, function (data) {
2013-09-17 13:05:54 -04:00
if (data.topics.length) {
onTopicsLoaded(data.topics);
}
loadingMoreTopics = false;
});
}
$(window).off('scroll').on('scroll', function (ev) {
var bottom = ($(document).height() - $(window).height()) * 0.9;
if ($(window).scrollTop() > bottom && !loadingMoreTopics) {
loadMoreTopics(cid);
}
});
})();