« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/sorting-rule.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/things/sorting-rule.js')
-rw-r--r--src/data/things/sorting-rule.js75
1 files changed, 46 insertions, 29 deletions
diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js
index 88f37719..8d1c61b9 100644
--- a/src/data/things/sorting-rule.js
+++ b/src/data/things/sorting-rule.js
@@ -46,18 +46,10 @@ export class SortingRule extends Thing {
   });
 }
 
-export class DocumentSortingRule extends SortingRule {
+export class ThingSortingRule extends SortingRule {
   static [Thing.getPropertyDescriptors] = () => ({
     // Update & expose
 
-    // TODO: glob :plead:
-    filename: {
-      flags: {update: true, expose: true},
-      update: {
-        validate: isStringNonEmpty,
-      },
-    },
-
     properties: {
       flags: {update: true, expose: true},
       update: {
@@ -68,30 +60,11 @@ export class DocumentSortingRule extends SortingRule {
 
   static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(SortingRule, {
     fields: {
-      'Sort Documents': {property: 'filename'},
       'By Properties': {property: 'properties'},
     },
   });
 
-  apply(layout) {
-    const fresh = {...layout};
-
-    let sortable = null;
-    switch (fresh.documentMode) {
-      case documentModes.headerAndEntries:
-        sortable = fresh.entryThings =
-          fresh.entryThings.slice();
-        break;
-
-      case documentModes.allInOne:
-        sortable = fresh.things =
-          fresh.things.slice();
-        break;
-
-      default:
-        throw new Error(`Invalid document type for sorting`);
-    }
-
+  sort(sortable) {
     if (this.properties) {
       for (const property of this.properties.slice().reverse()) {
         const get = thing => thing[property];
@@ -134,6 +107,50 @@ export class DocumentSortingRule extends SortingRule {
       }
     }
 
+    return sortable;
+  }
+}
+
+export class DocumentSortingRule extends ThingSortingRule {
+  static [Thing.getPropertyDescriptors] = () => ({
+    // Update & expose
+
+    // TODO: glob :plead:
+    filename: {
+      flags: {update: true, expose: true},
+      update: {
+        validate: isStringNonEmpty,
+      },
+    },
+  });
+
+  static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(ThingSortingRule, {
+    fields: {
+      'Sort Documents': {property: 'filename'},
+    },
+  });
+
+  apply(layout) {
+    const fresh = {...layout};
+
+    let sortable = null;
+    switch (fresh.documentMode) {
+      case documentModes.headerAndEntries:
+        sortable = fresh.entryThings =
+          fresh.entryThings.slice();
+        break;
+
+      case documentModes.allInOne:
+        sortable = fresh.things =
+          fresh.things.slice();
+        break;
+
+      default:
+        throw new Error(`Invalid document type for sorting`);
+    }
+
+    this.sort(sortable);
+
     return fresh;
   }
 }