Skip to content

Commit f3c8df7

Browse files
author
Andrey Nelyubin
committed
Ensuring that drag does not move slides more than options.flickMaxPages allows
1 parent d7e1f08 commit f3c8df7

10 files changed

+43
-13
lines changed

dist/js/splide-renderer.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-renderer.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.

dist/js/splide.cjs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Splide.js
33
* Version : 4.1.4
44
* License : MIT
5-
* Copyright: 2022 Naotoshi Fujita
5+
* Copyright: 2023 Naotoshi Fujita
66
*/
77
'use strict';
88

@@ -2264,7 +2264,11 @@ function Drag(Splide2, Components2, options) {
22642264
} else if (Splide2.is(SLIDE) && exceeded && rewind) {
22652265
Controller.go(exceededLimit(true) ? ">" : "<");
22662266
} else {
2267-
Controller.go(Controller.toDest(destination), true);
2267+
var sign1 = sign(velocity);
2268+
var expectedDestination = Controller.toDest(destination);
2269+
var maxPages = options.flickMaxPages || 1;
2270+
var goTo = sign1 < 0 ? min(expectedDestination, Splide2.index + maxPages) : max(expectedDestination, Splide2.index - maxPages);
2271+
Controller.go(goTo, true);
22682272
}
22692273

22702274
reduce(true);

dist/js/splide.esm.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
66
* Splide.js
77
* Version : 4.1.4
88
* License : MIT
9-
* Copyright: 2022 Naotoshi Fujita
9+
* Copyright: 2023 Naotoshi Fujita
1010
*/
1111
var MEDIA_PREFERS_REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
1212
var CREATED = 1;
@@ -2259,7 +2259,11 @@ function Drag(Splide2, Components2, options) {
22592259
} else if (Splide2.is(SLIDE) && exceeded && rewind) {
22602260
Controller.go(exceededLimit(true) ? ">" : "<");
22612261
} else {
2262-
Controller.go(Controller.toDest(destination), true);
2262+
var sign1 = sign(velocity);
2263+
var expectedDestination = Controller.toDest(destination);
2264+
var maxPages = options.flickMaxPages || 1;
2265+
var goTo = sign1 < 0 ? min(expectedDestination, Splide2.index + maxPages) : max(expectedDestination, Splide2.index - maxPages);
2266+
Controller.go(goTo, true);
22632267
}
22642268

22652269
reduce(true);

dist/js/splide.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
66
* Splide.js
77
* Version : 4.1.4
88
* License : MIT
9-
* Copyright: 2022 Naotoshi Fujita
9+
* Copyright: 2023 Naotoshi Fujita
1010
*/
1111
(function (global, factory) {
1212
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Splide = factory());
@@ -2257,7 +2257,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
22572257
} else if (Splide2.is(SLIDE) && exceeded && rewind) {
22582258
Controller.go(exceededLimit(true) ? ">" : "<");
22592259
} else {
2260-
Controller.go(Controller.toDest(destination), true);
2260+
var sign1 = sign(velocity);
2261+
var expectedDestination = Controller.toDest(destination);
2262+
var maxPages = options.flickMaxPages || 1;
2263+
var goTo = sign1 < 0 ? min(expectedDestination, Splide2.index + maxPages) : max(expectedDestination, Splide2.index - maxPages);
2264+
Controller.go(goTo, true);
22612265
}
22622266

22632267
reduce(true);

dist/js/splide.min.js

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

dist/js/splide.min.js.gz

30 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.

src/js/components/Drag/Drag.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FADE, LOOP, SLIDE } from '../../constants/types';
66
import { EventInterface } from '../../constructors';
77
import { Splide } from '../../core/Splide/Splide';
88
import { BaseComponent, Components, Options } from '../../types';
9-
import { abs, isObject, matches, min, noop, prevent, sign, timeOf } from '../../utils';
9+
import { abs, isObject, matches, min, max, noop, prevent, sign, timeOf } from '../../utils';
1010
import { FRICTION, LOG_INTERVAL, POINTER_DOWN_EVENTS, POINTER_MOVE_EVENTS, POINTER_UP_EVENTS } from './constants';
1111

1212

@@ -237,7 +237,11 @@ export function Drag( Splide: Splide, Components: Components, options: Options )
237237
} else if ( Splide.is( SLIDE ) && exceeded && rewind ) {
238238
Controller.go( exceededLimit( true ) ? '>' : '<' );
239239
} else {
240-
Controller.go( Controller.toDest( destination ), true );
240+
const sign1 = sign( velocity );
241+
const expectedDestination = Controller.toDest( destination );
242+
const maxPages = options.flickMaxPages || 1;
243+
const goTo = sign1 < 0 ? min( expectedDestination, Splide.index + maxPages ) : max( expectedDestination, Splide.index - maxPages );
244+
Controller.go( goTo, true );
241245
}
242246

243247
reduce( true );

src/js/components/Drag/test/general.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ describe( 'Drag', () => {
6060
expect( splide.index ).toBe( 0 );
6161
} );
6262

63+
test.each( [ 1, 2 ] )( 'should change the slide index no longer than flickMaxPages (%s).', ( flickMaxPages ) => {
64+
const width = 600;
65+
const xOffset = width * flickMaxPages;
66+
const splide = init( { speed: 0, width, flickMaxPages } );
67+
const track = splide.Components.Elements.track;
68+
69+
fireWithCoord( track, 'mousedown', { x: 0, timeStamp: 1 } );
70+
fireWithCoord( window, 'mousemove', { x: 1, timeStamp: 1 } );
71+
fireWithCoord( window, 'mousemove', { x: -20 - xOffset, timeStamp: 100 } );
72+
fireWithCoord( window, 'mouseup', { x: -20 - xOffset, timeStamp: 100 } );
73+
74+
expect( splide.index ).toBe( flickMaxPages );
75+
} );
76+
6377
test( 'should start moving the slider immediately if the pointing device is a mouse.', () => {
6478
const splide = init();
6579
const onDrag = jest.fn();

0 commit comments

Comments
 (0)