Move.ts 8.8 KB

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