1 | export default function Grid({
|
2 | swiper,
|
3 | extendParams
|
4 | }) {
|
5 | extendParams({
|
6 | grid: {
|
7 | rows: 1,
|
8 | fill: 'column'
|
9 | }
|
10 | });
|
11 | let slidesNumberEvenToRows;
|
12 | let slidesPerRow;
|
13 | let numFullColumns;
|
14 |
|
15 | const initSlides = slidesLength => {
|
16 | const {
|
17 | slidesPerView
|
18 | } = swiper.params;
|
19 | const {
|
20 | rows,
|
21 | fill
|
22 | } = swiper.params.grid;
|
23 | slidesPerRow = slidesNumberEvenToRows / rows;
|
24 | numFullColumns = Math.floor(slidesLength / rows);
|
25 |
|
26 | if (Math.floor(slidesLength / rows) === slidesLength / rows) {
|
27 | slidesNumberEvenToRows = slidesLength;
|
28 | } else {
|
29 | slidesNumberEvenToRows = Math.ceil(slidesLength / rows) * rows;
|
30 | }
|
31 |
|
32 | if (slidesPerView !== 'auto' && fill === 'row') {
|
33 | slidesNumberEvenToRows = Math.max(slidesNumberEvenToRows, slidesPerView * rows);
|
34 | }
|
35 | };
|
36 |
|
37 | const updateSlide = (i, slide, slidesLength, getDirectionLabel) => {
|
38 | const {
|
39 | slidesPerGroup,
|
40 | spaceBetween
|
41 | } = swiper.params;
|
42 | const {
|
43 | rows,
|
44 | fill
|
45 | } = swiper.params.grid;
|
46 |
|
47 | let newSlideOrderIndex;
|
48 | let column;
|
49 | let row;
|
50 |
|
51 | if (fill === 'row' && slidesPerGroup > 1) {
|
52 | const groupIndex = Math.floor(i / (slidesPerGroup * rows));
|
53 | const slideIndexInGroup = i - rows * slidesPerGroup * groupIndex;
|
54 | const columnsInGroup = groupIndex === 0 ? slidesPerGroup : Math.min(Math.ceil((slidesLength - groupIndex * rows * slidesPerGroup) / rows), slidesPerGroup);
|
55 | row = Math.floor(slideIndexInGroup / columnsInGroup);
|
56 | column = slideIndexInGroup - row * columnsInGroup + groupIndex * slidesPerGroup;
|
57 | newSlideOrderIndex = column + row * slidesNumberEvenToRows / rows;
|
58 | slide.css({
|
59 | '-webkit-order': newSlideOrderIndex,
|
60 | order: newSlideOrderIndex
|
61 | });
|
62 | } else if (fill === 'column') {
|
63 | column = Math.floor(i / rows);
|
64 | row = i - column * rows;
|
65 |
|
66 | if (column > numFullColumns || column === numFullColumns && row === rows - 1) {
|
67 | row += 1;
|
68 |
|
69 | if (row >= rows) {
|
70 | row = 0;
|
71 | column += 1;
|
72 | }
|
73 | }
|
74 | } else {
|
75 | row = Math.floor(i / slidesPerRow);
|
76 | column = i - row * slidesPerRow;
|
77 | }
|
78 |
|
79 | slide.css(getDirectionLabel('margin-top'), row !== 0 ? spaceBetween && `${spaceBetween}px` : '');
|
80 | };
|
81 |
|
82 | const updateWrapperSize = (slideSize, snapGrid, getDirectionLabel) => {
|
83 | const {
|
84 | spaceBetween,
|
85 | centeredSlides,
|
86 | roundLengths
|
87 | } = swiper.params;
|
88 | const {
|
89 | rows
|
90 | } = swiper.params.grid;
|
91 | swiper.virtualSize = (slideSize + spaceBetween) * slidesNumberEvenToRows;
|
92 | swiper.virtualSize = Math.ceil(swiper.virtualSize / rows) - spaceBetween;
|
93 | swiper.$wrapperEl.css({
|
94 | [getDirectionLabel('width')]: `${swiper.virtualSize + spaceBetween}px`
|
95 | });
|
96 |
|
97 | if (centeredSlides) {
|
98 | snapGrid.splice(0, snapGrid.length);
|
99 | const newSlidesGrid = [];
|
100 |
|
101 | for (let i = 0; i < snapGrid.length; i += 1) {
|
102 | let slidesGridItem = snapGrid[i];
|
103 | if (roundLengths) slidesGridItem = Math.floor(slidesGridItem);
|
104 | if (snapGrid[i] < swiper.virtualSize + snapGrid[0]) newSlidesGrid.push(slidesGridItem);
|
105 | }
|
106 |
|
107 | snapGrid.push(...newSlidesGrid);
|
108 | }
|
109 | };
|
110 |
|
111 | swiper.grid = {
|
112 | initSlides,
|
113 | updateSlide,
|
114 | updateWrapperSize
|
115 | };
|
116 | } |
\ | No newline at end of file |