2014-01-24 22:26:11 -05:00
|
|
|
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
|
|
|
|
2014-02-27 15:35:50 -05:00
|
|
|
$(window).on('action:popstate', function(ev, data) {
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
Category.init = function() {
|
2013-10-28 10:36:39 -04:00
|
|
|
var cid = templates.get('category_id'),
|
2014-01-14 14:30:09 -05:00
|
|
|
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;
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2013-11-23 17:07:31 -05:00
|
|
|
app.enterRoom('category_' + cid);
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2014-02-23 19:38:26 -05:00
|
|
|
$('#twitter-share').on('click', function () {
|
2014-01-14 14:30:09 -05:00
|
|
|
window.open(twitterUrl, '_blank', 'width=550,height=420,scrollbars=no,status=no');
|
2013-10-03 15:04:25 -04:00
|
|
|
return false;
|
2013-10-30 16:23:36 -04:00
|
|
|
});
|
2014-01-24 22:26:11 -05:00
|
|
|
|
|
|
|
|
$('#facebook-share').on('click', function () {
|
2014-01-14 14:30:09 -05:00
|
|
|
window.open(facebookUrl, '_blank', 'width=626,height=436,scrollbars=no,status=no');
|
2013-10-03 15:04:25 -04:00
|
|
|
return false;
|
2013-10-30 16:23:36 -04:00
|
|
|
});
|
2014-01-24 22:26:11 -05:00
|
|
|
|
|
|
|
|
$('#google-share').on('click', function () {
|
2014-01-14 14:30:09 -05:00
|
|
|
window.open(googleUrl, '_blank', 'width=500,height=570,scrollbars=no,status=no');
|
2013-10-03 15:04:25 -04:00
|
|
|
return false;
|
2013-10-30 16:23:36 -04:00
|
|
|
});
|
2013-10-03 15:04:25 -04:00
|
|
|
|
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
|
|
|
});
|
2013-10-03 15:04:25 -04:00
|
|
|
|
|
|
|
|
ajaxify.register_events([
|
|
|
|
|
'event:new_topic'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
socket.on('event:new_topic', Category.onNewTopic);
|
|
|
|
|
|
2014-01-24 22:26:11 -05:00
|
|
|
enableInfiniteLoading();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function enableInfiniteLoading() {
|
|
|
|
|
if(!config.usePagination) {
|
2014-01-26 14:56:16 -05:00
|
|
|
app.enableInfiniteLoading(function() {
|
|
|
|
|
if(!loadingMoreTopics) {
|
2014-01-25 22:30:48 -05:00
|
|
|
Category.loadMoreTopics(templates.get('category_id'));
|
2014-01-24 22:26:11 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2014-01-25 19:19:25 -05:00
|
|
|
pagination.init(templates.get('currentPage'), templates.get('pageCount'));
|
2014-01-24 22:26:11 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-29 12:17:44 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
Category.onNewTopic = function(data) {
|
2014-02-21 16:02:48 -05:00
|
|
|
$(window).trigger('filter:categories.new_topic', data);
|
|
|
|
|
|
2013-09-17 13:05:54 -04:00
|
|
|
var html = templates.prepare(templates['category'].blocks['topics']).parse({
|
|
|
|
|
topics: [data]
|
2013-12-07 16:11:37 -05:00
|
|
|
});
|
2013-12-22 15:15:59 -05:00
|
|
|
|
2013-12-07 16:11:37 -05:00
|
|
|
translator.translate(html, function(translatedHTML) {
|
|
|
|
|
var topic = $(translatedHTML),
|
|
|
|
|
container = $('#topics-container'),
|
|
|
|
|
topics = $('#topics-container').children('.category-item'),
|
|
|
|
|
numTopics = topics.length;
|
|
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
$('#topics-container, .category-sidebar').removeClass('hidden');
|
|
|
|
|
$('#category-no-topics').remove();
|
2013-12-07 16:11:37 -05:00
|
|
|
|
|
|
|
|
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;
|
2013-11-21 15:33:41 -05:00
|
|
|
}
|
2013-12-07 16:11:37 -05:00
|
|
|
topic.insertBefore(topics[x]);
|
|
|
|
|
break;
|
2013-11-21 15:33:41 -05:00
|
|
|
}
|
2013-12-07 16:11:37 -05:00
|
|
|
} else {
|
|
|
|
|
container.append(topic);
|
2013-05-29 12:17:44 -04:00
|
|
|
}
|
2013-08-19 14:43:37 -04:00
|
|
|
|
2013-12-07 16:11:37 -05:00
|
|
|
topic.hide().fadeIn('slow');
|
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) {
|
2014-01-25 21:35:09 -05:00
|
|
|
pagination.recreatePaginationLinks(newPageCount);
|
2014-01-24 23:05:59 -05:00
|
|
|
});
|
|
|
|
|
|
2013-12-07 16:11:37 -05:00
|
|
|
$('#topics-container span.timeago').timeago();
|
2014-01-24 23:05:59 -05:00
|
|
|
app.createUserTooltips();
|
2014-02-21 16:02:48 -05:00
|
|
|
|
|
|
|
|
$(window).trigger('action:categories.new_topic.loaded');
|
2013-12-07 16:11:37 -05:00
|
|
|
});
|
2013-08-09 20:03:19 -04:00
|
|
|
}
|
2013-05-29 12:17:44 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
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-07 16:14:44 -05:00
|
|
|
});
|
2013-12-22 15:15:59 -05:00
|
|
|
|
2013-12-07 16:14:44 -05:00
|
|
|
translator.translate(html, function(translatedHTML) {
|
|
|
|
|
var container = $('#topics-container');
|
2013-08-09 20:03:19 -04:00
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
$('#topics-container, .category-sidebar').removeClass('hidden');
|
|
|
|
|
$('#category-no-topics').remove();
|
2013-08-09 20:03:19 -04:00
|
|
|
|
2013-12-07 16:14:44 -05:00
|
|
|
html = $(translatedHTML);
|
2014-01-24 22:26:11 -05:00
|
|
|
|
|
|
|
|
if(config.usePagination) {
|
|
|
|
|
container.empty().append(html);
|
|
|
|
|
} else {
|
|
|
|
|
container.append(html);
|
|
|
|
|
}
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2013-12-07 16:14:44 -05:00
|
|
|
$('#topics-container span.timeago').timeago();
|
2014-01-24 23:05:59 -05:00
|
|
|
app.createUserTooltips();
|
2013-12-07 16:14:44 -05:00
|
|
|
app.makeNumbersHumanReadable(html.find('.human-readable-number'));
|
|
|
|
|
});
|
2013-09-19 15:02:35 -04:00
|
|
|
}
|
2013-08-09 20:03:19 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
Category.loadMoreTopics = function(cid) {
|
2014-02-10 19:59:04 -05:00
|
|
|
if (loadingMoreTopics || !$('#topics-container').length) {
|
2013-11-26 23:55:55 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2013-11-28 11:16:52 -05:00
|
|
|
|
2014-02-21 01:55:25 -05:00
|
|
|
$(window).trigger('action:categories.loading');
|
2013-08-09 20:03:19 -04:00
|
|
|
loadingMoreTopics = true;
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('categories.loadMore', {
|
2013-08-20 12:11:17 -04:00
|
|
|
cid: cid,
|
2014-01-26 17:17:34 -05:00
|
|
|
after: $('#topics-container').attr('data-nextstart')
|
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) {
|
2013-10-03 15:04:25 -04:00
|
|
|
Category.onTopicsLoaded(data.topics);
|
2014-01-26 17:17:34 -05:00
|
|
|
$('#topics-container').attr('data-nextstart', data.nextStart);
|
2013-08-09 20:03:19 -04:00
|
|
|
}
|
2013-08-09 20:44:08 -04:00
|
|
|
loadingMoreTopics = false;
|
2014-02-21 01:55:25 -05:00
|
|
|
$(window).trigger('action:categories.loaded');
|
2013-08-09 20:03:19 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
return Category;
|
|
|
|
|
});
|