« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateAlbumNavLinks.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/dependencies/generateAlbumNavLinks.js')
-rw-r--r--src/content/dependencies/generateAlbumNavLinks.js121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/content/dependencies/generateAlbumNavLinks.js b/src/content/dependencies/generateAlbumNavLinks.js
new file mode 100644
index 00000000..d9645319
--- /dev/null
+++ b/src/content/dependencies/generateAlbumNavLinks.js
@@ -0,0 +1,121 @@
+import {empty} from '../../util/sugar.js';
+
+export default {
+  contentDependencies: [
+    'linkTrack',
+    'linkAlbumCommentary',
+    'linkAlbumGallery',
+  ],
+
+  extraDependencies: ['html', 'language'],
+
+  relations(relation, album, track) {
+    const relations = {};
+
+    if (track) {
+      const index = album.tracks.indexOf(track);
+
+      if (index > 0) {
+        relations.previousTrackLink =
+          relation('linkTrack', album.tracks[index - 1]);
+      }
+
+      if (index < album.tracks.length - 1) {
+        relations.nextTrackLink =
+          relation('linkTrack', album.tracks[index + 1]);
+      }
+    }
+
+    if (album.tracks.some(t => t.hasUniqueCoverArt)) {
+      relations.albumGalleryLink =
+        relation('linkAlbumGallery', album);
+    }
+
+    if (album.commentary || album.tracks.some(t => t.commentary)) {
+      relations.albumCommentaryLink =
+        relation('linkAlbumCommentary', album);
+    }
+
+    return relations;
+  },
+
+  data(album, track) {
+    return {
+      hasMultipleTracks: album.tracks.length > 1,
+      isTrackPage: !!track,
+    };
+  },
+
+  generate(data, relations, {html, language}) {
+    return html.template({
+      annotation: `generateAlbumNavLinks`,
+
+      slots: {
+        showTrackNavigation: {type: 'boolean', default: false},
+        showExtraLinks: {type: 'boolean', default: false},
+
+        currentExtra: {
+          validate: v => v.is('gallery', 'commentary'),
+        },
+      },
+
+      content(slots) {
+        const extraLinks =
+          (slots.showExtraLinks
+            ? [
+                relations.albumGalleryLink.slots({
+                  attributes: {class: slots.currentExtra === 'gallery' && 'current'},
+                  content: language.$('albumPage.nav.gallery'),
+                }),
+
+                relations.albumCommentaryLink.slots({
+                  attributes: {class: slots.currentExtra === 'commentary' && 'current'},
+                  content: language.$('albumPage.nav.commentary'),
+                }),
+              ]
+            : []);
+
+        const previousNextLinks =
+          (slots.showTrackNavigation
+            ? [
+                relations.previousTrackLink?.slots({
+                  tooltip: true,
+                  attributes: {id: 'previous-button'},
+                  content: language.$('misc.nav.previous'),
+                }),
+                relations.nextTrackLink?.slots({
+                  tooltip: true,
+                  attributes: {id: 'next-button'},
+                  content: language.$('misc.nav.next'),
+                }),
+              ]
+            : []);
+
+        const randomLink =
+          slots.showTrackNavigation &&
+          data.hasMultipleTracks &&
+            html.tag('a',
+              {
+                href: '#',
+                'data-random': 'track-in-album',
+                id: 'random-button',
+              },
+              (data.isTrackPage
+                ? language.$('trackPage.nav.random')
+                : language.$('albumPage.nav.randomTrack')));
+
+        const allLinks = [
+          ...previousNextLinks,
+          ...extraLinks,
+          randomLink,
+        ].filter(Boolean);
+
+        if (empty(allLinks)) {
+          return html.blank();
+        }
+
+        return `(${language.formatUnitList(allLinks)})`
+      },
+    });
+  },
+};