File size: 1,929 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

const galleryComponent = function () {
  return {
    skip: 1,
    atBeginning: false,
    atEnd: false,
    next() {
      this.to((current, offset) => current + offset * this.skip);
    },
    prev() {
      this.to((current, offset) => current - offset * this.skip);
    },
    to(strategy) {
      const slider = this.$refs.slider;
      const current = slider.scrollLeft;
      const offset = slider.getBoundingClientRect().width;
      slider.scrollTo({ left: strategy(current, offset), behavior: 'smooth' });
    },
    focusableWhenVisible: {
      'x-intersect:enter'() {
        this.$el.removeAttribute('tabindex');
      },
      'x-intersect:leave'() {
        this.$el.setAttribute('tabindex', '-1');
      }
    },
    disableNextAndPreviousButtons: {
      'x-intersect:enter.threshold.05'() {

        if (!this.$el?.parentElement?.children)
        {
          this.atBeginning = true;
          this.atEnd = true;
          return
        }
        const slideEls = this.$el.parentElement.children;

        // If this is the first slide.
        if (slideEls[0] === this.$el) {
          this.atBeginning = true;
          // If this is the last slide.
        } else if (slideEls[slideEls.length - 1] === this.$el) {
          this.atEnd = true;
        }
      },
      'x-intersect:leave.threshold.05'() {
        if (!this.$el?.parentElement?.children)
        {
          this.atBeginning = true;
          this.atEnd = true;
          return
        }
        const slideEls = this.$el.parentElement.children;

        // If this is the first slide.
        if (slideEls[0] === this.$el) {
          this.atBeginning = false;
          // If this is the last slide.
        } else if (slideEls[slideEls.length - 1] === this.$el) {
          this.atEnd = false;
        }
      }
    }
  };
};

export { galleryComponent };