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
|
|
|
|
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();
|
2014-02-27 23:45:12 -05:00
|
|
|
|
|
|
|
|
$('#topics-container').on('click', '.topic-title', function() {
|
|
|
|
|
var clickedTid = $(this).parents('li.category-item[data-tid]').attr('data-tid');
|
|
|
|
|
$('#topics-container li.category-item').each(function(index, el) {
|
|
|
|
|
if($(el).offset().top - $(window).scrollTop() > 0) {
|
|
|
|
|
tid = $(el).attr('data-tid');
|
|
|
|
|
|
|
|
|
|
localStorage.setItem('category:bookmark', tid);
|
|
|
|
|
localStorage.setItem('category:bookmark:clicked', clickedTid);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$(window).on('action:popstate', function(ev, data) {
|
|
|
|
|
if(data.url.indexOf('category/') === 0) {
|
|
|
|
|
var bookmark = localStorage.getItem('category:bookmark');
|
|
|
|
|
var clicked = localStorage.getItem('category:bookmark:clicked');
|
|
|
|
|
|
|
|
|
|
if (bookmark) {
|
|
|
|
|
|
|
|
|
|
if(config.usePagination) {
|
|
|
|
|
socket.emit('topics.getTidPage', bookmark, function(err, page) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(parseInt(page, 10) !== pagination.currentPage) {
|
|
|
|
|
pagination.loadPage(page);
|
|
|
|
|
} else {
|
|
|
|
|
Category.scrollToTopic(bookmark, clicked, 400);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
socket.emit('topics.getTidIndex', bookmark, function(err, index) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(index === 0) {
|
|
|
|
|
Category.highlightTopic(clicked);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$('#topics-container').empty();
|
|
|
|
|
loadingMoreTopics = false;
|
|
|
|
|
|
|
|
|
|
Category.loadMoreTopics(templates.get('category_id'), after, function() {
|
|
|
|
|
Category.scrollToTopic(bookmark, clicked, 0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Category.highlightTopic = function(tid) {
|
|
|
|
|
var highlight = $('#topics-container li.category-item[data-tid="' + tid + '"]');
|
|
|
|
|
if(highlight.length && !highlight.hasClass('highlight')) {
|
|
|
|
|
highlight.addClass('highlight');
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
highlight.removeClass('highlight');
|
|
|
|
|
}, 5000);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Category.scrollToTopic = function(tid, clickedTid, duration, offset) {
|
|
|
|
|
if(!tid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!offset) {
|
|
|
|
|
offset = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($('#topics-container li.category-item[data-tid="' + tid + '"]').length) {
|
|
|
|
|
var cid = templates.get('category_id');
|
|
|
|
|
var scrollTo = $('#topics-container li.category-item[data-tid="' + tid + '"]');
|
|
|
|
|
|
|
|
|
|
if (cid && scrollTo.length) {
|
|
|
|
|
$('html, body').animate({
|
|
|
|
|
scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + 'px'
|
|
|
|
|
}, duration !== undefined ? duration : 400, function() {
|
|
|
|
|
Category.highlightTopic(clickedTid);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-24 22:26:11 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function enableInfiniteLoading() {
|
|
|
|
|
if(!config.usePagination) {
|
2014-02-27 23:45:12 -05:00
|
|
|
app.enableInfiniteLoading(function(direction) {
|
|
|
|
|
|
|
|
|
|
if(!loadingMoreTopics && $('#topics-container').children().length) {
|
|
|
|
|
|
|
|
|
|
var after = 0;
|
|
|
|
|
var el = null;
|
|
|
|
|
|
|
|
|
|
if(direction > 0) {
|
|
|
|
|
el = $('#topics-container .category-item[data-tid]').last();
|
|
|
|
|
after = parseInt(el.attr('data-index'), 10) + 1;
|
|
|
|
|
} else {
|
|
|
|
|
el = $('#topics-container .category-item[data-tid]').first();
|
|
|
|
|
after = parseInt(el.attr('data-index'), 10);
|
|
|
|
|
after -= config.topicsPerPage;
|
|
|
|
|
if(after < 0) {
|
|
|
|
|
after = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var offset = el.offset().top - $('#header-menu').offset().top + $('#header-menu').height();
|
|
|
|
|
|
|
|
|
|
Category.loadMoreTopics(templates.get('category_id'), after, function() {
|
|
|
|
|
if(direction < 0 && el) {
|
|
|
|
|
Category.scrollToTopic(el.attr('data-tid'), null, 0, offset);
|
|
|
|
|
}
|
|
|
|
|
});
|
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
|
|
|
});
|
2014-02-27 23:45:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Category.onTopicsLoaded = function(topics, callback) {
|
|
|
|
|
if(!topics || !topics.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeAlreadyAddedTopics() {
|
|
|
|
|
topics = topics.filter(function(topic) {
|
|
|
|
|
return $('#topics-container li[data-tid="' + topic.tid +'"]').length === 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var after = null,
|
|
|
|
|
before = null;
|
|
|
|
|
|
|
|
|
|
function findInsertionPoint() {
|
|
|
|
|
if (!$('#topics-container .category-item[data-tid]').length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var last = $('#topics-container .category-item[data-tid]').last();
|
|
|
|
|
var lastIndex = last.attr('data-index');
|
|
|
|
|
var firstIndex = topics[topics.length - 1].index;
|
|
|
|
|
if (firstIndex > lastIndex) {
|
|
|
|
|
after = last;
|
|
|
|
|
} else {
|
|
|
|
|
before = $('#topics-container .category-item[data-tid]').first();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeAlreadyAddedTopics();
|
|
|
|
|
if(!topics.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findInsertionPoint();
|
2013-05-29 12:17:44 -04:00
|
|
|
|
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) {
|
2014-02-27 23:45:12 -05:00
|
|
|
var container = $('#topics-container'),
|
|
|
|
|
html = $(translatedHTML);
|
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
|
|
|
|
2014-01-24 22:26:11 -05:00
|
|
|
if(config.usePagination) {
|
|
|
|
|
container.empty().append(html);
|
|
|
|
|
} else {
|
2014-02-27 23:45:12 -05:00
|
|
|
if(after) {
|
|
|
|
|
html.insertAfter(after);
|
|
|
|
|
} else if(before) {
|
|
|
|
|
html.insertBefore(before);
|
|
|
|
|
} else {
|
|
|
|
|
container.append(html);
|
|
|
|
|
}
|
2014-01-24 22:26:11 -05:00
|
|
|
}
|
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'));
|
2014-02-27 23:45:12 -05:00
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
callback(topics);
|
|
|
|
|
}
|
2013-12-07 16:14:44 -05:00
|
|
|
});
|
2014-02-27 23:45:12 -05:00
|
|
|
};
|
2013-08-09 20:03:19 -04:00
|
|
|
|
2014-02-27 23:45:12 -05:00
|
|
|
Category.loadMoreTopics = function(cid, after, callback) {
|
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-27 23:45:12 -05:00
|
|
|
if(after === 0 && $('#topics-container li.category-item[data-index="0"]').length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 01:55:25 -05:00
|
|
|
$(window).trigger('action:categories.loading');
|
2013-08-09 20:03:19 -04:00
|
|
|
loadingMoreTopics = true;
|
2014-02-27 23:45:12 -05:00
|
|
|
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('categories.loadMore', {
|
2013-08-20 12:11:17 -04:00
|
|
|
cid: cid,
|
2014-02-27 23:45:12 -05:00
|
|
|
after: after
|
2014-01-16 19:57:28 -05:00
|
|
|
}, function (err, data) {
|
2014-02-27 23:45:12 -05:00
|
|
|
loadingMoreTopics = false;
|
|
|
|
|
|
2014-01-17 12:49:21 -05:00
|
|
|
if(err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data && data.topics.length) {
|
2014-02-27 23:45:12 -05:00
|
|
|
Category.onTopicsLoaded(data.topics, callback);
|
2014-01-26 17:17:34 -05:00
|
|
|
$('#topics-container').attr('data-nextstart', data.nextStart);
|
2014-02-27 23:45:12 -05:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
callback(data.topics);
|
|
|
|
|
}
|
2013-08-09 20:03:19 -04:00
|
|
|
}
|
2014-02-27 23:45:12 -05:00
|
|
|
|
|
|
|
|
|
2014-02-21 01:55:25 -05:00
|
|
|
$(window).trigger('action:categories.loaded');
|
2013-08-09 20:03:19 -04:00
|
|
|
});
|
2014-02-27 23:45:12 -05:00
|
|
|
};
|
2013-08-09 20:03:19 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
return Category;
|
|
|
|
|
});
|