« 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/data/composite/things/album/withTracks.js19
-rw-r--r--src/data/things/album.js80
2 files changed, 76 insertions, 23 deletions
diff --git a/src/data/composite/things/album/withTracks.js b/src/data/composite/things/album/withTracks.js
index 05f5b24d..3fe6dd2e 100644
--- a/src/data/composite/things/album/withTracks.js
+++ b/src/data/composite/things/album/withTracks.js
@@ -1,8 +1,10 @@
 import {input, templateCompositeFrom} from '#composite';
 
-import {exitWithoutDependency, raiseOutputWithoutDependency}
-  from '#composite/control-flow';
+import find from '#find';
+
+import {exitWithoutDependency} from '#composite/control-flow';
 import {withFlattenedList, withPropertyFromList} from '#composite/data';
+import {withResolvedReferenceList} from '#composite/wiki-data';
 
 export default templateCompositeFrom({
   annotation: `withTracks`,
@@ -10,15 +12,16 @@ export default templateCompositeFrom({
   outputs: ['#tracks'],
 
   steps: () => [
-    raiseOutputWithoutDependency({
-      dependency: 'trackSections',
-      output: input.value({
-        '#tracks': [],
-      }),
+    withResolvedReferenceList({
+      list: 'trackSections',
+      data: 'ownTrackSectionData',
+      find: input.value(find.unqualifiedTrackSection),
+    }).outputs({
+      ['#resolvedReferenceList']: '#trackSections',
     }),
 
     withPropertyFromList({
-      list: 'trackSections',
+      list: '#trackSections',
       property: input.value('tracks'),
     }),
 
diff --git a/src/data/things/album.js b/src/data/things/album.js
index 78ecb294..253c6641 100644
--- a/src/data/things/album.js
+++ b/src/data/things/album.js
@@ -121,17 +121,11 @@ export class Album extends Thing {
     commentary: commentary(),
     additionalFiles: additionalFiles(),
 
-    trackSections: {
-      flags: {update: true, expose: true},
-
-      update: {
-        validate:
-          validateWikiData({
-            referenceType:
-              TrackSection[Thing.referenceType],
-          }),
-      },
-    },
+    trackSections: referenceList({
+      referenceType: input.value('unqualified-track-section'),
+      data: 'ownTrackSectionData',
+      find: input.value(find.unqualifiedTrackSection),
+    }),
 
     artistContribs: contributionList(),
     coverArtistContribs: contributionList(),
@@ -172,6 +166,10 @@ export class Album extends Thing {
       class: input.value(Group),
     }),
 
+    ownTrackSectionData: wikiData({
+      class: input.value(TrackSection),
+    }),
+
     // Expose only
 
     commentatorArtists: commentatorArtists(),
@@ -360,6 +358,7 @@ export class Album extends Thing {
 
     save(results) {
       const albumData = [];
+      const trackSectionData = [];
       const trackData = [];
 
       for (const {header: album, entries} of results) {
@@ -376,7 +375,10 @@ export class Album extends Thing {
         const albumRef = Thing.getReference(album);
 
         const closeCurrentTrackSection = () => {
-          if (empty(currentTrackSectionTracks)) {
+          if (
+            currentTrackSection.isDefaultTrackSection &&
+            empty(currentTrackSectionTracks)
+          ) {
             return;
           }
 
@@ -391,6 +393,7 @@ export class Album extends Thing {
             [album];
 
           trackSections.push(currentTrackSection);
+          trackSectionData.push(currentTrackSection);
         };
 
         for (const entry of entries) {
@@ -402,7 +405,6 @@ export class Album extends Thing {
           }
 
           currentTrackSectionTracks.push(entry);
-
           trackData.push(entry);
 
           entry.dataSourceAlbum = albumRef;
@@ -412,10 +414,16 @@ export class Album extends Thing {
 
         albumData.push(album);
 
-        album.trackSections = trackSections;
+        album.trackSections =
+          trackSections
+            .map(trackSection =>
+              `unqualified-track-section:` +
+              trackSection.unqualifiedDirectory);
+
+        album.ownTrackSectionData = trackSections;
       }
 
-      return {albumData, trackData};
+      return {albumData, trackSectionData, trackData};
     },
 
     sort({albumData, trackData}) {
@@ -478,6 +486,34 @@ export class TrackSection extends Thing {
 
     // Expose only
 
+    directory: [
+      withAlbum(),
+
+      exitWithoutDependency({
+        dependency: '#album',
+      }),
+
+      withPropertyFromObject({
+        object: '#album',
+        property: input.value('directory'),
+      }),
+
+      withDirectory({
+        directory: 'unqualifiedDirectory',
+      }).outputs({
+        '#directory': '#unqualifiedDirectory',
+      }),
+
+      {
+        dependencies: ['#album.directory', '#unqualifiedDirectory'],
+        compute: ({
+          ['#album.directory']: albumDirectory,
+          ['#unqualifiedDirectory']: unqualifiedDirectory,
+        }) =>
+          albumDirectory + '/' + unqualifiedDirectory,
+      },
+    ],
+
     startIndex: [
       withAlbum(),
 
@@ -517,6 +553,20 @@ export class TrackSection extends Thing {
     ],
   });
 
+  static [Thing.findSpecs] = {
+    trackSection: {
+      referenceTypes: ['track-section'],
+      bindTo: 'trackSectionData',
+    },
+
+    unqualifiedTrackSection: {
+      referenceTypes: ['unqualified-track-section'],
+
+      getMatchableDirectories: trackSection =>
+        [trackSection.unqualifiedDirectory],
+    },
+  };
+
   static [Thing.yamlDocumentSpec] = {
     fields: {
       'Section': {property: 'name'},