horizontal.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * The resolver component for horizontal layout.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import { applyStyle } from "../../../utils/dom";
  8. import { unit, toPixel } from "../../../utils/utils";
  9. import { RTL } from '../../../constants/directions';
  10. /**
  11. * Max width of a slide.
  12. *
  13. * @type {number}
  14. */
  15. const SLIDE_MAX_WIDTH = 5000;
  16. /**
  17. * The resolver component for horizontal layout.
  18. *
  19. * @param {Splide} Splide - A Splide instance.
  20. * @param {Object} Components - An object containing components.
  21. *
  22. * @return {Object} - The resolver object.
  23. */
  24. export default ( Splide, Components ) => {
  25. /**
  26. * Keep the Elements component.
  27. *
  28. * @type {string}
  29. */
  30. const Elements = Components.Elements;
  31. /**
  32. * Keep the root element.
  33. *
  34. * @type {Element}
  35. */
  36. const root = Splide.root;
  37. /**
  38. * Keep the track element.
  39. *
  40. * @type {Element}
  41. */
  42. let track;
  43. /**
  44. * Keep the latest options.
  45. *
  46. * @type {Element}
  47. */
  48. let options = Splide.options;
  49. return {
  50. /**
  51. * Margin property name.
  52. *
  53. * @type {string}
  54. */
  55. margin: 'margin' + ( options.direction === RTL ? 'Left' : 'Right' ),
  56. /**
  57. * Always 0 because the height will be determined by inner contents.
  58. *
  59. * @type {number}
  60. */
  61. height: 0,
  62. /**
  63. * Always 0 because the height will be determined by inner contents.
  64. *
  65. * @type {number}
  66. */
  67. listHeight: 0,
  68. /**
  69. * Initialization.
  70. */
  71. init() {
  72. options = Splide.options;
  73. track = Elements.track;
  74. this.gap = toPixel( root, options.gap );
  75. const padding = options.padding;
  76. const { left = padding, right = padding } = padding;
  77. this.padding = {
  78. left : toPixel( root, left ),
  79. right: toPixel( root, right ),
  80. };
  81. applyStyle( track, {
  82. paddingLeft : unit( left ),
  83. paddingRight: unit( right ),
  84. } );
  85. },
  86. /**
  87. * Accumulate slide width including the gap to the designated index.
  88. *
  89. * @param {number|undefined} index - If undefined, width of all slides will be accumulated.
  90. *
  91. * @return {number} - Accumulated width.
  92. */
  93. totalWidth( index ) {
  94. return Elements.getSlides( true )
  95. .filter( Slide => Slide.index <= index )
  96. .reduce( ( accumulator, Slide ) => {
  97. return accumulator + this.slideWidth( Slide.index ) + this.gap;
  98. }, 0 );
  99. },
  100. /**
  101. * Return the slide width in px.
  102. *
  103. * @param {number} index - Slide index.
  104. *
  105. * @return {number} - The slide width.
  106. */
  107. slideWidth( index ) {
  108. if ( options.autoWidth ) {
  109. const Slide = Elements.getSlide( index );
  110. return Slide ? Slide.slide.offsetWidth : 0;
  111. }
  112. const width = options.fixedWidth || ( ( this.width + this.gap ) / options.perPage ) - this.gap;
  113. return toPixel( root, width );
  114. },
  115. /**
  116. * Return the slide height in px.
  117. *
  118. * @return {number} - The slide height.
  119. */
  120. slideHeight() {
  121. const height = options.height || options.fixedHeight || this.width * options.heightRatio;
  122. return toPixel( root, height );
  123. },
  124. /**
  125. * Return slider width without padding.
  126. *
  127. * @return {number} - Current slider width.
  128. */
  129. get width() {
  130. return track.clientWidth - this.padding.left - this.padding.right;
  131. },
  132. /**
  133. * Return list width.
  134. *
  135. * @return {number} - Current list width.
  136. */
  137. get listWidth() {
  138. const total = Elements.total;
  139. return options.autoWidth ? total * SLIDE_MAX_WIDTH : this.totalWidth( total );
  140. },
  141. }
  142. }