« 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/generateAlbumSidebarGroupBox.js68
-rw-r--r--src/content/dependencies/generateAlbumTrackList.js103
2 files changed, 141 insertions, 30 deletions
diff --git a/src/content/dependencies/generateAlbumSidebarGroupBox.js b/src/content/dependencies/generateAlbumSidebarGroupBox.js
index 4e46c931..b3ee0abe 100644
--- a/src/content/dependencies/generateAlbumSidebarGroupBox.js
+++ b/src/content/dependencies/generateAlbumSidebarGroupBox.js
@@ -5,45 +5,53 @@ export default {
   extraDependencies: ['html', 'language', 'transformMultiline'],
 
   contracts: {
-    relations(contract, [album, group]) {
-      contract.provide({
-        group, album,
+    relations: {
+      hook(contract, [relation, album, group]) {
+        contract.provide({
+          group, album,
+
+          urls: contract.selectProperty(group, 'urls'),
+          adjacentAlbums: contract.subcontract('adjacentAlbumsInGroup', album, group),
+        });
+      },
 
-        urls: contract.selectProperty(group, 'urls'),
-        adjacentAlbums: contract.subcontract('adjacentAlbumsInGroup', album, group),
-      });
-    },
-  },
+      compute({relation, group, album, urls, adjacentAlbums}) {
+        const relations = {};
 
-  relations(relation, {group, album, urls, adjacentAlbums}) {
-    const relations = {};
+        relations.groupLink =
+          relation('linkGroup', group);
 
-    relations.groupLink =
-      relation('linkGroup', group);
+        relations.externalLinks =
+          urls.map(url =>
+            relation('linkExternal', urls));
 
-    relations.externalLinks =
-      urls.map(url =>
-        relation('linkExternal', urls));
+        const {previousAlbum, nextAlbum} = adjacentAlbums;
 
-    const {previousAlbum, nextAlbum} = adjacentAlbums;
+        if (previousAlbum) {
+          relations.previousAlbumLink =
+            relation('linkAlbum', previousAlbum);
+        }
 
-    if (previousAlbum) {
-      relations.previousAlbumLink =
-        relation('linkAlbum', previousAlbum);
-    }
+        if (nextAlbum) {
+          relations.nextAlbumLink =
+            relation('linkAlbum', nextAlbum);
+        }
 
-    if (nextAlbum) {
-      relations.nextAlbumLink =
-        relation('linkAlbum', nextAlbum);
-    }
+        return relations;
+      },
+    },
 
-    return relations;
-  },
+    data: {
+      hook(contract, [album, group]) {
+        contract.provide({
+          description: contract.selectProperty(group, 'descriptionShort'),
+        });
+      },
 
-  data(album, group) {
-    return {
-      description: group.descriptionShort,
-    };
+      compute({description}) {
+        return {description};
+      },
+    },
   },
 
   generate(data, relations, {html, language, transformMultiline}) {
diff --git a/src/content/dependencies/generateAlbumTrackList.js b/src/content/dependencies/generateAlbumTrackList.js
index f2f2279d..a0fad460 100644
--- a/src/content/dependencies/generateAlbumTrackList.js
+++ b/src/content/dependencies/generateAlbumTrackList.js
@@ -44,6 +44,109 @@ export default {
     'language',
   ],
 
+  contracts: {
+    length: {
+      hook(contract, [array]) {
+        contract.provide({
+          length: contract.selectProperty(array, 'length'),
+        });
+      },
+
+      compute({length}) {
+        return length;
+      },
+    },
+
+    isDefault: {
+      hook(contract, [trackSection]) {
+        contract.provide({
+          isDefault: contract.selectProperty(trackSection, 'isDefaultTrackSection', false),
+        });
+      },
+
+      compute({isDefault}) {
+        return isDefault;
+      },
+    },
+
+    firstIsDefault: {
+      hook(contract, [trackSections]) {
+        contract.provide({
+          isDefault: contract.subcontract('#isDefault', contract.selectProperty(trackSections, '0')),
+        });
+      },
+
+      compute({isDefault}) {
+        return isDefault;
+      },
+    },
+
+    displayTrackSections: {
+      hook(contract, [album]) {
+        contract.provide({
+          numTrackSections: contract.subcontract('#length', contract.selectProperty(album, 'trackSections')),
+          firstIsDefault: contract.subcontract('#firstIsDefault', contract.selectProperty(album, 'trackSections')),
+        });
+      },
+
+      compute({numTrackSections, firstIsDefault}) {
+        return numTrackSections >= 2 || firstIsDefault;
+      },
+    },
+
+    displayTracks: {
+      hook(contract, [album]) {
+        contract.provide({
+          numTracks: contract.subcontract('#length', contract.selectProperty(album, 'tracks')),
+        });
+      },
+
+      compute({numTracks}) {
+        return numTracks >= 1;
+      },
+    },
+
+    displayMode: {
+      hook(contract, [album]) {
+        contract.provide({
+          displayTrackSections: contract.subcontract('#displayTrackSections', album),
+          displayTracks: contract.subcontract('#displayTracks', album),
+        });
+      },
+
+      compute({displayTrackSections, displayTracks}) {
+        if (displayTrackSections) {
+          return 'trackSections';
+        } else if (displayTracks) {
+          return 'tracks';
+        } else {
+          return 'none';
+        }
+      },
+    },
+
+    relations: {
+      hook(contract, [relation, album]) {
+        contract.branch({
+          subcontract: ['#displayMode', album],
+          branches: {
+            trackSections() {
+              contract.provide({
+                trackSections: contract.selectProperty(album, 'trackSections'),
+              });
+            },
+
+            tracks() {
+              contract.provide({
+                tracks: contract.selectProperty(album, 'tracks'),
+              });
+            },
+          },
+        });
+      },
+    },
+  },
+
   relations(relation, album) {
     const relations = {};