« get me outta code hell

generateTrackListDividedByGroups.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/generateTrackListDividedByGroups.js
blob: 69fedb28936c3d57a3a6574ffa5e16200d4cf705 (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
import {empty} from '../../util/sugar.js';

import groupTracksByGroup from '../util/groupTracksByGroup.js';

export default {
  contentDependencies: ['linkTrack', 'linkContribution'],

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

  relations(relation, tracks, groups) {
    if (empty(tracks)) {
      return {};
    }

    const trackRelations = track => ({
      trackLink:
        relation('linkTrack', track),

      contributionLinks:
        track.artistContribs.map(contrib =>
          relation('linkContribution', contrib.who, contrib.what)),
    });

    if (empty(groups)) {
      return {
        flatItems: tracks.map(trackRelations),
      };
    }

    const lists = groupTracksByGroup(tracks, groups);

    return {
      groupedItems:
        Array.from(lists.entries()).map(([groupOrOther, tracks]) => ({
          ...(groupOrOther === 'other'
                ? {other: true}
                : {groupLink: relation('linkGroup', groupOrOther)}),

          items: tracks.map(trackRelations),
        })),
    };
  },

  generate(relations, {html, language}) {
    // TODO: This is copy-pasted from generateTrackInfoPageContent, seems bad

    const formatContributions =
      (contributionLinks, {showContribution = true, showIcons = true} = {}) =>
        language.formatConjunctionList(
          contributionLinks.map(link =>
            link.slots({showContribution, showIcons})));

    const formatTrackItem = ({trackLink, contributionLinks}) =>
      html.tag('li',
        language.$('trackList.item.withArtists', {
          track: trackLink,
          by:
            html.tag('span', {class: 'by'},
              language.$('trackList.item.withArtists.by', {
                artists:
                  formatContributions(contributionLinks, {
                    showContribution: false,
                    showIcons: false,
                  }),
              })),
        }));

    if (relations.flatItems) {
      return html.tag('ul',
        relations.flatItems.map(formatTrackItem));
    }

    return html.tag('dl',
      relations.groupedItems.map(({other, groupLink, items}) => [
        html.tag('dt',
          (other
            ? language.$('trackList.group.fromOther')
            : language.$('trackList.group', {
                group: groupLink
              }))),

        html.tag('dd',
          html.tag('ul',
            items.map(formatTrackItem))),
      ]));
  },
};