« get me outta code hell

homepage-layout.js « things « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/homepage-layout.js
blob: 5948ff46458c67a83cc6a0c3227934cb98fd06ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import Thing from './thing.js';

import find from '../../util/find.js';

export class HomepageLayout extends Thing {
  static [Thing.getPropertyDescriptors] = ({
    HomepageLayoutRow,

    validators: {
      validateArrayItems,
      validateInstanceOf,
    },
  }) => ({
    // Update & expose

    sidebarContent: Thing.common.simpleString(),

    rows: {
      flags: {update: true, expose: true},

      update: {
        validate: validateArrayItems(validateInstanceOf(HomepageLayoutRow)),
      },
    },
  })
}

export class HomepageLayoutRow extends Thing {
  static [Thing.getPropertyDescriptors] = ({
    Album,
    Group,
  }) => ({
    // Update & expose

    name: Thing.common.name('Unnamed Homepage Row'),

    type: {
      flags: {update: true, expose: true},

      update: {
        validate() {
          throw new Error(`'type' property validator must be overridden`);
        },
      },
    },

    color: Thing.common.color(),

    // Update only

    // These aren't necessarily used by every HomepageLayoutRow subclass, but
    // for convenience of providing this data, every row accepts all wiki data
    // arrays depended upon by any subclass's behavior.
    albumData: Thing.common.wikiData(Album),
    groupData: Thing.common.wikiData(Group),
  });
}

export class HomepageLayoutAlbumsRow extends HomepageLayoutRow {
  static [Thing.getPropertyDescriptors] = (opts, {
    Album,
    Group,

    validators: {
      isCountingNumber,
      isString,
      validateArrayItems,
    },
  } = opts) => ({
    ...HomepageLayoutRow[Thing.getPropertyDescriptors](opts),

    // Update & expose

    type: {
      flags: {update: true, expose: true},
      update: {
        validate(value) {
          if (value !== 'albums') {
            throw new TypeError(`Expected 'albums'`);
          }

          return true;
        },
      },
    },

    sourceGroupByRef: Thing.common.singleReference(Group),
    sourceAlbumsByRef: Thing.common.referenceList(Album),

    countAlbumsFromGroup: {
      flags: {update: true, expose: true},
      update: {validate: isCountingNumber},
    },

    actionLinks: {
      flags: {update: true, expose: true},
      update: {validate: validateArrayItems(isString)},
    },

    // Expose only

    sourceGroup: Thing.common.dynamicThingFromSingleReference(
      'sourceGroupByRef',
      'groupData',
      find.group
    ),

    sourceAlbums: Thing.common.dynamicThingsFromReferenceList(
      'sourceAlbumsByRef',
      'albumData',
      find.album
    ),
  });
}