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

416 lines
12 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-06-25 16:17:10 -04:00
var dependencies = [
'forum/pagination',
'forum/infinitescroll',
'forum/topic/threadTools',
'forum/topic/postTools',
'forum/topic/events',
'forum/topic/browsing',
2014-06-25 16:17:10 -04:00
'navigator'
];
define('forum/topic', dependencies, function(pagination, infinitescroll, threadTools, postTools, events, browsing, navigator) {
var Topic = {},
currentUrl = '';
2013-08-23 13:45:57 -04:00
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');
events.removeListeners();
socket.removeListener('event:new_post', onNewPost);
socket.removeListener('event:new_notification', onNewNotification);
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')
},
postCount = ajaxify.variables.get('postcount');
$(window).trigger('action:topic.loading');
app.enterRoom('topic_' + tid);
processPage($('.topic'));
showBottomPostBar();
2014-01-31 15:13:52 -05:00
postTools.init(tid, thread_state);
threadTools.init(tid, thread_state);
events.init();
handleSorting();
hidePostToolsForDeletedPosts();
2014-05-17 17:55:51 -04:00
enableInfiniteLoadingOrPagination();
2014-06-28 17:04:09 -04:00
addBlockQuoteHandler();
2014-05-05 12:15:17 -04:00
addBlockquoteEllipses($('.topic .post-content > blockquote'));
2014-06-25 16:21:33 -04:00
handleBookmark(tid);
2014-06-27 15:35:53 -04:00
navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback, Topic.toTop, Topic.toBottom);
2014-06-25 16:21:33 -04:00
socket.on('event:new_post', onNewPost);
socket.on('event:new_notification', onNewNotification);
2014-06-25 16:21:33 -04:00
$(window).on('scroll', updateTopicTitle);
$(window).trigger('action:topic.loaded');
socket.emit('topics.markAsRead', tid);
socket.emit('topics.increaseViewCount', tid);
};
2014-06-27 15:35:53 -04:00
Topic.toTop = function() {
navigator.scrollTop(1);
};
Topic.toBottom = function() {
socket.emit('topics.lastPostIndex', ajaxify.variables.get('topic_id'), function(err, index) {
navigator.scrollBottom(index);
});
};
2014-06-25 16:21:33 -04:00
function handleBookmark(tid) {
var bookmark = localStorage.getItem('topic:' + tid + ':bookmark');
var postIndex = getPostIndex();
if (postIndex) {
navigator.scrollToPost(postIndex - 1, true);
2014-06-25 16:21:33 -04:00
} else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && ajaxify.variables.get('postcount') > 1) {
app.alert({
alert_id: 'bookmark',
message: '[[topic:bookmark_instructions]]',
timeout: 0,
type: 'info',
clickfn : function() {
navigator.scrollToPost(parseInt(bookmark, 10), true);
},
closefn : function() {
localStorage.removeItem('topic:' + tid + ':bookmark');
2014-04-20 15:07:53 -04:00
}
});
}
2014-06-25 16:21:33 -04:00
}
2014-04-20 15:07:53 -04:00
2014-06-25 16:21:33 -04:00
function getPostIndex() {
var parts = window.location.pathname.split('/');
return parts[4] ? parseInt(parts[4], 10) : 0;
}
2014-03-25 14:17:00 -04:00
function handleSorting() {
var threadSort = $('.thread-sort');
threadSort.find('i').removeClass('fa-check');
var currentSetting = threadSort.find('a[data-sort="' + config.topicPostSort + '"]');
currentSetting.find('i').addClass('fa-check');
$('.thread-sort').on('click', 'a', function() {
var newSetting = $(this).attr('data-sort');
socket.emit('user.setTopicSort', newSetting, function(err) {
config.topicPostSort = newSetting;
ajaxify.go('topic/' + ajaxify.variables.get('topic_slug'));
});
});
}
function showBottomPostBar() {
if($('#post-container .post-row').length > 1 || !$('#post-container li[data-index="0"]').length) {
$('.bottom-post-bar').removeClass('hide');
2014-03-25 14:17:00 -04:00
}
}
2014-03-25 14:17:00 -04:00
function onNewPost(data) {
var tid = ajaxify.variables.get('topic_id');
if(data && data.posts && data.posts.length && data.posts[0].tid !== tid) {
return;
2014-03-15 17:50:19 -04:00
}
if(config.usePagination) {
return onNewPostPagination(data);
2014-03-15 17:50:19 -04:00
}
2014-02-06 14:48:05 -05:00
for (var i=0; i<data.posts.length; ++i) {
var postcount = $('.user_postcount_' + data.posts[i].uid);
postcount.html(parseInt(postcount.html(), 10) + 1);
2014-03-15 17:50:19 -04:00
}
socket.emit('topics.markAsRead', tid);
createNewPosts(data);
}
function onNewNotification(data) {
var tid = ajaxify.variables.get('topic_id');
if (data && data.tid && parseInt(data.tid, 10) === tid) {
socket.emit('topics.markTopicNotificationsRead', tid);
}
}
2014-06-28 17:04:09 -04:00
function addBlockQuoteHandler() {
$('#post-container').on('click', 'blockquote .toggle', function() {
var blockQuote = $(this).parent('blockquote');
var toggle = $(this);
blockQuote.toggleClass('uncollapsed');
var collapsed = !blockQuote.hasClass('uncollapsed');
toggle.toggleClass('fa-angle-down', collapsed).toggleClass('fa-angle-up', !collapsed);
2014-06-28 17:04:09 -04:00
});
}
2014-06-28 17:04:09 -04:00
function addBlockquoteEllipses(blockquotes) {
blockquotes.each(function() {
var $this = $(this);
2014-07-16 19:51:24 -04:00
if ($this.find(':hidden:not(br)').length && !$this.find('.toggle').length) {
2014-06-28 17:04:09 -04:00
$this.append('<i class="fa fa-angle-down pointer toggle"></i>');
}
2014-05-05 12:15:17 -04:00
});
}
2014-05-17 17:55:51 -04:00
function enableInfiniteLoadingOrPagination() {
if(!config.usePagination) {
infinitescroll.init(loadMorePosts, $('#post-container .post-row[data-index="0"]').height());
} else {
navigator.hide();
2014-01-16 22:06:23 -05:00
pagination.init(parseInt(ajaxify.variables.get('currentPage'), 10), parseInt(ajaxify.variables.get('pageCount'), 10));
2014-03-15 17:50:19 -04:00
}
}
function hidePostToolsForDeletedPosts() {
$('#post-container li.deleted').each(function() {
postTools.toggle($(this).attr('data-pid'), true);
});
}
2014-04-04 12:42:41 -04:00
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 postIndex = parseInt(element.attr('data-index'), 10);
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');
if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) {
localStorage.setItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark', postIndex);
2014-04-04 12:42:41 -04:00
app.removeAlert('bookmark');
}
2014-02-25 15:10:50 -05:00
if (!navigator.scrollActive) {
var parts = ajaxify.removeRelativePath(window.location.pathname.slice(1)).split('/');
var topicId = parts[1],
slug = parts[2];
var newUrl = 'topic/' + topicId + '/' + (slug ? slug : '');
if (postIndex > 0) {
newUrl += '/' + (postIndex + 1);
}
2014-03-02 16:58:49 -05:00
2014-04-04 12:42:41 -04:00
if (newUrl !== currentUrl) {
if (history.replaceState) {
var search = (window.location.search ? window.location.search : '');
2014-04-04 12:42:41 -04:00
history.replaceState({
url: newUrl + search
}, null, window.location.protocol + '//' + window.location.host + RELATIVE_PATH + '/' + newUrl + search);
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
}
};
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() {
var firstPostTimestamp = parseInt(data.posts[0].timestamp, 10);
var firstPostVotes = parseInt(data.posts[0].votes, 10);
var firstPostPid = data.posts[0].pid;
var firstReply = $('#post-container li.post-row[data-index!="0"]').first();
var lastReply = $('#post-container li.post-row[data-index!="0"]').last();
if (config.topicPostSort === 'oldest_to_newest') {
if (firstPostTimestamp < parseInt(firstReply.attr('data-timestamp'), 10)) {
before = firstReply;
} else if(firstPostTimestamp >= parseInt(lastReply.attr('data-timestamp'), 10)) {
after = lastReply;
}
} else if(config.topicPostSort === 'newest_to_oldest') {
if (firstPostTimestamp > parseInt(firstReply.attr('data-timestamp'), 10)) {
before = firstReply;
} else if(firstPostTimestamp <= parseInt(lastReply.attr('data-timestamp'), 10)) {
after = lastReply;
}
} else if(config.topicPostSort === 'most_votes') {
if (firstPostVotes > parseInt(firstReply.attr('data-votes'), 10)) {
before = firstReply;
} else if(firstPostVotes < parseInt(firstReply.attr('data-votes'), 10)) {
after = lastReply;
} else {
if (firstPostPid > firstReply.attr('data-pid')) {
before = firstReply;
} else if(firstPostPid <= firstReply.attr('data-pid')) {
after = lastReply;
}
2014-02-17 20:57:12 -05:00
}
}
}
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 = $('<div></div>').text(ajaxify.variables.get('topic_name')).html();
data.viewcount = ajaxify.variables.get('viewcount');
2014-05-17 17:55:51 -04:00
infinitescroll.parseAndTranslate('topic', 'posts', data, function(html) {
2014-02-17 20:57:12 -05:00
if(after) {
2014-05-17 17:55:51 -04:00
html.insertAfter(after);
2014-02-17 20:57:12 -05:00
} else if(before) {
2014-05-17 17:55:51 -04:00
html.insertBefore(before);
2014-02-17 20:57:12 -05:00
} else {
2014-05-17 17:55:51 -04:00
$('#post-container').append(html);
2014-02-17 20:57:12 -05:00
}
2014-05-17 17:55:51 -04:00
html.hide().fadeIn('slow');
2014-02-17 20:57:12 -05:00
2014-05-17 17:55:51 -04:00
addBlockquoteEllipses(html.find('.post-content > blockquote'));
$(window).trigger('action:posts.loaded');
2014-05-17 17:55:51 -04:00
onNewPostsLoaded(html, data.posts);
if (typeof callback === 'function') {
2014-02-17 20:57:12 -05:00
callback();
}
2014-01-24 20:50:55 -05:00
});
}
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);
}
toggleModTools(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
processPage(html);
}
function processPage(element) {
browsing.populateOnlineUsers();
2014-01-24 20:50:55 -05:00
app.createUserTooltips();
app.replaceSelfLinks(element.find('a'));
utils.addCommasToNumbers(element.find('.formatted-number'));
utils.makeNumbersHumanReadable(element.find('.human-readable-number'));
element.find('span.timeago').timeago();
element.find('.post-content img:not(.emoji)').addClass('img-responsive').each(function() {
$(this).wrap('<a href="' + $(this).attr('src') + '" target="_blank">');
});
postTools.updatePostCount();
2014-01-24 20:50:55 -05:00
showBottomPostBar();
}
function toggleModTools(postHtml, privileges) {
2014-05-22 14:56:15 -04:00
postHtml.find('.edit, .delete').toggleClass('none', !privileges.editable);
postHtml.find('.move').toggleClass('none', !privileges.move);
2014-05-17 17:55:51 -04:00
postHtml.find('.reply, .quote').toggleClass('none', !$('.post_reply').length);
var isSelfPost = parseInt(postHtml.attr('data-uid'), 10) === parseInt(app.uid, 10);
postHtml.find('.chat, .flag').toggleClass('none', isSelfPost);
2014-01-12 15:37:33 -05:00
}
2014-05-17 17:55:51 -04:00
function loadMorePosts(direction) {
if (!$('#post-container').length || navigator.scrollActive) {
return;
}
infinitescroll.calculateAfter(direction, '#post-container .post-row[data-index!="0"]', config.postsPerPage, function(after, offset, el) {
2014-05-17 17:55:51 -04:00
loadPostsAfter(after, function() {
if (direction < 0 && el) {
navigator.scrollToPost(el.attr('data-index'), false, 0, offset);
2014-05-17 17:55:51 -04:00
}
});
});
}
function loadPostsAfter(after, callback) {
var tid = ajaxify.variables.get('topic_id');
if (!utils.isNumber(tid) || !utils.isNumber(after) || (after === 0 && $('#post-container li.post-row[data-index="1"]').length)) {
2014-02-17 20:57:12 -05:00
return;
}
2014-05-17 17:55:51 -04:00
var indicatorEl = $('.loading-indicator');
if (!indicatorEl.is(':animated')) {
indicatorEl.fadeIn();
}
2014-02-17 20:57:12 -05:00
2014-05-17 17:55:51 -04:00
infinitescroll.loadMore('topics.loadMore', {
tid: tid,
2014-02-17 20:57:12 -05:00
after: after
2014-06-02 20:41:03 -04:00
}, function (data, done) {
2014-05-17 17:55:51 -04:00
indicatorEl.fadeOut();
2014-02-17 20:57:12 -05:00
if (data && data.posts && data.posts.length) {
2014-06-02 20:41:03 -04:00
createNewPosts(data, function() {
done();
callback();
});
2014-05-17 17:55:51 -04:00
hidePostToolsForDeletedPosts();
2014-02-17 20:57:12 -05:00
} else {
2014-04-04 12:42:41 -04:00
navigator.update();
2014-06-02 20:41:03 -04:00
done();
2014-02-17 20:57:12 -05:00
}
});
}
return Topic;
2014-04-10 20:31:57 +01:00
});