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

171 lines
4.8 KiB
JavaScript
Raw Normal View History

2013-12-22 15:15:59 -05:00
define(['composer'], function(composer) {
2013-11-26 23:55:55 -05:00
var Category = {},
loadingMoreTopics = false;
2013-10-28 10:36:39 -04:00
Category.init = function() {
2013-10-28 10:36:39 -04:00
var cid = templates.get('category_id'),
twitterEl = jQuery('#twitter-intent'),
facebookEl = jQuery('#facebook-share'),
googleEl = jQuery('#google-share'),
twitter_url = templates.get('twitter-intent-url'),
facebook_url = templates.get('facebook-share-url'),
2013-11-26 23:55:55 -05:00
google_url = templates.get('google-share-url');
app.enterRoom('category_' + cid);
twitterEl.on('click', function () {
window.open(twitter_url, '_blank', 'width=550,height=420,scrollbars=no,status=no');
return false;
});
facebookEl.on('click', function () {
window.open(facebook_url, '_blank', 'width=626,height=436,scrollbars=no,status=no');
return false;
});
googleEl.on('click', function () {
window.open(google_url, '_blank', 'width=500,height=570,scrollbars=no,status=no');
return false;
});
2013-11-01 15:04:54 -04:00
$('#new_post').on('click', function () {
2013-12-22 15:15:59 -05:00
composer.newTopic(cid);
2013-11-01 15:07:41 -04:00
});
ajaxify.register_events([
'event:new_topic'
]);
socket.on('event:new_topic', Category.onNewTopic);
socket.emit('api:categories.getRecentReplies', cid);
socket.on('api:categories.getRecentReplies', function (posts) {
if (!posts || posts.length === 0) {
return;
}
var recent_replies = document.getElementById('category_recent_replies');
recent_replies.innerHTML = '';
var frag = document.createDocumentFragment(),
li = document.createElement('li');
for (var i = 0, numPosts = posts.length; i < numPosts; i++) {
li.setAttribute('data-pid', posts[i].pid);
2014-01-08 16:11:47 -05:00
li.innerHTML = '<a href="' + RELATIVE_PATH + '/user/' + posts[i].userslug + '"><img title="' + posts[i].username + '" class="img-rounded user-img" src="' + posts[i].picture + '"/></a>' +
'<a href="' + RELATIVE_PATH + '/topic/' + posts[i].topicSlug + '#' + posts[i].pid + '">' +
2013-11-01 16:25:48 -04:00
'<strong><span>'+ posts[i].username + '</span></strong>' +
'<p>' +
posts[i].content +
'</p>' +
2013-11-01 16:25:48 -04:00
'</a>' +
'<span class="timeago pull-right" title="' + posts[i].relativeTime + '"></span>';
frag.appendChild(li.cloneNode(true));
recent_replies.appendChild(frag);
}
$('#category_recent_replies span.timeago').timeago();
2013-11-24 22:48:58 -05:00
app.createUserTooltips();
});
$(window).off('scroll').on('scroll', function (ev) {
var bottom = ($(document).height() - $(window).height()) * 0.9;
if ($(window).scrollTop() > bottom && !loadingMoreTopics) {
Category.loadMoreTopics(cid);
}
});
};
Category.onNewTopic = function(data) {
2013-09-17 13:05:54 -04:00
var html = templates.prepare(templates['category'].blocks['topics']).parse({
topics: [data]
});
2013-12-22 15:15:59 -05:00
translator.translate(html, function(translatedHTML) {
var topic = $(translatedHTML),
container = $('#topics-container'),
topics = $('#topics-container').children('.category-item'),
numTopics = topics.length;
jQuery('#topics-container, .category-sidebar').removeClass('hidden');
jQuery('#category-no-topics').remove();
if (numTopics > 0) {
for (var x = 0; x < numTopics; x++) {
if ($(topics[x]).find('.fa-thumb-tack').length) {
if(x === numTopics - 1) {
topic.insertAfter(topics[x]);
}
continue;
}
topic.insertBefore(topics[x]);
break;
}
} else {
container.append(topic);
}
2013-08-19 14:43:37 -04:00
topic.hide().fadeIn('slow');
socket.emit('api:categories.getRecentReplies', templates.get('category_id'));
2013-11-01 16:01:48 -04:00
addActiveUser(data);
2013-11-01 16:01:48 -04:00
$('#topics-container span.timeago').timeago();
});
}
2013-11-01 16:01:48 -04:00
function addActiveUser(data) {
var activeUser = $('.category-sidebar .active-users').find('a[data-uid="' + data.uid + '"]');
if(!activeUser.length) {
var newUser = templates.prepare(templates['category'].blocks['active_users']).parse({
active_users: [{
uid: data.uid,
username: data.username,
userslug: data.userslug,
picture: data.teaser_userpicture
}]
});
$(newUser).appendTo($('.category-sidebar .active-users'));
}
}
Category.onTopicsLoaded = function(topics) {
2013-09-17 13:05:54 -04:00
var html = templates.prepare(templates['category'].blocks['topics']).parse({
topics: topics
});
2013-12-22 15:15:59 -05:00
translator.translate(html, function(translatedHTML) {
var container = $('#topics-container');
jQuery('#topics-container, .category-sidebar').removeClass('hidden');
jQuery('#category-no-topics').remove();
html = $(translatedHTML);
container.append(html);
2013-08-20 12:11:17 -04:00
$('#topics-container span.timeago').timeago();
app.makeNumbersHumanReadable(html.find('.human-readable-number'));
});
2013-09-19 15:02:35 -04:00
}
Category.loadMoreTopics = function(cid) {
2013-11-26 23:55:55 -05:00
if (loadingMoreTopics) {
return;
}
2013-11-28 11:16:52 -05:00
loadingMoreTopics = true;
socket.emit('api:category.loadMore', {
2013-08-20 12:11:17 -04:00
cid: cid,
2013-11-04 17:17:04 -05:00
after: $('#topics-container').children('.category-item').length
}, function (data) {
2013-09-17 13:05:54 -04:00
if (data.topics.length) {
Category.onTopicsLoaded(data.topics);
}
loadingMoreTopics = false;
});
}
return Category;
});