index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * The component for handing slide layouts and their sizes.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import Horizontal from './directions/horizontal';
  8. import Vertical from './directions/vertical';
  9. import { unit } from '../../utils/utils';
  10. import { throttle } from '../../utils/time';
  11. import { applyStyle, removeAttribute } from '../../utils/dom';
  12. import { assign } from "../../utils/object";
  13. import { TTB } from "../../constants/directions";
  14. /**
  15. * Interval time for throttle.
  16. *
  17. * @type {number}
  18. */
  19. const THROTTLE = 100;
  20. /**
  21. * The component for handing slide layouts and their sizes.
  22. *
  23. * @param {Splide} Splide - A Splide instance.
  24. * @param {Object} Components - An object containing components.
  25. *
  26. * @return {Object} - The component object.
  27. */
  28. export default ( Splide, Components ) => {
  29. /**
  30. * Keep the Elements component.
  31. *
  32. * @type {string}
  33. */
  34. const Elements = Components.Elements;
  35. /**
  36. * Layout component object.
  37. *
  38. * @type {Object}
  39. */
  40. const Layout = assign( {
  41. /**
  42. * Called when the component is mounted.
  43. */
  44. mount() {
  45. bind();
  46. init();
  47. },
  48. /**
  49. * Destroy.
  50. */
  51. destroy() {
  52. removeAttribute( [ Elements.list, Elements.track ], 'style' );
  53. },
  54. }, Splide.options.direction === TTB ? Vertical( Splide, Components ) : Horizontal( Splide, Components ) );
  55. /**
  56. * Init slider styles according to options.
  57. */
  58. function init() {
  59. Layout.init();
  60. applyStyle( Splide.root, { maxWidth: unit( Splide.options.width ) } );
  61. Elements.each( Slide => { Slide.slide.style[ Layout.margin ] = unit( Layout.gap ) } );
  62. resize();
  63. }
  64. /**
  65. * Listen the resize native event with throttle.
  66. * Initialize when the component is mounted or options are updated.
  67. */
  68. function bind() {
  69. Splide
  70. .on( 'resize load', throttle( () => { Splide.emit( 'resize' ) }, THROTTLE ), window )
  71. .on( 'resize', resize )
  72. .on( 'updated', init );
  73. }
  74. /**
  75. * Resize the list and slides including clones.
  76. */
  77. function resize() {
  78. applyStyle( Elements.list, { width: unit( Layout.listWidth ), height: unit( Layout.listHeight ) } );
  79. applyStyle( Elements.track, { height: unit( Layout.height ) } );
  80. const slideHeight = unit( Layout.slideHeight() );
  81. Elements.each( Slide => {
  82. applyStyle( Slide.container, { height: slideHeight } );
  83. applyStyle( Slide.slide, {
  84. width : Splide.options.autoWidth ? null : unit( Layout.slideWidth( Slide.index ) ),
  85. height: Slide.container ? null : slideHeight,
  86. } );
  87. } );
  88. }
  89. return Layout;
  90. }