2014-10-08 15:36:47 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
2014-11-18 14:33:14 -05:00
|
|
|
define('forum/topic', [
|
2014-10-08 15:36:47 -04:00
|
|
|
'forum/infinitescroll',
|
|
|
|
|
'forum/topic/threadTools',
|
|
|
|
|
'forum/topic/postTools',
|
|
|
|
|
'forum/topic/events',
|
2014-11-18 14:33:14 -05:00
|
|
|
'forum/topic/posts',
|
2017-03-02 18:11:31 +03:00
|
|
|
'forum/topic/images',
|
2015-01-08 13:47:15 -05:00
|
|
|
'navigator',
|
2015-03-31 14:10:20 -04:00
|
|
|
'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 = '';
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$(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;
|
|
|
|
|
}
|
2020-08-11 10:20:17 -04:00
|
|
|
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();
|
2014-10-08 15:36:47 -04:00
|
|
|
app.removeAlert('bookmark');
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
require(['search'], function (search) {
|
2015-06-10 14:16:35 -04:00
|
|
|
if (search.topicDOM.active) {
|
|
|
|
|
search.topicDOM.end();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Topic.init = function () {
|
2015-07-06 14:33:43 -04:00
|
|
|
var tid = ajaxify.data.tid;
|
2020-06-05 17:56:31 -04:00
|
|
|
currentUrl = ajaxify.currentPage;
|
2014-10-08 15:36:47 -04:00
|
|
|
$(window).trigger('action:topic.loading');
|
|
|
|
|
|
|
|
|
|
app.enterRoom('topic_' + tid);
|
|
|
|
|
|
2018-11-17 14:07:48 -05:00
|
|
|
posts.onTopicPageLoad(components.get('post'));
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2015-04-02 22:34:23 -04:00
|
|
|
postTools.init(tid);
|
|
|
|
|
threadTools.init(tid);
|
2014-10-08 15:36:47 -04:00
|
|
|
events.init();
|
|
|
|
|
|
2015-07-06 14:33:43 -04:00
|
|
|
sort.handleSort('topicPostSort', 'user.setTopicSort', 'topic/' + ajaxify.data.slug);
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2016-09-15 19:16:52 +03:00
|
|
|
if (!config.usePagination) {
|
|
|
|
|
infinitescroll.init($('[component="topic"]'), posts.loadMorePosts);
|
|
|
|
|
}
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
addBlockQuoteHandler();
|
2015-09-29 11:03:29 -04:00
|
|
|
addParentHandler();
|
2018-10-11 14:26:53 -04:00
|
|
|
addDropupHandler();
|
2018-11-17 20:50:07 -05:00
|
|
|
addRepliesHandler();
|
2015-09-29 11:03:29 -04:00
|
|
|
|
2019-07-03 12:48:26 -04:00
|
|
|
navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback);
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2016-03-27 15:52:26 -04:00
|
|
|
handleBookmark(tid);
|
|
|
|
|
|
2014-10-08 15:36:47 -04:00
|
|
|
$(window).on('scroll', updateTopicTitle);
|
|
|
|
|
|
2015-06-09 15:10:30 -04:00
|
|
|
handleTopicSearch();
|
2015-09-25 18:21:25 -04:00
|
|
|
|
2016-05-10 11:39:38 +03:00
|
|
|
$(window).trigger('action:topic.loaded', ajaxify.data);
|
2014-10-08 15:36:47 -04:00
|
|
|
};
|
|
|
|
|
|
2015-06-09 15:10:30 -04:00
|
|
|
function handleTopicSearch() {
|
2018-11-18 10:32:08 -05:00
|
|
|
$('.topic-search').off('click')
|
|
|
|
|
.on('click', '.prev', function () {
|
|
|
|
|
require(['search'], function (search) {
|
2015-06-09 15:10:30 -04:00
|
|
|
search.topicDOM.prev();
|
2018-11-18 10:32:08 -05:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.on('click', '.next', function () {
|
|
|
|
|
require(['search'], function (search) {
|
2015-06-09 15:10:30 -04:00
|
|
|
search.topicDOM.next();
|
|
|
|
|
});
|
2018-11-18 10:32:08 -05:00
|
|
|
});
|
2015-06-09 15:10:30 -04:00
|
|
|
|
2018-11-18 10:32:08 -05:00
|
|
|
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;
|
2015-06-09 15:10:30 -04:00
|
|
|
if (match) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
tid = match[1];
|
|
|
|
|
$('#search-fields input').val('in:topic-' + tid + ' ');
|
|
|
|
|
app.prepareSearch();
|
|
|
|
|
}
|
2018-11-18 10:32:08 -05:00
|
|
|
});
|
2015-06-09 15:10:30 -04:00
|
|
|
});
|
2018-11-18 10:32:08 -05:00
|
|
|
}
|
2015-06-09 15:10:30 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Topic.toTop = function () {
|
2015-09-25 19:01:15 -04:00
|
|
|
navigator.scrollTop(0);
|
2014-10-08 15:36:47 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2019-07-03 12:48:26 -04:00
|
|
|
|
2015-02-25 17:59:59 -05:00
|
|
|
navigator.scrollBottom(postCount - 1);
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
2015-08-27 15:32:27 -04:00
|
|
|
}
|
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) {
|
2014-10-08 15:36:47 -04:00
|
|
|
app.alert({
|
|
|
|
|
alert_id: 'bookmark',
|
|
|
|
|
message: '[[topic:bookmark_instructions]]',
|
|
|
|
|
timeout: 0,
|
|
|
|
|
type: 'info',
|
2017-02-18 01:27:46 -07:00
|
|
|
clickfn: function () {
|
2020-07-31 10:34:10 -04:00
|
|
|
navigator.scrollToIndex(parseInt(bookmark, 10), true);
|
2014-10-08 15:36:47 -04:00
|
|
|
},
|
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
|
|
|
},
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
2016-10-13 11:43:39 +02:00
|
|
|
setTimeout(function () {
|
2015-08-28 17:17:31 -04:00
|
|
|
app.removeAlert('bookmark');
|
|
|
|
|
}, 10000);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addBlockQuoteHandler() {
|
2016-10-13 11:43:39 +02:00
|
|
|
components.get('topic').on('click', 'blockquote .toggle', function () {
|
2014-10-08 15:36:47 -04:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-29 11:03:29 -04:00
|
|
|
function addParentHandler() {
|
2016-10-13 11:43:39 +02:00
|
|
|
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);
|
2016-08-16 23:27:43 +03:00
|
|
|
return false;
|
2015-09-29 14:35:28 -04:00
|
|
|
}
|
2015-09-29 11:03:29 -04:00
|
|
|
});
|
|
|
|
|
}
|
2014-10-08 15:36:47 -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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 15:36:47 -04:00
|
|
|
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');
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
2016-03-21 19:49:35 +00:00
|
|
|
if ($(window).scrollTop() > 300) {
|
|
|
|
|
app.removeAlert('bookmark');
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 16:56:54 -05:00
|
|
|
Topic.navigatorCallback = function (index, elementCount) {
|
2014-10-08 15:36:47 -04:00
|
|
|
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
|
|
|
|
|
if (!path.startsWith('topic')) {
|
2016-06-21 14:43:38 +03:00
|
|
|
return;
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
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);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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]);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
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);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-03 15:32:05 -04:00
|
|
|
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);
|
2020-02-27 18:56:22 -05:00
|
|
|
if (config.topicPostSort === 'newest_to_oldest') {
|
|
|
|
|
index = Math.max(1, ajaxify.data.postcount - index + 2);
|
|
|
|
|
}
|
2015-10-03 15:32:05 -04:00
|
|
|
|
2018-02-08 09:50:12 -06:00
|
|
|
if (ajaxify.data.postcount > ajaxify.data.bookmarkThreshold && (!currentBookmark || parseInt(index, 10) > parseInt(currentBookmark, 10) || ajaxify.data.postcount < parseInt(currentBookmark, 10))) {
|
2015-10-03 15:32:05 -04:00
|
|
|
if (app.user.uid) {
|
|
|
|
|
socket.emit('topics.bookmark', {
|
2017-02-18 01:19:20 -07:00
|
|
|
tid: ajaxify.data.tid,
|
|
|
|
|
index: index,
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err) {
|
2015-10-03 15:32:05 -04:00
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
2020-07-31 10:34:10 -04:00
|
|
|
ajaxify.data.bookmark = index + 1;
|
2015-10-03 15:32:05 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
2017-04-19 20:33:03 -06:00
|
|
|
storage.setItem(bookmarkKey, index);
|
2015-10-03 15:32:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// removes the bookmark alert when we get to / past the bookmark
|
|
|
|
|
if (!currentBookmark || parseInt(index, 10) >= parseInt(currentBookmark, 10)) {
|
|
|
|
|
app.removeAlert('bookmark');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 15:36:47 -04:00
|
|
|
|
|
|
|
|
return Topic;
|
|
|
|
|
});
|