« get me outta code hell

Merge branch 'preview' of github.com:hsmusic/hsmusic-wiki into preview - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/album.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-01-15 22:06:41 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-01-15 22:06:41 -0400
commitf8a38da3d747a15bd25e2759b819c1b83aaf0ba1 (patch)
treefc1b24bb02a6eabd610a022ae7cd6b72f5d1ad84 /src/data/things/album.js
parent8494805e994dd165cc8426723e799f06c8cbefcf (diff)
parent62dbb79342927cb1fe19651e33fcc27bc2804f6e (diff)
Merge branch 'preview' of github.com:hsmusic/hsmusic-wiki into preview
Diffstat (limited to 'src/data/things/album.js')
-rw-r--r--src/data/things/album.js113
1 files changed, 39 insertions, 74 deletions
diff --git a/src/data/things/album.js b/src/data/things/album.js
index ade34e3d..0c0c7fb4 100644
--- a/src/data/things/album.js
+++ b/src/data/things/album.js
@@ -10,13 +10,11 @@ export class Album extends Thing {
     Artist,
     Group,
     Track,
-    TrackGroup,
 
     validators: {
       isDate,
       isDimensions,
-      validateArrayItems,
-      validateInstanceOf,
+      isTrackSectionList,
     },
   }) => ({
     // Update & expose
@@ -56,11 +54,39 @@ export class Album extends Thing {
     groupsByRef: Thing.common.referenceList(Group),
     artTagsByRef: Thing.common.referenceList(ArtTag),
 
-    trackGroups: {
+    trackSections: {
       flags: {update: true, expose: true},
 
       update: {
-        validate: validateArrayItems(validateInstanceOf(TrackGroup)),
+        validate: isTrackSectionList,
+      },
+
+      expose: {
+        dependencies: ['color', 'trackData'],
+        transform(trackSections, {
+          color: albumColor,
+          trackData,
+        }) {
+          let startIndex = 0;
+          return trackSections?.map(section => ({
+            name: section.name ?? null,
+            color: section.color ?? albumColor ?? null,
+            dateOriginallyReleased: section.dateOriginallyReleased ?? null,
+            isDefaultTrackSection: section.isDefaultTrackSection ?? false,
+
+            startIndex: (
+              startIndex += section.tracksByRef.length,
+              startIndex - section.tracksByRef.length
+            ),
+
+            tracksByRef: section.tracksByRef ?? [],
+            tracks:
+              (trackData && section.tracksByRef
+                ?.map(ref => find.track(ref, trackData, {mode: 'quiet'}))
+                .filter(Boolean)) ??
+              [],
+          }));
+        },
       },
     },
 
@@ -114,11 +140,11 @@ export class Album extends Thing {
       flags: {expose: true},
 
       expose: {
-        dependencies: ['trackGroups', 'trackData'],
-        compute: ({trackGroups, trackData}) =>
-          trackGroups && trackData
-            ? trackGroups
-                .flatMap((group) => group.tracksByRef ?? [])
+        dependencies: ['trackSections', 'trackData'],
+        compute: ({trackSections, trackData}) =>
+          trackSections && trackData
+            ? trackSections
+                .flatMap((section) => section.tracksByRef ?? [])
                 .map((ref) => find.track(ref, trackData, {mode: 'quiet'}))
                 .filter(Boolean)
             : [],
@@ -179,72 +205,11 @@ export class Album extends Thing {
   });
 }
 
-export class TrackGroup extends Thing {
-  static [Thing.getPropertyDescriptors] = ({
-    isColor,
-    Track,
-
-    validators: {
-      validateInstanceOf,
-    },
-  }) => ({
-    // Update & expose
-
+export class TrackSectionHelper extends Thing {
+  static [Thing.getPropertyDescriptors] = () => ({
     name: Thing.common.name('Unnamed Track Group'),
-
-    color: {
-      flags: {update: true, expose: true},
-
-      update: {validate: isColor},
-
-      expose: {
-        dependencies: ['album'],
-
-        transform(color, {album}) {
-          return color ?? album?.color ?? null;
-        },
-      },
-    },
-
+    color: Thing.common.color(),
     dateOriginallyReleased: Thing.common.simpleDate(),
-
-    tracksByRef: Thing.common.referenceList(Track),
-
     isDefaultTrackGroup: Thing.common.flag(false),
-
-    // Update only
-
-    album: {
-      flags: {update: true},
-      update: {validate: validateInstanceOf(Album)},
-    },
-
-    trackData: Thing.common.wikiData(Track),
-
-    // Expose only
-
-    tracks: {
-      flags: {expose: true},
-
-      expose: {
-        dependencies: ['tracksByRef', 'trackData'],
-        compute: ({tracksByRef, trackData}) =>
-          tracksByRef && trackData
-            ? tracksByRef.map((ref) => find.track(ref, trackData)).filter(Boolean)
-            : [],
-      },
-    },
-
-    startIndex: {
-      flags: {expose: true},
-
-      expose: {
-        dependencies: ['album'],
-        compute: ({album, [TrackGroup.instance]: trackGroup}) =>
-          album.trackGroups
-            .slice(0, album.trackGroups.indexOf(trackGroup))
-            .reduce((acc, tg) => acc + tg.tracks.length, 0),
-      },
-    },
   })
 }