horizontal.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * The resolver component for horizontal 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: options.direction === RTL ? 'marginLeft' : 'marginRight',
  39. /**
  40. * Always 0 because the height will be determined by inner contents.
  41. *
  42. * @type {number}
  43. */
  44. height: 0,
  45. /**
  46. * Always 0 because the height will be determined by inner contents.
  47. *
  48. * @type {number}
  49. */
  50. listHeight: 0,
  51. /**
  52. * Initialization.
  53. */
  54. init() {
  55. const { left = 0, right = 0 } = options.padding;
  56. applyStyle( track, { paddingLeft : unit( left ), paddingRight: unit( right ) } );
  57. },
  58. /**
  59. * Return slider width without padding.
  60. *
  61. * @return {number} - Current slider width.
  62. */
  63. get width() {
  64. return track.clientWidth - this.padding.left - this.padding.right;
  65. },
  66. /**
  67. * Return slide height without padding.
  68. *
  69. * @return {number} - Slider height.
  70. */
  71. // get height() {
  72. // const height = options.height || options.fixedHeight || this.width * options.heightRatio;
  73. // return toPixel( Splide.root, height );
  74. // },
  75. /**
  76. * Return list width.
  77. *
  78. * @return {number} - Current list width.
  79. */
  80. get listWidth() {
  81. return ( this.slideWidth + this.gap ) * Components.Slides.total;
  82. },
  83. /**
  84. * Return the slide width in px.
  85. *
  86. * @return {number} - The slide width.
  87. */
  88. get slideWidth() {
  89. let width = options.fixedWidth;
  90. if ( ! width ) {
  91. width = ( ( this.width + this.gap ) / options.perPage ) - this.gap;
  92. }
  93. return toPixel( Splide.root, width );
  94. },
  95. /**
  96. * Return the slide height in px.
  97. *
  98. * @return {number} - The slide height.
  99. */
  100. get slideHeight() {
  101. const height = options.height || options.fixedHeight || this.width * options.heightRatio;
  102. return toPixel( Splide.root, height );
  103. },
  104. /**
  105. * Return gap in px.
  106. *
  107. * @return {Object} - Gap amount in px.
  108. */
  109. get gap() {
  110. const style = getComputedStyle( Elements.slides[ 0 ] );
  111. return parseFloat( style[ this.marginProp ] ) || 0;
  112. },
  113. /**
  114. * Return padding object.
  115. *
  116. * @return {Object} - An object containing padding left and right.
  117. */
  118. get padding() {
  119. const style = getComputedStyle( track );
  120. return {
  121. left : parseFloat( style.paddingLeft ) || 0,
  122. right : parseFloat( style.paddingRight ) || 0,
  123. };
  124. },
  125. }
  126. }