« 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: f2f2279d5ef5c85a6e418b6d2f343fda6e4142a5 (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
import {accumulateSum, empty} from '../../util/sugar.js';

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',
  ],

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

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

    const displayMode = getDisplayMode(album);

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

    if (displayMode === 'tracks') {
      relations.itemsByTrack =
        album.tracks.map(track =>
          relation('generateAlbumTrackListItem', track, album));
    }

    return relations;
  },

  data(album) {
    const data = {};

    data.hasTrackNumbers = album.hasTrackNumbers;

    if (displayTrackSections && !empty(album.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;
        });
    }

    return data;
  },

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

    if (relations.itemsByTrackSection) {
      return html.tag('dl',
        {class: 'album-group-list'},
        data.trackSectionInfo.map((info, index) => [
          html.tag('dt',
            {class: 'content-heading'},
            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} : {},
              relations.itemsByTrackSection[index])),
        ]));
    }

    if (relations.itemsByTrack) {
      return html.tag(listTag, relations.itemsByTrack);
    }

    return html.blank();
  }
};