2014-10-08 15:36:47 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-02-17 21:55:19 -07:00
|
|
|
|
2018-01-25 12:52:51 -05:00
|
|
|
define('forum/categories', ['components'], function (components) {
|
2015-02-17 13:25:13 -05:00
|
|
|
var categories = {};
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$(window).on('action:ajaxify.start', function (ev, data) {
|
2015-03-06 19:02:18 -05:00
|
|
|
if (ajaxify.currentPage !== data.url) {
|
2015-02-17 13:25:13 -05:00
|
|
|
socket.removeListener('event:new_post', categories.onNewPost);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
categories.init = function () {
|
2015-02-17 13:25:13 -05:00
|
|
|
app.enterRoom('categories');
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2016-03-03 11:59:40 -05:00
|
|
|
socket.removeListener('event:new_post', categories.onNewPost);
|
|
|
|
|
socket.on('event:new_post', categories.onNewPost);
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2015-02-17 13:25:13 -05:00
|
|
|
$('.category-header').tooltip({
|
2017-02-17 19:31:21 -07:00
|
|
|
placement: 'bottom',
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
categories.onNewPost = function (data) {
|
2014-10-08 15:36:47 -04:00
|
|
|
if (data && data.posts && data.posts.length && data.posts[0].topic) {
|
|
|
|
|
renderNewPost(data.posts[0].topic.cid, data.posts[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function renderNewPost(cid, post) {
|
2015-04-12 11:19:22 -04:00
|
|
|
var category = components.get('categories/category', 'cid', cid);
|
2014-10-08 15:36:47 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2015-04-12 11:19:22 -04:00
|
|
|
var recentPosts = category.find('[component="category/posts"]');
|
2014-10-08 15:36:47 -04:00
|
|
|
|
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');
|
2014-10-08 15:36:47 -04:00
|
|
|
html.hide();
|
2017-02-18 01:52:56 -07:00
|
|
|
if (recentPosts.length === 0) {
|
2015-04-12 11:19:22 -04:00
|
|
|
html.appendTo(category);
|
2014-10-08 15:36:47 -04:00
|
|
|
} else {
|
|
|
|
|
html.insertBefore(recentPosts.first());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html.fadeIn();
|
|
|
|
|
|
|
|
|
|
app.createUserTooltips();
|
2016-03-03 20:35:54 +02:00
|
|
|
html.find('.timeago').timeago();
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2015-04-12 11:19:22 -04:00
|
|
|
if (category.find('[component="category/posts"]').length > parseInt(numRecentReplies, 10)) {
|
2014-10-08 15:36:47 -04:00
|
|
|
recentPosts.last().remove();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 12:30:49 -07:00
|
|
|
$(window).trigger('action:posts.loaded', { posts: [post] });
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 13:25:13 -05:00
|
|
|
return categories;
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|