« get me outta code hell

generateArtTagSidebar.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateArtTagSidebar.js
blob: f6787a8c6782de99c1f30911a5af94931b83ddc2 (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
import {empty, stitchArrays} from '#sugar';
import {collectTreeLeaves} from '#wiki-data';

export default {
  contentDependencies: [
    'generateArtTagAncestorDescendantMapList',
    'linkArtTagDynamically',
  ],

  extraDependencies: ['html', 'language', 'wikiData'],

  sprawl: ({artTagData}) =>
    ({artTagData}),

  query(sprawl, artTag) {
    const baobab = artTag.ancestorArtTagBaobabTree;
    const uniqueLeaves = new Set(collectTreeLeaves(baobab));

    // Just match the order in tag data.
    const furthestAncestorArtTags =
      sprawl.artTagData
        .filter(artTag => uniqueLeaves.has(artTag));

    return {furthestAncestorArtTags};
  },

  relations: (relation, query, sprawl, artTag) => ({
    artTagLink:
      relation('linkArtTagDynamically', artTag),

    directDescendantArtTagLinks:
      artTag.directDescendantArtTags
        .map(descendantArtTag =>
          relation('linkArtTagDynamically', descendantArtTag)),

    furthestAncestorArtTagMapLists:
      query.furthestAncestorArtTags
        .map(ancestorArtTag =>
          relation('generateArtTagAncestorDescendantMapList',
            ancestorArtTag,
            artTag)),
  }),

  data: (query, sprawl, artTag) => ({
    name: artTag.name,

    furthestAncestorArtTagNames:
      query.furthestAncestorArtTags
        .map(ancestorArtTag => ancestorArtTag.name),
  }),

  generate: (data, relations, {html, language}) => ({
    leftSidebarContent: [
      html.tag('h1',
        relations.artTagLink),

      !empty(relations.directDescendantArtTagLinks) &&
        html.tag('details', {class: 'current', open: true}, [
          html.tag('summary',
            html.tag('span', {class: 'group-name'},
              language.sanitize(data.name))),

          html.tag('ul',
            relations.directDescendantArtTagLinks
              .map(link => html.tag('li', link))),
        ]),

      stitchArrays({
        name: data.furthestAncestorArtTagNames,
        list: relations.furthestAncestorArtTagMapLists,
      }).map(({name, list}) =>
          html.tag('details',
            {
              class: 'has-tree-list',
              open:
                empty(relations.directDescendantArtTagLinks) &&
                relations.furthestAncestorArtTagMapLists.length === 1,
            },
            [
              html.tag('summary',
                html.tag('span', {class: 'group-name'},
                  language.sanitize(name))),

              list,
            ])),
    ],
  }),
};