Controller.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { Components, ControllerComponent, Options } from '@splidejs/splide';
  2. import { EVENT_REFRESH, EVENT_SCROLLED, EVENT_UPDATED } from '../../constants/events';
  3. import { LOOP } from '../../constants/types';
  4. import { Splide } from '../../core/Splide/Splide';
  5. import { EventInterface } from '../../constructors';
  6. import { between, clamp, floor, isString, isUndefined, max } from '../../utils';
  7. /**
  8. * The component for controlling the slider.
  9. *
  10. * @since 3.0.0
  11. *
  12. * @param Splide - A Splide instance.
  13. * @param Components - A collection of components.
  14. * @param options - Options.
  15. *
  16. * @return A Controller component object.
  17. */
  18. export function Controller( Splide: Splide, Components: Components, options: Options ): ControllerComponent {
  19. const { on } = EventInterface( Splide );
  20. const { Move } = Components;
  21. const { isEnough, getLength } = Components.Slides;
  22. const isLoop = Splide.is( LOOP );
  23. /**
  24. * The current index.
  25. */
  26. let currIndex = options.start || 0;
  27. /**
  28. * The previous index.
  29. */
  30. let prevIndex = currIndex;
  31. /**
  32. * The latest number of slides.
  33. */
  34. let slideCount = getLength( true );
  35. /**
  36. * The latest `perMove` value.
  37. */
  38. let perMove = options.perMove;
  39. /**
  40. * The latest `perMove` value.
  41. */
  42. let perPage = options.perPage;
  43. /**
  44. * Called when the component is mounted.
  45. */
  46. function mount(): void {
  47. Move.jump( currIndex );
  48. on( [ EVENT_UPDATED, EVENT_REFRESH ], () => {
  49. slideCount = getLength( true );
  50. perMove = options.perMove;
  51. perPage = options.perPage;
  52. } );
  53. on( EVENT_SCROLLED, () => {
  54. setIndex( Move.toIndex( Move.getPosition() ) );
  55. }, 0 );
  56. }
  57. /**
  58. * Moves the slider by the control pattern.
  59. *
  60. * @see `Splide#go()`
  61. *
  62. * @param control - A control pattern.
  63. * @param allowSameIndex - Optional. Determines whether to allow to go to the current index or not.
  64. */
  65. function go( control: number | string, allowSameIndex?: boolean ): void {
  66. const dest = parse( control );
  67. const index = loop( dest );
  68. if ( ! Move.isBusy() && index > -1 && ( allowSameIndex || index !== currIndex ) ) {
  69. setIndex( index );
  70. Move.move( dest, index, prevIndex );
  71. }
  72. }
  73. /**
  74. * Parses the control and returns a slide index.
  75. *
  76. * @param control - A control pattern to parse.
  77. */
  78. function parse( control: number | string ): number {
  79. let index = currIndex;
  80. if ( isString( control ) ) {
  81. const [ , indicator, number ] = control.match( /([+\-<>])(\d+)?/ ) || [];
  82. if ( indicator === '+' || indicator === '-' ) {
  83. index = computeDestIndex( currIndex + +`${ indicator }${ +number || 1 }`, currIndex, true );
  84. } else if ( indicator === '>' ) {
  85. index = number ? toIndex( +number ) : getNext( true );
  86. } else if ( indicator === '<' ) {
  87. index = getPrev( true );
  88. }
  89. } else {
  90. if ( isLoop ) {
  91. index = clamp( control, -perPage, slideCount + perPage - 1 );
  92. } else {
  93. index = clamp( control, 0, getEnd() );
  94. }
  95. }
  96. return index;
  97. }
  98. /**
  99. * Returns a next destination index.
  100. *
  101. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  102. *
  103. * @return A next index if available, or otherwise `-1`.
  104. */
  105. function getNext( destination?: boolean ): number {
  106. return getAdjacent( false, destination );
  107. }
  108. /**
  109. * Returns a previous destination index.
  110. *
  111. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  112. *
  113. * @return A previous index if available, or otherwise `-1`.
  114. */
  115. function getPrev( destination?: boolean ): number {
  116. return getAdjacent( true, destination );
  117. }
  118. /**
  119. * Returns an adjacent destination index.
  120. *
  121. * @param prev - Determines whether to return a previous or next index.
  122. * @param destination - Optional. Determines whether to get a destination index or a slide one.
  123. *
  124. * @return An adjacent index if available, or otherwise `-1`.
  125. */
  126. function getAdjacent( prev: boolean, destination?: boolean ): number {
  127. const dest = computeDestIndex( currIndex + getPerMove() * ( prev ? -1 : 1 ), currIndex );
  128. return destination ? dest : loop( dest );
  129. }
  130. /**
  131. * Converts the desired destination index to the valid one.
  132. * - This may return clone indices if the editor is the loop mode,
  133. * or `-1` if there is no slide to go.
  134. * - There are still slides where the slider can go if borders are between `from` and `dest`.
  135. *
  136. * @param dest - The desired destination.
  137. * @param from - A base index.
  138. * @param incremental - Optional. Whether the control is incremental or not.
  139. *
  140. * @return A converted destination index, including clones.
  141. */
  142. function computeDestIndex( dest: number, from: number, incremental?: boolean ): number {
  143. if ( isEnough() ) {
  144. const end = getEnd();
  145. // Will overrun:
  146. if ( dest < 0 || dest > end ) {
  147. if ( between( 0, dest, from, true ) || between( end, from, dest, true ) ) {
  148. dest = toIndex( toPage( dest ) );
  149. } else {
  150. if ( isLoop ) {
  151. dest = perMove
  152. ? dest
  153. : dest < 0 ? - ( slideCount % perPage || perPage ) : slideCount;
  154. } else if ( options.rewind ) {
  155. dest = dest < 0 ? end : 0;
  156. } else {
  157. dest = -1;
  158. }
  159. }
  160. } else {
  161. if ( ! isLoop && ! incremental && dest !== from ) {
  162. dest = toIndex( toPage( from ) + ( dest < from ? -1 : 1 ) );
  163. }
  164. }
  165. } else {
  166. dest = -1;
  167. }
  168. return dest;
  169. }
  170. /**
  171. * Returns the end index where the slider can go.
  172. * For example, if the slider has 10 slides and the `perPage` option is 3,
  173. * the slider can go to the slide 8 (the index is 7).
  174. *
  175. * @return An end index.
  176. */
  177. function getEnd(): number {
  178. let end = slideCount - perPage;
  179. if ( hasFocus() || ( isLoop && perMove ) ) {
  180. end = slideCount - 1;
  181. }
  182. return max( end, 0 );
  183. }
  184. /**
  185. * Loops the provided index only in the loop mode.
  186. *
  187. * @param index - An index to loop.
  188. *
  189. * @return A looped index.
  190. */
  191. function loop( index: number ): number {
  192. if ( isLoop ) {
  193. return isEnough() ? index % slideCount + ( index < 0 ? slideCount : 0 ) : -1;
  194. }
  195. return index;
  196. }
  197. /**
  198. * Converts the page index to the slide index.
  199. *
  200. * @param page - A page index to convert.
  201. *
  202. * @return A slide index.
  203. */
  204. function toIndex( page: number ): number {
  205. return clamp( hasFocus() ? page : perPage * page, 0, getEnd() );
  206. }
  207. /**
  208. * Converts the slide index to the page index.
  209. *
  210. * @param index - An index to convert.
  211. */
  212. function toPage( index: number ): number {
  213. if ( ! hasFocus() ) {
  214. index = between( index, slideCount - perPage, slideCount - 1 ) ? slideCount - 1 : index;
  215. index = floor( index / perPage );
  216. }
  217. return index;
  218. }
  219. /**
  220. * Returns the number of slides to move for '>' and '<'.
  221. *
  222. * @return The number of slides to move.
  223. */
  224. function getPerMove(): number {
  225. return perMove || hasFocus() ? 1 : perPage;
  226. }
  227. /**
  228. * Sets a new index and retains old one.
  229. *
  230. * @param index - A new index to set.
  231. */
  232. function setIndex( index: number ): void {
  233. if ( index !== currIndex ) {
  234. prevIndex = currIndex;
  235. currIndex = index;
  236. }
  237. }
  238. /**
  239. * Returns the current/previous index slide index.
  240. *
  241. * @param prev - Optional. Whether to return previous index or not.
  242. */
  243. function getIndex( prev?: boolean ): number {
  244. return prev ? prevIndex : currIndex;
  245. }
  246. /**
  247. * Verifies if the focus option is available or not.
  248. *
  249. * @return `true` if the slider has the focus option.
  250. */
  251. function hasFocus(): boolean {
  252. return ! isUndefined( options.focus ) || options.isNavigation;
  253. }
  254. return {
  255. mount,
  256. go,
  257. getNext,
  258. getPrev,
  259. getEnd,
  260. getIndex,
  261. toIndex,
  262. toPage,
  263. hasFocus,
  264. };
  265. }