1 | import { h } from 'vue';
|
2 | import Swiper from 'swiper';
|
3 |
|
4 | function calcLoopedSlides(slides, swiperParams) {
|
5 | let slidesPerViewParams = swiperParams.slidesPerView;
|
6 |
|
7 | if (swiperParams.breakpoints) {
|
8 | const breakpoint = Swiper.prototype.getBreakpoint(swiperParams.breakpoints);
|
9 | const breakpointOnlyParams = breakpoint in swiperParams.breakpoints ? swiperParams.breakpoints[breakpoint] : undefined;
|
10 |
|
11 | if (breakpointOnlyParams && breakpointOnlyParams.slidesPerView) {
|
12 | slidesPerViewParams = breakpointOnlyParams.slidesPerView;
|
13 | }
|
14 | }
|
15 |
|
16 | let loopedSlides = Math.ceil(parseFloat(swiperParams.loopedSlides || slidesPerViewParams, 10));
|
17 | loopedSlides += swiperParams.loopAdditionalSlides;
|
18 |
|
19 | if (loopedSlides > slides.length) {
|
20 | loopedSlides = slides.length;
|
21 | }
|
22 |
|
23 | return loopedSlides;
|
24 | }
|
25 |
|
26 | function renderLoop(swiperRef, slides, swiperParams) {
|
27 | const modifiedSlides = slides.map((child, index) => {
|
28 | if (!child.props) child.props = {};
|
29 | child.props.swiperRef = swiperRef;
|
30 | child.props['data-swiper-slide-index'] = index;
|
31 | return child;
|
32 | });
|
33 |
|
34 | function duplicateSlide(child, index, position) {
|
35 | if (!child.props) child.props = {};
|
36 | return h(child.type, { ...child.props,
|
37 | key: `${child.key}-duplicate-${index}-${position}`,
|
38 | class: `${child.props.className || ''} ${swiperParams.slideDuplicateClass} ${child.props.class || ''}`
|
39 | }, child.children);
|
40 | }
|
41 |
|
42 | if (swiperParams.loopFillGroupWithBlank) {
|
43 | const blankSlidesNum = swiperParams.slidesPerGroup - modifiedSlides.length % swiperParams.slidesPerGroup;
|
44 |
|
45 | if (blankSlidesNum !== swiperParams.slidesPerGroup) {
|
46 | for (let i = 0; i < blankSlidesNum; i += 1) {
|
47 | const blankSlide = h('div', {
|
48 | class: `${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 | const loopedSlides = calcLoopedSlides(modifiedSlides, swiperParams);
|
60 | const prependSlides = [];
|
61 | const appendSlides = [];
|
62 | modifiedSlides.forEach((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 (swiperRef.value) {
|
73 | swiperRef.value.loopedSlides = loopedSlides;
|
74 | }
|
75 |
|
76 | return [...prependSlides, ...modifiedSlides, ...appendSlides];
|
77 | }
|
78 |
|
79 | export { calcLoopedSlides, renderLoop }; |
\ | No newline at end of file |