2 import Util from './util'
5 * --------------------------------------------------------------------------
6 * Bootstrap (v4.1.3): carousel.js
7 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
8 * --------------------------------------------------------------------------
11 const Carousel = (($) => {
13 * ------------------------------------------------------------------------
15 * ------------------------------------------------------------------------
18 const NAME = 'carousel'
19 const VERSION = '4.1.3'
20 const DATA_KEY = 'bs.carousel'
21 const EVENT_KEY = `.${DATA_KEY}`
22 const DATA_API_KEY = '.data-api'
23 const JQUERY_NO_CONFLICT = $.fn[NAME]
24 const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key
25 const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
26 const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
37 interval : '(number|boolean)',
39 slide : '(boolean|string)',
40 pause : '(string|boolean)',
52 SLIDE : `slide${EVENT_KEY}`,
53 SLID : `slid${EVENT_KEY}`,
54 KEYDOWN : `keydown${EVENT_KEY}`,
55 MOUSEENTER : `mouseenter${EVENT_KEY}`,
56 MOUSELEAVE : `mouseleave${EVENT_KEY}`,
57 TOUCHEND : `touchend${EVENT_KEY}`,
58 LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`,
59 CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
63 CAROUSEL : 'carousel',
66 RIGHT : 'carousel-item-right',
67 LEFT : 'carousel-item-left',
68 NEXT : 'carousel-item-next',
69 PREV : 'carousel-item-prev',
70 ITEM : 'carousel-item'
75 ACTIVE_ITEM : '.active.carousel-item',
76 ITEM : '.carousel-item',
77 NEXT_PREV : '.carousel-item-next, .carousel-item-prev',
78 INDICATORS : '.carousel-indicators',
79 DATA_SLIDE : '[data-slide], [data-slide-to]',
80 DATA_RIDE : '[data-ride="carousel"]'
84 * ------------------------------------------------------------------------
86 * ------------------------------------------------------------------------
90 constructor(element, config) {
93 this._activeElement = null
95 this._isPaused = false
96 this._isSliding = false
98 this.touchTimeout = null
100 this._config = this._getConfig(config)
101 this._element = $(element)[0]
102 this._indicatorsElement = this._element.querySelector(Selector.INDICATORS)
104 this._addEventListeners()
109 static get VERSION() {
113 static get Default() {
120 if (!this._isSliding) {
121 this._slide(Direction.NEXT)
126 // Don't call next when the page isn't visible
127 // or the carousel or its parent isn't visible
128 if (!document.hidden &&
129 ($(this._element).is(':visible') && $(this._element).css('visibility') !== 'hidden')) {
135 if (!this._isSliding) {
136 this._slide(Direction.PREV)
142 this._isPaused = true
145 if (this._element.querySelector(Selector.NEXT_PREV)) {
146 Util.triggerTransitionEnd(this._element)
150 clearInterval(this._interval)
151 this._interval = null
156 this._isPaused = false
159 if (this._interval) {
160 clearInterval(this._interval)
161 this._interval = null
164 if (this._config.interval && !this._isPaused) {
165 this._interval = setInterval(
166 (document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
167 this._config.interval
173 this._activeElement = this._element.querySelector(Selector.ACTIVE_ITEM)
175 const activeIndex = this._getItemIndex(this._activeElement)
177 if (index > this._items.length - 1 || index < 0) {
181 if (this._isSliding) {
182 $(this._element).one(Event.SLID, () => this.to(index))
186 if (activeIndex === index) {
192 const direction = index > activeIndex
196 this._slide(direction, this._items[index])
200 $(this._element).off(EVENT_KEY)
201 $.removeData(this._element, DATA_KEY)
206 this._interval = null
207 this._isPaused = null
208 this._isSliding = null
209 this._activeElement = null
210 this._indicatorsElement = null
220 Util.typeCheckConfig(NAME, config, DefaultType)
224 _addEventListeners() {
225 if (this._config.keyboard) {
227 .on(Event.KEYDOWN, (event) => this._keydown(event))
230 if (this._config.pause === 'hover') {
232 .on(Event.MOUSEENTER, (event) => this.pause(event))
233 .on(Event.MOUSELEAVE, (event) => this.cycle(event))
234 if ('ontouchstart' in document.documentElement) {
235 // If it's a touch-enabled device, mouseenter/leave are fired as
236 // part of the mouse compatibility events on first tap - the carousel
237 // would stop cycling until user tapped out of it;
238 // here, we listen for touchend, explicitly pause the carousel
239 // (as if it's the second time we tap on it, mouseenter compat event
240 // is NOT fired) and after a timeout (to allow for mouse compatibility
241 // events to fire) we explicitly restart cycling
242 $(this._element).on(Event.TOUCHEND, () => {
244 if (this.touchTimeout) {
245 clearTimeout(this.touchTimeout)
247 this.touchTimeout = setTimeout((event) => this.cycle(event), TOUCHEVENT_COMPAT_WAIT + this._config.interval)
254 if (/input|textarea/i.test(event.target.tagName)) {
258 switch (event.which) {
259 case ARROW_LEFT_KEYCODE:
260 event.preventDefault()
263 case ARROW_RIGHT_KEYCODE:
264 event.preventDefault()
271 _getItemIndex(element) {
272 this._items = element && element.parentNode
273 ? [].slice.call(element.parentNode.querySelectorAll(Selector.ITEM))
275 return this._items.indexOf(element)
278 _getItemByDirection(direction, activeElement) {
279 const isNextDirection = direction === Direction.NEXT
280 const isPrevDirection = direction === Direction.PREV
281 const activeIndex = this._getItemIndex(activeElement)
282 const lastItemIndex = this._items.length - 1
283 const isGoingToWrap = isPrevDirection && activeIndex === 0 ||
284 isNextDirection && activeIndex === lastItemIndex
286 if (isGoingToWrap && !this._config.wrap) {
290 const delta = direction === Direction.PREV ? -1 : 1
291 const itemIndex = (activeIndex + delta) % this._items.length
293 return itemIndex === -1
294 ? this._items[this._items.length - 1] : this._items[itemIndex]
297 _triggerSlideEvent(relatedTarget, eventDirectionName) {
298 const targetIndex = this._getItemIndex(relatedTarget)
299 const fromIndex = this._getItemIndex(this._element.querySelector(Selector.ACTIVE_ITEM))
300 const slideEvent = $.Event(Event.SLIDE, {
302 direction: eventDirectionName,
307 $(this._element).trigger(slideEvent)
312 _setActiveIndicatorElement(element) {
313 if (this._indicatorsElement) {
314 const indicators = [].slice.call(this._indicatorsElement.querySelectorAll(Selector.ACTIVE))
316 .removeClass(ClassName.ACTIVE)
318 const nextIndicator = this._indicatorsElement.children[
319 this._getItemIndex(element)
323 $(nextIndicator).addClass(ClassName.ACTIVE)
328 _slide(direction, element) {
329 const activeElement = this._element.querySelector(Selector.ACTIVE_ITEM)
330 const activeElementIndex = this._getItemIndex(activeElement)
331 const nextElement = element || activeElement &&
332 this._getItemByDirection(direction, activeElement)
333 const nextElementIndex = this._getItemIndex(nextElement)
334 const isCycling = Boolean(this._interval)
336 let directionalClassName
338 let eventDirectionName
340 if (direction === Direction.NEXT) {
341 directionalClassName = ClassName.LEFT
342 orderClassName = ClassName.NEXT
343 eventDirectionName = Direction.LEFT
345 directionalClassName = ClassName.RIGHT
346 orderClassName = ClassName.PREV
347 eventDirectionName = Direction.RIGHT
350 if (nextElement && $(nextElement).hasClass(ClassName.ACTIVE)) {
351 this._isSliding = false
355 const slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName)
356 if (slideEvent.isDefaultPrevented()) {
360 if (!activeElement || !nextElement) {
361 // Some weirdness is happening, so we bail
365 this._isSliding = true
371 this._setActiveIndicatorElement(nextElement)
373 const slidEvent = $.Event(Event.SLID, {
374 relatedTarget: nextElement,
375 direction: eventDirectionName,
376 from: activeElementIndex,
380 if ($(this._element).hasClass(ClassName.SLIDE)) {
381 $(nextElement).addClass(orderClassName)
383 Util.reflow(nextElement)
385 $(activeElement).addClass(directionalClassName)
386 $(nextElement).addClass(directionalClassName)
388 const transitionDuration = Util.getTransitionDurationFromElement(activeElement)
391 .one(Util.TRANSITION_END, () => {
393 .removeClass(`${directionalClassName} ${orderClassName}`)
394 .addClass(ClassName.ACTIVE)
396 $(activeElement).removeClass(`${ClassName.ACTIVE} ${orderClassName} ${directionalClassName}`)
398 this._isSliding = false
400 setTimeout(() => $(this._element).trigger(slidEvent), 0)
402 .emulateTransitionEnd(transitionDuration)
404 $(activeElement).removeClass(ClassName.ACTIVE)
405 $(nextElement).addClass(ClassName.ACTIVE)
407 this._isSliding = false
408 $(this._element).trigger(slidEvent)
418 static _jQueryInterface(config) {
419 return this.each(function () {
420 let data = $(this).data(DATA_KEY)
426 if (typeof config === 'object') {
433 const action = typeof config === 'string' ? config : _config.slide
436 data = new Carousel(this, _config)
437 $(this).data(DATA_KEY, data)
440 if (typeof config === 'number') {
442 } else if (typeof action === 'string') {
443 if (typeof data[action] === 'undefined') {
444 throw new TypeError(`No method named "${action}"`)
447 } else if (_config.interval) {
454 static _dataApiClickHandler(event) {
455 const selector = Util.getSelectorFromElement(this)
461 const target = $(selector)[0]
463 if (!target || !$(target).hasClass(ClassName.CAROUSEL)) {
471 const slideIndex = this.getAttribute('data-slide-to')
474 config.interval = false
477 Carousel._jQueryInterface.call($(target), config)
480 $(target).data(DATA_KEY).to(slideIndex)
483 event.preventDefault()
488 * ------------------------------------------------------------------------
489 * Data Api implementation
490 * ------------------------------------------------------------------------
494 .on(Event.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel._dataApiClickHandler)
496 $(window).on(Event.LOAD_DATA_API, () => {
497 const carousels = [].slice.call(document.querySelectorAll(Selector.DATA_RIDE))
498 for (let i = 0, len = carousels.length; i < len; i++) {
499 const $carousel = $(carousels[i])
500 Carousel._jQueryInterface.call($carousel, $carousel.data())
505 * ------------------------------------------------------------------------
507 * ------------------------------------------------------------------------
510 $.fn[NAME] = Carousel._jQueryInterface
511 $.fn[NAME].Constructor = Carousel
512 $.fn[NAME].noConflict = function () {
513 $.fn[NAME] = JQUERY_NO_CONFLICT
514 return Carousel._jQueryInterface
520 export default Carousel