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

190 lines
5.5 KiB
JavaScript
Raw Normal View History

define(['composer', 'forum/pagination'], function(composer, pagination) {
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'),
categoryName = templates.get('category_name'),
categoryUrl = encodeURIComponent(window.location.href),
twitterUrl = "https://twitter.com/intent/tweet?url=" + categoryUrl + "&text=" + encodeURIComponent(categoryName),
facebookUrl = "https://www.facebook.com/sharer/sharer.php?u=" + categoryUrl,
googleUrl = "https://plus.google.com/share?url=" + categoryUrl;
app.enterRoom('category_' + cid);
$('#twitter-intent').on('click', function () {
window.open(twitterUrl, '_blank', 'width=550,height=420,scrollbars=no,status=no');
return false;
});
$('#facebook-share').on('click', function () {
window.open(facebookUrl, '_blank', 'width=626,height=436,scrollbars=no,status=no');
return false;
});
$('#google-share').on('click', function () {
window.open(googleUrl, '_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('categories.getRecentReplies', cid, renderRecentReplies);
enableInfiniteLoading();
};
function enableInfiniteLoading() {
if(!config.usePagination) {
$(window).off('scroll').on('scroll', function (ev) {
var bottom = ($(document).height() - $(window).height()) * 0.9;
if ($(window).scrollTop() > bottom && !loadingMoreTopics) {
Category.loadMoreTopics(cid);
}
});
} else {
pagination.init(templates.get('currentPage'), templates.get('pageCount'));
}
}
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('categories.getRecentReplies', templates.get('category_id'), renderRecentReplies);
2013-11-01 16:01:48 -04:00
addActiveUser(data);
2013-11-01 16:01:48 -04:00
2014-01-24 23:05:59 -05:00
socket.emit('categories.getPageCount', templates.get('category_id'), function(err, newPageCount) {
pagination.recreatePaginationLinks('category', newPageCount);
});
$('#topics-container span.timeago').timeago();
2014-01-24 23:05:59 -05:00
app.createUserTooltips();
});
}
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);
if(config.usePagination) {
container.empty().append(html);
} else {
container.append(html);
}
2013-08-20 12:11:17 -04:00
$('#topics-container span.timeago').timeago();
2014-01-24 23:05:59 -05:00
app.createUserTooltips();
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('categories.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
2014-01-16 19:57:28 -05:00
}, function (err, data) {
2014-01-17 12:49:21 -05:00
if(err) {
return app.alertError(err.message);
}
if (data && data.topics.length) {
Category.onTopicsLoaded(data.topics);
}
loadingMoreTopics = false;
});
}
2014-01-16 19:57:28 -05:00
function renderRecentReplies(err, posts) {
if (err || !posts || posts.length === 0) {
return;
}
var recentReplies = $('#category_recent_replies');
2014-01-18 16:00:05 -05:00
var replies = '';
for (var i = 0, numPosts = posts.length; i < numPosts; ++i) {
2014-01-20 21:29:14 -05:00
replies += '<li data-pid="'+ posts[i].pid +'" class="clearfix">' +
'<a href="' + RELATIVE_PATH + '/user/' + posts[i].userslug + '"><img title="' + posts[i].username + '" class="img-rounded user-img" src="' + posts[i].picture + '"/></a>' +
2014-01-20 21:00:10 -05:00
'<strong><span>'+ posts[i].username + '</span></strong>' +
2014-01-20 21:29:14 -05:00
'<p>' + posts[i].content + '</p>' +
'<span class="pull-right">'+
'<a href="' + RELATIVE_PATH + '/topic/' + posts[i].topicSlug + '#' + posts[i].pid + '">posted</a> '+
'<span class="timeago" title="' + posts[i].relativeTime + '"></span>' +
'</span>'+
2014-01-22 11:08:52 -05:00
'</li>';
}
2014-01-18 16:00:05 -05:00
recentReplies.html(replies);
$('#category_recent_replies span.timeago').timeago();
app.createUserTooltips();
};
return Category;
});