Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions public/src/modules/topicList.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,44 @@ define('topicList', [
});
}

function onTopicsLoaded(templateName, topics, showSelect, direction, callback) {
function noTopics(topics, callback) {
if (!topics || !topics.length) {
$('#load-more-btn').hide();
return callback();
callback();
return true;
}
topics = filterTopicsOnDom(topics);
return false;
}

if (!topics.length) {
$('#load-more-btn').hide();
return callback();
}
function getInsertionPoints(direction, topicEls) {
return {
after: direction > 0 ? topicEls.last() : null,
before: direction < 0 ? topicEls.first() : null,
};
}

function insertHtml(html, after, before, topicListEl) {
if (after?.length) {
html.insertAfter(after);
} else if (before?.length) {
const height = $(document).height();
const scrollTop = $(window).scrollTop();

let after;
let before;
const topicEls = topicListEl.find('[component="category/topic"]');
html.insertBefore(before);

if (direction > 0 && topics.length) {
after = topicEls.last();
} else if (direction < 0 && topics.length) {
before = topicEls.first();
$(window).scrollTop(scrollTop + ($(document).height() - height));
} else {
topicListEl.append(html);
}
}

function onTopicsLoaded(templateName, topics, showSelect, direction, callback) {
if (noTopics(topics, callback)) return;
topics = filterTopicsOnDom(topics);

if (noTopics(topics, callback)) return;

const { after, before } = getInsertionPoints(direction, topicListEl.find('[component="category/topic"]'));

const tplData = {
topics: topics,
Expand All @@ -224,18 +241,7 @@ define('topicList', [
topicListEl.removeClass('hidden');
$('#category-no-topics').remove();

if (after && after.length) {
html.insertAfter(after);
} else if (before && before.length) {
const height = $(document).height();
const scrollTop = $(window).scrollTop();

html.insertBefore(before);

$(window).scrollTop(scrollTop + ($(document).height() - height));
} else {
topicListEl.append(html);
}
insertHtml(html, after, before, topicListEl);

if (!topicSelect.getSelectedTids().length) {
infinitescroll.removeExtra(topicListEl.find('[component="category/topic"]'), direction, Math.max(60, config.topicsPerPage * 3));
Expand Down