« get me outta code hell

generateWikiHomeAlbumsRow.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/generateWikiHomeAlbumsRow.js
blob: 84ed5545e51f5f52ae65612f51342f3f324efa4f (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
138
139
140
141
142
143
144
145
146
147
148
149
150
import {empty, stitchArrays} from '#sugar';
import {getNewAdditions, getNewReleases} from '#wiki-data';

export default {
  contentDependencies: [
    'generateWikiHomeContentRow',
    'generateCoverCarousel',
    'generateCoverGrid',
    'image',
    'linkAlbum',
    'transformContent',
  ],

  extraDependencies: ['language', 'wikiData'],

  sprawl({albumData}, row) {
    const sprawl = {};

    switch (row.sourceGroup) {
      case 'new-releases':
        sprawl.albums = getNewReleases(row.countAlbumsFromGroup, {albumData});
        break;

      case 'new-additions':
        sprawl.albums = getNewAdditions(row.countAlbumsFromGroup, {albumData});
        break;

      default:
        sprawl.albums =
          (row.sourceGroup
            ? row.sourceGroup.albums
                .slice()
                .reverse()
                .filter(album => album.isListedOnHomepage)
                .slice(0, row.countAlbumsFromGroup)
            : []);
    }

    if (!empty(row.sourceAlbums)) {
      sprawl.albums.push(...row.sourceAlbums);
    }

    return sprawl;
  },

  relations(relation, sprawl, row) {
    const relations = {};

    relations.contentRow =
      relation('generateWikiHomeContentRow', row);

    if (row.displayStyle === 'grid') {
      relations.coverGrid =
        relation('generateCoverGrid');
    }

    if (row.displayStyle === 'carousel') {
      relations.coverCarousel =
        relation('generateCoverCarousel');
    }

    relations.links =
      sprawl.albums
        .map(album => relation('linkAlbum', album));

    relations.images =
      sprawl.albums
        .map(album => relation('image', album.artTags));

    if (row.actionLinks) {
      relations.actionLinks =
        row.actionLinks
          .map(content => relation('transformContent', content));
    }

    return relations;
  },

  data(sprawl, row) {
    const data = {};

    data.displayStyle = row.displayStyle;

    if (row.displayStyle === 'grid') {
      data.names =
        sprawl.albums
          .map(album => album.name);
    }

    data.paths =
      sprawl.albums
        .map(album =>
          (album.hasCoverArt
            ? ['media.albumCover', album.directory, album.coverArtFileExtension]
            : null));

    return data;
  },

  generate(data, relations, {language}) {
    // Grids and carousels share some slots! Very convenient.
    const commonSlots = {};

    commonSlots.links =
      relations.links;

    commonSlots.images =
      stitchArrays({
        image: relations.images,
        path: data.paths,
        name: data.names ?? data.paths.slice().fill(null),
      }).map(({image, path, name}) =>
          image.slots({
            path,
            missingSourceContent:
              language.$('misc.coverGrid.noCoverArt', {
                [language.onlyIfOptions]: ['album'],
                album: name,
              }),
            }));

    commonSlots.actionLinks =
      (relations.actionLinks
        ? relations.actionLinks
            .map(contents =>
              contents
                .slot('mode', 'single-link')
                .content)
        : null);

    let content;

    switch (data.displayStyle) {
      case 'grid':
        content =
          relations.coverGrid.slots({
            ...commonSlots,
            names: data.names,
          });
        break;

      case 'carousel':
        content =
          relations.coverCarousel.slots(commonSlots);
        break;
    }

    return relations.contentRow.slots({content});
  },
};