Skip to content

Commit 40ad465

Browse files
Simplify the "shift" condition.
1 parent 8aff099 commit 40ad465

File tree

12 files changed

+107
-94
lines changed

12 files changed

+107
-94
lines changed

dist/js/splide.cjs.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* Splide.js
3-
* Version : 4.0.5
3+
* Version : 4.0.6
44
* License : MIT
55
* Copyright: 2022 Naotoshi Fujita
66
*/
@@ -1427,16 +1427,9 @@ function Move(Splide2, Components2, options) {
14271427
}
14281428

14291429
function move(dest, index, prev, callback) {
1430-
var position = getPosition();
1431-
var shifted = shift(position, dest > prev);
1432-
var oriented = orient(shifted);
1433-
var destination = toPosition(dest);
1434-
var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
1435-
var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1436-
1437-
if (shouldShift && canShift) {
1430+
if (dest !== index && canShift(dest > prev)) {
14381431
cancel();
1439-
translate(shifted, true);
1432+
translate(shift(getPosition(), dest > prev), true);
14401433
}
14411434

14421435
set(MOVING);
@@ -1462,9 +1455,9 @@ function Move(Splide2, Components2, options) {
14621455

14631456
function loop(position) {
14641457
if (Splide2.is(LOOP)) {
1465-
var diff = orient(position - getPosition());
1466-
var exceededMin = exceededLimit(false, position) && diff < 0;
1467-
var exceededMax = exceededLimit(true, position) && diff > 0;
1458+
var index = toIndex(position);
1459+
var exceededMax = index > Components2.Controller.getEnd();
1460+
var exceededMin = index < 0;
14681461

14691462
if (exceededMin || exceededMax) {
14701463
position = shift(position, exceededMax);
@@ -1533,6 +1526,11 @@ function Move(Splide2, Components2, options) {
15331526
return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
15341527
}
15351528

1529+
function canShift(backwards) {
1530+
var shifted = orient(shift(getPosition(), backwards));
1531+
return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1532+
}
1533+
15361534
function exceededLimit(max, position) {
15371535
position = isUndefined(position) ? getPosition() : position;
15381536
var exceededMin = max !== true && orient(position) < orient(getLimit(false));

dist/js/splide.esm.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
44

55
/*!
66
* Splide.js
7-
* Version : 4.0.5
7+
* Version : 4.0.6
88
* License : MIT
99
* Copyright: 2022 Naotoshi Fujita
1010
*/
@@ -1422,16 +1422,9 @@ function Move(Splide2, Components2, options) {
14221422
}
14231423

14241424
function move(dest, index, prev, callback) {
1425-
var position = getPosition();
1426-
var shifted = shift(position, dest > prev);
1427-
var oriented = orient(shifted);
1428-
var destination = toPosition(dest);
1429-
var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
1430-
var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1431-
1432-
if (shouldShift && canShift) {
1425+
if (dest !== index && canShift(dest > prev)) {
14331426
cancel();
1434-
translate(shifted, true);
1427+
translate(shift(getPosition(), dest > prev), true);
14351428
}
14361429

14371430
set(MOVING);
@@ -1457,9 +1450,9 @@ function Move(Splide2, Components2, options) {
14571450

14581451
function loop(position) {
14591452
if (Splide2.is(LOOP)) {
1460-
var diff = orient(position - getPosition());
1461-
var exceededMin = exceededLimit(false, position) && diff < 0;
1462-
var exceededMax = exceededLimit(true, position) && diff > 0;
1453+
var index = toIndex(position);
1454+
var exceededMax = index > Components2.Controller.getEnd();
1455+
var exceededMin = index < 0;
14631456

14641457
if (exceededMin || exceededMax) {
14651458
position = shift(position, exceededMax);
@@ -1528,6 +1521,11 @@ function Move(Splide2, Components2, options) {
15281521
return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
15291522
}
15301523

1524+
function canShift(backwards) {
1525+
var shifted = orient(shift(getPosition(), backwards));
1526+
return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1527+
}
1528+
15311529
function exceededLimit(max, position) {
15321530
position = isUndefined(position) ? getPosition() : position;
15331531
var exceededMin = max !== true && orient(position) < orient(getLimit(false));

dist/js/splide.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
44

55
/*!
66
* Splide.js
7-
* Version : 4.0.5
7+
* Version : 4.0.6
88
* License : MIT
99
* Copyright: 2022 Naotoshi Fujita
1010
*/
@@ -1420,16 +1420,9 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
14201420
}
14211421

14221422
function move(dest, index, prev, callback) {
1423-
var position = getPosition();
1424-
var shifted = shift(position, dest > prev);
1425-
var oriented = orient(shifted);
1426-
var destination = toPosition(dest);
1427-
var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
1428-
var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1429-
1430-
if (shouldShift && canShift) {
1423+
if (dest !== index && canShift(dest > prev)) {
14311424
cancel();
1432-
translate(shifted, true);
1425+
translate(shift(getPosition(), dest > prev), true);
14331426
}
14341427

14351428
set(MOVING);
@@ -1455,9 +1448,9 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
14551448

14561449
function loop(position) {
14571450
if (Splide2.is(LOOP)) {
1458-
var diff = orient(position - getPosition());
1459-
var exceededMin = exceededLimit(false, position) && diff < 0;
1460-
var exceededMax = exceededLimit(true, position) && diff > 0;
1451+
var index = toIndex(position);
1452+
var exceededMax = index > Components2.Controller.getEnd();
1453+
var exceededMin = index < 0;
14611454

14621455
if (exceededMin || exceededMax) {
14631456
position = shift(position, exceededMax);
@@ -1526,6 +1519,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
15261519
return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
15271520
}
15281521

1522+
function canShift(backwards) {
1523+
var shifted = orient(shift(getPosition(), backwards));
1524+
return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1525+
}
1526+
15291527
function exceededLimit(max, position) {
15301528
position = isUndefined(position) ? getPosition() : position;
15311529
var exceededMin = max !== true && orient(position) < orient(getLimit(false));

dist/js/splide.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/splide.min.js.gz

-29 Bytes
Binary file not shown.

dist/js/splide.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splidejs/splide",
3-
"version": "4.0.5",
3+
"version": "4.0.6",
44
"description": "Splide is a lightweight, flexible and accessible slider/carousel. No dependencies, no Lighthouse errors.",
55
"author": "Naotoshi Fujita",
66
"license": "MIT",

src/js/components/Autoplay/test/event.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ describe( 'Autoplay', () => {
6767

6868
// Around 2000ms
6969
await wait( 400 + 50 );
70-
expect( progressRate ).toBeLessThan( 0.1 );
70+
expect( progressRate ).toBeLessThan( 0.15 );
7171
} );
7272
} );

src/js/components/Move/Move.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,9 @@ export function Move( Splide: Splide, Components: Components, options: Options )
9090
* @param callback - Optional. A callback function invoked after transition ends.
9191
*/
9292
function move( dest: number, index: number, prev: number, callback?: AnyFunction ): void {
93-
const position = getPosition();
94-
const shifted = shift( position, dest > prev );
95-
const oriented = orient( shifted );
96-
const destination = toPosition( dest );
97-
const shouldShift = dest !== index || abs( shifted - destination ) < abs( position - destination );
98-
99-
const canShift = dest > prev
100-
? oriented >= 0
101-
: oriented <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ];
102-
103-
if ( shouldShift && canShift ) {
93+
if ( dest !== index && canShift( dest > prev ) ) {
10494
cancel();
105-
translate( shifted, true );
95+
translate( shift( getPosition(), dest > prev ), true );
10696
}
10797

10898
set( MOVING );
@@ -139,15 +129,15 @@ export function Move( Splide: Splide, Components: Components, options: Options )
139129
}
140130

141131
/**
142-
* Loops the provided position if it exceeds bounds.
132+
* Loops the provided position if it exceeds bounds (limit indices).
143133
*
144134
* @param position - A position to loop.
145135
*/
146136
function loop( position: number ): number {
147137
if ( Splide.is( LOOP ) ) {
148-
const diff = orient( position - getPosition() );
149-
const exceededMin = exceededLimit( false, position ) && diff < 0;
150-
const exceededMax = exceededLimit( true, position ) && diff > 0;
138+
const index = toIndex( position );
139+
const exceededMax = index > Components.Controller.getEnd();
140+
const exceededMin = index < 0;
151141

152142
if ( exceededMin || exceededMax ) {
153143
position = shift( position, exceededMax );
@@ -267,6 +257,20 @@ export function Move( Splide: Splide, Components: Components, options: Options )
267257
return toPosition( max ? Components.Controller.getEnd() : 0, !! options.trimSpace );
268258
}
269259

260+
/**
261+
* Checks if there is enough width to shift the slider.
262+
*
263+
* @param backwards - `true` for checking backwards, or `false` for doing forwards.
264+
*
265+
* @return `true` if the slider can be shifted for the specified direction, or otherwise `false`.
266+
*/
267+
function canShift( backwards: boolean ): boolean {
268+
const shifted = orient( shift( getPosition(), backwards ) );
269+
return backwards
270+
? shifted >= 0
271+
: shifted <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ];
272+
}
273+
270274
/**
271275
* Checks if the provided position exceeds the minimum or maximum limit or not.
272276
*

0 commit comments

Comments
 (0)