« get me outta code hell

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:
Diffstat (limited to 'src/content')
-rw-r--r--src/content/dependencies/generateAlbumTrackList.js126
-rw-r--r--src/content/dependencies/linkTrack.js8
2 files changed, 134 insertions, 0 deletions
diff --git a/src/content/dependencies/generateAlbumTrackList.js b/src/content/dependencies/generateAlbumTrackList.js
new file mode 100644
index 00000000..a2b03049
--- /dev/null
+++ b/src/content/dependencies/generateAlbumTrackList.js
@@ -0,0 +1,126 @@
+import {accumulateSum, empty} from '../../util/sugar.js';
+
+function displayTrackSections(album) {
+  if (empty(album.trackSections)) {
+    return false;
+  }
+
+  if (album.trackSections.length > 1) {
+    return true;
+  }
+
+  if (!album.trackSections[0].isDefaultTrackSection) {
+    return true;
+  }
+
+  return false;
+}
+
+function displayTracks(album) {
+  if (empty(album.tracks)) {
+    return false;
+  }
+
+  return true;
+}
+
+function getDisplayMode(album) {
+  if (displayTrackSections(album)) {
+    return 'trackSections';
+  } else if (displayTracks(album)) {
+    return 'tracks';
+  } else {
+    return 'none';
+  }
+}
+
+export default {
+  contentDependencies: [
+    'generateAlbumTrackListItem',
+  ],
+
+  extraDependencies: [
+    'html',
+    'language',
+  ],
+
+  relations(relation, album) {
+    const relations = {};
+
+    const displayMode = getDisplayMode(album);
+
+    if (displayMode === 'trackSections') {
+      relations.itemsByTrackSection =
+        album.trackSections.map(section =>
+          section.tracks.map(track =>
+            relation('generateAlbumTrackListItem', track, album)));
+    }
+
+    if (displayMode === 'tracks') {
+      relations.itemsByTrack =
+        album.tracks.map(track =>
+          relation('generateAlbumTrackListItem', track, album));
+    }
+
+    return relations;
+  },
+
+  data(album) {
+    const data = {};
+
+    data.hasTrackNumbers = album.hasTrackNumbers;
+
+    if (displayTrackSections && !empty(album.trackSections)) {
+      data.trackSectionInfo =
+        album.trackSections.map(section => {
+          const info = {};
+
+          info.name = section.name;
+          info.duration = accumulateSum(section.tracks, track => track.duration);
+          info.durationApproximate = section.tracks.length > 1;
+
+          if (album.hasTrackNumbers) {
+            info.startIndex = section.startIndex;
+          }
+
+          return info;
+        });
+    }
+
+    return data;
+  },
+
+  generate(data, relations, {
+    html,
+    language,
+  }) {
+    const listTag = (data.hasTrackNumbers ? 'ol' : 'ul');
+
+    if (relations.itemsByTrackSection) {
+      return html.tag('dl',
+        {class: 'album-group-list'},
+        data.trackSectionInfo.map((info, index) => [
+          html.tag('dt',
+            {class: 'content-heading'},
+            language.$('trackList.section.withDuration', {
+              section: info.name,
+              duration:
+                language.formatDuration(info.duration, {
+                  approximate: info.durationApproximate,
+                }),
+            })),
+
+          html.tag('dd',
+            html.tag(listTag,
+              data.hasTrackNumbers ? {start: info.startIndex + 1} : {},
+              relations.itemsByTrackSection[index])),
+        ]));
+    }
+
+    if (relations.itemsByTrack) {
+      return html.tag(listTag, relations.itemsByTrack);
+    }
+
+    return [];
+  }
+};
diff --git a/src/content/dependencies/linkTrack.js b/src/content/dependencies/linkTrack.js
new file mode 100644
index 00000000..d5d96726
--- /dev/null
+++ b/src/content/dependencies/linkTrack.js
@@ -0,0 +1,8 @@
+export default {
+  contentDependencies: ['linkThing'],
+
+  relations: (relation, track) =>
+    ({link: relation('linkThing', 'localized.track', track)}),
+
+  generate: (relations) => relations.link,
+};