From 872cee8edbfdd1678b826c912c8e37829e5c87a0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 14 Jan 2024 16:12:33 -0400 Subject: content: generateAlbumSidebarGroupBox: match group pages' sorts --- .../dependencies/generateAlbumSidebarGroupBox.js | 54 +++++++++++++++------- 1 file changed, 37 insertions(+), 17 deletions(-) (limited to 'src/content/dependencies') diff --git a/src/content/dependencies/generateAlbumSidebarGroupBox.js b/src/content/dependencies/generateAlbumSidebarGroupBox.js index f3705450..9e1b4cde 100644 --- a/src/content/dependencies/generateAlbumSidebarGroupBox.js +++ b/src/content/dependencies/generateAlbumSidebarGroupBox.js @@ -1,4 +1,5 @@ -import {empty} from '#sugar'; +import {atOffset, empty} from '#sugar'; +import {sortChronologically} from '#wiki-data'; export default { contentDependencies: [ @@ -10,7 +11,33 @@ export default { extraDependencies: ['html', 'language'], - relations(relation, album, group) { + query(album, group) { + const query = {}; + + if (album.date) { + const albums = + group.albums.filter(album => album.date); + + // Sort by latest first. This matches the sorting order used on group + // gallery pages, ensuring that previous/next matches moving up/down + // the gallery. Note that this makes the index offsets "backwards" + // compared to how latest-last chronological lists are accessed. + sortChronologically(albums, {latestFirst: true}); + + const index = + albums.indexOf(album); + + query.previousAlbum = + atOffset(albums, index, +1); + + query.nextAlbum = + atOffset(albums, index, -1); + } + + return query; + }, + + relations(relation, query, album, group) { const relations = {}; relations.groupLink = @@ -25,21 +52,14 @@ export default { relation('transformContent', group.descriptionShort); } - if (album.date) { - const albums = group.albums.filter(album => album.date); - const index = albums.indexOf(album); - const previousAlbum = (index > 0) && albums[index - 1]; - const nextAlbum = (index < albums.length - 1) && albums[index + 1]; - - if (previousAlbum) { - relations.previousAlbumLink = - relation('linkAlbum', previousAlbum); - } - - if (nextAlbum) { - relations.nextAlbumLink = - relation('linkAlbum', nextAlbum); - } + if (query.previousAlbum) { + relations.previousAlbumLink = + relation('linkAlbum', query.previousAlbum); + } + + if (query.nextAlbum) { + relations.nextAlbumLink = + relation('linkAlbum', query.nextAlbum); } return relations; -- cgit 1.3.0-6-gf8a5