Browse code

Refactor and rewrite as contao bundle

Benjamin Roth authored on04/11/2022 22:32:32
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,88 @@
1
+"use strict";
2
+
3
+exports.__esModule = true;
4
+exports.calcLoopedSlides = calcLoopedSlides;
5
+exports.renderLoop = renderLoop;
6
+
7
+var _react = _interopRequireDefault(require("react"));
8
+
9
+var _core = _interopRequireDefault(require("../../core"));
10
+
11
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+
13
+// eslint-disable-next-line
14
+function calcLoopedSlides(slides, swiperParams) {
15
+  var slidesPerViewParams = swiperParams.slidesPerView;
16
+
17
+  if (swiperParams.breakpoints) {
18
+    var breakpoint = _core.default.prototype.getBreakpoint(swiperParams.breakpoints);
19
+
20
+    var breakpointOnlyParams = breakpoint in swiperParams.breakpoints ? swiperParams.breakpoints[breakpoint] : undefined;
21
+
22
+    if (breakpointOnlyParams && breakpointOnlyParams.slidesPerView) {
23
+      slidesPerViewParams = breakpointOnlyParams.slidesPerView;
24
+    }
25
+  }
26
+
27
+  var loopedSlides = Math.ceil(parseFloat(swiperParams.loopedSlides || slidesPerViewParams, 10));
28
+  loopedSlides += swiperParams.loopAdditionalSlides;
29
+
30
+  if (loopedSlides > slides.length) {
31
+    loopedSlides = slides.length;
32
+  }
33
+
34
+  return loopedSlides;
35
+}
36
+
37
+function renderLoop(swiper, slides, swiperParams) {
38
+  var modifiedSlides = slides.map(function (child, index) {
39
+    return /*#__PURE__*/_react.default.cloneElement(child, {
40
+      swiper: swiper,
41
+      'data-swiper-slide-index': index
42
+    });
43
+  });
44
+
45
+  function duplicateSlide(child, index, position) {
46
+    return /*#__PURE__*/_react.default.cloneElement(child, {
47
+      key: child.key + "-duplicate-" + index + "-" + position,
48
+      className: (child.props.className || '') + " " + swiperParams.slideDuplicateClass
49
+    });
50
+  }
51
+
52
+  if (swiperParams.loopFillGroupWithBlank) {
53
+    var blankSlidesNum = swiperParams.slidesPerGroup - modifiedSlides.length % swiperParams.slidesPerGroup;
54
+
55
+    if (blankSlidesNum !== swiperParams.slidesPerGroup) {
56
+      for (var i = 0; i < blankSlidesNum; i += 1) {
57
+        var blankSlide = /*#__PURE__*/_react.default.createElement("div", {
58
+          className: swiperParams.slideClass + " " + swiperParams.slideBlankClass
59
+        });
60
+
61
+        modifiedSlides.push(blankSlide);
62
+      }
63
+    }
64
+  }
65
+
66
+  if (swiperParams.slidesPerView === 'auto' && !swiperParams.loopedSlides) {
67
+    swiperParams.loopedSlides = modifiedSlides.length;
68
+  }
69
+
70
+  var loopedSlides = calcLoopedSlides(modifiedSlides, swiperParams);
71
+  var prependSlides = [];
72
+  var appendSlides = [];
73
+  modifiedSlides.forEach(function (child, index) {
74
+    if (index < loopedSlides) {
75
+      appendSlides.push(duplicateSlide(child, index, 'prepend'));
76
+    }
77
+
78
+    if (index < modifiedSlides.length && index >= modifiedSlides.length - loopedSlides) {
79
+      prependSlides.push(duplicateSlide(child, index, 'append'));
80
+    }
81
+  });
82
+
83
+  if (swiper) {
84
+    swiper.loopedSlides = loopedSlides;
85
+  }
86
+
87
+  return [].concat(prependSlides, modifiedSlides, appendSlides);
88
+}
0 89
\ No newline at end of file