2013-10-03 15:04:25 -04:00
|
|
|
define(function () {
|
2013-10-28 10:36:39 -04:00
|
|
|
var Category = {};
|
|
|
|
|
|
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'),
|
|
|
|
|
room = 'category_' + cid,
|
2013-10-30 16:23:36 -04:00
|
|
|
twitterEl = jQuery('#twitter-intent'),
|
|
|
|
|
facebookEl = jQuery('#facebook-share'),
|
|
|
|
|
googleEl = jQuery('#google-share'),
|
2013-10-03 15:04:25 -04:00
|
|
|
twitter_url = templates.get('twitter-intent-url'),
|
|
|
|
|
facebook_url = templates.get('facebook-share-url'),
|
|
|
|
|
google_url = templates.get('google-share-url'),
|
|
|
|
|
loadingMoreTopics = false;
|
|
|
|
|
|
|
|
|
|
app.enter_room(room);
|
|
|
|
|
|
2013-10-30 16:23:36 -04:00
|
|
|
twitterEl.on('click', function () {
|
2013-10-03 15:04:25 -04:00
|
|
|
window.open(twitter_url, '_blank', 'width=550,height=420,scrollbars=no,status=no');
|
|
|
|
|
return false;
|
2013-10-30 16:23:36 -04:00
|
|
|
});
|
|
|
|
|
facebookEl.on('click', function () {
|
2013-10-03 15:04:25 -04:00
|
|
|
window.open(facebook_url, '_blank', 'width=626,height=436,scrollbars=no,status=no');
|
|
|
|
|
return false;
|
2013-10-30 16:23:36 -04:00
|
|
|
});
|
|
|
|
|
googleEl.on('click', function () {
|
2013-10-03 15:04:25 -04:00
|
|
|
window.open(google_url, '_blank', 'width=500,height=570,scrollbars=no,status=no');
|
|
|
|
|
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-10-03 15:04:25 -04:00
|
|
|
require(['composer'], function (cmp) {
|
|
|
|
|
cmp.push(0, cid);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ajaxify.register_events([
|
|
|
|
|
'event:new_topic'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
socket.on('event:new_topic', Category.onNewTopic);
|
|
|
|
|
|
|
|
|
|
socket.emit('api:categories.getRecentReplies', cid);
|
|
|
|
|
socket.on('api:categories.getRecentReplies', function (posts) {
|
|
|
|
|
if (!posts || posts.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var recent_replies = document.getElementById('category_recent_replies');
|
|
|
|
|
|
|
|
|
|
recent_replies.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
var frag = document.createDocumentFragment(),
|
|
|
|
|
li = document.createElement('li');
|
|
|
|
|
for (var i = 0, numPosts = posts.length; i < numPosts; i++) {
|
|
|
|
|
|
|
|
|
|
li.setAttribute('data-pid', posts[i].pid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
li.innerHTML = '<a href="/user/' + posts[i].userslug + '"><img title="' + posts[i].username + '" style="width: 48px; height: 48px; /*temporary*/" class="img-rounded" src="' + posts[i].picture + '" class="" /></a>' +
|
|
|
|
|
'<a href="/topic/' + posts[i].topicSlug + '#' + posts[i].pid + '">' +
|
|
|
|
|
'<p>' +
|
|
|
|
|
posts[i].content +
|
|
|
|
|
'</p>' +
|
|
|
|
|
'<p class="meta"><strong>' + posts[i].username + '</strong></span> -<span class="timeago" title="' + posts[i].relativeTime + '"></span></p>' +
|
|
|
|
|
'</a>';
|
|
|
|
|
|
|
|
|
|
frag.appendChild(li.cloneNode(true));
|
|
|
|
|
recent_replies.appendChild(frag);
|
|
|
|
|
}
|
|
|
|
|
$('#category_recent_replies span.timeago').timeago();
|
2013-06-04 16:20:27 -04:00
|
|
|
});
|
2013-05-29 12:17:44 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
$(window).off('scroll').on('scroll', function (ev) {
|
|
|
|
|
var bottom = ($(document).height() - $(window).height()) * 0.9;
|
2013-05-29 12:17:44 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
if ($(window).scrollTop() > bottom && !loadingMoreTopics) {
|
|
|
|
|
Category.loadMoreTopics(cid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Category.onNewTopic = function(data) {
|
2013-09-17 13:05:54 -04:00
|
|
|
var html = templates.prepare(templates['category'].blocks['topics']).parse({
|
|
|
|
|
topics: [data]
|
|
|
|
|
}),
|
2013-09-25 12:30:07 -04:00
|
|
|
topic = $(html),
|
|
|
|
|
container = $('#topics-container'),
|
2013-10-28 10:27:03 -04:00
|
|
|
topics = $('#topics-container').children('.category-item'),
|
2013-09-25 12:30:07 -04:00
|
|
|
numTopics = topics.length;
|
2013-05-29 12:17:44 -04:00
|
|
|
|
2013-06-11 16:36:33 -04:00
|
|
|
jQuery('#topics-container, .category-sidebar').removeClass('hidden');
|
|
|
|
|
jQuery('#category-no-topics').remove();
|
2013-05-29 12:17:44 -04:00
|
|
|
|
|
|
|
|
if (numTopics > 0) {
|
2013-09-25 12:30:07 -04:00
|
|
|
for (var x = 0; x < numTopics; x++) {
|
|
|
|
|
if ($(topics[x]).find('.icon-pushpin').length)
|
|
|
|
|
continue;
|
|
|
|
|
topic.insertBefore(topics[x]);
|
|
|
|
|
topic.hide().fadeIn('slow');
|
2013-05-29 12:17:44 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2013-09-25 12:30:07 -04:00
|
|
|
container.append(topic);
|
|
|
|
|
topic.hide().fadeIn('slow');
|
2013-05-29 12:17:44 -04:00
|
|
|
}
|
2013-08-19 14:43:37 -04:00
|
|
|
|
2013-10-28 10:36:39 -04:00
|
|
|
socket.emit('api:categories.getRecentReplies', templates.get('category_id'));
|
2013-09-19 15:10:03 -04:00
|
|
|
$('#topics-container span.timeago').timeago();
|
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-08-09 20:03:19 -04:00
|
|
|
|
2013-09-17 13:05:54 -04:00
|
|
|
var html = templates.prepare(templates['category'].blocks['topics']).parse({
|
|
|
|
|
topics: topics
|
|
|
|
|
}),
|
2013-08-09 20:03:19 -04:00
|
|
|
container = $('#topics-container');
|
|
|
|
|
|
|
|
|
|
jQuery('#topics-container, .category-sidebar').removeClass('hidden');
|
|
|
|
|
jQuery('#category-no-topics').remove();
|
|
|
|
|
|
|
|
|
|
container.append(html);
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2013-09-19 15:02:35 -04:00
|
|
|
$('#topics-container span.timeago').timeago();
|
|
|
|
|
}
|
2013-08-09 20:03:19 -04:00
|
|
|
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
Category.loadMoreTopics = function(cid) {
|
2013-08-09 20:03:19 -04:00
|
|
|
loadingMoreTopics = true;
|
|
|
|
|
socket.emit('api:category.loadMore', {
|
2013-08-20 12:11:17 -04:00
|
|
|
cid: cid,
|
|
|
|
|
after: $('#topics-container').children().length
|
2013-09-19 16:38:04 -04:00
|
|
|
}, function (data) {
|
2013-09-17 13:05:54 -04:00
|
|
|
if (data.topics.length) {
|
2013-10-03 15:04:25 -04:00
|
|
|
Category.onTopicsLoaded(data.topics);
|
2013-08-09 20:03:19 -04:00
|
|
|
}
|
2013-08-09 20:44:08 -04:00
|
|
|
loadingMoreTopics = false;
|
2013-08-09 20:03:19 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
return Category;
|
|
|
|
|
});
|