Skip to content

Commit

Permalink
feat(topic-events): handle newest_to_oldest sort in topic events, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Jan 27, 2021
1 parent 2293a07 commit 882e6a1
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions public/src/client/topic/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,26 +280,39 @@ define('forum/topic/posts', [
return;
}

// TODO: Handle oldest_to_newest
const postTimestamps = ajaxify.data.posts.map(post => post.timestamp);
ajaxify.data.events.forEach((event) => {
const beforeIdx = postTimestamps.findIndex(timestamp => timestamp > event.timestamp);
let postTimestamps = ajaxify.data.posts.map(post => post.timestamp);

const reverse = config.topicPostSort === 'newest_to_oldest';
const events = ajaxify.data.events.slice(0);
if (reverse) {
events.reverse();
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].pid}"]`);
postEl = document.querySelector(`[component="post"][data-pid="${ajaxify.data.posts[beforeIdx + (reverse ? 1 : 0)].pid}"]`);
}

app.parseAndTranslate('partials/topic/event', event, function (html) {
html = html.get(0);
return new Promise((resolve) => {
app.parseAndTranslate('partials/topic/event', event, function (html) {
html = html.get(0);

if (postEl) {
document.querySelector('[component="topic"]').insertBefore(html, postEl);
} else {
document.querySelector('[component="topic"]').append(html);
}
if (postEl) {
console.log('insert before', ajaxify.data.posts[beforeIdx].pid);
document.querySelector('[component="topic"]').insertBefore(html, postEl);
} else {
console.log('append to bttom?');
document.querySelector('[component="topic"]').append(html);
}

$(html).find('.timeago').timeago();
resolve();
});
});
})).then(() => {
$('[component="topic/event"] .timeago').timeago();
});
}

Expand Down

0 comments on commit 882e6a1

Please sign in to comment.