vertical.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 { toPixel, 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. return {
  33. /**
  34. * Margin property name.
  35. *
  36. * @type {string}
  37. */
  38. marginProp: 'marginBottom',
  39. /**
  40. * Init slider styles according to options.
  41. */
  42. init() {
  43. let padding = options.padding;
  44. if ( padding ) {
  45. if ( typeof padding !== 'object' ) {
  46. padding = {
  47. top : padding,
  48. bottom: padding,
  49. }
  50. }
  51. applyStyle( track, { paddingTop : unit( padding.top ), paddingBottom: unit( padding.bottom ) } );
  52. }
  53. },
  54. /**
  55. * Return slider width without padding.
  56. *
  57. * @return {number} - Current slider width.
  58. */
  59. get width() {
  60. return track.clientWidth;
  61. },
  62. /**
  63. * Return slide height without padding.
  64. *
  65. * @return {number} - Slider height.
  66. */
  67. get height() {
  68. const height = options.height || this.width * options.heightRatio;
  69. exist( height, '"height" or "heightRatio" must be given in TTB mode.' );
  70. return toPixel( Splide.root, height );
  71. },
  72. /**
  73. * Return list width.
  74. *
  75. * @return {number} - Current list width.
  76. */
  77. get listWidth() {
  78. return this.width;
  79. },
  80. /**
  81. * Return list height.
  82. *
  83. * @return {number} - Current list height.
  84. */
  85. get listHeight() {
  86. return ( this.slideHeight + this.gap ) * Components.Slides.total;
  87. },
  88. /**
  89. * Return the slide width in px.
  90. *
  91. * @return {number} - The slide width.
  92. */
  93. get slideWidth() {
  94. return toPixel( Splide.root, options.fixedWidth || this.width );
  95. },
  96. /**
  97. * Return the slide height in px.
  98. *
  99. * @return {number} - The slide height.
  100. */
  101. get slideHeight() {
  102. let height = options.fixedHeight;
  103. if ( ! height ) {
  104. height = ( this.height + this.gap ) / options.perPage - this.gap;
  105. }
  106. return toPixel( Splide.root, height );
  107. },
  108. /**
  109. * Return gap in px.
  110. *
  111. * @return {Object} - Gap amount in px.
  112. */
  113. get gap() {
  114. const style = getComputedStyle( Elements.slides[ 0 ] );
  115. return parseFloat( style[ this.marginProp ] ) || 0;
  116. },
  117. /**
  118. * Return padding object.
  119. *
  120. * @return {Object} - An object containing padding top and bottom.
  121. */
  122. get padding() {
  123. const style = getComputedStyle( track );
  124. return {
  125. top : parseFloat( style.paddingTop ) || 0,
  126. bottom: parseFloat( style.paddingBottom ) || 0,
  127. };
  128. },
  129. }
  130. }