123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import { EVENT_DRAG, EVENT_DRAGGED, EVENT_DRAGGING } from '../../constants/events';
- import { FADE, LOOP, SLIDE } from '../../constants/types';
- import { EventInterface } from '../../constructors';
- import { Splide } from '../../core/Splide/Splide';
- import { BaseComponent, Components, Options } from '../../types';
- import { abs, clamp, min, prevent, sign } from '../../utils';
- import { FRICTION, POINTER_DOWN_EVENTS, POINTER_MOVE_EVENTS, POINTER_UP_EVENTS, SAMPLING_INTERVAL } from './constants';
- /**
- * The interface for the Drag component.
- *
- * @since 3.0.0
- */
- export interface DragComponent extends BaseComponent {
- }
- /**
- * The component for dragging the slider.
- *
- * @since 3.0.0
- *
- * @param Splide - A Splide instance.
- * @param Components - A collection of components.
- * @param options - Options.
- *
- * @return A Drag component object.
- */
- export function Drag( Splide: Splide, Components: Components, options: Options ): DragComponent {
- const { emit, bind, unbind } = EventInterface( Splide );
- const { track } = Components.Elements;
- const { resolve, orient } = Components.Direction;
- const { listSize } = Components.Layout;
- const { go, getEnd } = Components.Controller;
- const { Move, Scroll } = Components;
- const { translate, toIndex, getPosition, isExceeded } = Move;
- const isSlide = Splide.is( SLIDE );
- const isFade = Splide.is( FADE );
- const isFree = options.drag === 'free';
- /**
- * The coord where a pointer becomes active.
- */
- let startCoord: number;
- /**
- * Keeps the last time when the component detects dragging.
- */
- let lastTime: number;
- /**
- * The base slider position where the diff of coords is applied.
- */
- let basePosition: number;
- /**
- * The base coord to calculate the diff of coords.
- */
- let baseCoord: number;
- /**
- * The base time when the base position and the base coord are saved.
- */
- let baseTime: number;
- /**
- * Keeps the last TouchEvent/MouseEvent object.
- */
- let lastEvent: TouchEvent | MouseEvent;
- /**
- * Indicates whether the user is dragging the slider or not.
- */
- let moving: boolean;
- /**
- * Indicates whether the user drags the slider by the mouse or not.
- */
- let isMouse: boolean;
- /**
- * The target element to attach listeners.
- */
- let target: Window | HTMLElement;
- /**
- * Indicates whether the slider exceeds borders or not.
- */
- let exceeded: boolean;
- /**
- * Called when the component is mounted.
- */
- function mount(): void {
- if ( options.drag ) {
- bind( track, POINTER_DOWN_EVENTS, onPointerDown );
- }
- }
- /**
- * Called when the user clicks or touches the slider.
- *
- * @param e - A TouchEvent or MouseEvent object
- */
- function onPointerDown( e: TouchEvent | MouseEvent ): void {
- isMouse = e.type === 'mousedown';
- target = isMouse ? window : track;
- if ( ! ( isMouse && ( e as MouseEvent ).button ) ) {
- if ( ! Move.isBusy() ) {
- bind( target, POINTER_MOVE_EVENTS, onPointerMove );
- bind( target, POINTER_UP_EVENTS, onPointerUp );
- Move.cancel();
- Scroll.cancel();
- startCoord = getCoord( e );
- } else {
- prevent( e );
- }
- }
- }
- /**
- * Called while the user moves the pointer on the slider.
- *
- * @param e - A TouchEvent or MouseEvent object
- */
- function onPointerMove( e: TouchEvent | MouseEvent ): void {
- if ( e.cancelable ) {
- const min = options.dragMinThreshold || 15;
- if ( isMouse || abs( getCoord( e ) - startCoord ) > min ) {
- moving = true;
- onDrag();
- }
- if ( moving ) {
- onDragging( e );
- prevent( e, true );
- }
- } else {
- onPointerUp( e );
- }
- }
- /**
- * Called when the user releases pointing devices.
- * Be aware that the TouchEvent object provided by the `touchend` does not contain `Touch` objects,
- * which means the last touch position is not available.
- *
- * @param e - A TouchEvent or MouseEvent object
- */
- function onPointerUp( e: TouchEvent | MouseEvent ): void {
- unbind( target, `${ POINTER_MOVE_EVENTS } ${ POINTER_UP_EVENTS }` );
- moving = false;
- if ( lastEvent ) {
- onDragged( e );
- lastEvent = null;
- }
- }
- /**
- * Called when the user starts dragging the slider.
- */
- function onDrag(): void {
- bind( track, 'click', e => {
- unbind( track, 'click' );
- prevent( e, true );
- }, { capture: true } );
- emit( EVENT_DRAG );
- }
- /**
- * Called while the user is dragging the slider.
- *
- * @param e - A TouchEvent or MouseEvent object
- */
- function onDragging( e: TouchEvent | MouseEvent ): void {
- const { timeStamp } = e;
- const expired = ! lastTime || ( timeStamp - lastTime > SAMPLING_INTERVAL );
- if ( expired || isExceeded() !== exceeded ) {
- basePosition = getPosition();
- baseCoord = getCoord( e );
- baseTime = timeStamp;
- }
- exceeded = isExceeded();
- lastTime = timeStamp;
- lastEvent = e;
- if ( ! isFade ) {
- translate( basePosition + constrain( getCoord( e ) - baseCoord ) );
- }
- emit( EVENT_DRAGGING );
- }
- /**
- * Called when the user finishes dragging.
- *
- * @param e - A TouchEvent or MouseEvent object
- */
- function onDragged( e: TouchEvent | MouseEvent ): void {
- const velocity = computeVelocity( e );
- if ( isFade ) {
- go( Splide.index + orient( sign( velocity ) ) );
- } else {
- const destination = computeDestination( velocity );
- if ( isFree ) {
- Scroll.scroll( destination );
- } else {
- go( computeIndex( destination ), true );
- }
- }
- lastTime = 0;
- emit( EVENT_DRAGGED );
- }
- /**
- * Computes the drag velocity.
- *
- * @param e - A TouchEvent or MouseEvent object
- *
- * @return The drag velocity.
- */
- function computeVelocity( e: TouchEvent | MouseEvent ): number {
- if ( Splide.is( LOOP ) || ! isExceeded() ) {
- const diffCoord = getCoord( lastEvent ) - baseCoord;
- const diffTime = lastEvent.timeStamp - baseTime;
- const isFlick = e.timeStamp - lastTime < SAMPLING_INTERVAL;
- if ( diffTime && isFlick ) {
- return diffCoord / diffTime;
- }
- }
- return 0;
- }
- /**
- * Computes the destination by the velocity and the `flickPower` option.
- *
- * @param velocity - The drag velocity.
- *
- * @return The destination.
- */
- function computeDestination( velocity: number ): number {
- const flickPower = options.flickPower || 600;
- return getPosition() + sign( velocity ) * min(
- abs( velocity ) * flickPower,
- isFree ? Infinity : listSize() * ( options.flickMaxPages || 1 )
- );
- }
- /**
- * Converts the destination to the slide index.
- *
- * @param destination - The target destination.
- *
- * @return The destination index.
- */
- function computeIndex( destination: number ): number {
- const dest = toIndex( destination );
- return isSlide ? clamp( dest, 0, getEnd() ) : dest;
- }
- /**
- * Returns the `pageX` and `pageY` coordinates provided by the event.
- * Be aware that IE does not support both TouchEvent and MouseEvent constructors.
- *
- * @param e - A TouchEvent or MouseEvent object.
- *
- * @return A pageX or pageY coordinate.
- */
- function getCoord( e: TouchEvent | MouseEvent ): number {
- return ( isMouse ? e : ( e as TouchEvent ).touches[ 0 ] )[ resolve( 'pageX' ) ];
- }
- /**
- * Reduces the distance to move by the predefined friction.
- * This does nothing when the slider type is not `slide`, or the position is inside borders.
- *
- * @param diff - Diff to constrain.
- *
- * @return The constrained diff.
- */
- function constrain( diff: number ): number {
- return diff / ( exceeded && isSlide ? FRICTION : 1 );
- }
- return {
- mount,
- };
- }
|