Move.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {
  2. EVENT_MOUNTED,
  3. EVENT_MOVE,
  4. EVENT_MOVED,
  5. EVENT_REFRESH,
  6. EVENT_RESIZED,
  7. EVENT_SHIFTED,
  8. EVENT_UPDATED,
  9. } from '../../constants/events';
  10. import { IDLE, MOVING } from '../../constants/states';
  11. import { FADE, LOOP, SLIDE } from '../../constants/types';
  12. import { Splide } from '../../core/Splide/Splide';
  13. import { AnyFunction, BaseComponent, Components, Options, TransitionComponent } from '../../types';
  14. import { abs, ceil, clamp, isUndefined, rect, style } from '../../utils';
  15. import { EventInterface } from '@splidejs/utils';
  16. /**
  17. * The interface for the Move component.
  18. *
  19. * @since 3.0.0
  20. */
  21. export interface MoveComponent extends BaseComponent {
  22. move( dest: number, index: number, prev: number, callback?: AnyFunction ): void;
  23. jump( index: number ): void;
  24. translate( position: number, preventLoop?: boolean ): void;
  25. shift( position: number, backwards: boolean ): number;
  26. cancel(): void;
  27. toIndex( position: number ): number;
  28. toPosition( index: number, trimming?: boolean ): number;
  29. getPosition(): number;
  30. getLimit( max: boolean ): number;
  31. exceededLimit( max?: boolean | undefined, position?: number ): boolean;
  32. /** @internal */
  33. reposition(): void;
  34. }
  35. /**
  36. * The component for moving the slider.
  37. *
  38. * @since 3.0.0
  39. *
  40. * @param Splide - A Splide instance.
  41. * @param Components - A collection of components.
  42. * @param options - Options.
  43. * @param event - An EventInterface instance.
  44. *
  45. * @return A Move component object.
  46. */
  47. export function Move(
  48. Splide: Splide,
  49. Components: Components,
  50. options: Options,
  51. event: EventInterface
  52. ): MoveComponent {
  53. const { on, emit } = event;
  54. const { set } = Splide.state;
  55. const { slideSize, getPadding, totalSize, listSize, sliderSize } = Components.Layout;
  56. const { resolve, orient } = Components.Direction;
  57. const { list, track } = Components.Elements;
  58. /**
  59. * Holds the Transition component.
  60. */
  61. let Transition: TransitionComponent;
  62. /**
  63. * Called when the component is mounted.
  64. */
  65. function mount(): void {
  66. Transition = Components.Transition;
  67. on( [ EVENT_MOUNTED, EVENT_RESIZED, EVENT_UPDATED, EVENT_REFRESH ], reposition );
  68. }
  69. /**
  70. * Repositions the slider.
  71. * - Do not call `cancel()` here because LazyLoad may emit resize while transitioning.
  72. * - iOS Safari emits window resize event while the user swipes the slider because of the bottom bar.
  73. */
  74. function reposition(): void {
  75. if ( ! Components.Controller.isBusy() ) {
  76. Components.Scroll.cancel();
  77. jump( Splide.index );
  78. Components.Slides.update();
  79. }
  80. }
  81. /**
  82. * Moves the slider to the dest index with the Transition component.
  83. *
  84. * @param dest - A destination index to go to, including clones'.
  85. * @param index - A slide index.
  86. * @param prev - A previous index.
  87. * @param callback - Optional. A callback function invoked after transition ends.
  88. */
  89. function move( dest: number, index: number, prev: number, callback?: AnyFunction ): void {
  90. if ( dest !== index && canShift( dest > prev ) ) {
  91. cancel();
  92. translate( shift( getPosition(), dest > prev ), true );
  93. }
  94. set( MOVING );
  95. emit( EVENT_MOVE, index, prev, dest );
  96. Transition.start( index, () => {
  97. set( IDLE );
  98. emit( EVENT_MOVED, index, prev, dest );
  99. callback && callback();
  100. } );
  101. }
  102. /**
  103. * Jumps to the slide at the specified index.
  104. *
  105. * @param index - An index to jump to.
  106. */
  107. function jump( index: number ): void {
  108. translate( toPosition( index, true ) );
  109. }
  110. /**
  111. * Moves the slider to the provided position.
  112. *
  113. * @param position - The position to move to.
  114. * @param preventLoop - Optional. If `true`, sets the provided position as is.
  115. */
  116. function translate( position: number, preventLoop?: boolean ): void {
  117. if ( ! Splide.is( FADE ) ) {
  118. const destination = preventLoop ? position : loop( position );
  119. style( list, 'transform', `translate${ resolve( 'X' ) }(${ destination }px)` );
  120. position !== destination && emit( EVENT_SHIFTED );
  121. }
  122. }
  123. /**
  124. * Loops the provided position if it exceeds bounds (limit indices).
  125. *
  126. * @param position - A position to loop.
  127. */
  128. function loop( position: number ): number {
  129. if ( Splide.is( LOOP ) ) {
  130. const index = toIndex( position );
  131. const exceededMax = index > Components.Controller.getEnd();
  132. const exceededMin = index < 0;
  133. if ( exceededMin || exceededMax ) {
  134. position = shift( position, exceededMax );
  135. }
  136. }
  137. return position;
  138. }
  139. /**
  140. * Adds or subtracts the slider width to the provided position.
  141. *
  142. * @param position - A position to shift.
  143. * @param backwards - Determines whether to shift the slider backwards or forwards.
  144. *
  145. * @return The shifted position.
  146. */
  147. function shift( position: number, backwards: boolean ): number {
  148. const excess = position - getLimit( backwards );
  149. const size = sliderSize();
  150. position -= orient( size * ( ceil( abs( excess ) / size ) || 1 ) ) * ( backwards ? 1 : -1 );
  151. return position;
  152. }
  153. /**
  154. * Cancels transition.
  155. */
  156. function cancel(): void {
  157. translate( getPosition(), true );
  158. Transition.cancel();
  159. }
  160. /**
  161. * Returns the closest index to the position.
  162. *
  163. * @param position - A position to convert.
  164. *
  165. * @return The closest index to the position.
  166. */
  167. function toIndex( position: number ): number {
  168. const Slides = Components.Slides.get();
  169. let index = 0;
  170. let minDistance = Infinity;
  171. for ( let i = 0; i < Slides.length; i++ ) {
  172. const slideIndex = Slides[ i ].index;
  173. const distance = abs( toPosition( slideIndex, true ) - position );
  174. if ( distance <= minDistance ) {
  175. minDistance = distance;
  176. index = slideIndex;
  177. } else {
  178. break;
  179. }
  180. }
  181. return index;
  182. }
  183. /**
  184. * Converts the slide index to the position.
  185. *
  186. * @param index - An index to convert.
  187. * @param trimming - Optional. Whether to trim edge spaces or not.
  188. *
  189. * @return The position corresponding with the index.
  190. */
  191. function toPosition( index: number, trimming?: boolean ): number {
  192. const position = orient( totalSize( index - 1 ) - offset( index ) );
  193. return trimming ? trim( position ) : position;
  194. }
  195. /**
  196. * Returns the current position.
  197. *
  198. * @return The position of the list element.
  199. */
  200. function getPosition(): number {
  201. const left = resolve( 'left' );
  202. return rect( list )[ left ] - rect( track )[ left ] + orient( getPadding( false ) );
  203. }
  204. /**
  205. * Trims spaces on the edge of the slider.
  206. *
  207. * @param position - A position to trim.
  208. *
  209. * @return A trimmed position.
  210. */
  211. function trim( position: number ): number {
  212. if ( options.trimSpace && Splide.is( SLIDE ) ) {
  213. position = clamp( position, 0, orient( sliderSize( true ) - listSize() ) );
  214. }
  215. return position;
  216. }
  217. /**
  218. * Returns the offset amount.
  219. *
  220. * @param index - An index.
  221. */
  222. function offset( index: number ): number {
  223. const { focus } = options;
  224. return focus === 'center' ? ( listSize() - slideSize( index, true ) ) / 2 : +focus * slideSize( index ) || 0;
  225. }
  226. /**
  227. * Returns the limit number that the slider can move to.
  228. *
  229. * @param max - Determines whether to return the maximum or minimum limit.
  230. *
  231. * @return The border number.
  232. */
  233. function getLimit( max: boolean ): number {
  234. return toPosition( max ? Components.Controller.getEnd() : 0, !! options.trimSpace );
  235. }
  236. /**
  237. * Checks if there is enough width to shift the slider.
  238. *
  239. * @param backwards - `true` for checking backwards, or `false` for doing forwards.
  240. *
  241. * @return `true` if the slider can be shifted for the specified direction, or otherwise `false`.
  242. */
  243. function canShift( backwards: boolean ): boolean {
  244. const shifted = orient( shift( getPosition(), backwards ) );
  245. return backwards
  246. ? shifted >= 0
  247. : shifted <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ];
  248. }
  249. /**
  250. * Checks if the provided position exceeds the minimum or maximum limit or not.
  251. *
  252. * @param max - Optional. `true` for testing max, `false` for min, and `undefined` for both.
  253. * @param position - Optional. A position to test. If omitted, tests the current position.
  254. *
  255. * @return `true` if the position exceeds the limit, or otherwise `false`.
  256. */
  257. function exceededLimit( max?: boolean | undefined, position?: number ): boolean {
  258. position = isUndefined( position ) ? getPosition() : position;
  259. const exceededMin = max !== true && orient( position ) < orient( getLimit( false ) );
  260. const exceededMax = max !== false && orient( position ) > orient( getLimit( true ) );
  261. return exceededMin || exceededMax;
  262. }
  263. return {
  264. mount,
  265. move,
  266. jump,
  267. translate,
  268. shift,
  269. cancel,
  270. toIndex,
  271. toPosition,
  272. getPosition,
  273. getLimit,
  274. exceededLimit,
  275. reposition,
  276. };
  277. }