feat(topic-events): handle newest_to_oldest sort in topic events, WIP

This commit is contained in:
Julian Lam
2021-01-09 21:28:32 -05:00
parent 2293a07a06
commit 882e6a159c

View File

@@ -280,27 +280,40 @@ define('forum/topic/posts', [
return; return;
} }
// TODO: Handle oldest_to_newest let postTimestamps = ajaxify.data.posts.map(post => post.timestamp);
const postTimestamps = ajaxify.data.posts.map(post => post.timestamp);
ajaxify.data.events.forEach((event) => { const reverse = config.topicPostSort === 'newest_to_oldest';
const beforeIdx = postTimestamps.findIndex(timestamp => timestamp > event.timestamp); const events = ajaxify.data.events.slice(0);
let postEl; if (reverse) {
if (beforeIdx > -1) { events.reverse();
postEl = document.querySelector(`[component="post"][data-pid="${ajaxify.data.posts[beforeIdx].pid}"]`); postTimestamps = postTimestamps.slice(1); // OP is always at top, so exclude from calculations
} }
Promise.all(events.map((event) => {
const beforeIdx = postTimestamps.findIndex(timestamp => (reverse ? (timestamp < event.timestamp) : (timestamp > event.timestamp)));
let postEl;
if (beforeIdx > -1) {
postEl = document.querySelector(`[component="post"][data-pid="${ajaxify.data.posts[beforeIdx + (reverse ? 1 : 0)].pid}"]`);
}
return new Promise((resolve) => {
app.parseAndTranslate('partials/topic/event', event, function (html) { app.parseAndTranslate('partials/topic/event', event, function (html) {
html = html.get(0); html = html.get(0);
if (postEl) { if (postEl) {
console.log('insert before', ajaxify.data.posts[beforeIdx].pid);
document.querySelector('[component="topic"]').insertBefore(html, postEl); document.querySelector('[component="topic"]').insertBefore(html, postEl);
} else { } else {
console.log('append to bttom?');
document.querySelector('[component="topic"]').append(html); document.querySelector('[component="topic"]').append(html);
} }
$(html).find('.timeago').timeago(); resolve();
}); });
}); });
})).then(() => {
$('[component="topic/event"] .timeago').timeago();
});
} }
function addNecroPostMessage() { function addNecroPostMessage() {