Move.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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, 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, 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, trimming?: boolean ): number;
  27. getPosition(): number;
  28. getRate(): 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 } = Splide.state;
  50. const { Slides } = Components;
  51. const { slideSize, getPadding, listSize, sliderSize, totalSize } = 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. /**
  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. 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. Transition.cancel();
  91. if ( dest !== index && canShift( dest > prev ) ) {
  92. translate( shift( getPosition(), dest > prev ), true );
  93. }
  94. indices = [ index, prev, dest ];
  95. set( MOVING );
  96. emit( EVENT_MOVE, index, prev, dest );
  97. Transition.start( index, () => {
  98. set( IDLE );
  99. emit( EVENT_MOVED, index, prev, dest );
  100. callback && callback();
  101. } );
  102. }
  103. /**
  104. * Jumps to the slide at the specified index (silently).
  105. *
  106. * @param index - An index to jump to.
  107. */
  108. function jump( index: number ): void {
  109. translate( toPosition( index, true ) );
  110. }
  111. /**
  112. * Moves the slider to the provided position.
  113. *
  114. * @param position - The position to move to.
  115. * @param preventLoop - Optional. If `true`, sets the provided position as is.
  116. */
  117. function translate( position: number, preventLoop?: boolean ): void {
  118. if ( ! Splide.is( FADE ) ) {
  119. const destination = preventLoop ? position : loop( position );
  120. style( list, 'transform', `translate${ resolve( 'X' ) }(${ destination }px)` );
  121. position !== destination && emit( EVENT_SHIFTED );
  122. }
  123. }
  124. /**
  125. * Loops the provided position if it exceeds bounds (limit indices).
  126. *
  127. * @param position - A position to loop.
  128. *
  129. * @return A looped position.
  130. */
  131. function loop( position: number ): number {
  132. if ( Splide.is( LOOP ) ) {
  133. const diff = orient( position ) - orient( getPosition() );
  134. if ( diff && exceededLimit( diff > 0, position ) ) {
  135. position = shift( position, diff > 0 );
  136. }
  137. }
  138. return position;
  139. }
  140. /**
  141. * Adds or subtracts the slider width to the provided position.
  142. *
  143. * @param position - A position to shift.
  144. * @param backwards - Determines whether to shift the slider backwards or forwards.
  145. *
  146. * @return The shifted position.
  147. */
  148. function shift( position: number, backwards: boolean ): number {
  149. const excess = position - getLimit( backwards );
  150. const size = sliderSize();
  151. position -= orient( size * ( ceil( abs( excess ) / size ) || 1 ) ) * ( backwards ? 1 : -1 );
  152. return position;
  153. }
  154. /**
  155. * Cancels transition.
  156. */
  157. function cancel(): void {
  158. if ( Splide.state.is( MOVING ) && indices ) {
  159. translate( getPosition(), true );
  160. Transition.cancel();
  161. emit( EVENT_MOVED, ...indices );
  162. }
  163. }
  164. /**
  165. * Returns the closest index to the position.
  166. *
  167. * @param position - A position to convert.
  168. *
  169. * @return The closest index to the position.
  170. */
  171. function toIndex( position: number ): number {
  172. const slides = Slides.get();
  173. let index = 0;
  174. let minDistance = Infinity;
  175. for ( let i = 0; i < slides.length; i++ ) {
  176. const slideIndex = slides[ i ].index;
  177. const distance = abs( toPosition( slideIndex, true ) - position );
  178. if ( distance <= minDistance ) {
  179. minDistance = distance;
  180. index = slideIndex;
  181. } else {
  182. break;
  183. }
  184. }
  185. return index;
  186. }
  187. /**
  188. * Converts the slide index to the position.
  189. *
  190. * @param index - An index to convert.
  191. * @param trimming - Optional. Whether to trim edge spaces or not.
  192. *
  193. * @return The position corresponding with the index.
  194. */
  195. function toPosition( index: number, trimming?: boolean ): number {
  196. const position = orient( totalSize( index - 1 ) - offset( index ) );
  197. return trimming ? trim( position ) : position;
  198. }
  199. /**
  200. * Returns the current position.
  201. *
  202. * @return The position of the list element.
  203. */
  204. function getPosition(): number {
  205. const left = resolve( 'left' );
  206. return rect( list )[ left ] - rect( track )[ left ] + orient( getPadding( false ) );
  207. }
  208. /**
  209. * Returns the carousel progress rate.
  210. *
  211. * @return The progress rate.
  212. */
  213. function getRate(): number {
  214. let rate;
  215. if ( Splide.is( FADE ) ) {
  216. rate = Splide.index / ( Splide.length - 1 );
  217. } else {
  218. const isLoop = Splide.is( LOOP );
  219. const position = orient( getPosition() );
  220. const min = orient( getLimit( false ) );
  221. const max = orient( getLimit( true ) );
  222. const size = sliderSize();
  223. const curr = ( position - min ) % size;
  224. const base = isLoop ? size : max - min;
  225. rate = ( curr / base ) || 0;
  226. if ( isLoop && rate < 0 ) {
  227. rate += 1;
  228. }
  229. }
  230. return clamp( rate, 0, 1 );
  231. }
  232. /**
  233. * Trims spaces on the edge of the slider.
  234. *
  235. * @param position - A position to trim.
  236. *
  237. * @return A trimmed position.
  238. */
  239. function trim( position: number ): number {
  240. if ( options.trimSpace && Splide.is( SLIDE ) ) {
  241. position = clamp( position, 0, orient( sliderSize( true ) - listSize() ) );
  242. }
  243. return position;
  244. }
  245. /**
  246. * Returns the offset amount.
  247. *
  248. * @param index - An index.
  249. */
  250. function offset( index: number ): number {
  251. const { focus } = options;
  252. return focus === 'center'
  253. ? ( listSize() - slideSize( index, true ) ) / 2
  254. : +focus * slideSize( index ) || 0;
  255. }
  256. /**
  257. * Returns the limit number that the slider can move to.
  258. *
  259. * @param max - Determines whether to return the maximum or minimum limit.
  260. *
  261. * @return The border number.
  262. */
  263. function getLimit( max: boolean ): number {
  264. return toPosition( max ? Components.Controller.getEnd() : 0, !! options.trimSpace );
  265. }
  266. /**
  267. * Checks if there is enough width to shift the slider.
  268. *
  269. * @param backwards - `true` for checking backwards, or `false` for doing forwards.
  270. *
  271. * @return `true` if the slider can be shifted for the specified direction, or otherwise `false`.
  272. */
  273. function canShift( backwards: boolean ): boolean {
  274. const padding = getPadding( false );
  275. const shifted = orient( shift( getPosition(), backwards ) );
  276. return backwards
  277. ? shifted >= padding
  278. : shifted <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ] + 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. };