1 | import React from 'react';
|
2 |
|
3 | import Swiper from '../../core';
|
4 |
|
5 | function calcLoopedSlides(slides, swiperParams) {
|
6 | var slidesPerViewParams = swiperParams.slidesPerView;
|
7 |
|
8 | if (swiperParams.breakpoints) {
|
9 | var breakpoint = Swiper.prototype.getBreakpoint(swiperParams.breakpoints);
|
10 | var breakpointOnlyParams = breakpoint in swiperParams.breakpoints ? swiperParams.breakpoints[breakpoint] : undefined;
|
11 |
|
12 | if (breakpointOnlyParams && breakpointOnlyParams.slidesPerView) {
|
13 | slidesPerViewParams = breakpointOnlyParams.slidesPerView;
|
14 | }
|
15 | }
|
16 |
|
17 | var loopedSlides = Math.ceil(parseFloat(swiperParams.loopedSlides || slidesPerViewParams, 10));
|
18 | loopedSlides += swiperParams.loopAdditionalSlides;
|
19 |
|
20 | if (loopedSlides > slides.length) {
|
21 | loopedSlides = slides.length;
|
22 | }
|
23 |
|
24 | return loopedSlides;
|
25 | }
|
26 |
|
27 | function renderLoop(swiper, slides, swiperParams) {
|
28 | var modifiedSlides = slides.map(function (child, index) {
|
29 | return React.cloneElement(child, {
|
30 | swiper: swiper,
|
31 | 'data-swiper-slide-index': index
|
32 | });
|
33 | });
|
34 |
|
35 | function duplicateSlide(child, index, position) {
|
36 | return React.cloneElement(child, {
|
37 | key: child.key + "-duplicate-" + index + "-" + position,
|
38 | className: (child.props.className || '') + " " + swiperParams.slideDuplicateClass
|
39 | });
|
40 | }
|
41 |
|
42 | if (swiperParams.loopFillGroupWithBlank) {
|
43 | var blankSlidesNum = swiperParams.slidesPerGroup - modifiedSlides.length % swiperParams.slidesPerGroup;
|
44 |
|
45 | if (blankSlidesNum !== swiperParams.slidesPerGroup) {
|
46 | for (var i = 0; i < blankSlidesNum; i += 1) {
|
47 | var blankSlide = React.createElement("div", {
|
48 | className: swiperParams.slideClass + " " + swiperParams.slideBlankClass
|
49 | });
|
50 | modifiedSlides.push(blankSlide);
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 | if (swiperParams.slidesPerView === 'auto' && !swiperParams.loopedSlides) {
|
56 | swiperParams.loopedSlides = modifiedSlides.length;
|
57 | }
|
58 |
|
59 | var loopedSlides = calcLoopedSlides(modifiedSlides, swiperParams);
|
60 | var prependSlides = [];
|
61 | var appendSlides = [];
|
62 | modifiedSlides.forEach(function (child, index) {
|
63 | if (index < loopedSlides) {
|
64 | appendSlides.push(duplicateSlide(child, index, 'prepend'));
|
65 | }
|
66 |
|
67 | if (index < modifiedSlides.length && index >= modifiedSlides.length - loopedSlides) {
|
68 | prependSlides.push(duplicateSlide(child, index, 'append'));
|
69 | }
|
70 | });
|
71 |
|
72 | if (swiper) {
|
73 | swiper.loopedSlides = loopedSlides;
|
74 | }
|
75 |
|
76 | return [].concat(prependSlides, modifiedSlides, appendSlides);
|
77 | }
|
78 |
|
79 | export { calcLoopedSlides, renderLoop }; |
\ | No newline at end of file |