« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/content/dependencies/listAlbumsByDate.js52
-rw-r--r--src/content/dependencies/listAlbumsByDuration.js57
-rw-r--r--src/content/dependencies/listAlbumsByTracks.js53
-rw-r--r--src/listing-spec.js41
4 files changed, 167 insertions, 36 deletions
diff --git a/src/content/dependencies/listAlbumsByDate.js b/src/content/dependencies/listAlbumsByDate.js
new file mode 100644
index 00000000..1c584282
--- /dev/null
+++ b/src/content/dependencies/listAlbumsByDate.js
@@ -0,0 +1,52 @@
+import {stitchArrays} from '../../util/sugar.js';
+import {sortChronologically} from '../../util/wiki-data.js';
+
+export default {
+  contentDependencies: ['generateListingPage', 'linkAlbum'],
+  extraDependencies: ['language', 'wikiData'],
+
+  sprawl({albumData}) {
+    return {albumData};
+  },
+
+  query({albumData}, spec) {
+    return {
+      spec,
+
+      albums:
+        sortChronologically(albumData.filter(album => album.date)),
+    };
+  },
+
+  relations(relation, query) {
+    return {
+      page: relation('generateListingPage', query.spec),
+
+      albumLinks:
+        query.albums
+          .map(album => relation('linkAlbum', album)),
+    };
+  },
+
+  data(query) {
+    return {
+      dates:
+        query.albums
+          .map(album => album.date),
+    };
+  },
+
+  generate(data, relations, {language}) {
+    return relations.page.slots({
+      type: 'rows',
+      rows:
+        stitchArrays({
+          link: relations.albumLinks,
+          date: data.dates,
+        }).map(({link, date}) => ({
+            album: link,
+            date: language.formatDate(date),
+          })),
+    });
+  },
+};
diff --git a/src/content/dependencies/listAlbumsByDuration.js b/src/content/dependencies/listAlbumsByDuration.js
new file mode 100644
index 00000000..e922ebc9
--- /dev/null
+++ b/src/content/dependencies/listAlbumsByDuration.js
@@ -0,0 +1,57 @@
+import {stitchArrays} from '../../util/sugar.js';
+import {getTotalDuration} from '../../util/wiki-data.js';
+
+export default {
+  contentDependencies: ['generateListingPage', 'linkAlbum'],
+  extraDependencies: ['language', 'wikiData'],
+
+  sprawl({albumData}) {
+    return {albumData};
+  },
+
+  query({albumData}, spec) {
+    const albumToDuration =
+      new Map(albumData.map(album => [album, getTotalDuration(album.tracks)]));
+
+    return {
+      spec,
+
+      albums:
+        albumData
+          .filter(album => albumToDuration.get(album) > 0)
+          .sort((a, b) => albumToDuration.get(b) - albumToDuration.get(a)),
+    };
+  },
+
+  relations(relation, query) {
+    return {
+      page: relation('generateListingPage', query.spec),
+
+      albumLinks:
+        query.albums
+          .map(album => relation('linkAlbum', album)),
+    };
+  },
+
+  data(query) {
+    return {
+      durations:
+        query.albums
+          .map(album => getTotalDuration(album.tracks)),
+    };
+  },
+
+  generate(data, relations, {language}) {
+    return relations.page.slots({
+      type: 'rows',
+      rows:
+        stitchArrays({
+          link: relations.albumLinks,
+          duration: data.durations,
+        }).map(({link, duration}) => ({
+            album: link,
+            duration: language.formatDuration(duration),
+          })),
+    });
+  },
+};
diff --git a/src/content/dependencies/listAlbumsByTracks.js b/src/content/dependencies/listAlbumsByTracks.js
new file mode 100644
index 00000000..acec5da4
--- /dev/null
+++ b/src/content/dependencies/listAlbumsByTracks.js
@@ -0,0 +1,53 @@
+import {empty, stitchArrays} from '../../util/sugar.js';
+
+export default {
+  contentDependencies: ['generateListingPage', 'linkAlbum'],
+  extraDependencies: ['language', 'wikiData'],
+
+  sprawl({albumData}) {
+    return {albumData};
+  },
+
+  query({albumData}, spec) {
+    return {
+      spec,
+
+      albums:
+        albumData
+          .filter(album => !empty(album.tracks))
+          .sort((a, b) => b.tracks.length - a.tracks.length),
+    };
+  },
+
+  relations(relation, query) {
+    return {
+      page: relation('generateListingPage', query.spec),
+
+      albumLinks:
+        query.albums
+          .map(album => relation('linkAlbum', album)),
+    };
+  },
+
+  data(query) {
+    return {
+      counts:
+        query.albums
+          .map(album => album.tracks.length),
+    };
+  },
+
+  generate(data, relations, {language}) {
+    return relations.page.slots({
+      type: 'rows',
+      rows:
+        stitchArrays({
+          link: relations.albumLinks,
+          count: data.counts,
+        }).map(({link, count}) => ({
+            album: link,
+            tracks: language.countTracks(count, {unit: true}),
+          })),
+    });
+  },
+};
diff --git a/src/listing-spec.js b/src/listing-spec.js
index 9c1134f4..e9be40cd 100644
--- a/src/listing-spec.js
+++ b/src/listing-spec.js
@@ -31,56 +31,23 @@ listingSpec.push({
 listingSpec.push({
   directory: 'albums/by-tracks',
   stringsKey: 'listAlbums.byTracks',
-
-  data: ({wikiData: {albumData}}) =>
-    albumData.slice()
-      .sort((a, b) => b.tracks.length - a.tracks.length),
-
-  row: (album, {language, link}) =>
-    language.$('listingPage.listAlbums.byTracks.item', {
-      album: link.album(album),
-      tracks: language.countTracks(album.tracks.length, {unit: true}),
-    }),
+  contentFunction: 'listAlbumsByTracks',
 });
 
 listingSpec.push({
   directory: 'albums/by-duration',
   stringsKey: 'listAlbums.byDuration',
-
-  data: ({wikiData: {albumData}}) =>
-    albumData
-      .map(album => ({
-        album,
-        duration: getTotalDuration(album.tracks),
-      }))
-      .filter(({duration}) => duration > 0)
-      .sort((a, b) => b.duration - a.duration),
-
-  row: ({album, duration}, {language, link}) =>
-    language.$('listingPage.listAlbums.byDuration.item', {
-      album: link.album(album),
-      duration: language.formatDuration(duration),
-    }),
+  contentFunction: 'listAlbumsByDuration',
 });
 
 listingSpec.push({
   directory: 'albums/by-date',
   stringsKey: 'listAlbums.byDate',
+  contentFunction: 'listAlbumsByDate',
 
   seeAlso: [
     'tracks/by-date',
   ],
-
-  data: ({wikiData: {albumData}}) =>
-    sortChronologically(
-      albumData
-        .filter(album => album.date)),
-
-  row: (album, {language, link}) =>
-    language.$('listingPage.listAlbums.byDate.item', {
-      album: link.album(album),
-      date: language.formatDate(album.date),
-    }),
 });
 
 listingSpec.push({
@@ -1103,6 +1070,8 @@ listingSpec.push({
       if (!empty(suberrors)) {
         errors.push(new AggregateError(suberrors, `Errors matching "see also" listings for ${listing.directory}`));
       }
+    } else {
+      listing.seeAlso = null;
     }
   }