vertical.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * The resolver component for vertical layout.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import { applyStyle } from "../../../utils/dom";
  8. import { unit } from "../../../utils/utils";
  9. import { exist } from "../../../utils/error";
  10. /**
  11. * The resolver component for vertical layout.
  12. *
  13. * @param {Splide} Splide - A Splide instance.
  14. * @param {Object} Components - An object containing components.
  15. * @param {Object} options - Current options.
  16. *
  17. * @return {Object} - The resolver object.
  18. */
  19. export default ( Splide, Components, options ) => {
  20. /**
  21. * Keep the Elements component.
  22. *
  23. * @type {string}
  24. */
  25. const Elements = Components.Elements;
  26. /**
  27. * Keep the track element.
  28. *
  29. * @type {Element}
  30. */
  31. const track = Elements.track;
  32. /**
  33. * Keep the fixed width if available.
  34. *
  35. * @type {number}
  36. */
  37. let fixedWidth;
  38. /**
  39. * Keep the fixed height if available.
  40. *
  41. * @type {number}
  42. */
  43. let fixedHeight;
  44. /**
  45. * Keep the temporary listHeight.
  46. *
  47. * @type {number}
  48. */
  49. let listHeight;
  50. return {
  51. /**
  52. * Margin property name.
  53. *
  54. * @type {string}
  55. */
  56. marginProp: 'marginBottom',
  57. /**
  58. * Init slider styles according to options.
  59. */
  60. init() {
  61. const { top = 0, bottom = 0 } = options.padding;
  62. applyStyle( track, { paddingTop: unit( top ), paddingBottom: unit( bottom ) } );
  63. const firstSlide = Elements.slides[ 0 ];
  64. const position = firstSlide.style.position || 'static';
  65. const { fixedWidth: fixedW, fixedHeight: fixedH, height } = options;
  66. applyStyle( firstSlide, { position: 'absolute' } );
  67. if ( fixedW ) {
  68. applyStyle( firstSlide, { width: unit( fixedW ) } );
  69. fixedWidth = parseFloat( getComputedStyle( firstSlide ).width );
  70. }
  71. if ( fixedH ) {
  72. applyStyle( firstSlide, { height: unit( fixedH ) } );
  73. fixedHeight = parseFloat( getComputedStyle( firstSlide ).height );
  74. }
  75. applyStyle( firstSlide, { position } );
  76. if ( height ) {
  77. const list = Elements.list;
  78. applyStyle( list, { height: unit( height ) } );
  79. listHeight = parseFloat( getComputedStyle( list ).height );
  80. }
  81. },
  82. /**
  83. * Return the slide width with/without a gap space.
  84. *
  85. * @return {number} - Slide width in px.
  86. */
  87. getSlideWidth() {
  88. return fixedWidth || this.width;
  89. },
  90. /**
  91. * Return the slide height with/without a gap space.
  92. *
  93. * @param {boolean} includeGap - Whether to include a gap space or not.
  94. *
  95. * @return {number} - Slide height in px.
  96. */
  97. getSlideHeight( includeGap ) {
  98. const height = fixedHeight || ( this.listHeight + this.gap ) / options.perPage;
  99. return includeGap ? height : height - this.gap;
  100. },
  101. /**
  102. * Return slider width without padding.
  103. *
  104. * @return {number} - Current slide width.
  105. */
  106. get width() {
  107. return track.clientWidth;
  108. },
  109. /**
  110. * Return list width.
  111. *
  112. * @return {number} - Current list width.
  113. */
  114. get listWidth() {
  115. return this.width;
  116. },
  117. /**
  118. * Return list height.
  119. *
  120. * @return {number} - Current list height.
  121. */
  122. get listHeight() {
  123. const height = listHeight || this.width * options.heightRatio;
  124. exist( height, '"height" or "heightRatio" must be given in TTB mode.' );
  125. return height - this.padding.top - this.padding.bottom;
  126. },
  127. /**
  128. * Return gap in px.
  129. *
  130. * @return {Object} - Gap amount in px.
  131. */
  132. get gap() {
  133. const style = getComputedStyle( Elements.slides[ 0 ] );
  134. return parseFloat( style[ this.marginProp ] ) || 0;
  135. },
  136. /**
  137. * Return padding object.
  138. *
  139. * @return {Object} - An object containing padding top and bottom.
  140. */
  141. get padding() {
  142. const style = getComputedStyle( track );
  143. return {
  144. top : parseFloat( style.paddingTop ) || 0,
  145. bottom: parseFloat( style.paddingBottom ) || 0,
  146. };
  147. },
  148. }
  149. }