Layout.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { TTB } from '../../constants/directions';
  2. import { EVENT_OVERFLOW, EVENT_REFRESH, EVENT_RESIZE, EVENT_RESIZED, EVENT_UPDATED } from '../../constants/events';
  3. import { EventInterface, Throttle } from '../../constructors';
  4. import { Splide } from '../../core/Splide/Splide';
  5. import { BaseComponent, Components, Options } from '../../types';
  6. import { abs, apply, assert, isObject, rect, style, unit } from '../../utils';
  7. import { FADE } from '../../constants/types';
  8. /**
  9. * The interface for the Layout component.
  10. *
  11. * @since 3.0.0
  12. */
  13. export interface LayoutComponent extends BaseComponent {
  14. listSize(): number;
  15. slideSize( index: number, withoutGap?: boolean ): number;
  16. sliderSize(): number;
  17. totalSize( index?: number, withoutGap?: boolean ): number;
  18. getPadding( right: boolean ): number;
  19. }
  20. /**
  21. * The component that adjusts slider styles and provides methods for dimensions.
  22. *
  23. * @since 3.0.0
  24. *
  25. * @param Splide - A Splide instance.
  26. * @param Components - A collection of components.
  27. * @param options - Options.
  28. *
  29. * @return An Layout component object.
  30. */
  31. export function Layout( Splide: Splide, Components: Components, options: Options ): LayoutComponent {
  32. const { on, bind, emit } = EventInterface( Splide );
  33. const { Slides } = Components;
  34. const { resolve } = Components.Direction;
  35. const { root, track, list } = Components.Elements;
  36. const { getAt, style: styleSlides } = Slides;
  37. /**
  38. * Indicates whether the slider direction is vertical or not.
  39. */
  40. let vertical: boolean;
  41. /**
  42. * Keeps the DOMRect object of the root element.
  43. */
  44. let rootRect: DOMRect;
  45. /**
  46. * Turns into `true` when the carousel is wider than the list.
  47. */
  48. let overflow: boolean;
  49. /**
  50. * Called when the component is mounted.
  51. */
  52. function mount(): void {
  53. init();
  54. bind( window, 'resize load', Throttle( apply( emit, EVENT_RESIZE ) ) );
  55. on( [ EVENT_UPDATED, EVENT_REFRESH ], init );
  56. on( EVENT_RESIZE, resize );
  57. }
  58. /**
  59. * Initializes the component on `mount` or `updated`.
  60. * - Uses `max-width` for the root to prevent the slider from exceeding the parent element.
  61. * - Needs to always resize the carousel in case that `refresh` is requested in multiple times at the same time.
  62. */
  63. function init(): void {
  64. vertical = options.direction === TTB;
  65. style( root, 'maxWidth', unit( options.width ) );
  66. style( track, resolve( 'paddingLeft' ), cssPadding( false ) );
  67. style( track, resolve( 'paddingRight' ), cssPadding( true ) );
  68. resize( true );
  69. }
  70. /**
  71. * Updates dimensions of some elements when the carousel is resized.
  72. * Also checks the carousel size and emits `overflow` events when it exceeds the list width.
  73. *
  74. * @param force - Skips checking the root dimension change and always performs the resizing process.
  75. */
  76. function resize( force?: boolean ): void {
  77. const newRect = rect( root );
  78. if ( force || rootRect.width !== newRect.width || rootRect.height !== newRect.height ) {
  79. style( track, 'height', cssTrackHeight() );
  80. styleSlides( resolve( 'marginRight' ), unit( options.gap ) );
  81. styleSlides( 'width', cssSlideWidth() );
  82. styleSlides( 'height', cssSlideHeight(), true );
  83. rootRect = newRect;
  84. emit( EVENT_RESIZED );
  85. if ( overflow !== ( overflow = isOverflow() ) ) {
  86. emit( EVENT_OVERFLOW, overflow );
  87. }
  88. }
  89. }
  90. /**
  91. * Parses the padding option and returns the value for each side.
  92. * This method returns `paddingTop` or `paddingBottom` for the vertical slider.
  93. *
  94. * @param right - Determines whether to get `paddingRight/Bottom` or `paddingLeft/Top`.
  95. *
  96. * @return The padding value as a CSS string.
  97. */
  98. function cssPadding( right: boolean ): string {
  99. const { padding } = options;
  100. const prop = resolve( right ? 'right' : 'left' );
  101. return padding && unit( padding[ prop ] || ( isObject( padding ) ? 0 : padding ) ) || '0px';
  102. }
  103. /**
  104. * Returns the height of the track element as a CSS string.
  105. *
  106. * @return The height of the track.
  107. */
  108. function cssTrackHeight(): string {
  109. let height = '';
  110. if ( vertical ) {
  111. height = cssHeight();
  112. assert( height, 'height or heightRatio is missing.' );
  113. height = `calc(${ height } - ${ cssPadding( false ) } - ${ cssPadding( true ) })`;
  114. }
  115. return height;
  116. }
  117. /**
  118. * Converts options related with height to a CSS string.
  119. *
  120. * @return The height as a CSS string if available, or otherwise an empty string.
  121. */
  122. function cssHeight(): string {
  123. return unit( options.height || rect( list ).width * options.heightRatio );
  124. }
  125. /**
  126. * Returns the width of the slide as a CSS string.
  127. *
  128. * @return The width of the slide.
  129. */
  130. function cssSlideWidth(): string | null {
  131. return options.autoWidth ? null : unit( options.fixedWidth ) || ( vertical ? '' : cssSlideSize() );
  132. }
  133. /**
  134. * Returns the height of the slide as a CSS string.
  135. *
  136. * @return The height of the slide.
  137. */
  138. function cssSlideHeight(): string | null {
  139. return unit( options.fixedHeight )
  140. || ( vertical ? ( options.autoHeight ? null : cssSlideSize() ) : cssHeight() );
  141. }
  142. /**
  143. * Returns the CSS string for slide width or height without gap.
  144. *
  145. * @return The CSS string for slide width or height.
  146. */
  147. function cssSlideSize(): string {
  148. const gap = unit( options.gap );
  149. return `calc((100%${ gap && ` + ${ gap }` })/${ options.perPage || 1 }${ gap && ` - ${ gap }` })`;
  150. }
  151. /**
  152. * Returns the list width for the horizontal slider, or the height for the vertical slider.
  153. *
  154. * @return The size of the list element in pixel.
  155. */
  156. function listSize(): number {
  157. return rect( list )[ resolve( 'width' ) ];
  158. }
  159. /**
  160. * Returns the slide width for the horizontal slider, or the height for the vertical slider.
  161. *
  162. * @param index - Optional. A slide index.
  163. * @param withoutGap - Optional. Determines whether to exclude the gap amount or not.
  164. *
  165. * @return The size of the specified slide element in pixel.
  166. */
  167. function slideSize( index?: number, withoutGap?: boolean ): number {
  168. const Slide = getAt( index || 0 );
  169. return Slide
  170. ? rect( Slide.slide )[ resolve( 'width' ) ] + ( withoutGap ? 0 : getGap() )
  171. : 0;
  172. }
  173. /**
  174. * Returns the total width or height of slides from the head of the slider to the specified index.
  175. * This includes sizes of clones before the first slide.
  176. *
  177. * @param index - A slide index. If omitted, uses the last index.
  178. * @param withoutGap - Optional. Determines whether to exclude the last gap or not.
  179. *
  180. * @return The total width of slides in the horizontal slider, or the height in the vertical one.
  181. */
  182. function totalSize( index: number, withoutGap?: boolean ): number {
  183. const Slide = getAt( index );
  184. if ( Slide ) {
  185. const right = rect( Slide.slide )[ resolve( 'right' ) ];
  186. const left = rect( list )[ resolve( 'left' ) ];
  187. return abs( right - left ) + ( withoutGap ? 0 : getGap() );
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Returns the slider size without clones before the first slide.
  193. * Do not use the clone's size because it's unstable while initializing and refreshing process.
  194. *
  195. * @return The width or height of the slider without clones.
  196. */
  197. function sliderSize(): number {
  198. return totalSize( Splide.length - 1, true ) - totalSize( 0, true ) + slideSize( 0, true );
  199. }
  200. /**
  201. * Returns the gap value.
  202. *
  203. * @return The gap value in pixel.
  204. */
  205. function getGap(): number {
  206. const Slide = getAt( 0 );
  207. return Slide && parseFloat( style( Slide.slide, resolve( 'marginRight' ) ) ) || 0;
  208. }
  209. /**
  210. * Returns the padding value.
  211. * This method resolves the difference of the direction.
  212. *
  213. * @param right - Determines whether to get `paddingRight/Bottom` or `paddingLeft/Top`.
  214. *
  215. * @return The padding value in pixel.
  216. */
  217. function getPadding( right: boolean ): number {
  218. return parseFloat( style( track, resolve( `padding${ right ? 'Right' : 'Left' }` ) ) ) || 0;
  219. }
  220. /**
  221. * Checks if the carousel is wider than the list.
  222. * This method always returns `true` for a fade carousel.
  223. *
  224. * @return `true` if the carousel is wider than the list, or otherwise `false`.
  225. */
  226. function isOverflow(): boolean {
  227. return Splide.is( FADE ) || sliderSize() > listSize();
  228. }
  229. return {
  230. mount,
  231. listSize,
  232. slideSize,
  233. sliderSize,
  234. totalSize,
  235. getPadding,
  236. };
  237. }