Controller.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { EVENT_REFRESH, EVENT_UPDATED } from '../../constants/events';
  2. import { MOVING, SCROLLING } from '../../constants/states';
  3. import { LOOP, SLIDE } from '../../constants/types';
  4. import { EventInterface } from '../../constructors';
  5. import { Splide } from '../../core/Splide/Splide';
  6. import { AnyFunction, BaseComponent, Components, Options } from '../../types';
  7. import { apply, approximatelyEqual, between, clamp, floor, isString, isUndefined, max } from '../../utils';
  8. /**
  9. * The interface for the Controller component.
  10. *
  11. * @since 3.0.0
  12. */
  13. export interface ControllerComponent extends BaseComponent {
  14. go( control: number | string, allowSameIndex?: boolean, callback?: AnyFunction ): void;
  15. scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void;
  16. getNext( destination?: boolean ): number;
  17. getPrev( destination?: boolean ): number;
  18. getAdjacent( prev: boolean, destination?: boolean ): number;
  19. getEnd(): number;
  20. setIndex( index: number ): void;
  21. getIndex( prev?: boolean ): number;
  22. toIndex( page: number ): number;
  23. toPage( index: number ): number;
  24. toDest( position: number ): number;
  25. hasFocus(): boolean;
  26. isBusy(): boolean;
  27. }
  28. /**
  29. * The component for controlling the slider.
  30. *
  31. * @since 3.0.0
  32. *
  33. * @param Splide - A Splide instance.
  34. * @param Components - A collection of components.
  35. * @param options - Options.
  36. *
  37. * @return A Controller component object.
  38. */
  39. export function Controller( Splide: Splide, Components: Components, options: Options ): ControllerComponent {
  40. const { on } = EventInterface( Splide );
  41. const { Move } = Components;
  42. const { getPosition, getLimit, toPosition } = Move;
  43. const { isEnough, getLength } = Components.Slides;
  44. const isLoop = Splide.is( LOOP );
  45. const isSlide = Splide.is( SLIDE );
  46. const getNext = apply( getAdjacent, false );
  47. const getPrev = apply( getAdjacent, true );
  48. /**
  49. * The current index.
  50. */
  51. let currIndex = options.start || 0;
  52. /**
  53. * The previous index.
  54. */
  55. let prevIndex = currIndex;
  56. /**
  57. * The latest number of slides.
  58. */
  59. let slideCount: number;
  60. /**
  61. * The latest `perMove` value.
  62. */
  63. let perMove: number;
  64. /**
  65. * The latest `perMove` value.
  66. */
  67. let perPage: number;
  68. /**
  69. * Called when the component is mounted.
  70. */
  71. function mount(): void {
  72. init();
  73. on( [ EVENT_UPDATED, EVENT_REFRESH ], init );
  74. }
  75. /**
  76. * Initializes some parameters.
  77. * Needs to check the slides length since the current index may be out of the range after refresh.
  78. * The process order must be Elements -> Controller -> Move.
  79. */
  80. function init(): void {
  81. slideCount = getLength( true );
  82. perMove = options.perMove;
  83. perPage = options.perPage;
  84. const index = clamp( currIndex, 0, slideCount - 1 );
  85. if ( index !== currIndex ) {
  86. currIndex = index;
  87. Move.reposition();
  88. }
  89. }
  90. /**
  91. * Moves the slider by the control pattern.
  92. *
  93. * @see `Splide#go()`
  94. *
  95. * @param control - A control pattern.
  96. * @param allowSameIndex - Optional. Determines whether to allow to go to the current index or not.
  97. * @param callback - Optional. A callback function invoked after transition ends.
  98. */
  99. function go( control: number | string, allowSameIndex?: boolean, callback?: AnyFunction ): void {
  100. if ( ! isBusy() ) {
  101. const dest = parse( control );
  102. const index = loop( dest );
  103. if ( index > -1 && ( allowSameIndex || index !== currIndex ) ) {
  104. setIndex( index );
  105. Move.move( dest, index, prevIndex, callback );
  106. }
  107. }
  108. }
  109. /**
  110. * Scrolls the slider to the specified destination with updating indices.
  111. *
  112. * @param destination - The position to scroll the slider to.
  113. * @param duration - Optional. Specifies the scroll duration.
  114. * @param snap - Optional. Whether to snap the slider to the closest slide or not.
  115. * @param callback - Optional. A callback function invoked after scroll ends.
  116. */
  117. function scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void {
  118. Components.Scroll.scroll( destination, duration, snap, () => {
  119. setIndex( loop( Move.toIndex( Move.getPosition() ) ) );
  120. callback && callback();
  121. } );
  122. }
  123. /**
  124. * Parses the control and returns a slide index.
  125. *
  126. * @param control - A control pattern to parse.
  127. *
  128. * @return A `dest` index.
  129. */
  130. function parse( control: number | string ): number {
  131. let index = currIndex;
  132. if ( isString( control ) ) {
  133. const [ , indicator, number ] = control.match( /([+\-<>])(\d+)?/ ) || [];
  134. if ( indicator === '+' || indicator === '-' ) {
  135. index = computeDestIndex( currIndex + +`${ indicator }${ +number || 1 }`, currIndex );
  136. } else if ( indicator === '>' ) {
  137. index = number ? toIndex( +number ) : getNext( true );
  138. } else if ( indicator === '<' ) {
  139. index = getPrev( true );
  140. }
  141. } else {
  142. index = isLoop ? control : clamp( control, 0, getEnd() );
  143. }
  144. return index;
  145. }
  146. /**
  147. * Returns an adjacent destination index.
  148. *
  149. * @internal
  150. *
  151. * @param prev - Determines whether to return a previous or next index.
  152. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  153. *
  154. * @return An adjacent index if available, or otherwise `-1`.
  155. */
  156. function getAdjacent( prev: boolean, destination?: boolean ): number {
  157. const number = perMove || ( hasFocus() ? 1 : perPage );
  158. const dest = computeDestIndex( currIndex + number * ( prev ? -1 : 1 ), currIndex, ! ( perMove || hasFocus() ) );
  159. if ( dest === -1 && isSlide ) {
  160. if ( ! approximatelyEqual( getPosition(), getLimit( ! prev ), 1 ) ) {
  161. return prev ? 0 : getEnd();
  162. }
  163. }
  164. return destination ? dest : loop( dest );
  165. }
  166. /**
  167. * Converts the desired destination index to the valid one.
  168. * - If the `move` option is `true`, finds the dest index whose position is different with the current one.
  169. * - This may return clone indices if the editor is the loop mode,
  170. * or `-1` if there is no slide to go.
  171. * - There are still slides where the slider can go if borders are between `from` and `dest`.
  172. *
  173. * @param dest - The desired destination.
  174. * @param from - A base index.
  175. * @param snapPage - Optional. Whether to snap a page or not.
  176. *
  177. * @return A converted destination index, including clones.
  178. */
  179. function computeDestIndex( dest: number, from: number, snapPage?: boolean ): number {
  180. if ( isEnough() ) {
  181. const end = getEnd();
  182. const index = computeMovableDestIndex( dest );
  183. if ( index !== dest ) {
  184. from = dest;
  185. dest = index;
  186. snapPage = false;
  187. }
  188. if ( dest < 0 || dest > end ) {
  189. if ( between( 0, dest, from, true ) || between( end, from, dest, true ) ) {
  190. dest = toIndex( toPage( dest ) );
  191. } else {
  192. if ( isLoop ) {
  193. dest = snapPage
  194. ? dest < 0 ? - ( slideCount % perPage || perPage ) : slideCount
  195. : dest;
  196. } else if ( options.rewind ) {
  197. dest = dest < 0 ? end : 0;
  198. } else {
  199. dest = -1;
  200. }
  201. }
  202. } else {
  203. if ( snapPage && dest !== from ) {
  204. dest = toIndex( toPage( from ) + ( dest < from ? -1 : 1 ) );
  205. }
  206. }
  207. } else {
  208. dest = -1;
  209. }
  210. return dest;
  211. }
  212. /**
  213. * Finds the dest index whose position is different with the current one.
  214. * This can be negative or greater than `length - 1`.
  215. *
  216. * @param dest - A dest index.
  217. *
  218. * @return A dest index.
  219. */
  220. function computeMovableDestIndex( dest: number ): number {
  221. if ( isSlide && options.trimSpace === 'move' && dest !== currIndex ) {
  222. const position = getPosition();
  223. while ( position === toPosition( dest, true ) && between( dest, 0, Splide.length - 1, ! options.rewind ) ) {
  224. dest < currIndex ? --dest : ++dest;
  225. }
  226. }
  227. return dest;
  228. }
  229. /**
  230. * Loops the provided index only in the loop mode.
  231. *
  232. * @param index - An index to loop.
  233. *
  234. * @return A looped index.
  235. */
  236. function loop( index: number ): number {
  237. return isLoop ? ( index + slideCount ) % slideCount || 0 : index;
  238. }
  239. /**
  240. * Returns the end index where the slider can go.
  241. * For example, if the slider has 10 slides and the `perPage` option is 3,
  242. * the slider can go to the slide 8 (the index is 7).
  243. *
  244. * @return An end index.
  245. */
  246. function getEnd(): number {
  247. return max( slideCount - ( hasFocus() || ( isLoop && perMove ) ? 1 : perPage ), 0 );
  248. }
  249. /**
  250. * Converts the page index to the slide index.
  251. *
  252. * @param page - A page index to convert.
  253. *
  254. * @return A slide index.
  255. */
  256. function toIndex( page: number ): number {
  257. return clamp( hasFocus() ? page : perPage * page, 0, getEnd() );
  258. }
  259. /**
  260. * Converts the slide index to the page index.
  261. *
  262. * @param index - An index to convert.
  263. *
  264. * @return A page index.
  265. */
  266. function toPage( index: number ): number {
  267. return hasFocus()
  268. ? index
  269. : floor( ( index >= getEnd() ? slideCount - 1 : index ) / perPage );
  270. }
  271. /**
  272. * Converts the destination position to the dest index.
  273. *
  274. * @param destination - A position to convert.
  275. *
  276. * @return A dest index.
  277. */
  278. function toDest( destination: number ): number {
  279. const closest = Move.toIndex( destination );
  280. return isSlide ? clamp( closest, 0, getEnd() ) : closest;
  281. }
  282. /**
  283. * Sets a new index and retains old one.
  284. *
  285. * @param index - A new index to set.
  286. */
  287. function setIndex( index: number ): void {
  288. if ( index !== currIndex ) {
  289. prevIndex = currIndex;
  290. currIndex = index;
  291. }
  292. }
  293. /**
  294. * Returns the current/previous index.
  295. *
  296. * @param prev - Optional. Whether to return previous index or not.
  297. */
  298. function getIndex( prev?: boolean ): number {
  299. return prev ? prevIndex : currIndex;
  300. }
  301. /**
  302. * Verifies if the focus option is available or not.
  303. *
  304. * @return `true` if the slider has the focus option.
  305. */
  306. function hasFocus(): boolean {
  307. return ! isUndefined( options.focus ) || options.isNavigation;
  308. }
  309. /**
  310. * Checks if the slider is moving/scrolling or not.
  311. *
  312. * @return `true` if the slider can move, or otherwise `false`.
  313. */
  314. function isBusy(): boolean {
  315. return Splide.state.is( [ MOVING, SCROLLING ] ) && !! options.waitForTransition;
  316. }
  317. return {
  318. mount,
  319. go,
  320. scroll,
  321. getNext,
  322. getPrev,
  323. getAdjacent,
  324. getEnd,
  325. setIndex,
  326. getIndex,
  327. toIndex,
  328. toPage,
  329. toDest,
  330. hasFocus,
  331. isBusy,
  332. };
  333. }