« get me outta code hell

generateIntrapageDotSwitcher.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/generateIntrapageDotSwitcher.js
blob: cd92b165ee44644f22fdeb7a74e1a795cae97372 (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
import {stitchArrays} from '#sugar';

export default {
  contentDependencies: ['generateDotSwitcherTemplate'],
  extraDependencies: ['html', 'language'],

  relations: (relation) => ({
    template:
      relation('generateDotSwitcherTemplate'),
  }),

  slots: {
    attributes: {
      type: 'attributes',
      mutable: false,
    },

    initialOptionIndex: {type: 'number'},

    titles: {
      validate: v => v.strictArrayOf(v.isHTML),
    },

    targetIDs: {
      validate: v => v.strictArrayOf(v.isString),
    },
  },

  generate: (relations, slots, {html, language}) =>
    relations.template.slots({
      attributes: [
        {class: 'intrapage'},
        slots.attributes,
      ],

      initialOptionIndex: slots.initialOptionIndex,

      options:
        stitchArrays({
          title: slots.titles,
          targetID: slots.targetIDs,
        }).map(({title, targetID}) => {
            const {content} = html.smush(title);

            const customCue =
              content.find(item =>
                item?.tagName === 'span' &&
                item.attributes.has('class', 'dot-switcher-interaction-cue'));

            const cue =
              (customCue && !html.isBlank(customCue)
                ? customCue.content
                : language.sanitize(title));

            const a =
              html.tag('a', {href: '#'},
                {'data-target-id': targetID},
                {[html.onlyIfContent]: true},

                cue);

            if (customCue) {
              content.splice(content.indexOf(customCue), 1, a);
              return html.tags(content, {[html.joinChildren]: ''});
            } else {
              return a;
            }
          }),
    }),
};