« get me outta code hell

divide referenced-by lists by configurable groups - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/misc-templates.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-05-22 12:00:01 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-05-22 12:00:23 -0300
commitf6bd91ea9beef64e0ff0865560219a64a367bb5b (patch)
tree053700f2bc57d395148d90b77daa6e4e3cb4a849 /src/misc-templates.js
parent3b542d286f66e54cb10507fb196a61b7b2f1ee94 (diff)
divide referenced-by lists by configurable groups
...instead of hard-coding 'em
Diffstat (limited to 'src/misc-templates.js')
-rw-r--r--src/misc-templates.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/misc-templates.js b/src/misc-templates.js
index 58c45f5c..d3817500 100644
--- a/src/misc-templates.js
+++ b/src/misc-templates.js
@@ -242,6 +242,52 @@ export function getAlbumStylesheet(album, {to}) {
     ].filter(Boolean).join('\n');
 }
 
+// Divided track lists
+
+export function generateTrackListDividedByGroups(tracks, {
+    getTrackItem,
+    language,
+    wikiData,
+}) {
+    const { divideTrackListsByGroups: groups } = wikiData.wikiInfo;
+
+    if (!groups?.length) {
+        return html.tag('ul', tracks.map(t => getTrackItem(t)));
+    }
+
+    const lists = Object.fromEntries(groups.map(group => [group.directory, {group, tracks: []}]));
+    const other = [];
+
+    for (const track of tracks) {
+        const { album } = track;
+        const group = groups.find(g => g.albums.includes(album));
+        if (group) {
+            lists[group.directory].tracks.push(track);
+        } else {
+            other.push(track);
+        }
+    }
+
+    const ddul = tracks => fixWS`
+        <dd><ul>
+            ${tracks.map(t => getTrackItem(t)).join('\n')}
+        </ul></dd>
+    `;
+
+    return html.tag('dl', Object.values(lists)
+        .filter(({ tracks }) => tracks.length)
+        .flatMap(({ group, tracks }) => [
+            html.tag('dt', language.formatString('trackList.group', {group: group.name})),
+            ddul(tracks)
+        ])
+        .concat(other.length ? [
+            `<dt>${language.formatString('trackList.group', {
+                group: language.formatString('trackList.group.other')
+            })}</dt>`,
+            ddul(other)
+        ] : []));
+}
+
 // Fancy lookin' links
 
 export function fancifyURL(url, {language, album = false} = {}) {