« get me outta code hell

generateAlbumNavLinks.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/generateAlbumNavLinks.js
blob: 4ce94e1c41016f1933361497ff8a21923b6dd24a (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
113
114
115
116
117
118
119
120
import {empty} from '../../util/sugar.js';

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

    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 {content: extraLinks = []} =
          slots.showExtraLinks &&
            {content: [
              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',
              {
                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)})`
      },
    });
  },
};