Skip to content

Commit

Permalink
Bug Fix: getAdjacent() returns an incorrect index when the `perMove…
Browse files Browse the repository at this point in the history
…` option is 2.
  • Loading branch information
NaotoshiFujita committed Oct 10, 2021
1 parent a1e0776 commit 120776f
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dist/js/splide-renderer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/js/splide.cjs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Splide.js
* Version : 3.1.2
* Version : 3.1.3
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
Expand Down Expand Up @@ -1354,7 +1354,7 @@ function Controller(Splide2, Components2, options) {
return getAdjacent(true, destination);
}
function getAdjacent(prev, destination) {
const number = perMove || hasFocus() ? 1 : perPage;
const number = perMove || (hasFocus() ? 1 : perPage);
const dest = computeDestIndex(currIndex + number * (prev ? -1 : 1), currIndex);
if (dest === -1 && isSlide) {
if (!approximatelyEqual(getPosition(), getLimit(!prev), 1)) {
Expand Down
4 changes: 2 additions & 2 deletions dist/js/splide.esm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Splide.js
* Version : 3.1.2
* Version : 3.1.3
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
Expand Down Expand Up @@ -1350,7 +1350,7 @@ function Controller(Splide2, Components2, options) {
return getAdjacent(true, destination);
}
function getAdjacent(prev, destination) {
const number = perMove || hasFocus() ? 1 : perPage;
const number = perMove || (hasFocus() ? 1 : perPage);
const dest = computeDestIndex(currIndex + number * (prev ? -1 : 1), currIndex);
if (dest === -1 && isSlide) {
if (!approximatelyEqual(getPosition(), getLimit(!prev), 1)) {
Expand Down
4 changes: 2 additions & 2 deletions dist/js/splide.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/splide.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/js/splide.min.js

Large diffs are not rendered by default.

Binary file modified dist/js/splide.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/types/types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export interface ResponsiveOptions {
*/
flickPower?: number;
/**
* Limies the number of pages to move by "flick".
* Limits the number of pages to move by "flick".
*/
flickMaxPages?: number;
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splidejs/splide",
"version": "3.1.2",
"version": "3.1.3",
"description": "Splide is a lightweight, flexible and accessible slider/carousel. No dependencies, no Lighthouse errors.",
"author": "Naotoshi Fujita",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/Controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
* @return An adjacent index if available, or otherwise `-1`.
*/
function getAdjacent( prev: boolean, destination?: boolean ): number {
const number = perMove || hasFocus() ? 1 : perPage;
const number = perMove || ( hasFocus() ? 1 : perPage );
const dest = computeDestIndex( currIndex + number * ( prev ? -1 : 1 ), currIndex );

if ( dest === -1 && isSlide ) {
Expand Down
13 changes: 13 additions & 0 deletions src/js/components/Controller/test/getAdjacent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ describe( 'Controller#getAdjacent()', () => {
expect( Controller.getPrev() ).toBe( 2 );
} );

test( 'can return the next/previous index with respecting the perMove option that is above 1.', () => {
const splide = init( { speed: 0, perPage: 3, perMove: 2 } );
const { Controller } = splide.Components;

expect( Controller.getNext() ).toBe( 2 );
expect( Controller.getPrev() ).toBe( -1 );

splide.go( 3 );

expect( Controller.getNext() ).toBe( 5 );
expect( Controller.getPrev() ).toBe( 1 );
} );

test( 'can return -1 if there is no adjacent slide.', () => {
const splide = init( { speed: 0 } );
const { Controller } = splide.Components;
Expand Down
84 changes: 48 additions & 36 deletions src/js/test/php/examples/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,62 @@
<script>
document.addEventListener( 'DOMContentLoaded', function () {
var splide = new Splide( '#splide01', {
type : 'loop',
perPage : 3,
gap : '1.5rem',
// speed: 600,
cover: true,
height : 400,
// waitForTransition: false,
// direction : 'ltr',
// drag : true,
// pagination: false,
// arrows: false,
useScroll: true,
// focus: 'center',
dragMinThreshold: {
mouse: 4,
},
classes: {
arrows: 'splide__arrows splide__test',
},
slideFocus: false,
type : 'slide',
perPage: 3,
// direction: 'ttb',
// height: 1000,
perMove: 2,
rewind: true,
// focus: 0,
breakpoints: {
1000: {
// direction: 'rtl',
// drag: false,
perPage: 2,
}
destroy: true,
// type : 'fade',
// perPage: 1,
},
},
} );

splide.on( 'moved', () => {
console.log( 'moved' );
} );
// splide.on( 'moved', () => {
// console.log( 'moved' );
// } );
//
// splide.on( 'visible', Slide => {
// console.log( 'visible', Slide.index );
// } );
//
// splide.on( 'hidden', Slide => {
// console.log( 'hidden', Slide.index );
// } );
//
// splide.on( 'click', () => {
// console.log( 'click' );
// } );

splide.on( 'visible', Slide => {
console.log( 'visible', Slide.index );
} );

splide.on( 'hidden', Slide => {
console.log( 'hidden', Slide.index );
} );
// let prevType;
//
// splide.on( 'update', function onUpdate( options ) {
// if ( prevType && prevType !== options.type ) {
// splide.destroy();
// splide.mount();
// splide.on( 'update', onUpdate );
//
// console.log( 'remount' );
// }
//
// prevType = options.type;
// } );

// let direction;

// splide.on( 'updated', options => {
// if ( direction !== options.direction ) {
// splide.refresh();
// direction = options.direction;
// }
// } );

splide.on( 'click', () => {
console.log( 'click' );
} );

splide.mount();
} );
Expand Down
2 changes: 1 addition & 1 deletion src/js/test/php/parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function render_slides( $number = 10, $text = false ) {
if ( $text ) {
printf( '<span>%s</span>', $i + 1 );
} else {
printf( '<img src="../../assets/images/pics/slide%02d.jpg">', $i + 1 );
printf( '<img src="../../assets/images/pics/slide%02d.jpg"><a href="#">hog</a>', $i + 1 );
}

echo '</li>' . PHP_EOL;
Expand Down

0 comments on commit 120776f

Please sign in to comment.