« get me outta code hell

content: generateTrackList (generic function) - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-04-18 21:23:40 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-04-18 21:25:08 -0300
commit07df83cb011400c499fc5ff859ba7bf5795553ed (patch)
treeac88eaec03572321a5a3541b4d006ef2b450b75e
parentfba7d3d6e6d4271744e66efa101d98777995d878 (diff)
content: generateTrackList (generic function)
This isn't quite portable enough for use as a dependency
in generateAlbumTrackList, which can maybe be improved on.
-rw-r--r--src/content/dependencies/generateTrackInfoPageContent.js26
-rw-r--r--src/content/dependencies/generateTrackList.js55
-rw-r--r--src/content/dependencies/generateTrackListDividedByGroups.js54
3 files changed, 69 insertions, 66 deletions
diff --git a/src/content/dependencies/generateTrackInfoPageContent.js b/src/content/dependencies/generateTrackInfoPageContent.js
index eea387bb..57bdc0c2 100644
--- a/src/content/dependencies/generateTrackInfoPageContent.js
+++ b/src/content/dependencies/generateTrackInfoPageContent.js
@@ -4,6 +4,7 @@ export default {
   contentDependencies: [
     'generateContentHeading',
     'generateCoverArtwork',
+    'generateTrackList',
     'generateTrackListDividedByGroups',
     'linkAlbum',
     'linkContribution',
@@ -94,11 +95,8 @@ export default {
       references.heading =
         relation('generateContentHeading');
 
-      references.items =
-        track.referencedTracks.map(track => ({
-          trackLink: relation('linkTrack', track),
-          contributionLinks: contributionLinksRelation(track.artistContribs),
-        }));
+      references.list =
+        relation('generateTrackList', track.referencedTracks);
     }
 
     // Section: Tracks that reference
@@ -161,21 +159,6 @@ export default {
           contributionLinks.map(link =>
             link.slots({showContribution, showIcons})));
 
-    const formatTrackItem = ({trackLink, contributionLinks}) =>
-      html.tag('li',
-        language.$('trackList.item.withArtists', {
-          track: trackLink,
-          by:
-            html.tag('span', {class: 'by'},
-              language.$('trackList.item.withArtists.by', {
-                artists:
-                  formatContributions(contributionLinks, {
-                    showContribution: false,
-                    showIcons: false,
-                  }),
-              })),
-        }));
-
     if (data.hasUniqueCoverArt) {
       content.cover = relations.cover
         .slots({
@@ -311,8 +294,7 @@ export default {
                 }),
             }),
 
-          html.tag('ul',
-            sec.references.items.map(formatTrackItem)),
+          sec.references.list,
         ],
 
         sec.referencedBy && [
diff --git a/src/content/dependencies/generateTrackList.js b/src/content/dependencies/generateTrackList.js
new file mode 100644
index 00000000..e2e9f48d
--- /dev/null
+++ b/src/content/dependencies/generateTrackList.js
@@ -0,0 +1,55 @@
+import {empty} from '../../util/sugar.js';
+
+export default {
+  contentDependencies: ['linkTrack', 'linkContribution'],
+
+  extraDependencies: ['html', 'language'],
+
+  relations(relation, tracks) {
+    if (empty(tracks)) {
+      return {};
+    }
+
+    return {
+      items: tracks.map(track => ({
+        trackLink:
+          relation('linkTrack', track),
+
+        contributionLinks:
+          track.artistContribs.map(contrib =>
+            relation('linkContribution', contrib.who, contrib.what)),
+      })),
+    };
+  },
+
+  generate(relations, {html, language}) {
+    return html.template({
+      annotation: `generateTrackList`,
+
+      slots: {
+        showContribution: {type: 'boolean', default: false},
+        showIcons: {type: 'boolean', default: false},
+      },
+
+      content(slots) {
+        return html.tag('ul',
+          relations.items.map(({trackLink, contributionLinks}) =>
+            html.tag('li',
+              language.$('trackList.item.withArtists', {
+                track: trackLink,
+                by:
+                  html.tag('span', {class: 'by'},
+                    language.$('trackList.item.withArtists.by', {
+                      artists:
+                        language.formatConjunctionList(
+                          contributionLinks.map(link =>
+                            link.slots({
+                              showContribution: slots.showContribution,
+                              showIcons: slots.showIcons,
+                            }))),
+                    })),
+              }))));
+      },
+    });
+  },
+};
diff --git a/src/content/dependencies/generateTrackListDividedByGroups.js b/src/content/dependencies/generateTrackListDividedByGroups.js
index 69fedb28..1ced2eb8 100644
--- a/src/content/dependencies/generateTrackListDividedByGroups.js
+++ b/src/content/dependencies/generateTrackListDividedByGroups.js
@@ -3,8 +3,7 @@ import {empty} from '../../util/sugar.js';
 import groupTracksByGroup from '../util/groupTracksByGroup.js';
 
 export default {
-  contentDependencies: ['linkTrack', 'linkContribution'],
-
+  contentDependencies: ['generateTrackList'],
   extraDependencies: ['html', 'language'],
 
   relations(relation, tracks, groups) {
@@ -12,66 +11,35 @@ export default {
       return {};
     }
 
-    const trackRelations = track => ({
-      trackLink:
-        relation('linkTrack', track),
-
-      contributionLinks:
-        track.artistContribs.map(contrib =>
-          relation('linkContribution', contrib.who, contrib.what)),
-    });
-
     if (empty(groups)) {
       return {
-        flatItems: tracks.map(trackRelations),
+        flatList:
+          relation('generateTrackList', tracks),
       };
     }
 
     const lists = groupTracksByGroup(tracks, groups);
 
     return {
-      groupedItems:
+      groupedLists:
         Array.from(lists.entries()).map(([groupOrOther, tracks]) => ({
           ...(groupOrOther === 'other'
                 ? {other: true}
                 : {groupLink: relation('linkGroup', groupOrOther)}),
 
-          items: tracks.map(trackRelations),
+          list:
+            relation('generateTrackList', tracks),
         })),
     };
   },
 
   generate(relations, {html, language}) {
-    // TODO: This is copy-pasted from generateTrackInfoPageContent, seems bad
-
-    const formatContributions =
-      (contributionLinks, {showContribution = true, showIcons = true} = {}) =>
-        language.formatConjunctionList(
-          contributionLinks.map(link =>
-            link.slots({showContribution, showIcons})));
-
-    const formatTrackItem = ({trackLink, contributionLinks}) =>
-      html.tag('li',
-        language.$('trackList.item.withArtists', {
-          track: trackLink,
-          by:
-            html.tag('span', {class: 'by'},
-              language.$('trackList.item.withArtists.by', {
-                artists:
-                  formatContributions(contributionLinks, {
-                    showContribution: false,
-                    showIcons: false,
-                  }),
-              })),
-        }));
-
-    if (relations.flatItems) {
-      return html.tag('ul',
-        relations.flatItems.map(formatTrackItem));
+    if (relations.flatList) {
+      return relations.flatList;
     }
 
     return html.tag('dl',
-      relations.groupedItems.map(({other, groupLink, items}) => [
+      relations.groupedLists.map(({other, groupLink, list}) => [
         html.tag('dt',
           (other
             ? language.$('trackList.group.fromOther')
@@ -79,9 +47,7 @@ export default {
                 group: groupLink
               }))),
 
-        html.tag('dd',
-          html.tag('ul',
-            items.map(formatTrackItem))),
+        html.tag('dd', list),
       ]));
   },
 };