« get me outta code hell

listing.js « page « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/page/listing.js
blob: b0766b283e1c2331c28349273a0aea627a5668ad (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
// Listing page specification.
//
// The targets here are a bit different than for most pages: rather than data
// objects loaded from text files in the wiki data directory, they're hard-
// coded specifications, with various JS functions for processing wiki data
// and turning it into user-readable HTML listings.
//
// Individual listing specs are described in src/listing-spec.js, but are
// provided via wikiData like other (normal) data objects.

// Imports

import fixWS from 'fix-whitespace';

import * as html from '../util/html.js';

import {
    UNRELEASED_TRACKS_DIRECTORY
} from '../util/magic-constants.js';

import {
    getTotalDuration
} from '../util/wiki-data.js';

// Page exports

export function condition({wikiData}) {
    return wikiData.wikiInfo.features.listings;
}

export function targets({wikiData}) {
    return wikiData.listingSpec;
}

export function write(listing, {wikiData}) {
    if (listing.condition && !listing.condition({wikiData})) {
        return null;
    }

    const { wikiInfo } = wikiData;

    const data = (listing.data
        ? listing.data({wikiData})
        : null);

    const page = {
        type: 'page',
        path: ['listing', listing.directory],
        page: ({
            link,
            strings
        }) => ({
            title: listing.title({strings}),

            main: {
                content: fixWS`
                    <h1>${listing.title({strings})}</h1>
                    ${listing.html && (listing.data
                        ? listing.html(data, {link, strings})
                        : listing.html({link, strings}))}
                    ${listing.row && fixWS`
                        <ul>
                            ${(data
                                .map(item => listing.row(item, {link, strings}))
                                .map(row => `<li>${row}</li>`)
                                .join('\n'))}
                        </ul>
                    `}
                `
            },

            sidebarLeft: {
                content: generateSidebarForListings(listing, {link, strings, wikiData})
            },

            nav: {
                links: [
                    {toHome: true},
                    {
                        path: ['localized.listingIndex'],
                        title: strings('listingIndex.title')
                    },
                    {toCurrentPage: true}
                ]
            }
        })
    };

    return [page];
}

export function writeTargetless({wikiData}) {
    const { albumData, trackData, wikiInfo } = wikiData;

    const releasedTracks = trackData.filter(track => track.album.directory !== UNRELEASED_TRACKS_DIRECTORY);
    const releasedAlbums = albumData.filter(album => album.directory !== UNRELEASED_TRACKS_DIRECTORY);
    const duration = getTotalDuration(releasedTracks);

    const page = {
        type: 'page',
        path: ['listingIndex'],
        page: ({
            strings,
            link
        }) => ({
            title: strings('listingIndex.title'),

            main: {
                content: fixWS`
                    <h1>${strings('listingIndex.title')}</h1>
                    <p>${strings('listingIndex.infoLine', {
                        wiki: wikiInfo.name,
                        tracks: `<b>${strings.count.tracks(releasedTracks.length, {unit: true})}</b>`,
                        albums: `<b>${strings.count.albums(releasedAlbums.length, {unit: true})}</b>`,
                        duration: `<b>${strings.count.duration(duration, {approximate: true, unit: true})}</b>`
                    })}</p>
                    <hr>
                    <p>${strings('listingIndex.exploreList')}</p>
                    ${generateLinkIndexForListings(null, {link, strings, wikiData})}
                `
            },

            sidebarLeft: {
                content: generateSidebarForListings(null, {link, strings, wikiData})
            },

            nav: {simple: true}
        })
    };

    return [page];
};

// Utility functions

function generateSidebarForListings(currentListing, {link, strings, wikiData}) {
    return fixWS`
        <h1>${link.listingIndex('', {text: strings('listingIndex.title')})}</h1>
        ${generateLinkIndexForListings(currentListing, {link, strings, wikiData})}
    `;
}

function generateLinkIndexForListings(currentListing, {link, strings, wikiData}) {
    const { listingSpec } = wikiData;

    return fixWS`
        <ul>
            ${(listingSpec
                .filter(({ condition }) => !condition || condition({wikiData}))
                .map(listing => html.tag('li',
                    {class: [listing === currentListing && 'current']},
                    link.listing(listing, {text: listing.title({strings})})
                ))
                .join('\n'))}
        </ul>
    `;
}