Move.ts 8.8 KB

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