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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
export const HOMEPAGE_LAYOUT_DATA_FILE = 'homepage.yaml';
import {inspect} from 'node:util';
import {colors} from '#cli';
import {input} from '#composite';
import Thing from '#thing';
import {empty} from '#sugar';
import {
anyOf,
is,
isCountingNumber,
isString,
isStringNonEmpty,
validateArrayItems,
validateReference,
} from '#validators';
import {exposeDependency} from '#composite/control-flow';
import {withResolvedReference} from '#composite/wiki-data';
import {
color,
contentString,
name,
referenceList,
soupyFind,
thing,
thingList,
} from '#composite/wiki-properties';
export class HomepageLayout extends Thing {
static [Thing.friendlyName] = `Homepage Layout`;
static [Thing.getPropertyDescriptors] = ({HomepageLayoutSection}) => ({
// Update & expose
sidebarContent: contentString(),
navbarLinks: {
flags: {update: true, expose: true},
update: {validate: validateArrayItems(isStringNonEmpty)},
},
sections: thingList({
class: input.value(HomepageLayoutSection),
}),
});
static [Thing.yamlDocumentSpec] = {
fields: {
'Homepage': {ignore: true},
'Sidebar Content': {property: 'sidebarContent'},
'Navbar Links': {property: 'navbarLinks'},
},
};
static [Thing.getYamlLoadingSpec] = ({
documentModes: {allInOne},
thingConstructors: {
HomepageLayout,
HomepageLayoutSection,
HomepageLayoutAlbumsRow,
},
}) => ({
title: `Process homepage layout file`,
file: HOMEPAGE_LAYOUT_DATA_FILE,
documentMode: allInOne,
documentThing: document => {
if (document['Homepage']) {
return HomepageLayout;
}
if (document['Section']) {
return HomepageLayoutSection;
}
if (document['Row']) {
switch (document['Row']) {
case 'actions':
return HomepageLayoutActionsRow;
case 'album carousel':
return HomepageLayoutAlbumCarouselRow;
case 'album grid':
return HomepageLayoutAlbumGridRow;
default:
throw new TypeError(`Unrecognized row type ${document['Row']}`);
}
}
return null;
},
save(results) {
if (!empty(results) && !(results[0] instanceof HomepageLayout)) {
throw new Error(`Expected 'Homepage' document at top of homepage layout file`);
}
const homepageLayout = results[0];
const sections = [];
let currentSection = null;
let currentSectionRows = [];
const closeCurrentSection = () => {
if (currentSection) {
for (const row of currentSectionRows) {
row.section = currentSection;
}
currentSection.rows = currentSectionRows;
sections.push(currentSection);
currentSection = null;
currentSectionRows = [];
}
};
for (const entry of results.slice(1)) {
if (entry instanceof HomepageLayout) {
throw new Error(`Expected only one 'Homepage' document in total`);
} else if (entry instanceof HomepageLayoutSection) {
closeCurrentSection();
currentSection = entry;
} else if (entry instanceof HomepageLayoutRow) {
currentSectionRows.push(entry);
}
}
closeCurrentSection();
homepageLayout.sections = sections;
return {homepageLayout};
},
});
}
export class HomepageLayoutSection extends Thing {
static [Thing.friendlyName] = `Homepage Section`;
static [Thing.getPropertyDescriptors] = ({HomepageLayoutRow}) => ({
// Update & expose
name: name(`Unnamed Homepage Section`),
color: color(),
rows: thingList({
class: input.value(HomepageLayoutRow),
}),
});
static [Thing.yamlDocumentSpec] = {
fields: {
'Section': {property: 'name'},
'Color': {property: 'color'},
},
};
}
export class HomepageLayoutRow extends Thing {
static [Thing.friendlyName] = `Homepage Row`;
static [Thing.getPropertyDescriptors] = ({HomepageLayoutSection}) => ({
// Update & expose
section: thing({
class: input.value(HomepageLayoutSection),
}),
// Update only
find: soupyFind(),
// Expose only
type: {
flags: {expose: true},
expose: {
compute() {
throw new Error(`'type' property validator must be overridden`);
},
},
},
});
static [Thing.yamlDocumentSpec] = {
fields: {
'Row': {ignore: true},
},
};
[inspect.custom](depth) {
const parts = [];
parts.push(Thing.prototype[inspect.custom].apply(this));
if (depth >= 0 && this.section) {
const sectionName = this.section.name;
const index = this.section.rows.indexOf(this);
const rowNum =
(index === -1
? 'indeterminate position'
: `#${index + 1}`);
parts.push(` (${colors.yellow(rowNum)} in ${colors.green(sectionName)})`);
}
return parts.join('');
}
}
export class HomepageLayoutActionsRow extends HomepageLayoutRow {
static [Thing.friendlyName] = `Homepage Actions Row`;
static [Thing.getPropertyDescriptors] = (opts) => ({
...HomepageLayoutRow[Thing.getPropertyDescriptors](opts),
// Update & expose
actionLinks: {
flags: {update: true, expose: true},
update: {validate: validateArrayItems(isString)},
},
// Expose only
type: {
flags: {expose: true},
expose: {compute: () => 'actions'},
},
});
static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(HomepageLayoutRow, {
fields: {
'Actions': {property: 'actionLinks'},
},
});
}
export class HomepageLayoutAlbumCarouselRow extends HomepageLayoutRow {
static [Thing.friendlyName] = `Homepage Album Carousel Row`;
static [Thing.getPropertyDescriptors] = (opts, {Album, Group} = opts) => ({
...HomepageLayoutRow[Thing.getPropertyDescriptors](opts),
// Update & expose
albums: referenceList({
class: input.value(Album),
find: soupyFind.input('album'),
}),
// Expose only
type: {
flags: {expose: true},
expose: {compute: () => 'album carousel'},
},
});
static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(HomepageLayoutRow, {
fields: {
'Albums': {property: 'albums'},
},
});
}
export class HomepageLayoutAlbumGridRow extends HomepageLayoutRow {
static [Thing.friendlyName] = `Homepage Album Grid Row`;
static [Thing.getPropertyDescriptors] = (opts, {Album, Group} = opts) => ({
...HomepageLayoutRow[Thing.getPropertyDescriptors](opts),
// Update & expose
sourceGroup: [
{
flags: {expose: true, update: true, compose: true},
update: {
validate:
anyOf(
is('new-releases', 'new-additions'),
validateReference(Group[Thing.referenceType])),
},
expose: {
transform: (value, continuation) =>
(value === 'new-releases' || value === 'new-additions'
? value
: continuation(value)),
},
},
withResolvedReference({
ref: input.updateValue(),
find: soupyFind.input('group'),
}),
exposeDependency({dependency: '#resolvedReference'}),
],
sourceAlbums: referenceList({
class: input.value(Album),
find: soupyFind.input('album'),
}),
countAlbumsFromGroup: {
flags: {update: true, expose: true},
update: {validate: isCountingNumber},
},
// Expose only
type: {
flags: {expose: true},
expose: {compute: () => 'album grid'},
},
});
static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(HomepageLayoutRow, {
fields: {
'Group': {property: 'sourceGroup'},
'Count': {property: 'countAlbumsFromGroup'},
'Albums': {property: 'sourceAlbums'},
},
});
}
|