« get me outta code hell

generateAlbumNavAccent.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateAlbumNavAccent.js
blob: 121af4391863c029f1f3b99bf41296c70b5746af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {empty} from '#sugar';

export default {
  contentDependencies: [
    'generatePreviousNextLinks',
    'linkTrack',
    'linkAlbumCommentary',
    'linkAlbumGallery',
  ],

  extraDependencies: ['html', 'language'],

  relations(relation, album, track) {
    const relations = {};

    relations.previousNextLinks =
      relation('generatePreviousNextLinks');

    relations.previousTrackLink = null;
    relations.nextTrackLink = null;

    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]);
      }
    }

    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,
      galleryIsStub: album.tracks.every(t => !t.hasUniqueCoverArt),
      isTrackPage: !!track,
    };
  },

  slots: {
    showTrackNavigation: {type: 'boolean', default: false},
    showExtraLinks: {type: 'boolean', default: false},

    currentExtra: {
      validate: v => v.is('gallery', 'commentary'),
    },
  },

  generate(data, relations, slots, {html, language}) {
    const {content: extraLinks = []} =
      slots.showExtraLinks &&
        {content: [
          (!data.galleryIsStub || slots.currentExtra === 'gallery') &&
            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 {content: previousNextLinks = []} =
      slots.showTrackNavigation &&
      data.isTrackPage &&
      data.hasMultipleTracks &&
        relations.previousNextLinks.slots({
          previousLink: relations.previousTrackLink,
          nextLink: relations.nextTrackLink,
        });

    const randomLink =
      slots.showTrackNavigation &&
      data.hasMultipleTracks &&
        html.tag('a',
          {id: 'random-button'},
          {href: '#', 'data-random': 'track-in-sidebar'},

          (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)})`;
  },
};