« get me outta code hell

generateCommentaryIndexPage.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/generateCommentaryIndexPage.js
blob: 0cb5a4089215ce7f774b061de080cf1d76f976a9 (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
import {accumulateSum, stitchArrays} from '../../util/sugar.js';
import {filterMultipleArrays, sortChronologically} from '../../util/wiki-data.js';

export default {
  contentDependencies: ['generatePageLayout', 'linkAlbumCommentary'],
  extraDependencies: ['html', 'language', 'wikiData'],

  sprawl({albumData}) {
    return {albumData};
  },

  query(sprawl) {
    const query = {};

    query.albums =
      sortChronologically(sprawl.albumData.slice());

    const entries =
      query.albums.map(album =>
        [album, ...album.tracks]
          .filter(({commentary}) => commentary)
          .map(({commentary}) => commentary));

    query.wordCounts =
      entries.map(entries =>
        accumulateSum(
          entries,
          entry => entry.split(' ').length));

    query.entryCounts =
      entries.map(entries => entries.length);

    filterMultipleArrays(query.albums, query.wordCounts, query.entryCounts,
      (album, wordCount, entryCount) => entryCount >= 1);

    return query;
  },

  relations(relation, query) {
    return {
      layout:
        relation('generatePageLayout'),

      albumLinks:
        query.albums
          .map(album => relation('linkAlbumCommentary', album)),
    };
  },

  data(query) {
    return {
      wordCounts: query.wordCounts,
      entryCounts: query.entryCounts,

      totalWordCount: accumulateSum(query.wordCounts),
      totalEntryCount: accumulateSum(query.entryCounts),
    };
  },

  generate(data, relations, {html, language}) {
    return relations.layout.slots({
      title: language.$('commentaryIndex.title'),

      headingMode: 'static',

      mainClasses: ['long-content'],
      mainContent: [
        html.tag('p', language.$('commentaryIndex.infoLine', {
          words:
            html.tag('b',
              language.formatWordCount(data.totalWordCount, {unit: true})),

          entries:
            html.tag('b',
                language.countCommentaryEntries(data.totalEntryCount, {unit: true})),
        })),

        html.tag('p',
          language.$('commentaryIndex.albumList.title')),

        html.tag('ul',
          stitchArrays({
            albumLink: relations.albumLinks,
            wordCount: data.wordCounts,
            entryCount: data.entryCounts,
          }).map(({albumLink, wordCount, entryCount}) =>
            html.tag('li',
              language.$('commentaryIndex.albumList.item', {
                album: albumLink,
                words: language.formatWordCount(wordCount, {unit: true}),
                entries: language.countCommentaryEntries(entryCount, {unit: true}),
              })))),
      ],

      navLinkStyle: 'hierarchical',
      navLinks: [
        {auto: 'home'},
        {auto: 'current'},
      ],
    });
  },
};