vertical.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * The resolver component for vertical layout.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import { applyStyle, getRect } 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. *
  16. * @return {Object} - The resolver object.
  17. */
  18. export default ( Splide, Components ) => {
  19. /**
  20. * Keep the Elements component.
  21. *
  22. * @type {string}
  23. */
  24. const Elements = Components.Elements;
  25. /**
  26. * Keep the root element.
  27. *
  28. * @type {Element}
  29. */
  30. const root = Splide.root;
  31. /**
  32. * Keep the track element.
  33. *
  34. * @type {Element}
  35. */
  36. let track;
  37. /**
  38. * Keep the latest options.
  39. *
  40. * @type {Element}
  41. */
  42. let options;
  43. return {
  44. /**
  45. * Margin property name.
  46. *
  47. * @type {string}
  48. */
  49. margin: 'marginBottom',
  50. /**
  51. * Init slider styles according to options.
  52. */
  53. init() {
  54. options = Splide.options;
  55. track = Elements.track;
  56. this.gap = toPixel( root, options.gap );
  57. const padding = options.padding;
  58. const top = toPixel( root, padding.top || padding );
  59. const bottom = toPixel( root, padding.bottom || padding );
  60. this.padding = { top, bottom };
  61. applyStyle( track, { paddingTop : unit( top ), paddingBottom: unit( bottom ) } );
  62. },
  63. /**
  64. * Return total height from the top of the list to the bottom of the slide specified by the provided index.
  65. *
  66. * @param {number} index - Optional. A slide index. If undefined, total height of the slider will be returned.
  67. *
  68. * @return {number} - Total height to the bottom of the specified slide, or 0 for an invalid index.
  69. */
  70. totalHeight( index = Splide.length - 1 ) {
  71. const Slide = Elements.getSlide( index );
  72. if ( Slide ) {
  73. return getRect( Slide.slide ).bottom - getRect( Elements.list ).top + this.gap;
  74. }
  75. return 0;
  76. },
  77. /**
  78. * Return the slide width in px.
  79. *
  80. * @return {number} - The slide width.
  81. */
  82. slideWidth() {
  83. return toPixel( root, options.fixedWidth || this.width );
  84. },
  85. /**
  86. * Return the slide height in px.
  87. *
  88. * @param {number} index - Slide index.
  89. *
  90. * @return {number} - The slide height.
  91. */
  92. slideHeight( index ) {
  93. if ( options.autoHeight ) {
  94. const Slide = Elements.getSlide( index );
  95. return Slide ? Slide.slide.offsetHeight : 0;
  96. }
  97. const height = options.fixedHeight || ( this.height + this.gap ) / options.perPage - this.gap;
  98. return toPixel( root, height );
  99. },
  100. /**
  101. * Return slider width without padding.
  102. *
  103. * @return {number} - Current slider width.
  104. */
  105. get width() {
  106. return track.clientWidth;
  107. },
  108. /**
  109. * Return slide height without padding.
  110. *
  111. * @return {number} - Slider height.
  112. */
  113. get height() {
  114. const height = options.height || this.width * options.heightRatio;
  115. exist( height, '"height" or "heightRatio" is missing.' );
  116. return toPixel( root, height ) - this.padding.top - this.padding.bottom;
  117. },
  118. }
  119. }