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

412 lines
11 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
define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/threadTools', 'forum/topic/postTools', 'forum/topic/events', 'navigator'], function(pagination, infinitescroll, threadTools, postTools, events, navigator) {
var Topic = {},
scrollingToPost = false,
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);
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);
showBottomPostBar();
2014-01-31 15:13:52 -05:00
postTools.init(tid, thread_state);
threadTools.init(tid, thread_state);
events.init();
hidePostToolsForDeletedPosts();
2014-05-17 17:55:51 -04:00
enableInfiniteLoadingOrPagination();
2014-05-05 12:15:17 -04:00
addBlockquoteEllipses($('.topic .post-content > blockquote'));
var bookmark = localStorage.getItem('topic:' + tid + ':bookmark');
var postIndex = getPostIndex();
if (postIndex) {
Topic.scrollToPost(postIndex, true);
} else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && postCount > 1) {
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-20 15:07:53 -04:00
}
});
}
navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback);
socket.on('event:new_post', onNewPost);
$(window).on('scroll', updateTopicTitle);
2014-03-25 14:17:00 -04:00
$(window).trigger('action:topic.loaded');
2014-05-28 18:20:02 -04:00
socket.emit('topics.markAsRead', tid);
2014-05-28 18:20:02 -04:00
socket.emit('topics.increaseViewCount', tid);
};
2014-03-25 14:17:00 -04:00
function getPostIndex() {
var parts = window.location.pathname.split('/');
return parts[4] ? (parseInt(parts[4], 10) - 1) : '';
}
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);
}
2014-05-05 12:15:17 -04:00
function addBlockquoteEllipses(blockquotes) {
blockquotes.each(function() {
var $this = $(this);
2014-05-06 15:04:02 -04:00
if ($this.find(':hidden').length && !$this.find('.toggle').length) {
2014-05-05 12:15:17 -04:00
$this.append('<i class="fa fa-ellipsis-h pointer toggle"></i>');
}
});
2014-05-08 19:17:31 -04:00
2014-05-05 12:15:17 -04:00
$('blockquote .toggle').on('click', function() {
$(this).parent('blockquote').toggleClass('uncollapsed');
});
}
2014-05-17 17:55:51 -04:00
function enableInfiniteLoadingOrPagination() {
if(!config.usePagination) {
2014-05-17 17:55:51 -04:00
infinitescroll.init(loadMorePosts);
} 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
2014-04-04 12:42:41 -04:00
if (!scrollingToPost) {
var parts = window.location.pathname.split('/');
var topicId = parts[2],
slug = parts[3];
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 + '/' + 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
}
};
Topic.scrollToPost = function(postIndex, highlight, duration, offset) {
if (!postIndex) {
return;
}
2014-02-18 01:18:37 -05:00
if (!offset) {
2014-02-17 20:57:12 -05:00
offset = 0;
}
2014-04-04 12:42:41 -04:00
scrollingToPost = true;
if($('#post_anchor_' + postIndex).length) {
return scrollToPid(postIndex);
2014-02-17 20:57:12 -05:00
}
if(config.usePagination) {
if (window.location.search.indexOf('page') !== -1) {
navigator.update();
scrollingToPost = false;
return;
}
2014-05-17 17:55:51 -04:00
var page = Math.ceil((postIndex + 1) / config.postsPerPage)
2014-03-02 16:58:49 -05:00
if(parseInt(page, 10) !== pagination.currentPage) {
pagination.loadPage(page, function() {
scrollToPid(postIndex);
2014-02-17 20:57:12 -05:00
});
} else {
scrollToPid(postIndex);
}
} else {
$('#post-container').empty();
var after = postIndex - config.postsPerPage + 1;
if(after < 0) {
after = 0;
}
loadPostsAfter(after, function() {
scrollToPid(postIndex);
2014-02-17 20:57:12 -05:00
});
}
function scrollToPid(postIndex) {
var scrollTo = $('#post_anchor_' + postIndex),
2014-02-17 20:57:12 -05:00
tid = $('#post-container').attr('data-tid');
function animateScroll() {
2014-05-17 17:55:51 -04: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-index="' + postIndex + '"]').attr('data-index') !== '0') {
2014-02-18 12:40:13 -05:00
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-05-05 12:15:17 -04:00
var $this = $(this);
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
2014-05-17 17:55:51 -04:00
if (!after) {
2014-02-17 20:57:12 -05:00
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-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'));
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
2014-01-24 20:50:55 -05:00
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');
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 || scrollingToPost) {
return;
}
2014-05-17 17:55:51 -04:00
infinitescroll.calculateAfter(direction, '#post-container .post-row', config.postsPerPage, function(after, offset, el) {
loadPostsAfter(after, function() {
if (direction < 0 && el) {
Topic.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="0"]').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-05-17 17:55:51 -04:00
}, function (data) {
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-03-25 13:24:51 -04:00
createNewPosts(data, 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-02-17 20:57:12 -05:00
}
});
}
return Topic;
2014-04-10 20:31:57 +01:00
});