Files
NodeBB/public/src/client/categories.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
2018-01-25 12:52:51 -05:00
define('forum/categories', ['components'], function (components) {
2015-02-17 13:25:13 -05:00
var categories = {};
$(window).on('action:ajaxify.start', function (ev, data) {
if (ajaxify.currentPage !== data.url) {
2015-02-17 13:25:13 -05:00
socket.removeListener('event:new_post', categories.onNewPost);
}
});
categories.init = function () {
2015-02-17 13:25:13 -05:00
app.enterRoom('categories');
socket.removeListener('event:new_post', categories.onNewPost);
socket.on('event:new_post', categories.onNewPost);
2015-02-17 13:25:13 -05:00
$('.category-header').tooltip({
2017-02-17 19:31:21 -07:00
placement: 'bottom',
});
};
categories.onNewPost = function (data) {
if (data && data.posts && data.posts.length && data.posts[0].topic) {
renderNewPost(data.posts[0].topic.cid, data.posts[0]);
}
};
function renderNewPost(cid, post) {
var category = components.get('categories/category', 'cid', cid);
var numRecentReplies = category.attr('data-numRecentReplies');
if (!numRecentReplies || !parseInt(numRecentReplies, 10)) {
return;
}
2019-03-08 13:22:48 -05:00
if (!category.find('[component="topic/teaser"]').length) {
return;
}
var recentPosts = category.find('[component="category/posts"]');
2018-01-25 12:29:54 -05:00
app.parseAndTranslate('partials/categories/lastpost', 'posts', { posts: [post] }, function (html) {
html.find('.post-content img:not(.not-responsive)').addClass('img-responsive');
html.hide();
if (recentPosts.length === 0) {
html.appendTo(category);
} else {
html.insertBefore(recentPosts.first());
}
html.fadeIn();
app.createUserTooltips();
2016-03-03 20:35:54 +02:00
html.find('.timeago').timeago();
if (category.find('[component="category/posts"]').length > parseInt(numRecentReplies, 10)) {
recentPosts.last().remove();
}
2017-02-18 12:30:49 -07:00
$(window).trigger('action:posts.loaded', { posts: [post] });
});
}
2015-02-17 13:25:13 -05:00
return categories;
});