fix: hacking handleBack module to work with world page

This commit is contained in:
Julian Lam
2026-03-06 15:02:57 -05:00
parent 942619dbea
commit 971c8603c5
2 changed files with 35 additions and 8 deletions

View File

@@ -3,10 +3,10 @@
define('forum/world', [
'forum/infinitescroll', 'search', 'sort', 'hooks',
'alerts', 'api', 'bootbox', 'helpers', 'forum/category/tools',
'translator', 'quickreply',
'translator', 'quickreply', 'handleBack',
], function (infinitescroll, search, sort, hooks,
alerts, api, bootbox, helpers, categoryTools,
translator, quickreply) {
translator, quickreply, handleBack) {
const World = {};
World.init = function () {
@@ -47,6 +47,20 @@ define('forum/world', [
}
}
handleBack.init((after, handleBackCb) => {
loadTopicsAfter(after, 1, (payload, callback) => {
app.parseAndTranslate(ajaxify.data.template.name, 'posts', payload, function (html) {
const listEl = document.getElementById('world-feed');
$(listEl).append(html);
html.find('.timeago').timeago();
handleImages();
handleShowMoreButtons();
callback();
handleBackCb();
});
});
}, { container: '#world-feed' });
search.enableQuickSearch({
searchElements: {
inputEl: $('[component="category-search"]'),
@@ -65,6 +79,10 @@ define('forum/world', [
if (!config.usePagination) {
infinitescroll.init((direction) => {
const posts = Array.from(document.querySelectorAll('[component="category/topic"]'));
if (!posts.length) {
return;
}
const afterEl = direction > 0 ? posts.pop() : posts.shift();
const after = (parseInt(afterEl.getAttribute('data-index'), 10) || 0) + (direction > 0 ? 1 : 0);
if (after < config.topicsPerPage) {
@@ -74,7 +92,7 @@ define('forum/world', [
loadTopicsAfter(after, direction, (payload, callback) => {
app.parseAndTranslate(ajaxify.data.template.name, 'posts', payload, function (html) {
const listEl = document.getElementById('world-feed');
$(listEl).append(html);
$(listEl)[direction === -1 ? 'prepend' : 'append'](html);
html.find('.timeago').timeago();
handleImages();
handleShowMoreButtons();

View File

@@ -8,9 +8,17 @@ define('handleBack', [
], function (components, storage, navigator, pagination) {
const handleBack = {};
let loadTopicsMethod;
const elements = new Map();
const defaults = new Map([
['container', '[component="category"]'],
]);
handleBack.init = function (_loadTopicsMethod) {
handleBack.init = function (_loadTopicsMethod, _elements = {}) {
loadTopicsMethod = _loadTopicsMethod;
['container'].forEach((prop) => {
elements.set(prop, _elements[prop] || defaults.get(prop));
});
saveClickedIndex();
$(window).off('action:popstate', onBackClicked).on('action:popstate', onBackClicked);
};
@@ -18,10 +26,10 @@ define('handleBack', [
handleBack.onBackClicked = onBackClicked;
function saveClickedIndex() {
$('[component="category"]').on('click', '[component="topic/header"]', function () {
const clickedIndex = $(this).parents('[data-index]').attr('data-index');
$(elements.get('container')).on('click', '[data-index]', function () {
const clickedIndex = $(this).attr('data-index');
const windowScrollTop = $(window).scrollTop();
$('[component="category/topic"]').each(function (index, el) {
$(elements.get('container')).find('[data-index]').each(function (index, el) {
if ($(el).offset().top - windowScrollTop > 0) {
storage.setItem('category:bookmark', $(el).attr('data-index'));
storage.setItem('category:bookmark:clicked', clickedIndex);
@@ -38,6 +46,7 @@ define('handleBack', [
ajaxify.data.template.category ||
ajaxify.data.template.recent ||
ajaxify.data.template.popular ||
ajaxify.data.template.world ||
highlightUnread
) {
let bookmarkIndex = storage.getItem('category:bookmark');
@@ -67,7 +76,7 @@ define('handleBack', [
return;
}
$('[component="category"]').empty();
$(elements.get('container')).empty();
loadTopicsMethod(Math.max(0, bookmarkIndex - 1) + 1, function () {
handleBack.scrollToTopic(bookmarkIndex, clickedIndex);
});