Files
NodeBB/public/src/forum/topic.js

774 lines
21 KiB
JavaScript
Raw Normal View History

2014-03-15 17:50:19 -04:00
'use strict';
2014-04-04 12:42:41 -04:00
/* globals define, app, templates, translator, socket, bootbox, config, ajaxify, RELATIVE_PATH, utils */
2014-03-15 17:50:19 -04:00
2014-04-04 12:42:41 -04:00
define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', 'navigator'], function(pagination, threadTools, postTools, navigator) {
var Topic = {},
2014-02-25 16:33:22 -05:00
infiniteLoaderActive = false,
scrollingToPost = false,
currentUrl = '';
2013-08-23 13:45:57 -04:00
function showBottomPostBar() {
if($('#post-container .post-row').length > 1 || !$('#post-container li[data-index="0"]').length) {
$('.bottom-post-bar').removeClass('hide');
}
}
2014-02-12 15:44:48 -05:00
$(window).on('action:ajaxify.start', function(ev, data) {
if(data.url.indexOf('topic') !== 0) {
2014-04-04 12:42:41 -04:00
navigator.hide();
2014-03-04 16:48:07 -05:00
$('.header-topic-title').find('span').text('').hide();
2014-02-26 21:58:04 -05:00
app.removeAlert('bookmark');
2014-01-27 19:58:03 -05:00
}
});
Topic.init = function() {
var tid = ajaxify.variables.get('topic_id'),
thread_state = {
locked: ajaxify.variables.get('locked'),
deleted: ajaxify.variables.get('deleted'),
pinned: ajaxify.variables.get('pinned')
},
currentPage = parseInt(ajaxify.variables.get('currentPage'), 10),
pageCount = parseInt(ajaxify.variables.get('pageCount'), 10);
Topic.postCount = ajaxify.variables.get('postcount');
$(window).trigger('action:topic.loading');
2014-03-25 13:24:51 -04:00
function hidePostToolsForDeletedPosts() {
$('#post-container li.deleted').each(function() {
toggle_post_tools($(this).attr('data-pid'), true);
});
}
$(function() {
2014-03-31 14:43:44 -04:00
utils.addCommasToNumbers($('.topic .formatted-number'));
app.enterRoom('topic_' + tid);
2013-10-25 18:00:08 -04:00
showBottomPostBar();
2013-10-25 18:00:08 -04:00
if (thread_state.locked === '1') {
set_locked_state(true);
}
if (thread_state.deleted === '1') {
set_delete_state(true);
}
if (thread_state.pinned === '1') {
set_pinned_state(true);
}
2014-03-19 14:53:31 -04:00
postTools.init(tid, thread_state);
2014-03-18 16:46:07 -04:00
threadTools.init(tid, thread_state);
2014-03-25 13:24:51 -04:00
hidePostToolsForDeletedPosts();
enableInfiniteLoading();
var bookmark = localStorage.getItem('topic:' + tid + ':bookmark');
if (window.location.hash) {
Topic.scrollToPost(window.location.hash.substr(1), true);
2014-03-09 16:39:31 -04:00
} else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && Topic.postCount > 1) {
2014-02-26 21:58:04 -05:00
app.alert({
alert_id: 'bookmark',
message: '[[topic:bookmark_instructions]]',
timeout: 0,
type: 'info',
clickfn : function() {
Topic.scrollToPost(parseInt(bookmark, 10), true);
},
closefn : function() {
localStorage.removeItem('topic:' + tid + ':bookmark');
}
});
}
2014-04-04 12:42:41 -04:00
if (!config.usePagination) {
navigator.init('.posts > .post-row', Topic.postCount, Topic.navigatorCallback);
2014-03-02 16:58:49 -05:00
}
2014-02-28 22:08:33 -05:00
2014-01-22 21:08:43 +01:00
$('#post-container').on('mouseenter', '.favourite-tooltip', function(e) {
if (!$(this).data('users-loaded')) {
$(this).data('users-loaded', "true");
var pid = $(this).parents('.post-row').attr('data-pid');
var el = $(this).attr('title', "Loading...");
socket.emit('posts.getFavouritedUsers', pid, function(err, usernames) {
el.attr('title', usernames).tooltip('show');
});
}
});
});
2013-08-23 13:45:57 -04:00
function enableInfiniteLoading() {
2014-01-24 13:27:36 -05:00
if(!config.usePagination) {
2014-02-17 20:57:12 -05:00
app.enableInfiniteLoading(function(direction) {
2014-01-26 14:56:16 -05:00
if (!infiniteLoaderActive && $('#post-container').children().length) {
2014-02-17 20:57:12 -05:00
var after = 0;
var el = null;
if(direction > 0) {
2014-03-25 13:24:51 -04:00
el = $('#post-container .post-row').last();
2014-02-17 20:57:12 -05:00
after = parseInt(el.attr('data-index'), 10) + 1;
} else {
2014-03-25 13:24:51 -04:00
el = $('#post-container .post-row').first();
2014-02-17 20:57:12 -05:00
after = parseInt(el.attr('data-index'), 10);
after -= config.postsPerPage;
if(after < 0) {
after = 0;
}
}
var offset = el.offset().top - $('#header-menu').offset().top + $('#header-menu').height();
loadMorePosts(tid, after, function() {
2014-03-25 13:24:51 -04:00
hidePostToolsForDeletedPosts();
2014-02-17 20:57:12 -05:00
if(direction < 0 && el) {
Topic.scrollToPost(el.attr('data-pid'), false, 0, offset);
2014-02-17 20:57:12 -05:00
}
2014-01-24 13:27:36 -05:00
});
}
});
} else {
2014-04-04 12:42:41 -04:00
navigator.hide();
2014-01-24 13:27:36 -05:00
pagination.init(currentPage, pageCount);
2014-01-24 20:00:56 -05:00
}
}
ajaxify.register_events([
'event:voted', 'event:favourited', 'event:new_post', 'get_users_in_room',
'event:topic_deleted', 'event:topic_restored', 'event:topic:locked',
'event:topic_unlocked', 'event:topic_pinned', 'event:topic_unpinned',
'event:topic_moved', 'event:post_edited', 'event:post_deleted', 'event:post_restored',
'posts.favourite', 'user.isOnline', 'posts.upvote', 'posts.downvote',
'event:topic.replyStart', 'event:topic.replyStop'
]);
2014-03-12 18:30:06 -04:00
function createUserIcon(uid, picture, userslug, username) {
if(!$('.thread_active_users').find('[data-uid="' + uid + '"]').length) {
var div = $('<div class="inline-block"><a data-uid="' + uid + '" href="' + RELATIVE_PATH + '/user/' + userslug + '"><img src="'+ picture +'"/></a></div>');
div.find('a').tooltip({
placement: 'top',
title: username
});
2014-03-01 22:51:39 -05:00
2014-03-12 18:30:06 -04:00
return div;
}
}
2014-03-12 18:30:06 -04:00
socket.on('get_users_in_room', function(data) {
2014-01-19 14:27:07 -05:00
if(data && data.room.indexOf('topic') !== -1) {
var activeEl = $('.thread_active_users');
2013-10-23 14:39:46 -04:00
// remove users that are no longer here
activeEl.find('a').each(function(index, element) {
2013-10-23 14:39:46 -04:00
if(element) {
2014-03-01 22:51:39 -05:00
var uid = $(element).attr('data-uid');
2014-03-15 17:50:19 -04:00
var absent = data.users.every(function(user) {
return parseInt(user.uid, 10) !== parseInt(uid, 10);
});
if (absent) {
$(element).remove();
2013-10-23 14:39:46 -04:00
}
}
});
2014-03-15 17:50:19 -04:00
var i=0, icon;
2013-10-23 14:39:46 -04:00
// add self
for(i = 0; i<data.users.length; ++i) {
2014-02-25 16:55:05 -05:00
if(parseInt(data.users[i].uid, 10) === parseInt(app.uid, 10)) {
2014-03-15 17:50:19 -04:00
icon = createUserIcon(data.users[i].uid, data.users[i].picture, data.users[i].userslug, data.users[i].username);
2013-10-23 14:39:46 -04:00
activeEl.prepend(icon);
data.users.splice(i, 1);
break;
}
}
// add other users
for(i=0; i<data.users.length; ++i) {
2014-03-15 17:50:19 -04:00
icon = createUserIcon(data.users[i].uid, data.users[i].picture, data.users[i].userslug, data.users[i].username);
2013-10-23 14:39:46 -04:00
activeEl.append(icon);
if(activeEl.children().length > 8) {
break;
}
}
var remainingUsers = data.users.length - 9;
remainingUsers = remainingUsers < 0 ? 0 : remainingUsers;
var anonymousCount = parseInt(data.anonymousCount, 10);
2013-10-23 15:00:06 -04:00
activeEl.find('.anonymous-box').remove();
2013-10-23 14:39:46 -04:00
if(anonymousCount || remainingUsers) {
2013-11-26 14:25:46 -05:00
var anonLink = $('<div class="anonymous-box inline-block"><i class="fa fa-user"></i></div>');
2013-10-23 14:39:46 -04:00
activeEl.append(anonLink);
var title = '';
2014-03-15 17:50:19 -04:00
if(remainingUsers && anonymousCount) {
2013-10-23 14:39:46 -04:00
title = remainingUsers + ' more user(s) and ' + anonymousCount + ' guest(s)';
2014-03-15 17:50:19 -04:00
} else if(remainingUsers) {
2013-10-23 14:39:46 -04:00
title = remainingUsers + ' more user(s)';
2014-03-15 17:50:19 -04:00
} else {
2013-10-23 14:39:46 -04:00
title = anonymousCount + ' guest(s)';
2014-03-15 17:50:19 -04:00
}
2013-10-23 14:39:46 -04:00
anonLink.tooltip({
placement: 'top',
2013-10-23 14:39:46 -04:00
title: title
});
}
// Get users who are currently replying to the topic entered
socket.emit('modules.composer.getUsersByTid', ajaxify.variables.get('topic_id'), function(err, uids) {
if (uids && uids.length) {
for(var x=0;x<uids.length;x++) {
activeEl.find('[data-uid="' + uids[x] + '"]').addClass('replying');
}
}
});
2013-10-23 14:39:46 -04:00
}
2014-01-31 15:13:52 -05:00
app.populateOnlineUsers();
});
socket.on('user.isOnline', function(err, data) {
app.populateOnlineUsers();
2013-09-19 16:40:02 -04:00
});
2013-09-19 16:24:30 -04:00
socket.on('event:voted', function(data) {
updatePostVotesAndUserReputation(data);
2013-06-04 16:47:07 -04:00
});
2014-02-06 14:48:05 -05:00
socket.on('event:favourited', function(data) {
updateFavouriteCount(data.post.pid, data.post.reputation);
2014-02-06 14:48:05 -05:00
});
socket.on('event:new_post', function(data) {
if(data && data.posts && data.posts.length && data.posts[0].tid !== ajaxify.variables.get('topic_id')) {
2014-03-13 15:13:47 -04:00
return;
}
2014-01-24 20:00:56 -05:00
if(config.usePagination) {
onNewPostPagination(data);
return;
}
for (var i=0; i<data.posts.length; ++i) {
var postcount = $('.user_postcount_' + data.posts[i].uid);
postcount.html(parseInt(postcount.html(), 10) + 1);
}
socket.emit('topics.markAsRead', {tid: tid, uid: app.uid});
createNewPosts(data);
});
socket.on('event:topic_deleted', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid === tid) {
set_locked_state(true);
set_delete_state(true);
}
});
socket.on('event:topic_restored', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid === tid) {
set_locked_state(false);
set_delete_state(false);
}
});
socket.on('event:topic_locked', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid === tid) {
set_locked_state(true, 1);
}
});
2013-08-26 13:18:20 -04:00
socket.on('event:topic_unlocked', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid === tid) {
set_locked_state(false, 1);
}
});
socket.on('event:topic_pinned', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid === tid) {
set_pinned_state(true, 1);
}
});
socket.on('event:topic_unpinned', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid === tid) {
set_pinned_state(false, 1);
}
});
socket.on('event:topic_moved', function(data) {
2014-01-16 19:58:57 -05:00
if (data && data.tid > 0) {
ajaxify.go('topic/' + data.tid);
}
});
2013-07-17 12:57:57 -04:00
socket.on('event:post_edited', function(data) {
2014-01-18 21:57:06 -05:00
var editedPostEl = $('#content_' + data.pid),
editedPostTitle = $('#topic_title_' + data.pid);
2014-01-18 21:57:06 -05:00
if (editedPostTitle.length) {
editedPostTitle.fadeOut(250, function() {
editedPostTitle.html(data.title);
editedPostTitle.fadeIn(250);
});
}
2014-01-18 21:57:06 -05:00
editedPostEl.fadeOut(250, function() {
editedPostEl.html(data.content);
editedPostEl.find('img').addClass('img-responsive');
editedPostEl.fadeIn(250);
});
});
socket.on('posts.upvote', function(pid) {
toggleUpvoteDownvote(pid, true, false);
2014-02-06 12:45:06 -05:00
});
socket.on('posts.downvote', function(pid) {
toggleUpvoteDownvote(pid, false, true);
2014-02-06 12:45:06 -05:00
});
socket.on('posts.unvote', function(pid) {
toggleUpvoteDownvote(pid, false, false);
2014-02-06 12:45:06 -05:00
});
function toggleUpvoteDownvote(pid, upvote, downvote) {
var post = $('li[data-pid="' + pid + '"]');
post.find('.upvote').toggleClass('btn-primary upvoted', upvote);
post.find('.downvote').toggleClass('btn-primary downvoted', downvote);
}
socket.on('posts.favourite', function(pid) {
toggleFavourite(pid, true);
});
socket.on('posts.unfavourite', function(pid) {
toggleFavourite(pid, false);
});
2014-03-25 14:17:00 -04:00
function toggleFavourite(pid, isFavourited) {
var favBtn = $('li[data-pid="' + pid + '"] .favourite');
if(favBtn.length) {
favBtn.addClass('btn-warning')
.attr('data-favourited', isFavourited);
var icon = favBtn.find('i');
var className = icon.attr('class');
if (isFavourited ? className.indexOf('-o') !== -1 : className.indexOf('-o') === -1) {
icon.attr('class', isFavourited ? className.replace('-o', '') : className + '-o');
}
}
}
socket.on('event:post_deleted', function(data) {
if (data.pid) {
2014-03-15 17:50:19 -04:00
toggle_post_delete_state(data.pid);
}
});
socket.on('event:post_restored', function(data) {
if (data.pid) {
toggle_post_delete_state(data.pid);
}
});
socket.on('event:topic.replyStart', function(uid) {
$('.thread_active_users [data-uid="' + uid + '"]').addClass('replying');
});
socket.on('event:topic.replyStop', function(uid) {
$('.thread_active_users [data-uid="' + uid + '"]').removeClass('replying');
});
function updatePostVotesAndUserReputation(data) {
var votes = $('li[data-pid="' + data.post.pid + '"] .votes'),
reputationElements = $('.reputation[data-uid="' + data.post.uid + '"]');
2013-08-23 13:45:57 -04:00
votes.html(data.post.votes).attr('data-votes', data.post.votes);
reputationElements.html(data.user.reputation).attr('data-reputation', data.user.reputation);
2014-03-15 17:50:19 -04:00
}
function updateFavouriteCount(pid, value) {
$('li[data-pid="' + pid + '"] .favouriteCount').html(value).attr('data-favourites', value);
2014-03-15 17:50:19 -04:00
}
2014-02-06 14:48:05 -05:00
function set_locked_state(locked, alert) {
2014-02-23 18:05:50 -05:00
translator.translate('<i class="fa fa-fw fa-' + (locked ? 'un': '') + 'lock"></i> [[topic:thread_tools.' + (locked ? 'un': '') + 'lock]]', function(translated) {
$('.lock_thread').html(translated);
});
2013-08-30 12:36:06 -04:00
2014-02-23 18:05:50 -05:00
$('#post-container .post_reply').html(locked ? 'Locked <i class="fa fa-lock"></i>' : 'Reply <i class="fa fa-reply"></i>');
$('#post-container').find('.quote, .edit, .delete').toggleClass('none', locked);
$('.topic-main-buttons .post_reply').attr('disabled', locked).html(locked ? 'Locked <i class="fa fa-lock"></i>' : 'Reply');
2014-02-23 18:05:50 -05:00
if (alert) {
app.alert({
'alert_id': 'thread_lock',
type: 'success',
title: 'Thread ' + (locked ? 'Locked' : 'Unlocked'),
message: 'Thread has been successfully ' + (locked ? 'locked' : 'unlocked'),
timeout: 5000
});
2013-08-19 14:28:51 -04:00
}
2014-02-23 18:05:50 -05:00
thread_state.locked = locked ? '1' : '0';
2014-03-15 17:50:19 -04:00
}
function set_delete_state(deleted) {
2014-02-23 18:19:10 -05:00
var threadEl = $('#post-container');
2014-02-23 18:19:10 -05:00
translator.translate('<i class="fa fa-fw ' + (deleted ? 'fa-comment' : 'fa-trash-o') + '"></i> [[topic:thread_tools.' + (deleted ? 'restore' : 'delete') + ']]', function(translated) {
$('.delete_thread span').html(translated);
});
2014-02-23 18:19:10 -05:00
threadEl.toggleClass('deleted', deleted);
thread_state.deleted = deleted ? '1' : '0';
2014-02-23 18:19:10 -05:00
if(deleted) {
2014-04-02 08:18:28 -04:00
translator.translate('[[topic:deleted_message]]', function(translated) {
$('<div id="thread-deleted" class="alert alert-warning">' + translated + '</div>').insertBefore(threadEl);
});
2014-02-23 18:19:10 -05:00
} else {
$('#thread-deleted').remove();
2013-08-23 13:45:57 -04:00
}
2014-03-15 17:50:19 -04:00
}
function set_pinned_state(pinned, alert) {
2014-02-12 14:03:02 -05:00
translator.translate('<i class="fa fa-fw fa-thumb-tack"></i> [[topic:thread_tools.' + (pinned ? 'unpin' : 'pin') + ']]', function(translated) {
2014-02-23 17:42:31 -05:00
$('.pin_thread').html(translated);
2014-02-23 17:42:31 -05:00
if (alert) {
app.alert({
'alert_id': 'thread_pin',
type: 'success',
title: 'Thread ' + (pinned ? 'Pinned' : 'Unpinned'),
message: 'Thread has been successfully ' + (pinned ? 'pinned' : 'unpinned'),
timeout: 5000
});
2014-02-10 12:41:29 -05:00
}
2014-02-23 17:42:31 -05:00
thread_state.pinned = pinned ? '1' : '0';
2014-02-10 12:41:29 -05:00
});
2014-03-15 17:50:19 -04:00
}
function toggle_post_delete_state(pid) {
var postEl = $('#post-container li[data-pid="' + pid + '"]');
if (postEl.length) {
postEl.toggleClass('deleted');
toggle_post_tools(pid, postEl.hasClass('deleted'));
2014-01-16 22:06:23 -05:00
updatePostCount();
}
2014-03-15 17:50:19 -04:00
}
function toggle_post_tools(pid, isDeleted) {
2014-02-23 15:07:47 -05:00
var postEl = $('#post-container li[data-pid="' + pid + '"]');
2014-02-23 18:19:10 -05:00
postEl.find('.quote, .favourite, .post_reply, .chat').toggleClass('none', isDeleted);
2014-02-23 15:07:47 -05:00
translator.translate(isDeleted ? ' [[topic:restore]]' : ' [[topic:delete]]', function(translated) {
postEl.find('.delete').find('span').html(translated);
});
2014-03-15 17:50:19 -04:00
}
2014-04-04 12:42:41 -04:00
$(window).on('scroll', updateTopicTitle);
$(window).trigger('action:topic.loaded');
};
2014-04-04 12:42:41 -04:00
function updateTopicTitle() {
2014-02-25 14:29:19 -05:00
if($(window).scrollTop() > 50) {
$('.header-topic-title').find('span').text(ajaxify.variables.get('topic_name')).show();
2014-02-12 16:02:07 -05:00
} else {
2014-03-04 16:48:07 -05:00
$('.header-topic-title').find('span').text('').hide();
2014-02-12 16:02:07 -05:00
}
2014-04-04 12:42:41 -04:00
}
2014-02-12 16:02:07 -05:00
2014-04-04 12:42:41 -04:00
Topic.navigatorCallback = function(element) {
var pid = element.attr('data-pid');
2014-04-04 16:20:13 -04:00
2014-04-04 12:42:41 -04:00
var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark');
2014-04-04 12:42:41 -04:00
if (!currentBookmark || parseInt(pid, 10) >= parseInt(currentBookmark, 10)) {
localStorage.setItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark', pid);
app.removeAlert('bookmark');
}
2014-02-25 15:10:50 -05:00
2014-04-04 12:42:41 -04:00
if (!scrollingToPost) {
2014-03-02 16:58:49 -05:00
2014-04-04 12:42:41 -04:00
var newUrl = window.location.href.replace(window.location.hash, '') + '#' + pid;
2014-03-02 16:58:49 -05:00
2014-04-04 12:42:41 -04:00
if (newUrl !== currentUrl) {
if (history.replaceState) {
history.replaceState({
url: window.location.pathname.slice(1) + (window.location.search ? window.location.search : '' ) + '#' + pid
}, null, newUrl);
2013-09-23 17:41:54 -04:00
}
2014-04-04 12:42:41 -04:00
currentUrl = newUrl;
}
2014-04-04 12:42:41 -04:00
}
};
Topic.scrollToPost = function(pid, highlight, duration, offset) {
if (!pid) {
return;
}
2014-02-18 01:18:37 -05:00
2014-02-17 20:57:12 -05:00
if(!offset) {
offset = 0;
}
2014-04-04 12:42:41 -04:00
scrollingToPost = true;
2014-02-17 20:57:12 -05:00
if($('#post_anchor_' + pid).length) {
return scrollToPid(pid);
}
if(config.usePagination) {
socket.emit('posts.getPidPage', pid, function(err, page) {
2014-01-25 19:50:50 -05:00
if(err) {
return;
}
if(parseInt(page, 10) !== pagination.currentPage) {
2014-03-02 16:58:49 -05:00
pagination.loadPage(page, function() {
scrollToPid(pid);
});
} else {
scrollToPid(pid);
}
});
} else {
2014-02-17 20:57:12 -05:00
socket.emit('posts.getPidIndex', pid, function(err, index) {
if(err) {
return;
}
var tid = $('#post-container').attr('data-tid');
$('#post-container').empty();
var after = index - config.postsPerPage + 1;
if(after < 0) {
after = 0;
}
2014-03-02 16:58:49 -05:00
2014-02-17 20:57:12 -05:00
loadMorePosts(tid, after, function() {
scrollToPid(pid);
});
});
}
function scrollToPid(pid) {
2014-02-17 20:57:12 -05:00
var scrollTo = $('#post_anchor_' + pid),
tid = $('#post-container').attr('data-tid');
function animateScroll() {
2014-02-24 15:23:26 -05:00
$("html, body").animate({
scrollTop: (scrollTo.offset().top - $('#header-menu').height() - offset) + "px"
2014-02-17 20:57:12 -05:00
}, duration !== undefined ? duration : 400, function() {
2014-02-25 16:33:22 -05:00
scrollingToPost = false;
2014-04-04 12:42:41 -04:00
navigator.update();
2014-03-24 14:30:11 -04:00
highlightPost();
2014-02-17 20:57:12 -05:00
});
}
2014-03-24 14:30:11 -04:00
function highlightPost() {
if (highlight) {
scrollTo.parent().find('.topic-item').addClass('highlight');
setTimeout(function() {
scrollTo.parent().find('.topic-item').removeClass('highlight');
}, 5000);
}
}
2014-02-18 12:40:13 -05:00
if (tid && scrollTo.length) {
if($('#post-container li.post-row[data-pid="' + pid + '"]').attr('data-index') !== '0') {
animateScroll();
} else {
2014-04-04 12:42:41 -04:00
navigator.update();
2014-03-24 14:30:11 -04:00
highlightPost();
2014-02-18 12:40:13 -05:00
}
}
}
};
function onNewPostPagination(data) {
var posts = data.posts;
socket.emit('topics.getPageCount', ajaxify.variables.get('topic_id'), function(err, newPageCount) {
2014-01-25 21:35:09 -05:00
pagination.recreatePaginationLinks(newPageCount);
if(pagination.currentPage === pagination.pageCount) {
createNewPosts(data);
} else if(data.posts && data.posts.length && parseInt(data.posts[0].uid, 10) === parseInt(app.uid, 10)) {
pagination.loadPage(pagination.pageCount);
}
});
}
2014-03-25 13:24:51 -04:00
function createNewPosts(data, callback) {
2013-11-29 13:09:26 -05:00
if(!data || (data.posts && !data.posts.length)) {
return;
2013-11-29 13:09:26 -05:00
}
function removeAlreadyAddedPosts() {
data.posts = data.posts.filter(function(post) {
return $('#post-container li[data-pid="' + post.pid +'"]').length === 0;
});
}
2014-02-17 20:57:12 -05:00
var after = null,
before = null;
function findInsertionPoint() {
2014-02-17 20:57:12 -05:00
var firstPid = parseInt(data.posts[0].pid, 10);
2014-01-24 20:50:55 -05:00
$('#post-container li[data-pid]').each(function() {
2014-02-17 20:57:12 -05:00
if(firstPid > parseInt($(this).attr('data-pid'), 10)) {
after = $(this);
if(after.next().length && after.next().hasClass('post-bar')) {
after = after.next();
}
} else {
return false;
}
});
2014-02-17 20:57:12 -05:00
if(!after) {
var firstPost = $('#post-container .post-row').first();
if(firstPid < parseInt(firstPost.attr('data-pid'), 10)) {
before = firstPost;
}
}
}
removeAlreadyAddedPosts();
if(!data.posts.length) {
return;
}
2013-11-26 14:25:46 -05:00
2014-02-17 20:57:12 -05:00
findInsertionPoint();
data.title = ajaxify.variables.get('topic_name');
data.viewcount = ajaxify.variables.get('viewcount');
2014-01-26 16:22:50 -05:00
parseAndTranslatePosts(data, function(translatedHTML) {
var translated = $(translatedHTML);
2013-11-28 18:45:36 -05:00
2014-02-17 20:57:12 -05:00
if(after) {
2014-03-15 17:50:19 -04:00
translated.insertAfter(after);
2014-02-17 20:57:12 -05:00
} else if(before) {
translated.insertBefore(before);
} else {
$('#post-container').append(translated);
}
2014-02-17 20:57:12 -05:00
translated.hide().fadeIn('slow');
onNewPostsLoaded(translated, data.posts);
2014-02-17 20:57:12 -05:00
if(typeof callback === 'function') {
callback();
}
2014-01-24 20:50:55 -05:00
});
}
2014-01-26 16:22:50 -05:00
function parseAndTranslatePosts(data, callback) {
ajaxify.loadTemplate('topic', function(topicTemplate) {
var html = templates.parse(templates.getBlock(topicTemplate, 'posts'), data);
translator.translate(html, callback);
});
}
2014-02-17 20:57:12 -05:00
function onNewPostsLoaded(html, posts) {
function getPostPrivileges(pid) {
2014-04-04 12:42:41 -04:00
socket.emit('posts.getPrivileges', pid, function(err, privileges) {
if(err) {
return app.alertError(err.message);
}
toggle_mod_tools(html, privileges);
});
2014-04-04 12:42:41 -04:00
}
2014-04-04 12:42:41 -04:00
for (var x = 0, numPosts = posts.length; x < numPosts; x++) {
getPostPrivileges(posts[x].pid);
2014-04-04 12:42:41 -04:00
}
2014-03-15 17:50:19 -04:00
2014-01-24 20:50:55 -05:00
infiniteLoaderActive = false;
app.populateOnlineUsers();
app.createUserTooltips();
2014-03-31 14:43:44 -04:00
utils.addCommasToNumbers(html.find('.formatted-number'));
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
2014-02-17 20:57:12 -05:00
html.find('span.timeago').timeago();
html.find('.post-content img').addClass('img-responsive');
2014-01-24 20:50:55 -05:00
updatePostCount();
showBottomPostBar();
}
function toggle_mod_tools(postHtml, privileges) {
postHtml.find('.edit, .delete').toggleClass('none', !privileges.editable);
postHtml.find('.move').toggleClass('none', !privileges.move);
2014-01-12 15:37:33 -05:00
}
function updatePostCount() {
socket.emit('topics.postcount', ajaxify.variables.get('topic_id'), function(err, postcount) {
if(!err) {
Topic.postCount = postcount;
$('#topic-post-count').html(Topic.postCount);
2014-04-04 12:42:41 -04:00
navigator.setCount(Topic.postCount);
}
2014-01-23 21:35:25 -05:00
});
}
2014-02-17 20:57:12 -05:00
function loadMorePosts(tid, after, callback) {
2013-11-23 18:05:19 -05:00
var indicatorEl = $('.loading-indicator');
2014-02-17 20:57:12 -05:00
if (infiniteLoaderActive || !$('#post-container').length) {
return;
}
2014-02-17 20:57:12 -05:00
if(after === 0 && $('#post-container li.post-row[data-index="0"]').length) {
return;
}
2014-02-17 20:57:12 -05:00
infiniteLoaderActive = true;
indicatorEl.fadeIn();
socket.emit('topics.loadMore', {
tid: tid,
after: after
}, function (err, data) {
indicatorEl.fadeOut(function() {
infiniteLoaderActive = false;
});
2014-02-17 20:57:12 -05:00
if(err) {
return app.alertError(err.message);
}
2014-02-17 20:57:12 -05:00
if (data && data.posts && data.posts.length) {
2014-03-25 13:24:51 -04:00
createNewPosts(data, callback);
2014-02-17 20:57:12 -05:00
} else {
2014-04-04 12:42:41 -04:00
navigator.update();
2014-02-17 20:57:12 -05:00
if (typeof callback === 'function') {
callback(data.posts);
}
2014-02-17 20:57:12 -05:00
}
});
}
return Topic;
});