Move.ts 9.9 KB

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