« get me outta code hell

homepage.js « page « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/page/homepage.js
blob: 2219593e58979643271f5888be84d0dda5d0a699 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Homepage specification.

import {getNewAdditions, getNewReleases} from '../util/wiki-data.js';

export function writeTargetless({wikiData}) {
  const {newsData, staticPageData, homepageLayout, wikiInfo} = wikiData;

  const rowData = homepageLayout.rows?.map(row => {
    const {color, name, type} = row;
    const entry = {row, color, name, type};

    switch (type) {
      case 'albums': {
        switch (row.sourceGroupByRef) {
          case 'new-releases':
            entry.gridEntries = getNewReleases(row.countAlbumsFromGroup, {wikiData});
            break;
          case 'new-additions':
            entry.gridEntries = getNewAdditions(row.countAlbumsFromGroup, {wikiData});
            break;
          default:
            entry.gridEntries = row.sourceGroup
              ? row.sourceGroup.albums
                  .slice()
                  .reverse()
                  .filter(album => album.isListedOnHomepage)
                  .slice(0, row.countAlbumsFromGroup)
                  .map(album => ({item: album}))
              : [];
        }

        if (row.sourceAlbums.length) {
          entry.gridEntries.push(...row.sourceAlbums.map(album => ({item: album})));
        }

        entry.actionLinks = row.actionLinks ?? [];
        break;
      }
    }

    return entry;
  });

  const page = {
    type: 'page',
    path: ['home'],
    page: ({
      getAlbumGridHTML,
      getLinkThemeString,
      html,
      language,
      link,
      to,
      transformInline,
      transformMultiline,
    }) => ({
      title: wikiInfo.name,
      showWikiNameInTitle: false,

      meta: {
        description: wikiInfo.description,
      },

      main: {
        classes: ['top-index'],
        content: html.fragment([
          html.tag('h1',
            wikiInfo.name),

          ...html.fragment(
            rowData.map((entry, i) =>
              html.tag('section',
                {
                  class: 'row',
                  style: getLinkThemeString(entry.color),
                },
                [
                  html.tag('h2',
                    entry.name),

                  entry.type === 'albums' &&
                    html.tag('div', {class: 'grid-listing'}, [
                      ...html.fragment(
                        getAlbumGridHTML({
                          entries: entry.gridEntries,
                          lazy: i > 0,
                        })),

                      entry.actionLinks.length &&
                        html.tag('div', {class: 'grid-actions'},
                          entry.actionLinks.map(action =>
                            transformInline(action)
                              .replace('<a', '<a class="box grid-item"'))),
                    ]),
                ]))),
        ]),
      },

      sidebarLeft: homepageLayout.sidebarContent && {
        wide: true,
        collapse: false,
        // This is a pretty filthy hack! 8ut otherwise, the [[news]] part
        // gets treated like it's a reference to the track named "news",
        // which o8viously isn't what we're going for. Gotta catch that
        // 8efore we pass it to transformMultiline, 'cuz otherwise it'll
        // get repl8ced with just the word "news" (or anything else that
        // transformMultiline does with references it can't match) -- and
        // we can't match that for replacing it with the news column!
        //
        // And no, I will not make [[news]] into part of transformMultiline
        // (even though that would 8e hilarious).
        content:
          transformMultiline(
            homepageLayout.sidebarContent
              .replace('[[news]]', '__GENERATE_NEWS__')
          )
            .replace('<p>__GENERATE_NEWS__</p>',
              wikiInfo.enableNews
                ? [
                    html.tag('h1',
                      language.$('homepage.news.title')),

                    ...newsData
                      .slice(0, 3)
                      .map((entry, i) =>
                        html.tag('article',
                          {
                            class: [
                              'news-entry',
                              i === 0 && 'first-news-entry',
                            ],
                          },
                          [
                            html.tag('h2', [
                              html.tag('time',
                                language.formatDate(entry.date)),
                              link.newsEntry(entry),
                            ]),

                            transformMultiline(entry.contentShort),

                            entry.contentShort !== entry.content &&
                              link.newsEntry(entry, {
                                text: language.$('homepage.news.entry.viewRest')
                              }),
                          ])),
                  ].join('\n')
                : html.tag('p',
                    html.tag('i',
                      `News requested in content description but this feature isn't enabled`))),
      },

      nav: {
        linkContainerClasses: ['nav-links-index'],
        links: [
          link.home('', {text: wikiInfo.nameShort, class: 'current', to}),

          wikiInfo.enableListings &&
            link.listingIndex('', {
              text: language.$('listingIndex.title'),
              to,
            }),

          wikiInfo.enableNews &&
            link.newsIndex('', {text: language.$('newsIndex.title'), to}),

          wikiInfo.enableFlashesAndGames &&
            link.flashIndex('', {text: language.$('flashIndex.title'), to}),

          ...staticPageData
            .filter((page) => page.showInNavigationBar)
            .map((page) => link.staticPage(page, {text: page.nameShort})),
        ]
          .filter(Boolean)
          .map((html) => ({html})),
      },
    }),
  };

  return [page];
}