vertical.js 2.6 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 } 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 = padding, bottom = padding } = padding;
  59. this.padding = {
  60. top : toPixel( root, top ),
  61. bottom: toPixel( root, bottom ),
  62. };
  63. applyStyle( track, {
  64. paddingTop : unit( top ),
  65. paddingBottom: unit( bottom ),
  66. } );
  67. },
  68. /**
  69. * Return the slide width in px.
  70. *
  71. * @return {number} - The slide width.
  72. */
  73. slideWidth() {
  74. return toPixel( root, options.fixedWidth || this.width );
  75. },
  76. /**
  77. * Return the slide height in px.
  78. *
  79. * @return {number} - The slide height.
  80. */
  81. slideHeight() {
  82. const height = options.fixedHeight || ( this.height + this.gap ) / options.perPage - this.gap;
  83. return toPixel( root, height );
  84. },
  85. /**
  86. * Return slider width without padding.
  87. *
  88. * @return {number} - Current slider width.
  89. */
  90. get width() {
  91. return track.clientWidth;
  92. },
  93. /**
  94. * Return slide height without padding.
  95. *
  96. * @return {number} - Slider height.
  97. */
  98. get height() {
  99. const height = options.height || this.width * options.heightRatio;
  100. exist( height, '"height" or "heightRatio" is missing.' );
  101. return toPixel( root, height ) - this.padding.top - this.padding.bottom;
  102. },
  103. /**
  104. * Return list width.
  105. *
  106. * @return {number} - Current list width.
  107. */
  108. get listWidth() {
  109. return this.width;
  110. },
  111. /**
  112. * Return list height.
  113. *
  114. * @return {number} - Current list height.
  115. */
  116. get listHeight() {
  117. return ( this.slideHeight() + this.gap ) * Elements.total;
  118. },
  119. }
  120. }