UNPKG

12.4 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = updateSlides;
5
6var _ssrWindow = require("ssr-window");
7
8var _utils = require("../../../utils/utils");
9
10function updateSlides() {
11 var swiper = this;
12
13 var getDirectionLabel = function getDirectionLabel(property) {
14 if (swiper.isHorizontal()) {
15 return property;
16 } // prettier-ignore
17
18
19 return {
20 'width': 'height',
21 'margin-top': 'margin-left',
22 'margin-bottom ': 'margin-right',
23 'margin-left': 'margin-top',
24 'margin-right': 'margin-bottom',
25 'padding-left': 'padding-top',
26 'padding-right': 'padding-bottom',
27 'marginRight': 'marginBottom'
28 }[property];
29 };
30
31 var getDirectionPropertyValue = function getDirectionPropertyValue(node, label) {
32 return parseFloat(node.getPropertyValue(getDirectionLabel(label)) || 0);
33 };
34
35 var window = (0, _ssrWindow.getWindow)();
36 var params = swiper.params;
37 var $wrapperEl = swiper.$wrapperEl,
38 swiperSize = swiper.size,
39 rtl = swiper.rtlTranslate,
40 wrongRTL = swiper.wrongRTL;
41 var isVirtual = swiper.virtual && params.virtual.enabled;
42 var previousSlidesLength = isVirtual ? swiper.virtual.slides.length : swiper.slides.length;
43 var slides = $wrapperEl.children("." + swiper.params.slideClass);
44 var slidesLength = isVirtual ? swiper.virtual.slides.length : slides.length;
45 var snapGrid = [];
46 var slidesGrid = [];
47 var slidesSizesGrid = [];
48
49 function slidesForMargin(slideEl, slideIndex) {
50 if (!params.cssMode) return true;
51
52 if (slideIndex === slides.length - 1) {
53 return false;
54 }
55
56 return true;
57 }
58
59 var offsetBefore = params.slidesOffsetBefore;
60
61 if (typeof offsetBefore === 'function') {
62 offsetBefore = params.slidesOffsetBefore.call(swiper);
63 }
64
65 var offsetAfter = params.slidesOffsetAfter;
66
67 if (typeof offsetAfter === 'function') {
68 offsetAfter = params.slidesOffsetAfter.call(swiper);
69 }
70
71 var previousSnapGridLength = swiper.snapGrid.length;
72 var previousSlidesGridLength = swiper.slidesGrid.length;
73 var spaceBetween = params.spaceBetween;
74 var slidePosition = -offsetBefore;
75 var prevSlideSize = 0;
76 var index = 0;
77
78 if (typeof swiperSize === 'undefined') {
79 return;
80 }
81
82 if (typeof spaceBetween === 'string' && spaceBetween.indexOf('%') >= 0) {
83 spaceBetween = parseFloat(spaceBetween.replace('%', '')) / 100 * swiperSize;
84 }
85
86 swiper.virtualSize = -spaceBetween; // reset margins
87
88 if (rtl) slides.css({
89 marginLeft: '',
90 marginTop: ''
91 });else slides.css({
92 marginRight: '',
93 marginBottom: ''
94 });
95 var slidesNumberEvenToRows;
96
97 if (params.slidesPerColumn > 1) {
98 if (Math.floor(slidesLength / params.slidesPerColumn) === slidesLength / swiper.params.slidesPerColumn) {
99 slidesNumberEvenToRows = slidesLength;
100 } else {
101 slidesNumberEvenToRows = Math.ceil(slidesLength / params.slidesPerColumn) * params.slidesPerColumn;
102 }
103
104 if (params.slidesPerView !== 'auto' && params.slidesPerColumnFill === 'row') {
105 slidesNumberEvenToRows = Math.max(slidesNumberEvenToRows, params.slidesPerView * params.slidesPerColumn);
106 }
107 } // Calc slides
108
109
110 var slideSize;
111 var slidesPerColumn = params.slidesPerColumn;
112 var slidesPerRow = slidesNumberEvenToRows / slidesPerColumn;
113 var numFullColumns = Math.floor(slidesLength / params.slidesPerColumn);
114
115 for (var i = 0; i < slidesLength; i += 1) {
116 slideSize = 0;
117 var slide = slides.eq(i);
118
119 if (params.slidesPerColumn > 1) {
120 // Set slides order
121 var newSlideOrderIndex = void 0;
122 var column = void 0;
123 var row = void 0;
124
125 if (params.slidesPerColumnFill === 'row' && params.slidesPerGroup > 1) {
126 var groupIndex = Math.floor(i / (params.slidesPerGroup * params.slidesPerColumn));
127 var slideIndexInGroup = i - params.slidesPerColumn * params.slidesPerGroup * groupIndex;
128 var columnsInGroup = groupIndex === 0 ? params.slidesPerGroup : Math.min(Math.ceil((slidesLength - groupIndex * slidesPerColumn * params.slidesPerGroup) / slidesPerColumn), params.slidesPerGroup);
129 row = Math.floor(slideIndexInGroup / columnsInGroup);
130 column = slideIndexInGroup - row * columnsInGroup + groupIndex * params.slidesPerGroup;
131 newSlideOrderIndex = column + row * slidesNumberEvenToRows / slidesPerColumn;
132 slide.css({
133 '-webkit-box-ordinal-group': newSlideOrderIndex,
134 '-moz-box-ordinal-group': newSlideOrderIndex,
135 '-ms-flex-order': newSlideOrderIndex,
136 '-webkit-order': newSlideOrderIndex,
137 order: newSlideOrderIndex
138 });
139 } else if (params.slidesPerColumnFill === 'column') {
140 column = Math.floor(i / slidesPerColumn);
141 row = i - column * slidesPerColumn;
142
143 if (column > numFullColumns || column === numFullColumns && row === slidesPerColumn - 1) {
144 row += 1;
145
146 if (row >= slidesPerColumn) {
147 row = 0;
148 column += 1;
149 }
150 }
151 } else {
152 row = Math.floor(i / slidesPerRow);
153 column = i - row * slidesPerRow;
154 }
155
156 slide.css(getDirectionLabel('margin-top'), row !== 0 && params.spaceBetween && params.spaceBetween + "px");
157 }
158
159 if (slide.css('display') === 'none') continue; // eslint-disable-line
160
161 if (params.slidesPerView === 'auto') {
162 var slideStyles = window.getComputedStyle(slide[0], null);
163 var currentTransform = slide[0].style.transform;
164 var currentWebKitTransform = slide[0].style.webkitTransform;
165
166 if (currentTransform) {
167 slide[0].style.transform = 'none';
168 }
169
170 if (currentWebKitTransform) {
171 slide[0].style.webkitTransform = 'none';
172 }
173
174 if (params.roundLengths) {
175 slideSize = swiper.isHorizontal() ? slide.outerWidth(true) : slide.outerHeight(true);
176 } else {
177 // eslint-disable-next-line
178 var width = getDirectionPropertyValue(slideStyles, 'width');
179 var paddingLeft = getDirectionPropertyValue(slideStyles, 'padding-left');
180 var paddingRight = getDirectionPropertyValue(slideStyles, 'padding-right');
181 var marginLeft = getDirectionPropertyValue(slideStyles, 'margin-left');
182 var marginRight = getDirectionPropertyValue(slideStyles, 'margin-right');
183 var boxSizing = slideStyles.getPropertyValue(slideStyles, 'box-sizing');
184
185 if (boxSizing && boxSizing === 'border-box') {
186 slideSize = width + marginLeft + marginRight;
187 } else {
188 var _slide$ = slide[0],
189 clientWidth = _slide$.clientWidth,
190 offsetWidth = _slide$.offsetWidth;
191 slideSize = width + paddingLeft + paddingRight + marginLeft + marginRight + (offsetWidth - clientWidth);
192 }
193 }
194
195 if (currentTransform) {
196 slide[0].style.transform = currentTransform;
197 }
198
199 if (currentWebKitTransform) {
200 slide[0].style.webkitTransform = currentWebKitTransform;
201 }
202
203 if (params.roundLengths) slideSize = Math.floor(slideSize);
204 } else {
205 slideSize = (swiperSize - (params.slidesPerView - 1) * spaceBetween) / params.slidesPerView;
206 if (params.roundLengths) slideSize = Math.floor(slideSize);
207
208 if (slides[i]) {
209 slides[i].style[getDirectionLabel('width')] = slideSize + "px";
210 }
211 }
212
213 if (slides[i]) {
214 slides[i].swiperSlideSize = slideSize;
215 }
216
217 slidesSizesGrid.push(slideSize);
218
219 if (params.centeredSlides) {
220 slidePosition = slidePosition + slideSize / 2 + prevSlideSize / 2 + spaceBetween;
221 if (prevSlideSize === 0 && i !== 0) slidePosition = slidePosition - swiperSize / 2 - spaceBetween;
222 if (i === 0) slidePosition = slidePosition - swiperSize / 2 - spaceBetween;
223 if (Math.abs(slidePosition) < 1 / 1000) slidePosition = 0;
224 if (params.roundLengths) slidePosition = Math.floor(slidePosition);
225 if (index % params.slidesPerGroup === 0) snapGrid.push(slidePosition);
226 slidesGrid.push(slidePosition);
227 } else {
228 if (params.roundLengths) slidePosition = Math.floor(slidePosition);
229 if ((index - Math.min(swiper.params.slidesPerGroupSkip, index)) % swiper.params.slidesPerGroup === 0) snapGrid.push(slidePosition);
230 slidesGrid.push(slidePosition);
231 slidePosition = slidePosition + slideSize + spaceBetween;
232 }
233
234 swiper.virtualSize += slideSize + spaceBetween;
235 prevSlideSize = slideSize;
236 index += 1;
237 }
238
239 swiper.virtualSize = Math.max(swiper.virtualSize, swiperSize) + offsetAfter;
240 var newSlidesGrid;
241
242 if (rtl && wrongRTL && (params.effect === 'slide' || params.effect === 'coverflow')) {
243 $wrapperEl.css({
244 width: swiper.virtualSize + params.spaceBetween + "px"
245 });
246 }
247
248 if (params.setWrapperSize) {
249 var _$wrapperEl$css;
250
251 $wrapperEl.css((_$wrapperEl$css = {}, _$wrapperEl$css[getDirectionLabel('width')] = swiper.virtualSize + params.spaceBetween + "px", _$wrapperEl$css));
252 }
253
254 if (params.slidesPerColumn > 1) {
255 var _$wrapperEl$css2;
256
257 swiper.virtualSize = (slideSize + params.spaceBetween) * slidesNumberEvenToRows;
258 swiper.virtualSize = Math.ceil(swiper.virtualSize / params.slidesPerColumn) - params.spaceBetween;
259 $wrapperEl.css((_$wrapperEl$css2 = {}, _$wrapperEl$css2[getDirectionLabel('width')] = swiper.virtualSize + params.spaceBetween + "px", _$wrapperEl$css2));
260
261 if (params.centeredSlides) {
262 newSlidesGrid = [];
263
264 for (var _i = 0; _i < snapGrid.length; _i += 1) {
265 var slidesGridItem = snapGrid[_i];
266 if (params.roundLengths) slidesGridItem = Math.floor(slidesGridItem);
267 if (snapGrid[_i] < swiper.virtualSize + snapGrid[0]) newSlidesGrid.push(slidesGridItem);
268 }
269
270 snapGrid = newSlidesGrid;
271 }
272 } // Remove last grid elements depending on width
273
274
275 if (!params.centeredSlides) {
276 newSlidesGrid = [];
277
278 for (var _i2 = 0; _i2 < snapGrid.length; _i2 += 1) {
279 var _slidesGridItem = snapGrid[_i2];
280 if (params.roundLengths) _slidesGridItem = Math.floor(_slidesGridItem);
281
282 if (snapGrid[_i2] <= swiper.virtualSize - swiperSize) {
283 newSlidesGrid.push(_slidesGridItem);
284 }
285 }
286
287 snapGrid = newSlidesGrid;
288
289 if (Math.floor(swiper.virtualSize - swiperSize) - Math.floor(snapGrid[snapGrid.length - 1]) > 1) {
290 snapGrid.push(swiper.virtualSize - swiperSize);
291 }
292 }
293
294 if (snapGrid.length === 0) snapGrid = [0];
295
296 if (params.spaceBetween !== 0) {
297 var _slides$filter$css;
298
299 var key = swiper.isHorizontal() && rtl ? 'marginLeft' : getDirectionLabel('marginRight');
300 slides.filter(slidesForMargin).css((_slides$filter$css = {}, _slides$filter$css[key] = spaceBetween + "px", _slides$filter$css));
301 }
302
303 if (params.centeredSlides && params.centeredSlidesBounds) {
304 var allSlidesSize = 0;
305 slidesSizesGrid.forEach(function (slideSizeValue) {
306 allSlidesSize += slideSizeValue + (params.spaceBetween ? params.spaceBetween : 0);
307 });
308 allSlidesSize -= params.spaceBetween;
309 var maxSnap = allSlidesSize - swiperSize;
310 snapGrid = snapGrid.map(function (snap) {
311 if (snap < 0) return -offsetBefore;
312 if (snap > maxSnap) return maxSnap + offsetAfter;
313 return snap;
314 });
315 }
316
317 if (params.centerInsufficientSlides) {
318 var _allSlidesSize = 0;
319 slidesSizesGrid.forEach(function (slideSizeValue) {
320 _allSlidesSize += slideSizeValue + (params.spaceBetween ? params.spaceBetween : 0);
321 });
322 _allSlidesSize -= params.spaceBetween;
323
324 if (_allSlidesSize < swiperSize) {
325 var allSlidesOffset = (swiperSize - _allSlidesSize) / 2;
326 snapGrid.forEach(function (snap, snapIndex) {
327 snapGrid[snapIndex] = snap - allSlidesOffset;
328 });
329 slidesGrid.forEach(function (snap, snapIndex) {
330 slidesGrid[snapIndex] = snap + allSlidesOffset;
331 });
332 }
333 }
334
335 (0, _utils.extend)(swiper, {
336 slides: slides,
337 snapGrid: snapGrid,
338 slidesGrid: slidesGrid,
339 slidesSizesGrid: slidesSizesGrid
340 });
341
342 if (slidesLength !== previousSlidesLength) {
343 swiper.emit('slidesLengthChange');
344 }
345
346 if (snapGrid.length !== previousSnapGridLength) {
347 if (swiper.params.watchOverflow) swiper.checkOverflow();
348 swiper.emit('snapGridLengthChange');
349 }
350
351 if (slidesGrid.length !== previousSlidesGridLength) {
352 swiper.emit('slidesGridLengthChange');
353 }
354
355 if (params.watchSlidesProgress || params.watchSlidesVisibility) {
356 swiper.updateSlidesOffset();
357 }
358}
\No newline at end of file