splide.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * The main class for applying Splide to an element.
  3. *
  4. * @author Naotoshi Fujita
  5. * @copyright Naotoshi Fujita. All rights reserved.
  6. */
  7. import Event from './core/event';
  8. import State from './core/state';
  9. import compose from './core/composer';
  10. import { error, exist } from './utils/error';
  11. import { applyStyle } from './utils/dom';
  12. import { merge, each, values } from './utils/object';
  13. import { DEFAULTS } from './constants/defaults';
  14. import * as STATES from './constants/states';
  15. /**
  16. * The main class for applying Splide to an element,
  17. * providing some APIs to control the behavior.
  18. */
  19. export default class Splide {
  20. /**
  21. * Splide constructor.
  22. *
  23. * @throws {Error} When the given root element or selector is invalid.
  24. *
  25. * @param {Element|string} root - A selector for a root element or an element itself.
  26. * @param {Object} options - Optional. Options to change default behaviour.
  27. * @param {Object} Components - Optional. Components.
  28. */
  29. constructor( root, options = {}, Components = {} ) {
  30. this.root = root instanceof Element ? root : document.querySelector( root );
  31. exist( this.root, 'An invalid element/selector was given.' );
  32. this.Components = null;
  33. this.Event = Event();
  34. this.State = State( STATES.CREATED );
  35. this.STATES = STATES;
  36. this._o = merge( DEFAULTS, options );
  37. this._i = 0;
  38. this._c = Components;
  39. this._e = {}; // Extensions
  40. this._t = null; // Transition
  41. }
  42. /**
  43. * Compose and mount components.
  44. *
  45. * @param {Object} Extensions - Optional. Additional components.
  46. * @param {function} Transition - Optional. Set a custom transition component.
  47. *
  48. * @return {Splide|undefined} - This instance or undefined if an exception occurred.
  49. */
  50. mount( Extensions = this._e, Transition = this._t ) {
  51. this._e = Extensions;
  52. this._t = Transition;
  53. this.Components = compose( this, merge( this._c, Extensions ), Transition );
  54. try {
  55. each( this.Components, ( component, key ) => {
  56. const required = component.required;
  57. if ( required === undefined || required ) {
  58. component.mount && component.mount();
  59. } else {
  60. delete this.Components[ key ];
  61. }
  62. } );
  63. } catch ( e ) {
  64. error( e.message );
  65. return;
  66. }
  67. const { State } = this;
  68. State.set( STATES.MOUNTED );
  69. each( this.Components, component => {
  70. component.mounted && component.mounted();
  71. } );
  72. this.emit( 'mounted' );
  73. State.set( STATES.IDLE );
  74. this.emit( 'ready' );
  75. applyStyle( this.root, { visibility: 'visible' } );
  76. this
  77. .on( 'move drag', () => State.set( STATES.MOVING ) )
  78. .on( 'moved dragged', () => State.set( STATES.IDLE ) );
  79. return this;
  80. }
  81. /**
  82. * Set sync target.
  83. *
  84. * @param {Splide} splide - A Splide instance.
  85. *
  86. * @return {Splide} - This instance.
  87. */
  88. sync( splide ) {
  89. this.sibling = splide;
  90. return this;
  91. }
  92. /**
  93. * Register callback fired on the given event(s).
  94. *
  95. * @param {string} events - An event name. Use space to separate multiple events.
  96. * Also, namespace is accepted by dot, such as 'resize.{namespace}'.
  97. * @param {function} handler - A callback function.
  98. * @param {Element} elm - Optional. Native event will be listened to when this arg is provided.
  99. * @param {Object} options - Optional. Options for addEventListener.
  100. *
  101. * @return {Splide} - This instance.
  102. */
  103. on( events, handler, elm = null, options = {} ) {
  104. this.Event.on( events, handler, elm, options );
  105. return this;
  106. }
  107. /**
  108. * Unsubscribe the given event.
  109. *
  110. * @param {string} events - A event name.
  111. * @param {Element} elm - Optional. removeEventListener() will be called when this arg is provided.
  112. *
  113. * @return {Splide} - This instance.
  114. */
  115. off( events, elm = null ) {
  116. this.Event.off( events, elm );
  117. return this;
  118. }
  119. /**
  120. * Emit an event.
  121. *
  122. * @param {string} event - An event name.
  123. * @param {*} args - Any number of arguments passed to handlers.
  124. */
  125. emit( event, ...args ) {
  126. this.Event.emit( event, ...args );
  127. return this;
  128. }
  129. /**
  130. * Go to the slide specified by the given control.
  131. *
  132. * @param {string|number} control - A control pattern.
  133. * @param {boolean} wait - Optional. Whether to wait for transition.
  134. */
  135. go( control, wait = true ) {
  136. if ( this.State.is( STATES.IDLE ) || ( this.State.is( STATES.MOVING ) && ! wait ) ) {
  137. this.Components.Controller.go( control, false );
  138. }
  139. return this;
  140. }
  141. /**
  142. * Verify whether the slider type is the given one or not.
  143. *
  144. * @param {string} type - A slider type.
  145. *
  146. * @return {boolean} - True if the slider type is the provided type or false if not.
  147. */
  148. is( type ) {
  149. return type === this._o.type;
  150. }
  151. /**
  152. * Insert a slide.
  153. *
  154. * @param {Element|string} slide - A slide element to be added.
  155. * @param {number} index - A slide will be added at the position.
  156. */
  157. add( slide, index = -1 ) {
  158. this.Components.Elements.add( slide, index, this.refresh.bind( this ) );
  159. return this;
  160. }
  161. /**
  162. * Remove the slide designated by the index.
  163. *
  164. * @param {number} index - A slide index.
  165. */
  166. remove( index ) {
  167. this.Components.Elements.remove( index );
  168. this.refresh();
  169. return this;
  170. }
  171. /**
  172. * Destroy all Slide objects and clones and recreate them again.
  173. */
  174. refresh() {
  175. this.emit( 'refresh' ).emit( 'resize' );
  176. return this;
  177. }
  178. /**
  179. * Destroy the Splide.
  180. * "Completely" boolean is mainly for breakpoints.
  181. *
  182. * @param {boolean} completely - Destroy completely.
  183. */
  184. destroy( completely = true ) {
  185. // Postpone destroy because it should be done after mount.
  186. if ( this.State.is( STATES.CREATED ) ) {
  187. this.on( 'ready', () => this.destroy( completely ) );
  188. return;
  189. }
  190. values( this.Components ).reverse().forEach( component => {
  191. component.destroy && component.destroy( completely );
  192. } );
  193. this.emit( 'destroy', completely );
  194. // Destroy all event handlers, including ones for native events.
  195. this.Event.destroy();
  196. this.State.set( STATES.DESTROYED );
  197. return this;
  198. }
  199. /**
  200. * Return the current slide index.
  201. *
  202. * @return {number} - The current slide index.
  203. // */
  204. get index() {
  205. return this._i;
  206. }
  207. /**
  208. * Set the current slide index.
  209. *
  210. * @param {number|string} index - A new index.
  211. */
  212. set index( index ) {
  213. this._i = parseInt( index );
  214. }
  215. /**
  216. * Return length of slides.
  217. * This is an alias of Elements.length.
  218. *
  219. * @return {number} - A number of slides.
  220. */
  221. get length() {
  222. return this.Components.Elements.length;
  223. }
  224. /**
  225. * Return options.
  226. *
  227. * @return {Object} - Options object.
  228. */
  229. get options() {
  230. return this._o;
  231. }
  232. /**
  233. * Set options with merging the given object to the current one.
  234. *
  235. * @param {Object} options - New options.
  236. */
  237. set options( options ) {
  238. const created = this.State.is( STATES.CREATED );
  239. if ( ! created ) {
  240. this.emit( 'update' );
  241. }
  242. this._o = merge( this._o, options );
  243. if ( ! created ) {
  244. this.emit( 'updated', this._o );
  245. }
  246. }
  247. /**
  248. * Return the class list.
  249. * This is an alias of Splide.options.classList.
  250. *
  251. * @return {Object} - An object containing all class list.
  252. */
  253. get classes() {
  254. return this._o.classes;
  255. }
  256. /**
  257. * Return the i18n strings.
  258. * This is an alias of Splide.options.i18n.
  259. *
  260. * @return {Object} - An object containing all i18n strings.
  261. */
  262. get i18n() {
  263. return this._o.i18n;
  264. }
  265. }