diff options
Diffstat (limited to 'src/data/things/group.js')
-rw-r--r-- | src/data/things/group.js | 170 |
1 files changed, 135 insertions, 35 deletions
diff --git a/src/data/things/group.js b/src/data/things/group.js index 26fe9a5..0dbbbb7 100644 --- a/src/data/things/group.js +++ b/src/data/things/group.js @@ -1,26 +1,47 @@ -import Thing from './thing.js'; - -import find from '../../util/find.js'; +export const GROUP_DATA_FILE = 'groups.yaml'; + +import {input} from '#composite'; +import find from '#find'; +import Thing from '#thing'; + +import { + color, + contentString, + directory, + name, + referenceList, + urls, + wikiData, +} from '#composite/wiki-properties'; export class Group extends Thing { static [Thing.referenceType] = 'group'; - static [Thing.getPropertyDescriptors] = ({ - Album, - }) => ({ + static [Thing.getPropertyDescriptors] = ({Album}) => ({ // Update & expose - name: Thing.common.name('Unnamed Group'), - directory: Thing.common.directory(), + name: name('Unnamed Group'), + directory: directory(), + + description: contentString(), - description: Thing.common.simpleString(), + urls: urls(), - urls: Thing.common.urls(), + featuredAlbums: referenceList({ + class: input.value(Album), + find: input.value(find.album), + data: 'albumData', + }), // Update only - albumData: Thing.common.wikiData(Album), - groupCategoryData: Thing.common.wikiData(GroupCategory), + albumData: wikiData({ + class: input.value(Album), + }), + + groupCategoryData: wikiData({ + class: input.value(GroupCategory), + }), // Expose only @@ -29,7 +50,10 @@ export class Group extends Thing { expose: { dependencies: ['description'], - compute: ({description}) => description.split('<hr class="split">')[0], + compute: ({description}) => + (description + ? description.split('<hr class="split">')[0] + : null), }, }, @@ -37,8 +61,8 @@ export class Group extends Thing { flags: {expose: true}, expose: { - dependencies: ['albumData'], - compute: ({albumData, [Group.instance]: group}) => + dependencies: ['this', 'albumData'], + compute: ({this: group, albumData}) => albumData?.filter((album) => album.groups.includes(group)) ?? [], }, }, @@ -47,9 +71,8 @@ export class Group extends Thing { flags: {expose: true}, expose: { - dependencies: ['groupCategoryData'], - - compute: ({groupCategoryData, [Group.instance]: group}) => + dependencies: ['this', 'groupCategoryData'], + compute: ({this: group, groupCategoryData}) => groupCategoryData.find((category) => category.groups.includes(group)) ?.color, }, @@ -59,36 +82,113 @@ export class Group extends Thing { flags: {expose: true}, expose: { - dependencies: ['groupCategoryData'], - compute: ({groupCategoryData, [Group.instance]: group}) => + dependencies: ['this', 'groupCategoryData'], + compute: ({this: group, groupCategoryData}) => groupCategoryData.find((category) => category.groups.includes(group)) ?? null, }, }, }); + + static [Thing.findSpecs] = { + group: { + referenceTypes: ['group', 'group-gallery'], + bindTo: 'groupData', + }, + }; + + static [Thing.yamlDocumentSpec] = { + fields: { + 'Group': {property: 'name'}, + 'Directory': {property: 'directory'}, + 'Description': {property: 'description'}, + 'URLs': {property: 'urls'}, + + 'Featured Albums': {property: 'featuredAlbums'}, + + 'Review Points': {ignore: true}, + }, + }; + + static [Thing.getYamlLoadingSpec] = ({ + documentModes: {allInOne}, + thingConstructors: {Group, GroupCategory}, + }) => ({ + title: `Process groups file`, + file: GROUP_DATA_FILE, + + documentMode: allInOne, + documentThing: document => + ('Category' in document + ? GroupCategory + : Group), + + save(results) { + let groupCategory; + let groupRefs = []; + + if (results[0] && !(results[0] instanceof GroupCategory)) { + throw new Error(`Expected a category at top of group data file`); + } + + for (const thing of results) { + if (thing instanceof GroupCategory) { + if (groupCategory) { + Object.assign(groupCategory, {groups: groupRefs}); + } + + groupCategory = thing; + groupRefs = []; + } else { + groupRefs.push(Thing.getReference(thing)); + } + } + + if (groupCategory) { + Object.assign(groupCategory, {groups: groupRefs}); + } + + const groupData = results.filter(x => x instanceof Group); + const groupCategoryData = results.filter(x => x instanceof GroupCategory); + + return {groupData, groupCategoryData}; + }, + + // Groups aren't sorted at all, always preserving the order in the data + // file as-is. + sort: null, + }); } export class GroupCategory extends Thing { - static [Thing.getPropertyDescriptors] = ({ - Group, - }) => ({ - // Update & expose + static [Thing.referenceType] = 'group-category'; + static [Thing.friendlyName] = `Group Category`; - name: Thing.common.name('Unnamed Group Category'), - color: Thing.common.color(), + static [Thing.getPropertyDescriptors] = ({Group}) => ({ + // Update & expose - groupsByRef: Thing.common.referenceList(Group), + name: name('Unnamed Group Category'), + directory: directory(), - // Update only + color: color(), - groupData: Thing.common.wikiData(Group), + groups: referenceList({ + class: input.value(Group), + find: input.value(find.group), + data: 'groupData', + }), - // Expose only + // Update only - groups: Thing.common.dynamicThingsFromReferenceList( - 'groupsByRef', - 'groupData', - find.group - ), + groupData: wikiData({ + class: input.value(Group), + }), }); + + static [Thing.yamlDocumentSpec] = { + fields: { + 'Category': {property: 'name'}, + 'Color': {property: 'color'}, + }, + }; } |