index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * The component for the root element.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import { find, addClass, child } from '../../utils/dom';
  8. import { exist } from '../../utils/error';
  9. import { values } from '../../utils/object';
  10. /**
  11. * The property name for UID stored in a window object.
  12. *
  13. * @type {string}
  14. */
  15. const UID_NAME = 'splideUid';
  16. /**
  17. * The component for the root element.
  18. *
  19. * @param {Splide} Splide - A Splide instance.
  20. *
  21. * @return {Object} - The component object.
  22. */
  23. export default ( Splide ) => {
  24. /**
  25. * Hold the root element.
  26. *
  27. * @type {Element}
  28. */
  29. const root = Splide.root;
  30. /**
  31. * Hold the class list.
  32. *
  33. * @type {Object}
  34. */
  35. const classes = Splide.classes;
  36. /*
  37. * Assign unique ID to the root element if it doesn't have the one.
  38. * Note that IE doesn't support padStart() to fill the uid by 0.
  39. */
  40. if ( ! root.id ) {
  41. let uid = window[ UID_NAME ] || 0;
  42. window[ UID_NAME ] = ++uid;
  43. root.id = `splide${ uid < 10 ? '0' + uid : uid }`;
  44. }
  45. /**
  46. * Elements component object.
  47. *
  48. * @type {Object}
  49. */
  50. const Elements = {
  51. /**
  52. * Called when the component is mounted.
  53. * Collect main elements and store them as member properties.
  54. */
  55. mount() {
  56. const message = 'was not found.';
  57. this.slider = child( root, classes.slider );
  58. this.track = find( root, `.${ classes.track }` );
  59. exist( this.track, `A track ${ message }` );
  60. this.list = child( this.track, classes.list );
  61. exist( this.list, `A list ${ message }` );
  62. this.slides = values( this.list.children );
  63. exist( this.slides.length, `A slide ${ message }` );
  64. const arrows = findParts( classes.arrows );
  65. this.arrows = {
  66. prev: find( arrows, `.${ classes.prev }` ),
  67. next: find( arrows, `.${ classes.next }` ),
  68. };
  69. const autoplay = findParts( classes.autoplay );
  70. this.bar = find( findParts( classes.progress ), `.${ classes.bar }` );
  71. this.play = find( autoplay, `.${ classes.play }` );
  72. this.pause = find( autoplay, `.${ classes.pause }` );
  73. init();
  74. },
  75. /**
  76. * Called after all components are mounted.
  77. */
  78. mounted() {
  79. const rootClass = classes.root;
  80. const options = Splide.options;
  81. addClass(
  82. root,
  83. `${ rootClass }`,
  84. `${ rootClass }--${ options.type }`,
  85. `${ rootClass }--${ options.direction }`,
  86. options.drag ? `${ rootClass }--draggable` : '',
  87. options.isNavigation ? `${ rootClass }--nav` : ''
  88. );
  89. },
  90. };
  91. /**
  92. * Find parts only from children of the root or track.
  93. *
  94. * @return {Element|null} - A found element or null.
  95. */
  96. function findParts( className ) {
  97. return child( root, className ) || child( Elements.slider, className );
  98. }
  99. /**
  100. * Initialization.
  101. * Assign ID to some elements if it's not available.
  102. */
  103. function init() {
  104. if ( ! Elements.track.id ) {
  105. Elements.track.id = `${ root.id }-track`;
  106. }
  107. if ( ! Elements.list.id ) {
  108. Elements.list.id = `${ root.id }-list`;
  109. }
  110. }
  111. return Elements;
  112. }