« get me outta code hell

content: generateTrackListDividedByGroups: headingString slot - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-06-09 16:59:16 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-06-09 17:53:29 -0300
commita99269edfb9673264f038178b58b942c5e6bc31a (patch)
treeb51d2829324d265ae8a53b12127c6871bd1d5065 /src/content
parent40b72accab9f0bdb3a6602c3f5a9bb4bf8a0bd0d (diff)
content: generateTrackListDividedByGroups: headingString slot
Diffstat (limited to 'src/content')
-rw-r--r--src/content/dependencies/generateTrackListDividedByGroups.js101
1 files changed, 77 insertions, 24 deletions
diff --git a/src/content/dependencies/generateTrackListDividedByGroups.js b/src/content/dependencies/generateTrackListDividedByGroups.js
index e070ac35..a9326205 100644
--- a/src/content/dependencies/generateTrackListDividedByGroups.js
+++ b/src/content/dependencies/generateTrackListDividedByGroups.js
@@ -1,12 +1,24 @@
-import {empty} from '#sugar';
+import {empty, stitchArrays} from '#sugar';
 
 import groupTracksByGroup from '../util/groupTracksByGroup.js';
 
 export default {
-  contentDependencies: ['generateTrackList', 'linkGroup'],
+  contentDependencies: [
+    'generateContentHeading',
+    'generateTrackList',
+    'linkGroup',
+  ],
+
   extraDependencies: ['html', 'language'],
 
-  relations(relation, tracks, groups) {
+  query: (tracks, groups) => ({
+    lists:
+      (empty(groups)
+        ? []
+        : groupTracksByGroup(tracks, groups)),
+  }),
+
+  relations(relation, query, tracks, groups) {
     if (empty(tracks)) {
       return {};
     }
@@ -18,36 +30,77 @@ export default {
       };
     }
 
-    const lists = groupTracksByGroup(tracks, groups);
-
     return {
+      contentHeading:
+        relation('generateContentHeading'),
+
       groupedLists:
-        Array.from(lists.entries()).map(([groupOrOther, tracks]) => ({
-          ...(groupOrOther === 'other'
-                ? {other: true}
-                : {groupLink: relation('linkGroup', groupOrOther)}),
-
-          list:
-            relation('generateTrackList', tracks),
-        })),
+        Array.from(query.lists.entries())
+          .map(([groupOrOther, tracks]) => ({
+            ...(groupOrOther === 'other'
+                  ? {other: true}
+                  : {groupLink: relation('linkGroup', groupOrOther)}),
+
+            list:
+              relation('generateTrackList', tracks),
+          })),
     };
   },
 
-  generate(relations, {html, language}) {
+  data: (query) => ({
+    groupNames:
+      Array.from(query.lists.keys())
+        .map(groupOrOther =>
+          (groupOrOther === 'group'
+            ? null
+            : groupOrOther.name)),
+  }),
+
+  slots: {
+    headingString: {
+      type: 'string',
+    },
+  },
+
+  generate(data, relations, slots, {html, language}) {
     if (relations.flatList) {
       return relations.flatList;
     }
 
     return html.tag('dl',
-      relations.groupedLists.map(({other, groupLink, list}) => [
-        html.tag('dt',
-          (other
-            ? language.$('trackList.group.fromOther')
-            : language.$('trackList.group', {
-                group: groupLink
-              }))),
-
-        html.tag('dd', list),
-      ]));
+      stitchArrays({
+        groupName: data.groupNames,
+        listEntry: relations.groupedLists
+      }).map(({
+          groupName,
+          listEntry: {other, groupLink, list},
+        }) => [
+          (slots.headingString
+            ? relations.contentHeading.clone().slots({
+                tag: 'dt',
+
+                title:
+                  (other
+                    ? language.$('trackList.fromOther')
+                    : language.$('trackList.fromGroup', {
+                        group: groupLink
+                      })),
+
+                stickyTitle:
+                  (other
+                    ? language.$(slots.headingString, 'sticky', 'fromOther')
+                    : language.$(slots.headingString, 'sticky', 'fromGroup', {
+                        group: groupName,
+                      })),
+              })
+            : html.tag('dt',
+                (other
+                  ? language.$('trackList.fromOther')
+                  : language.$('trackList.fromGroup', {
+                      group: groupLink
+                    })))),
+
+          html.tag('dd', list),
+        ]));
   },
 };