Layout.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. */
  62. function init(): void {
  63. rootRect = null;
  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();
  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. function resize(): void {
  75. const newRect = rect( root );
  76. if ( ! rootRect || rootRect.width !== newRect.width || rootRect.height !== newRect.height ) {
  77. style( track, 'height', cssTrackHeight() );
  78. styleSlides( resolve( 'marginRight' ), unit( options.gap ) );
  79. styleSlides( 'width', cssSlideWidth() );
  80. styleSlides( 'height', cssSlideHeight(), true );
  81. rootRect = newRect;
  82. emit( EVENT_RESIZED );
  83. if ( overflow !== ( overflow = isOverflow() ) ) {
  84. emit( EVENT_OVERFLOW, overflow );
  85. }
  86. }
  87. }
  88. /**
  89. * Parses the padding option and returns the value for each side.
  90. * This method returns `paddingTop` or `paddingBottom` for the vertical slider.
  91. *
  92. * @param right - Determines whether to get `paddingRight/Bottom` or `paddingLeft/Top`.
  93. *
  94. * @return The padding value as a CSS string.
  95. */
  96. function cssPadding( right: boolean ): string {
  97. const { padding } = options;
  98. const prop = resolve( right ? 'right' : 'left' );
  99. return padding && unit( padding[ prop ] || ( isObject( padding ) ? 0 : padding ) ) || '0px';
  100. }
  101. /**
  102. * Returns the height of the track element as a CSS string.
  103. *
  104. * @return The height of the track.
  105. */
  106. function cssTrackHeight(): string {
  107. let height = '';
  108. if ( vertical ) {
  109. height = cssHeight();
  110. assert( height, 'height or heightRatio is missing.' );
  111. height = `calc(${ height } - ${ cssPadding( false ) } - ${ cssPadding( true ) })`;
  112. }
  113. return height;
  114. }
  115. /**
  116. * Converts options related with height to a CSS string.
  117. *
  118. * @return The height as a CSS string if available, or otherwise an empty string.
  119. */
  120. function cssHeight(): string {
  121. return unit( options.height || rect( list ).width * options.heightRatio );
  122. }
  123. /**
  124. * Returns the width of the slide as a CSS string.
  125. *
  126. * @return The width of the slide.
  127. */
  128. function cssSlideWidth(): string | null {
  129. return options.autoWidth ? null : unit( options.fixedWidth ) || ( vertical ? '' : cssSlideSize() );
  130. }
  131. /**
  132. * Returns the height of the slide as a CSS string.
  133. *
  134. * @return The height of the slide.
  135. */
  136. function cssSlideHeight(): string | null {
  137. return unit( options.fixedHeight )
  138. || ( vertical ? ( options.autoHeight ? null : cssSlideSize() ) : cssHeight() );
  139. }
  140. /**
  141. * Returns the CSS string for slide width or height without gap.
  142. *
  143. * @return The CSS string for slide width or height.
  144. */
  145. function cssSlideSize(): string {
  146. const gap = unit( options.gap );
  147. return `calc((100%${ gap && ` + ${ gap }` })/${ options.perPage || 1 }${ gap && ` - ${ gap }` })`;
  148. }
  149. /**
  150. * Returns the list width for the horizontal slider, or the height for the vertical slider.
  151. *
  152. * @return The size of the list element in pixel.
  153. */
  154. function listSize(): number {
  155. return rect( list )[ resolve( 'width' ) ];
  156. }
  157. /**
  158. * Returns the slide width for the horizontal slider, or the height for the vertical slider.
  159. *
  160. * @param index - Optional. A slide index.
  161. * @param withoutGap - Optional. Determines whether to exclude the gap amount or not.
  162. *
  163. * @return The size of the specified slide element in pixel.
  164. */
  165. function slideSize( index?: number, withoutGap?: boolean ): number {
  166. const Slide = getAt( index || 0 );
  167. return Slide
  168. ? rect( Slide.slide )[ resolve( 'width' ) ] + ( withoutGap ? 0 : getGap() )
  169. : 0;
  170. }
  171. /**
  172. * Returns the total width or height of slides from the head of the slider to the specified index.
  173. * This includes sizes of clones before the first slide.
  174. *
  175. * @param index - A slide index. If omitted, uses the last index.
  176. * @param withoutGap - Optional. Determines whether to exclude the last gap or not.
  177. *
  178. * @return The total width of slides in the horizontal slider, or the height in the vertical one.
  179. */
  180. function totalSize( index: number, withoutGap?: boolean ): number {
  181. const Slide = getAt( index );
  182. if ( Slide ) {
  183. const right = rect( Slide.slide )[ resolve( 'right' ) ];
  184. const left = rect( list )[ resolve( 'left' ) ];
  185. return abs( right - left ) + ( withoutGap ? 0 : getGap() );
  186. }
  187. return 0;
  188. }
  189. /**
  190. * Returns the slider size without clones before the first slide.
  191. * Do not use the clone's size because it's unstable while initializing and refreshing process.
  192. *
  193. * @return The width or height of the slider without clones.
  194. */
  195. function sliderSize(): number {
  196. return totalSize( Splide.length - 1 ) - totalSize( 0 ) + slideSize( 0 );
  197. }
  198. /**
  199. * Returns the gap value.
  200. *
  201. * @return The gap value in pixel.
  202. */
  203. function getGap(): number {
  204. const Slide = getAt( 0 );
  205. return Slide && parseFloat( style( Slide.slide, resolve( 'marginRight' ) ) ) || 0;
  206. }
  207. /**
  208. * Returns the padding value.
  209. * This method resolves the difference of the direction.
  210. *
  211. * @param right - Determines whether to get `paddingRight/Bottom` or `paddingLeft/Top`.
  212. *
  213. * @return The padding value in pixel.
  214. */
  215. function getPadding( right: boolean ): number {
  216. return parseFloat( style( track, resolve( `padding${ right ? 'Right' : 'Left' }` ) ) ) || 0;
  217. }
  218. /**
  219. * Checks if the carousel is wider than the list.
  220. * This method always returns `true` for a fade carousel.
  221. *
  222. * @return `true` if the carousel is wider than the list, or otherwise `false`.
  223. */
  224. function isOverflow(): boolean {
  225. return Splide.is( FADE ) || sliderSize() > listSize();
  226. }
  227. return {
  228. mount,
  229. listSize,
  230. slideSize,
  231. sliderSize,
  232. totalSize,
  233. getPadding,
  234. };
  235. }