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
|
import {empty, filterMultipleArrays, stitchArrays} from '#sugar';
export default {
contentDependencies: [
'generateContentHeading',
'generateTrackList',
'linkGroup',
],
extraDependencies: ['html', 'language'],
query(tracks, dividingGroups) {
const groupings = new Map();
const ungroupedTracks = [];
// Entry order matters! Add blank lists for each group
// in the order that those groups are provided.
for (const group of dividingGroups) {
groupings.set(group, []);
}
for (const track of tracks) {
const firstMatchingGroup =
dividingGroups.find(group => group.albums.includes(track.album));
if (firstMatchingGroup) {
groupings.get(firstMatchingGroup).push(track);
} else {
ungroupedTracks.push(track);
}
}
const groups = Array.from(groupings.keys());
const groupedTracks = Array.from(groupings.values());
// Drop the empty lists, so just the groups which
// at least a single track matched are left.
filterMultipleArrays(
groups,
groupedTracks,
(_group, tracks) => !empty(tracks));
return {groups, groupedTracks, ungroupedTracks};
},
relations: (relation, query, tracks, groups) => ({
flatList:
(empty(groups)
? relation('generateTrackList', tracks)
: null),
contentHeading:
relation('generateContentHeading'),
groupLinks:
query.groups
.map(group => relation('linkGroup', group)),
groupedTrackLists:
query.groupedTracks
.map(tracks => relation('generateTrackList', tracks)),
ungroupedTrackList:
(empty(query.ungroupedTracks)
? null
: relation('generateTrackList', query.ungroupedTracks)),
}),
data: (query) => ({
groupNames:
query.groups
.map(group => group.name),
}),
slots: {
headingString: {
type: 'string',
},
},
generate: (data, relations, slots, {html, language}) =>
relations.flatList ??
html.tag('dl', {[html.onlyIfContent]: true}, [
stitchArrays({
groupName: data.groupNames,
groupLink: relations.groupLinks,
trackList: relations.groupedTrackLists,
}).map(({
groupName,
groupLink,
trackList,
}) => [
(slots.headingString
? relations.contentHeading.clone().slots({
tag: 'dt',
title:
language.$('trackList.fromGroup', {
group: groupLink
}),
stickyTitle:
language.$(slots.headingString, 'sticky', 'fromGroup', {
group: groupName,
}),
})
: html.tag('dt',
language.$('trackList.fromGroup', {
group: groupLink
}))),
html.tag('dd', trackList),
]),
relations.ungroupedTrackList && [
(slots.headingString
? relations.contentHeading.clone().slots({
tag: 'dt',
title:
language.$('trackList.fromOther'),
stickyTitle:
language.$(slots.headingString, 'sticky', 'fromOther'),
})
: html.tag('dt',
language.$('trackList.fromOther'))),
html.tag('dd', relations.ungroupedTrackList),
],
]),
};
|