« get me outta code hell

generateCoverCarousel.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/generateCoverCarousel.js
blob: 69220da64d393d1cc0870aec18416322e6c08f45 (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
import {empty, repeat, stitchArrays} from '#sugar';
import {getCarouselLayoutForNumberOfItems} from '#wiki-data';

export default {
  contentDependencies: ['generateGridActionLinks'],
  extraDependencies: ['html'],

  relations(relation) {
    return {
      actionLinks: relation('generateGridActionLinks'),
    };
  },

  slots: {
    images: {validate: v => v.strictArrayOf(v.isHTML)},
    links: {validate: v => v.strictArrayOf(v.isHTML)},

    lazy: {validate: v => v.anyOf(v.isWholeNumber, v.isBoolean)},
    actionLinks: {validate: v => v.sparseArrayOf(v.isHTML)},
  },

  generate(relations, slots, {html}) {
    const stitched =
      stitchArrays({
        image: slots.images,
        link: slots.links,
      });

    if (empty(stitched)) {
      return;
    }

    const layout = getCarouselLayoutForNumberOfItems(stitched.length);

    return html.tags([
      html.tag('div', {class: 'carousel-container'},
        {'data-carousel-rows': layout.rows},
        {'data-carousel-columns': layout.columns},

        repeat(3, [
          html.tag('div', {class: 'carousel-grid'},
            {'aria-hidden': 'true'},

            stitched.map(({image, link}, index) =>
              html.tag('div', {class: 'carousel-item'},
                link.slots({
                  attributes: {tabindex: '-1'},
                  content:
                    image.slots({
                      thumb: 'small',
                      square: true,
                      lazy:
                        (typeof slots.lazy === 'number'
                          ? index >= slots.lazy
                       : typeof slots.lazy === 'boolean'
                          ? slots.lazy
                          : false),
                    }),
                })))),
        ])),

      relations.actionLinks
        .slot('actionLinks', slots.actionLinks),
    ]);
  },
};