« get me outta code hell

generateAlbumSidebarTrackSection.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/generateAlbumSidebarTrackSection.js
blob: d3cd37f0d5d2f8e1801872c43d54d67f26a96540 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
export default {
  contentDependencies: ['linkTrack'],
  extraDependencies: ['getColors', 'html', 'language'],

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

    relations.trackLinks =
      trackSection.tracks.map(track =>
        relation('linkTrack', track));

    return relations;
  },

  data(album, track, trackSection) {
    const data = {};

    data.hasTrackNumbers = album.hasTrackNumbers;
    data.isTrackPage = !!track;

    data.name = trackSection.name;
    data.color = trackSection.color;
    data.isDefaultTrackSection = trackSection.isDefaultTrackSection;

    data.firstTrackNumber = trackSection.startIndex + 1;
    data.lastTrackNumber = trackSection.startIndex + trackSection.tracks.length;

    if (track) {
      const index = trackSection.tracks.indexOf(track);
      if (index !== -1) {
        data.includesCurrentTrack = true;
        data.currentTrackIndex = index;
      }
    }

    data.trackDirectories =
      trackSection.tracks
        .map(track => track.directory);

    data.tracksAreMissingCommentary =
      trackSection.tracks
        .map(track => !track.commentary);

    return data;
  },

  slots: {
    anchor: {type: 'boolean'},
    open: {type: 'boolean'},

    mode: {
      validate: v => v.is('info', 'commentary'),
      default: 'info',
    },
  },

  generate(data, relations, slots, {getColors, html, language}) {
    const sectionName =
      html.tag('span', {class: 'group-name'},
        (data.isDefaultTrackSection
          ? language.$('albumSidebar.trackList.fallbackSectionName')
          : data.name));

    let style;
    if (data.color) {
      const {primary} = getColors(data.color);
      style = `--primary-color: ${primary}`;
    }

    const trackListItems =
      relations.trackLinks.map((trackLink, index) =>
        html.tag('li',
          {
            class: [
              data.includesCurrentTrack &&
              index === data.currentTrackIndex &&
                'current',

              slots.mode === 'commentary' &&
              data.tracksAreMissingCommentary[index] &&
                'no-commentary',
            ],
          },
          language.$('albumSidebar.trackList.item', {
            track:
              (slots.mode === 'commentary' && data.tracksAreMissingCommentary[index]
                ? trackLink.slots({
                    linkless: true,
                  })
             : slots.anchor
                ? trackLink.slots({
                    anchor: true,
                    hash: data.trackDirectories[index],
                  })
                : trackLink),
          })));

    return html.tag('details',
      {
        class: data.includesCurrentTrack && 'current',

        open: (
          // Allow forcing open via a template slot.
          // This isn't exactly janky, but the rest of this function
          // kind of is when you contextualize it in a template...
          slots.open ||

          // Leave sidebar track sections collapsed on album info page,
          // since there's already a view of the full track listing
          // in the main content area.
          data.isTrackPage &&

          // Only expand the track section which includes the track
          // currently being viewed by default.
          data.includesCurrentTrack),
      },
      [
        html.tag('summary', {style},
          html.tag('span',
            (data.hasTrackNumbers
              ? language.$('albumSidebar.trackList.group.withRange', {
                  group: sectionName,
                  range: `${data.firstTrackNumber}–${data.lastTrackNumber}`
                })
              : language.$('albumSidebar.trackList.group', {
                  group: sectionName,
                })))),

        (data.hasTrackNumbers
          ? html.tag('ol',
              {start: data.firstTrackNumber},
              trackListItems)
          : html.tag('ul', trackListItems)),
      ]);
  },
};