« get me outta code hell

generateAlbumTrackList.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/generateAlbumTrackList.js
blob: 2732aaa4f51c99ca1363a915d8646fe4dc04f189 (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
137
import {accumulateSum, empty, stitchArrays} from '#sugar';

function displayTrackSections(album) {
  if (empty(album.trackSections)) {
    return false;
  }

  if (album.trackSections.length > 1) {
    return true;
  }

  if (!album.trackSections[0].isDefaultTrackSection) {
    return true;
  }

  return false;
}

function displayTracks(album) {
  if (empty(album.tracks)) {
    return false;
  }

  return true;
}

function getDisplayMode(album) {
  if (displayTrackSections(album)) {
    return 'trackSections';
  } else if (displayTracks(album)) {
    return 'tracks';
  } else {
    return 'none';
  }
}

export default {
  contentDependencies: ['generateAlbumTrackListItem', 'generateContentHeading'],
  extraDependencies: ['html', 'language'],

  query(album) {
    return {
      displayMode: getDisplayMode(album),
    };
  },

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

    switch (query.displayMode) {
      case 'trackSections':
        relations.trackSectionHeadings =
          album.trackSections.map(() =>
            relation('generateContentHeading'));

        relations.itemsByTrackSection =
          album.trackSections.map(section =>
            section.tracks.map(track =>
              relation('generateAlbumTrackListItem', track, album)));

        break;

      case 'tracks':
        relations.itemsByTrack =
          album.tracks.map(track =>
            relation('generateAlbumTrackListItem', track, album));
        break;
    }

    return relations;
  },

  data(query, album) {
    const data = {};

    data.displayMode = query.displayMode;
    data.hasTrackNumbers = album.hasTrackNumbers;

    switch (query.displayMode) {
      case 'trackSections':
        data.trackSectionInfo =
          album.trackSections.map(section => {
            const info = {};

            info.name = section.name;
            info.duration = accumulateSum(section.tracks, track => track.duration);
            info.durationApproximate = section.tracks.length > 1;

            if (album.hasTrackNumbers) {
              info.startIndex = section.startIndex;
            }

            return info;
          });
        break;
    }

    return data;
  },

  generate(data, relations, {html, language}) {
    const listTag = (data.hasTrackNumbers ? 'ol' : 'ul');

    switch (data.displayMode) {
      case 'trackSections':
        return html.tag('dl', {class: 'album-group-list'},
          stitchArrays({
            heading: relations.trackSectionHeadings,
            items: relations.itemsByTrackSection,
            info: data.trackSectionInfo,
          }).map(({heading, items, info}) => [
              heading.slots({
                tag: 'dt',
                title:
                  language.$('trackList.section.withDuration', {
                    section: info.name,
                    duration:
                      language.formatDuration(info.duration, {
                        approximate: info.durationApproximate,
                      }),
                  }),
              }),

              html.tag('dd',
                html.tag(listTag,
                  data.hasTrackNumbers ? {start: info.startIndex + 1} : {},
                  items)),
            ]));

      case 'tracks':
        return html.tag(listTag, relations.itemsByTrack);

      default:
        return html.blank();
    }
  }
};