Skip to content

Commit

Permalink
feat(core): move slidesPerColumn functionality to new Grid module
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 5, 2021
1 parent 7a6258c commit 0f1df44
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 214 deletions.
1 change: 1 addition & 0 deletions scripts/build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ module.exports = {
'effect-coverflow',
'thumbs',
'free-mode',
'grid',
],
};
5 changes: 2 additions & 3 deletions src/angular/src/swiper.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import Swiper from 'swiper/core';
import Swiper from 'swiper';
import { Observable, of, Subject } from 'rxjs';
import { getParams } from './utils/get-params';
import { SwiperSlideDirective } from './swiper-slide.directive';
Expand Down Expand Up @@ -76,8 +76,7 @@ export class SwiperComponent implements OnInit {
@Input() breakpoints: SwiperOptions['breakpoints'];
@Input() spaceBetween: SwiperOptions['spaceBetween'];
@Input() slidesPerView: SwiperOptions['slidesPerView'];
@Input() slidesPerColumn: SwiperOptions['slidesPerColumn'];
@Input() slidesPerColumnFill: SwiperOptions['slidesPerColumnFill'];
@Input() grid: SwiperOptions['grid'];
@Input() slidesPerGroup: SwiperOptions['slidesPerGroup'];
@Input() slidesPerGroupSkip: SwiperOptions['slidesPerGroupSkip'];
@Input() centeredSlides: SwiperOptions['centeredSlides'];
Expand Down
3 changes: 1 addition & 2 deletions src/angular/src/utils/params-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const paramsList = [
'breakpoints',
'_spaceBetween',
'_slidesPerView',
'_slidesPerColumn',
'_slidesPerColumnFill',
'_grid',
'_slidesPerGroup',
'_slidesPerGroupSkip',
'_centeredSlides',
Expand Down
38 changes: 11 additions & 27 deletions src/core/breakpoints/setBreakpoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { extend } from '../../shared/utils.js';

const isGridEnabled = (swiper, params) => {
return swiper.grid && params.grid && params.grid.rows > 1;
};

export default function setBreakpoint() {
const swiper = this;
const { activeIndex, initialized, loopedSlides = 0, params, $el } = swiper;
Expand All @@ -12,44 +16,24 @@ export default function setBreakpoint() {
if (!breakpoint || swiper.currentBreakpoint === breakpoint) return;

const breakpointOnlyParams = breakpoint in breakpoints ? breakpoints[breakpoint] : undefined;
if (breakpointOnlyParams) {
[
'slidesPerView',
'spaceBetween',
'slidesPerGroup',
'slidesPerGroupSkip',
'slidesPerColumn',
].forEach((param) => {
const paramValue = breakpointOnlyParams[param];
if (typeof paramValue === 'undefined') return;
if (param === 'slidesPerView' && (paramValue === 'AUTO' || paramValue === 'auto')) {
breakpointOnlyParams[param] = 'auto';
} else if (param === 'slidesPerView') {
breakpointOnlyParams[param] = parseFloat(paramValue);
} else {
breakpointOnlyParams[param] = parseInt(paramValue, 10);
}
});
}

const breakpointParams = breakpointOnlyParams || swiper.originalParams;
const wasMultiRow = params.slidesPerColumn > 1;
const isMultiRow = breakpointParams.slidesPerColumn > 1;
const wasMultiRow = isGridEnabled(swiper, params);
const isMultiRow = isGridEnabled(swiper, breakpointParams);

const wasEnabled = params.enabled;

if (wasMultiRow && !isMultiRow) {
$el.removeClass(
`${params.containerModifierClass}multirow ${params.containerModifierClass}multirow-column`,
`${params.containerModifierClass}grid ${params.containerModifierClass}grid-column`,
);
swiper.emitContainerClasses();
} else if (!wasMultiRow && isMultiRow) {
$el.addClass(`${params.containerModifierClass}multirow`);
$el.addClass(`${params.containerModifierClass}grid`);
if (
(breakpointParams.slidesPerColumnFill && breakpointParams.slidesPerColumnFill === 'column') ||
(!breakpointParams.slidesPerColumnFill && params.slidesPerColumnFill === 'column')
(breakpointParams.grid.fill && breakpointParams.grid.fill === 'column') ||
(!breakpointParams.grid.fill && params.grid.fill === 'column')
) {
$el.addClass(`${params.containerModifierClass}multirow-column`);
$el.addClass(`${params.containerModifierClass}grid-column`);
}
swiper.emitContainerClasses();
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/classes/addClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default function addClasses() {
{ 'free-mode': swiper.params.freeMode && params.freeMode.enabled },
{ 'autoheight': params.autoHeight },
{ 'rtl': rtl },
{ 'multirow': params.slidesPerColumn > 1 },
{ 'multirow-column': params.slidesPerColumn > 1 && params.slidesPerColumnFill === 'column' },
{ 'grid': params.grid && params.grid.rows > 1 },
{ 'grid-column': params.grid && params.grid.rows > 1 && params.grid.fill === 'column' },
{ 'android': device.android },
{ 'ios': device.ios },
{ 'css-mode': params.cssMode },
Expand Down
7 changes: 0 additions & 7 deletions src/core/core.less
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
.swiper-wrapper {
transform: translate3d(0px, 0, 0);
}
.swiper-container-multirow > .swiper-wrapper {
flex-wrap: wrap;
}
.swiper-container-multirow-column > .swiper-wrapper {
flex-wrap: wrap;
flex-direction: column;
}

.swiper-container-pointer-events {
touch-action: pan-y;
Expand Down
7 changes: 0 additions & 7 deletions src/core/core.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
.swiper-wrapper {
transform: translate3d(0px, 0, 0);
}
.swiper-container-multirow > .swiper-wrapper {
flex-wrap: wrap;
}
.swiper-container-multirow-column > .swiper-wrapper {
flex-wrap: wrap;
flex-direction: column;
}
.swiper-container-pointer-events {
touch-action: pan-y;
&.swiper-container-vertical {
Expand Down
2 changes: 0 additions & 2 deletions src/core/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export default {
// Slides grid
spaceBetween: 0,
slidesPerView: 1,
slidesPerColumn: 1,
slidesPerColumnFill: 'column',
slidesPerGroup: 1,
slidesPerGroupSkip: 0,
centeredSlides: false,
Expand Down
10 changes: 5 additions & 5 deletions src/core/slide/slideTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ export default function slideTo(
if (params.normalizeSlideIndex) {
for (let i = 0; i < slidesGrid.length; i += 1) {
const normalizedTranslate = -Math.floor(translate * 100);
const normalizedGird = Math.floor(slidesGrid[i] * 100);
const normalizedGrid = Math.floor(slidesGrid[i] * 100);
const normalizedGridNext = Math.floor(slidesGrid[i + 1] * 100);
if (typeof slidesGrid[i + 1] !== 'undefined') {
if (
normalizedTranslate >= normalizedGird &&
normalizedTranslate < normalizedGridNext - (normalizedGridNext - normalizedGird) / 2
normalizedTranslate >= normalizedGrid &&
normalizedTranslate < normalizedGridNext - (normalizedGridNext - normalizedGrid) / 2
) {
slideIndex = i;
} else if (
normalizedTranslate >= normalizedGird &&
normalizedTranslate >= normalizedGrid &&
normalizedTranslate < normalizedGridNext
) {
slideIndex = i + 1;
}
} else if (normalizedTranslate >= normalizedGird) {
} else if (normalizedTranslate >= normalizedGrid) {
slideIndex = i;
}
}
Expand Down
91 changes: 9 additions & 82 deletions src/core/update/updateSlides.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,77 +61,19 @@ export default function updateSlides() {
if (rtl) slides.css({ marginLeft: '', marginBottom: '', marginTop: '' });
else slides.css({ marginRight: '', marginBottom: '', marginTop: '' });

let slidesNumberEvenToRows;
if (params.slidesPerColumn > 1) {
if (
Math.floor(slidesLength / params.slidesPerColumn) ===
slidesLength / swiper.params.slidesPerColumn
) {
slidesNumberEvenToRows = slidesLength;
} else {
slidesNumberEvenToRows =
Math.ceil(slidesLength / params.slidesPerColumn) * params.slidesPerColumn;
}
if (params.slidesPerView !== 'auto' && params.slidesPerColumnFill === 'row') {
slidesNumberEvenToRows = Math.max(
slidesNumberEvenToRows,
params.slidesPerView * params.slidesPerColumn,
);
}
const gridEnabled = params.grid && params.grid.rows > 1 && swiper.grid;
if (gridEnabled) {
swiper.grid.initSlides(slidesLength);
}

// Calc slides
let slideSize;
const slidesPerColumn = params.slidesPerColumn;
const slidesPerRow = slidesNumberEvenToRows / slidesPerColumn;
const numFullColumns = Math.floor(slidesLength / params.slidesPerColumn);

for (let i = 0; i < slidesLength; i += 1) {
slideSize = 0;
const slide = slides.eq(i);
if (params.slidesPerColumn > 1) {
// Set slides order
let newSlideOrderIndex;
let column;
let row;
if (params.slidesPerColumnFill === 'row' && params.slidesPerGroup > 1) {
const groupIndex = Math.floor(i / (params.slidesPerGroup * params.slidesPerColumn));
const slideIndexInGroup = i - params.slidesPerColumn * params.slidesPerGroup * groupIndex;
const columnsInGroup =
groupIndex === 0
? params.slidesPerGroup
: Math.min(
Math.ceil(
(slidesLength - groupIndex * slidesPerColumn * params.slidesPerGroup) /
slidesPerColumn,
),
params.slidesPerGroup,
);
row = Math.floor(slideIndexInGroup / columnsInGroup);
column = slideIndexInGroup - row * columnsInGroup + groupIndex * params.slidesPerGroup;

newSlideOrderIndex = column + (row * slidesNumberEvenToRows) / slidesPerColumn;
slide.css({
'-webkit-order': newSlideOrderIndex,
order: newSlideOrderIndex,
});
} else if (params.slidesPerColumnFill === 'column') {
column = Math.floor(i / slidesPerColumn);
row = i - column * slidesPerColumn;
if (column > numFullColumns || (column === numFullColumns && row === slidesPerColumn - 1)) {
row += 1;
if (row >= slidesPerColumn) {
row = 0;
column += 1;
}
}
} else {
row = Math.floor(i / slidesPerRow);
column = i - row * slidesPerRow;
}
slide.css(
getDirectionLabel('margin-top'),
row !== 0 ? params.spaceBetween && `${params.spaceBetween}px` : '',
);
if (gridEnabled) {
swiper.grid.updateSlide(i, slide, slidesLength, getDirectionLabel);
}
if (slide.css('display') === 'none') continue; // eslint-disable-line

Expand Down Expand Up @@ -216,7 +158,6 @@ export default function updateSlides() {
index += 1;
}
swiper.virtualSize = Math.max(swiper.virtualSize, swiperSize) + offsetAfter;
let newSlidesGrid;

if (rtl && wrongRTL && (params.effect === 'slide' || params.effect === 'coverflow')) {
$wrapperEl.css({ width: `${swiper.virtualSize + params.spaceBetween}px` });
Expand All @@ -227,27 +168,13 @@ export default function updateSlides() {
});
}

if (params.slidesPerColumn > 1) {
swiper.virtualSize = (slideSize + params.spaceBetween) * slidesNumberEvenToRows;
swiper.virtualSize =
Math.ceil(swiper.virtualSize / params.slidesPerColumn) - params.spaceBetween;
$wrapperEl.css({
[getDirectionLabel('width')]: `${swiper.virtualSize + params.spaceBetween}px`,
});
if (params.centeredSlides) {
newSlidesGrid = [];
for (let i = 0; i < snapGrid.length; i += 1) {
let slidesGridItem = snapGrid[i];
if (params.roundLengths) slidesGridItem = Math.floor(slidesGridItem);
if (snapGrid[i] < swiper.virtualSize + snapGrid[0]) newSlidesGrid.push(slidesGridItem);
}
snapGrid = newSlidesGrid;
}
if (gridEnabled) {
swiper.grid.updateWrapperSize(slideSize, snapGrid, getDirectionLabel);
}

// Remove last grid elements depending on width
if (!params.centeredSlides) {
newSlidesGrid = [];
const newSlidesGrid = [];
for (let i = 0; i < snapGrid.length; i += 1) {
let slidesGridItem = snapGrid[i];
if (params.roundLengths) slidesGridItem = Math.floor(slidesGridItem);
Expand Down
1 change: 0 additions & 1 deletion src/modules/effect-cube/effect-cube.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export default function Cube({ swiper, extendParams, on }) {
swiper.classNames.push(`${swiper.params.containerModifierClass}3d`);
const overwriteParams = {
slidesPerView: 1,
slidesPerColumn: 1,
slidesPerGroup: 1,
watchSlidesProgress: true,
resistanceRatio: 0,
Expand Down
1 change: 0 additions & 1 deletion src/modules/effect-fade/effect-fade.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default function Fade({ swiper, extendParams, on }) {
swiper.classNames.push(`${swiper.params.containerModifierClass}fade`);
const overwriteParams = {
slidesPerView: 1,
slidesPerColumn: 1,
slidesPerGroup: 1,
watchSlidesProgress: true,
spaceBetween: 0,
Expand Down
1 change: 0 additions & 1 deletion src/modules/effect-flip/effect-flip.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export default function Flip({ swiper, extendParams, on }) {
swiper.classNames.push(`${swiper.params.containerModifierClass}3d`);
const overwriteParams = {
slidesPerView: 1,
slidesPerColumn: 1,
slidesPerGroup: 1,
watchSlidesProgress: true,
spaceBetween: 0,
Expand Down
Loading

0 comments on commit 0f1df44

Please sign in to comment.