« get me outta code hell

now this commit is big - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/client.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-03-16 14:26:14 -0300
committerFlorrie <towerofnix@gmail.com>2020-03-16 14:26:14 -0300
commitbdaca4a0d30499c105195259994eb1519a7084ec (patch)
treea51f59e8e22a440662abed9bb3dc9be24b65fe8d /client.js
parent5d7b838a030f8bdab66c5b8a48cc30c9b23b6737 (diff)
now this commit is big
Diffstat (limited to 'client.js')
-rw-r--r--client.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/client.js b/client.js
new file mode 100644
index 00000000..eb3c8697
--- /dev/null
+++ b/client.js
@@ -0,0 +1,52 @@
+// 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 = C.getArtistNames(albumData);
+const allTracks = C.getAllTracks(albumData);
+
+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 = `${C.ALBUM_DIRECTORY}/${album.directory}/index.html`;
+}
+
+function openTrack(track) {
+    location.href = `${C.TRACK_DIRECTORY}/${track.directory}/index.html`;
+}
+
+function openArtist(artist) {
+    location.href = `${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}) > 1)));
+            }
+        } finally {
+            evt.preventDefault();
+        }
+    });
+}