« get me outta code hell

client.js - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/client.js
blob: 1ae0a2d04ed2996d35e7086761223fed5224bc64 (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
// This is the JS file that gets loaded on the client! It's only really used for
// the random track feature right now - the idea is we only use it for stuff
// that cannot 8e done at static-site compile time, 8y its fundamentally
// ephemeral nature.

'use strict';

const officialAlbumData = albumData.filter(album => !album.isFanon);
const fandomAlbumData = albumData.filter(album => album.isFanon);
const artistNames = artistData.filter(artist => !artist.alias).map(artist => artist.name);
const allTracks = C.getAllTracks(albumData);

function rebase(href) {
    const relative = document.documentElement.dataset.rebase;
    if (relative) {
        return relative + "/" + href;
    } else {
        return href;
    }
}

function pick(array) {
    return array[Math.floor(Math.random() * array.length)];
}

function getAlbum(el) {
    const directory = getComputedStyle(el).getPropertyValue('--album-directory').trim();
    return albumData.find(album => album.directory === directory);
}

function openAlbum(album) {
    location.href = rebase(`${C.ALBUM_DIRECTORY}/${album.directory}/index.html`);
}

function openTrack(track) {
    location.href = rebase(`${C.TRACK_DIRECTORY}/${track.directory}/index.html`);
}

function openArtist(artist) {
    location.href = rebase(`${C.ARTIST_DIRECTORY}/${C.getArtistDirectory(artist)}/index.html`);
}

for (const a of document.body.querySelectorAll('[data-random]')) {
    a.addEventListener('click', evt => {
        try {
            switch (a.dataset.random) {
                case 'album': return openAlbum(pick(albumData));
                case 'album-in-fandom': return openAlbum(pick(fandomAlbumData));
                case 'album-in-official': openAlbum(pick(officialAlbumData));
                case 'track': return openTrack(pick(allTracks));
                case 'track-in-album': return openTrack(pick(getAlbum(a).tracks));
                case 'track-in-fandom': return openTrack(pick(fandomAlbumData.reduce((acc, album) => acc.concat(album.tracks), [])));
                case 'track-in-official': return openTrack(pick(officialAlbumData.reduce((acc, album) => acc.concat(album.tracks), [])));
                case 'artist': return openArtist(pick(artistNames));
                case 'artist-more-than-one-contrib': return openArtist(pick(artistNames.filter(name => C.getArtistNumContributions(name, {albumData, allTracks, flashData}) > 1)));
            }
        } finally {
            evt.preventDefault();
        }
    });
}