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

277 lines
7.5 KiB
JavaScript
Raw Normal View History

'use strict';
define('forum/topic', [
'forum/infinitescroll',
'forum/topic/threadTools',
'forum/topic/postTools',
'forum/topic/events',
'forum/topic/posts',
2017-03-02 18:11:31 +03:00
'forum/topic/images',
2015-01-08 13:47:15 -05:00
'navigator',
'sort',
2017-02-17 19:31:21 -07:00
'components',
2017-04-19 20:33:03 -06:00
'storage',
2018-11-17 20:50:07 -05:00
], function (infinitescroll, threadTools, postTools, events, posts, images, navigator, sort, components, storage) {
2017-02-17 20:20:42 -07:00
var Topic = {};
var currentUrl = '';
$(window).on('action:ajaxify.start', function (ev, data) {
2015-10-28 15:06:19 -04:00
if (Topic.replaceURLTimeout) {
clearTimeout(Topic.replaceURLTimeout);
Topic.replaceURLTimeout = 0;
}
events.removeListeners();
2015-10-28 15:06:19 -04:00
2017-07-14 17:29:31 -04:00
if (!String(data.url).startsWith('topic/')) {
2015-10-19 11:28:18 -04:00
navigator.disable();
2015-03-17 16:12:06 -04:00
components.get('navbar/title').find('span').text('').hide();
app.removeAlert('bookmark');
require(['search'], function (search) {
2015-06-10 14:16:35 -04:00
if (search.topicDOM.active) {
search.topicDOM.end();
}
});
}
});
Topic.init = function () {
2015-07-06 14:33:43 -04:00
var tid = ajaxify.data.tid;
currentUrl = ajaxify.currentPage;
$(window).trigger('action:topic.loading');
app.enterRoom('topic_' + tid);
posts.onTopicPageLoad(components.get('post'));
postTools.init(tid);
threadTools.init(tid);
events.init();
2015-07-06 14:33:43 -04:00
sort.handleSort('topicPostSort', 'user.setTopicSort', 'topic/' + ajaxify.data.slug);
2016-09-15 19:16:52 +03:00
if (!config.usePagination) {
infinitescroll.init($('[component="topic"]'), posts.loadMorePosts);
}
addBlockQuoteHandler();
addParentHandler();
2018-10-11 14:26:53 -04:00
addDropupHandler();
2018-11-17 20:50:07 -05:00
addRepliesHandler();
navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback);
handleBookmark(tid);
$(window).on('scroll', updateTopicTitle);
handleTopicSearch();
2015-09-25 18:21:25 -04:00
2016-05-10 11:39:38 +03:00
$(window).trigger('action:topic.loaded', ajaxify.data);
};
function handleTopicSearch() {
$('.topic-search').off('click')
.on('click', '.prev', function () {
require(['search'], function (search) {
search.topicDOM.prev();
});
})
.on('click', '.next', function () {
require(['search'], function (search) {
search.topicDOM.next();
});
});
if (config.topicSearchEnabled) {
require(['mousetrap'], function (mousetrap) {
2020-05-06 12:55:54 -04:00
mousetrap.bind(['command+f', 'ctrl+f'], function (e) {
2017-02-17 20:20:42 -07:00
var match = ajaxify.currentPage.match(/^topic\/([\d]+)/);
var tid;
if (match) {
e.preventDefault();
tid = match[1];
$('#search-fields input').val('in:topic-' + tid + ' ');
app.prepareSearch();
}
});
});
}
}
Topic.toTop = function () {
2015-09-25 19:01:15 -04:00
navigator.scrollTop(0);
};
Topic.toBottom = function () {
socket.emit('topics.postcount', ajaxify.data.tid, function (err, postCount) {
2016-08-16 19:46:59 +02:00
if (err) {
return app.alertError(err.message);
}
2015-02-25 17:59:59 -05:00
navigator.scrollBottom(postCount - 1);
});
};
function handleBookmark(tid) {
2020-06-05 18:24:11 -04:00
if (window.location.hash) {
var el = $(utils.escapeHTML(window.location.hash));
if (el.length) {
return navigator.scrollToElement(el, true, 0);
}
}
2017-04-19 20:33:03 -06:00
var bookmark = ajaxify.data.bookmark || storage.getItem('topic:' + tid + ':bookmark');
2017-06-13 20:15:48 -04:00
var postIndex = ajaxify.data.postIndex;
2020-06-05 18:24:11 -04:00
if (postIndex > 1) {
2017-06-13 20:15:48 -04:00
if (components.get('post/anchor', postIndex - 1).length) {
return navigator.scrollToPostIndex(postIndex - 1, true, 0);
}
2016-04-18 16:06:59 +03:00
} else if (bookmark && (!config.usePagination || (config.usePagination && ajaxify.data.pagination.currentPage === 1)) && ajaxify.data.postcount > ajaxify.data.bookmarkThreshold) {
app.alert({
alert_id: 'bookmark',
message: '[[topic:bookmark_instructions]]',
timeout: 0,
type: 'info',
2017-02-18 01:27:46 -07:00
clickfn: function () {
navigator.scrollToIndex(parseInt(bookmark, 10), true);
},
2017-02-18 01:27:46 -07:00
closefn: function () {
2017-04-19 20:33:03 -06:00
storage.removeItem('topic:' + tid + ':bookmark');
2017-02-17 19:31:21 -07:00
},
});
setTimeout(function () {
app.removeAlert('bookmark');
}, 10000);
}
}
function addBlockQuoteHandler() {
components.get('topic').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);
});
}
function addParentHandler() {
components.get('topic').on('click', '[component="post/parent"]', function (e) {
2015-09-29 14:35:28 -04:00
var toPid = $(this).attr('data-topid');
2015-09-29 15:46:11 -04:00
2017-12-19 16:03:05 -05:00
var toPost = $('[component="topic"]>[component="post"][data-pid="' + toPid + '"]');
2015-09-29 15:46:11 -04:00
if (toPost.length) {
2016-07-14 16:28:11 -05:00
e.preventDefault();
2017-04-21 13:48:43 -04:00
navigator.scrollToIndex(toPost.attr('data-index'), true);
return false;
2015-09-29 14:35:28 -04:00
}
});
}
2018-10-11 14:26:53 -04:00
function addDropupHandler() {
// Locate all dropdowns
var target = $('#content .dropdown-menu').parent();
// Toggle dropup if past 50% of screen
$(target).on('show.bs.dropdown', function () {
var dropUp = this.getBoundingClientRect().top > ($(window).height() / 2);
$(this).toggleClass('dropup', dropUp);
});
}
2018-11-17 20:50:07 -05:00
function addRepliesHandler() {
$('[component="topic"]').on('click', '[component="post/reply-count"]', function () {
var btn = $(this);
require(['forum/topic/replies'], function (replies) {
replies.init(btn);
});
});
}
function updateTopicTitle() {
2016-02-18 18:32:08 +02:00
var span = components.get('navbar/title').find('span');
2016-03-29 12:39:41 +03:00
if ($(window).scrollTop() > 50 && span.hasClass('hidden')) {
span.html(ajaxify.data.title).removeClass('hidden');
} else if ($(window).scrollTop() <= 50 && !span.hasClass('hidden')) {
span.html('').addClass('hidden');
}
if ($(window).scrollTop() > 300) {
app.removeAlert('bookmark');
}
}
Topic.navigatorCallback = function (index, elementCount) {
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
if (!path.startsWith('topic')) {
2016-06-21 14:43:38 +03:00
return;
}
2016-06-21 14:43:38 +03:00
if (navigator.scrollActive) {
return;
}
var newUrl = 'topic/' + ajaxify.data.slug + (index > 1 ? ('/' + index) : '');
if (newUrl !== currentUrl) {
if (Topic.replaceURLTimeout) {
clearTimeout(Topic.replaceURLTimeout);
}
Topic.replaceURLTimeout = setTimeout(function () {
2016-06-21 14:43:38 +03:00
if (index >= elementCount && app.user.uid) {
socket.emit('topics.markAsRead', [ajaxify.data.tid]);
}
2016-06-21 14:43:38 +03:00
updateUserBookmark(index);
Topic.replaceURLTimeout = 0;
if (history.replaceState) {
2016-09-15 19:16:52 +03:00
var search = window.location.search || '';
if (!config.usePagination) {
search = (search && !/^\?page=\d+$/.test(search) ? search : '');
}
2016-06-21 14:43:38 +03:00
history.replaceState({
2017-02-17 19:31:21 -07:00
url: newUrl + search,
2020-09-06 22:12:53 -04:00
}, null, window.location.protocol + '//' + window.location.host + config.relative_path + '/' + newUrl + search);
2016-06-21 14:43:38 +03:00
}
currentUrl = newUrl;
}, 500);
}
};
function updateUserBookmark(index) {
var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark';
2017-04-19 20:33:03 -06:00
var currentBookmark = ajaxify.data.bookmark || storage.getItem(bookmarkKey);
if (config.topicPostSort === 'newest_to_oldest') {
index = Math.max(1, ajaxify.data.postcount - index + 2);
}
if (ajaxify.data.postcount > ajaxify.data.bookmarkThreshold && (!currentBookmark || parseInt(index, 10) > parseInt(currentBookmark, 10) || ajaxify.data.postcount < parseInt(currentBookmark, 10))) {
if (app.user.uid) {
socket.emit('topics.bookmark', {
2017-02-18 01:19:20 -07:00
tid: ajaxify.data.tid,
index: index,
}, function (err) {
if (err) {
return app.alertError(err.message);
}
ajaxify.data.bookmark = index + 1;
});
} else {
2017-04-19 20:33:03 -06:00
storage.setItem(bookmarkKey, index);
}
}
// removes the bookmark alert when we get to / past the bookmark
if (!currentBookmark || parseInt(index, 10) >= parseInt(currentBookmark, 10)) {
app.removeAlert('bookmark');
}
}
return Topic;
});