« get me outta code hell

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:
Diffstat (limited to 'client.js')
-rw-r--r--client.js107
1 files changed, 106 insertions, 1 deletions
diff --git a/client.js b/client.js
index 1ae0a2d0..5d706b24 100644
--- a/client.js
+++ b/client.js
@@ -23,11 +23,20 @@ function pick(array) {
     return array[Math.floor(Math.random() * array.length)];
 }
 
+function cssProp(el, key) {
+    return getComputedStyle(el).getPropertyValue(key).trim();
+}
+
 function getAlbum(el) {
-    const directory = getComputedStyle(el).getPropertyValue('--album-directory').trim();
+    const directory = cssProp(el, '--album-directory');
     return albumData.find(album => album.directory === directory);
 }
 
+function getFlash(el) {
+    const directory = cssProp(el, '--flash-directory');
+    return flashData.find(flash => flash.directory === directory);
+}
+
 function openAlbum(album) {
     location.href = rebase(`${C.ALBUM_DIRECTORY}/${album.directory}/index.html`);
 }
@@ -40,6 +49,73 @@ function openArtist(artist) {
     location.href = rebase(`${C.ARTIST_DIRECTORY}/${C.getArtistDirectory(artist)}/index.html`);
 }
 
+function openFlash(flash) {
+    location.href = rebase(`${C.FLASH_DIRECTORY}/${flash.directory}/index.html`);
+}
+
+/* i implemented these functions but we dont actually use them anywhere lol
+function isFlashPage() {
+    return !!cssProp(document.body, '--flash-directory');
+}
+
+function isTrackOrAlbumPage() {
+    return !!cssProp(document.body, '--album-directory');
+}
+
+function isTrackPage() {
+    return !!cssProp(document.body, '--track-directory');
+}
+*/
+
+function getTrackListAndIndex() {
+    const album = getAlbum(document.body);
+    const directory = cssProp(document.body, '--track-directory');
+    if (!directory && !album) return {};
+    if (!directory) return {list: album.tracks};
+    const trackIndex = album.tracks.findIndex(track => track.directory === directory);
+    return {list: album.tracks, index: trackIndex};
+}
+
+function openNextTrack() {
+    const { list, index } = getTrackListAndIndex();
+    if (!list) return;
+    if (index === list.length) return;
+    openTrack(list[index + 1]);
+}
+
+function openPreviousTrack() {
+    const { list, index } = getTrackListAndIndex();
+    if (!list) return;
+    if (index === 0) return;
+    openTrack(list[index - 1]);
+}
+
+function openRandomTrack() {
+    const { list } = getTrackListAndIndex();
+    if (!list) return;
+    openTrack(pick(list));
+}
+
+function getFlashListAndIndex() {
+    const list = flashData.filter(flash => !flash.act8r8k)
+    const flash = getFlash(document.body);
+    if (!flash) return {list};
+    const flashIndex = list.indexOf(flash);
+    return {list, index: flashIndex};
+}
+
+function openNextFlash() {
+    const { list, index } = getFlashListAndIndex();
+    if (index === list.length) return;
+    openFlash(list[index + 1]);
+}
+
+function openPreviousFlash() {
+    const { list, index } = getFlashListAndIndex();
+    if (index === 0) return;
+    openFlash(list[index - 1]);
+}
+
 for (const a of document.body.querySelectorAll('[data-random]')) {
     a.addEventListener('click', evt => {
         try {
@@ -59,3 +135,32 @@ for (const a of document.body.querySelectorAll('[data-random]')) {
         }
     });
 }
+
+const next = document.getElementById('next-button');
+const previous = document.getElementById('previous-button');
+const random = document.getElementById('random-button');
+
+const prependTitle = (el, prepend) => {
+    const existing = el.getAttribute('title');
+    if (existing) {
+        el.setAttribute('title', prepend + ' ' + existing);
+    } else {
+        el.setAttribute('title', prepend);
+    }
+};
+
+if (next) prependTitle(next, '(Shift+N)');
+if (previous) prependTitle(previous, '(Shift+P)');
+if (random) prependTitle(random, '(Shift+R)');
+
+document.addEventListener('keypress', event => {
+    if (event.shiftKey) {
+        if (event.charCode === 'N'.charCodeAt(0)) {
+            if (next) next.click();
+        } else if (event.charCode === 'P'.charCodeAt(0)) {
+            if (previous) previous.click();
+        } else if (event.charCode === 'R'.charCodeAt(0)) {
+            if (random) random.click();
+        }
+    }
+});