carousel.js 16.7 KB
Newer Older
fat's avatar
fat committed
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v4.3.1): carousel.js
fat's avatar
fat committed
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
import {
9
  getjQuery,
10
11
  TRANSITION_END,
  emulateTransitionEnd,
12
  getElementFromSelector,
13
14
15
16
17
18
  getTransitionDurationFromElement,
  isVisible,
  makeArray,
  reflow,
  triggerTransitionEnd,
  typeCheckConfig
Johann-S's avatar
Johann-S committed
19
20
21
22
23
} from '../util/index'
import Data from '../dom/data'
import EventHandler from '../dom/event-handler'
import Manipulator from '../dom/manipulator'
import SelectorEngine from '../dom/selector-engine'
Johann-S's avatar
Johann-S committed
24

Johann-S's avatar
Johann-S committed
25
26
27
28
29
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
30

XhmikosR's avatar
XhmikosR committed
31
32
33
34
35
36
37
const NAME = 'carousel'
const VERSION = '4.3.1'
const DATA_KEY = 'bs.carousel'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key
const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
Johann-S's avatar
Johann-S committed
38
const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
XhmikosR's avatar
XhmikosR committed
39
const SWIPE_THRESHOLD = 40
Johann-S's avatar
Johann-S committed
40
41

const Default = {
XhmikosR's avatar
XhmikosR committed
42
43
44
45
46
47
  interval: 5000,
  keyboard: true,
  slide: false,
  pause: 'hover',
  wrap: true,
  touch: true
Johann-S's avatar
Johann-S committed
48
49
50
}

const DefaultType = {
XhmikosR's avatar
XhmikosR committed
51
52
53
54
55
56
  interval: '(number|boolean)',
  keyboard: 'boolean',
  slide: '(boolean|string)',
  pause: '(string|boolean)',
  wrap: 'boolean',
  touch: 'boolean'
Johann-S's avatar
Johann-S committed
57
58
59
}

const Direction = {
XhmikosR's avatar
XhmikosR committed
60
61
62
63
  NEXT: 'next',
  PREV: 'prev',
  LEFT: 'left',
  RIGHT: 'right'
Johann-S's avatar
Johann-S committed
64
65
66
}

const Event = {
XhmikosR's avatar
XhmikosR committed
67
68
69
70
71
72
73
74
75
76
77
78
79
  SLIDE: `slide${EVENT_KEY}`,
  SLID: `slid${EVENT_KEY}`,
  KEYDOWN: `keydown${EVENT_KEY}`,
  MOUSEENTER: `mouseenter${EVENT_KEY}`,
  MOUSELEAVE: `mouseleave${EVENT_KEY}`,
  TOUCHSTART: `touchstart${EVENT_KEY}`,
  TOUCHMOVE: `touchmove${EVENT_KEY}`,
  TOUCHEND: `touchend${EVENT_KEY}`,
  POINTERDOWN: `pointerdown${EVENT_KEY}`,
  POINTERUP: `pointerup${EVENT_KEY}`,
  DRAG_START: `dragstart${EVENT_KEY}`,
  LOAD_DATA_API: `load${EVENT_KEY}${DATA_API_KEY}`,
  CLICK_DATA_API: `click${EVENT_KEY}${DATA_API_KEY}`
Johann-S's avatar
Johann-S committed
80
81
82
}

const ClassName = {
XhmikosR's avatar
XhmikosR committed
83
84
85
86
87
88
89
90
91
  CAROUSEL: 'carousel',
  ACTIVE: 'active',
  SLIDE: 'slide',
  RIGHT: 'carousel-item-right',
  LEFT: 'carousel-item-left',
  NEXT: 'carousel-item-next',
  PREV: 'carousel-item-prev',
  ITEM: 'carousel-item',
  POINTER_EVENT: 'pointer-event'
Johann-S's avatar
Johann-S committed
92
93
94
}

const Selector = {
XhmikosR's avatar
XhmikosR committed
95
96
97
98
99
100
101
102
  ACTIVE: '.active',
  ACTIVE_ITEM: '.active.carousel-item',
  ITEM: '.carousel-item',
  ITEM_IMG: '.carousel-item img',
  NEXT_PREV: '.carousel-item-next, .carousel-item-prev',
  INDICATORS: '.carousel-indicators',
  DATA_SLIDE: '[data-slide], [data-slide-to]',
  DATA_RIDE: '[data-ride="carousel"]'
Johann-S's avatar
Johann-S committed
103
}
fat's avatar
fat committed
104

Johann-S's avatar
Johann-S committed
105
const PointerType = {
XhmikosR's avatar
XhmikosR committed
106
107
  TOUCH: 'touch',
  PEN: 'pen'
Johann-S's avatar
Johann-S committed
108
109
}

Johann-S's avatar
Johann-S committed
110
111
112
113
114
115
116
/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
class Carousel {
  constructor(element, config) {
XhmikosR's avatar
XhmikosR committed
117
118
    this._items = null
    this._interval = null
119
    this._activeElement = null
XhmikosR's avatar
XhmikosR committed
120
121
122
123
124
125
126
127
    this._isPaused = false
    this._isSliding = false
    this.touchTimeout = null
    this.touchStartX = 0
    this.touchDeltaX = 0

    this._config = this._getConfig(config)
    this._element = element
Johann-S's avatar
Johann-S committed
128
    this._indicatorsElement = SelectorEngine.findOne(Selector.INDICATORS, this._element)
XhmikosR's avatar
XhmikosR committed
129
130
    this._touchSupported = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0
    this._pointerEvent = Boolean(window.PointerEvent || window.MSPointerEvent)
fat's avatar
fat committed
131

Johann-S's avatar
Johann-S committed
132
    this._addEventListeners()
133
    Data.setData(element, DATA_KEY, this)
Johann-S's avatar
Johann-S committed
134
  }
fat's avatar
fat committed
135

Johann-S's avatar
Johann-S committed
136
  // Getters
137

Johann-S's avatar
Johann-S committed
138
139
140
  static get VERSION() {
    return VERSION
  }
141

Johann-S's avatar
Johann-S committed
142
143
144
  static get Default() {
    return Default
  }
145

Johann-S's avatar
Johann-S committed
146
  // Public
147

Johann-S's avatar
Johann-S committed
148
149
150
  next() {
    if (!this._isSliding) {
      this._slide(Direction.NEXT)
151
    }
Johann-S's avatar
Johann-S committed
152
  }
fat's avatar
fat committed
153

Johann-S's avatar
Johann-S committed
154
155
156
  nextWhenVisible() {
    // Don't call next when the page isn't visible
    // or the carousel or its parent isn't visible
157
    if (!document.hidden && isVisible(this._element)) {
Johann-S's avatar
Johann-S committed
158
      this.next()
fat's avatar
fat committed
159
    }
Johann-S's avatar
Johann-S committed
160
  }
fat's avatar
fat committed
161

Johann-S's avatar
Johann-S committed
162
163
164
  prev() {
    if (!this._isSliding) {
      this._slide(Direction.PREV)
fat's avatar
fat committed
165
    }
Johann-S's avatar
Johann-S committed
166
  }
fat's avatar
fat committed
167

Johann-S's avatar
Johann-S committed
168
169
170
  pause(event) {
    if (!event) {
      this._isPaused = true
171
172
    }

Johann-S's avatar
Johann-S committed
173
    if (SelectorEngine.findOne(Selector.NEXT_PREV, this._element)) {
174
      triggerTransitionEnd(this._element)
Johann-S's avatar
Johann-S committed
175
      this.cycle(true)
fat's avatar
fat committed
176
177
    }

Johann-S's avatar
Johann-S committed
178
179
180
    clearInterval(this._interval)
    this._interval = null
  }
fat's avatar
fat committed
181

Johann-S's avatar
Johann-S committed
182
183
184
185
  cycle(event) {
    if (!event) {
      this._isPaused = false
    }
fat's avatar
fat committed
186

Johann-S's avatar
Johann-S committed
187
    if (this._interval) {
fat's avatar
fat committed
188
189
190
191
      clearInterval(this._interval)
      this._interval = null
    }

Johann-S's avatar
Johann-S committed
192
    if (this._config && this._config.interval && !this._isPaused) {
Johann-S's avatar
Johann-S committed
193
194
195
196
197
198
      this._interval = setInterval(
        (document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
        this._config.interval
      )
    }
  }
fat's avatar
fat committed
199

Johann-S's avatar
Johann-S committed
200
  to(index) {
Johann-S's avatar
Johann-S committed
201
    this._activeElement = SelectorEngine.findOne(Selector.ACTIVE_ITEM, this._element)
Johann-S's avatar
Johann-S committed
202
203
204
205
    const activeIndex = this._getItemIndex(this._activeElement)

    if (index > this._items.length - 1 || index < 0) {
      return
fat's avatar
fat committed
206
207
    }

Johann-S's avatar
Johann-S committed
208
    if (this._isSliding) {
Johann-S's avatar
Johann-S committed
209
      EventHandler.one(this._element, Event.SLID, () => this.to(index))
Johann-S's avatar
Johann-S committed
210
211
      return
    }
fat's avatar
fat committed
212

Johann-S's avatar
Johann-S committed
213
214
215
216
217
    if (activeIndex === index) {
      this.pause()
      this.cycle()
      return
    }
fat's avatar
fat committed
218

XhmikosR's avatar
XhmikosR committed
219
220
221
    const direction = index > activeIndex ?
      Direction.NEXT :
      Direction.PREV
fat's avatar
fat committed
222

Johann-S's avatar
Johann-S committed
223
224
    this._slide(direction, this._items[index])
  }
fat's avatar
fat committed
225

Johann-S's avatar
Johann-S committed
226
  dispose() {
227
    EventHandler.off(this._element, EVENT_KEY)
Johann-S's avatar
Johann-S committed
228
    Data.removeData(this._element, DATA_KEY)
Johann-S's avatar
Johann-S committed
229

XhmikosR's avatar
XhmikosR committed
230
231
232
233
234
235
236
    this._items = null
    this._config = null
    this._element = null
    this._interval = null
    this._isPaused = null
    this._isSliding = null
    this._activeElement = null
Johann-S's avatar
Johann-S committed
237
238
    this._indicatorsElement = null
  }
fat's avatar
fat committed
239

Johann-S's avatar
Johann-S committed
240
  // Private
fat's avatar
fat committed
241

Johann-S's avatar
Johann-S committed
242
243
244
245
  _getConfig(config) {
    config = {
      ...Default,
      ...config
fat's avatar
fat committed
246
    }
247
    typeCheckConfig(NAME, config, DefaultType)
Johann-S's avatar
Johann-S committed
248
249
    return config
  }
fat's avatar
fat committed
250

Johann-S's avatar
Johann-S committed
251
252
253
254
255
256
257
258
259
  _handleSwipe() {
    const absDeltax = Math.abs(this.touchDeltaX)

    if (absDeltax <= SWIPE_THRESHOLD) {
      return
    }

    const direction = absDeltax / this.touchDeltaX

260
261
    this.touchDeltaX = 0

Johann-S's avatar
Johann-S committed
262
263
264
265
266
267
268
269
270
271
272
    // swipe left
    if (direction > 0) {
      this.prev()
    }

    // swipe right
    if (direction < 0) {
      this.next()
    }
  }

Johann-S's avatar
Johann-S committed
273
274
  _addEventListeners() {
    if (this._config.keyboard) {
Johann-S's avatar
Johann-S committed
275
      EventHandler
XhmikosR's avatar
XhmikosR committed
276
        .on(this._element, Event.KEYDOWN, event => this._keydown(event))
fat's avatar
fat committed
277
278
    }

Johann-S's avatar
Johann-S committed
279
    if (this._config.pause === 'hover') {
Johann-S's avatar
Johann-S committed
280
      EventHandler
XhmikosR's avatar
XhmikosR committed
281
        .on(this._element, Event.MOUSEENTER, event => this.pause(event))
Johann-S's avatar
Johann-S committed
282
      EventHandler
XhmikosR's avatar
XhmikosR committed
283
        .on(this._element, Event.MOUSELEAVE, event => this.cycle(event))
Johann-S's avatar
Johann-S committed
284
285
    }

Johann-S's avatar
Johann-S committed
286
    if (this._config.touch && this._touchSupported) {
287
288
      this._addTouchEventListeners()
    }
Johann-S's avatar
Johann-S committed
289
290
291
  }

  _addTouchEventListeners() {
XhmikosR's avatar
XhmikosR committed
292
    const start = event => {
Johann-S's avatar
Johann-S committed
293
294
      if (this._pointerEvent && PointerType[event.pointerType.toUpperCase()]) {
        this.touchStartX = event.clientX
295
      } else if (!this._pointerEvent) {
Johann-S's avatar
Johann-S committed
296
        this.touchStartX = event.touches[0].clientX
Johann-S's avatar
Johann-S committed
297
298
      }
    }
Johann-S's avatar
Johann-S committed
299

XhmikosR's avatar
XhmikosR committed
300
    const move = event => {
Johann-S's avatar
Johann-S committed
301
      // ensure swiping with one touch and not pinching
Johann-S's avatar
Johann-S committed
302
      if (event.touches && event.touches.length > 1) {
Johann-S's avatar
Johann-S committed
303
304
        this.touchDeltaX = 0
      } else {
Johann-S's avatar
Johann-S committed
305
        this.touchDeltaX = event.touches[0].clientX - this.touchStartX
Johann-S's avatar
Johann-S committed
306
307
308
      }
    }

XhmikosR's avatar
XhmikosR committed
309
    const end = event => {
Johann-S's avatar
Johann-S committed
310
      if (this._pointerEvent && PointerType[event.pointerType.toUpperCase()]) {
Johann-S's avatar
Johann-S committed
311
        this.touchDeltaX = event.clientX - this.touchStartX
Johann-S's avatar
Johann-S committed
312
      }
Johann-S's avatar
Johann-S committed
313
314
315

      this._handleSwipe()
      if (this._config.pause === 'hover') {
Johann-S's avatar
Johann-S committed
316
317
318
319
320
321
322
        // If it's a touch-enabled device, mouseenter/leave are fired as
        // part of the mouse compatibility events on first tap - the carousel
        // would stop cycling until user tapped out of it;
        // here, we listen for touchend, explicitly pause the carousel
        // (as if it's the second time we tap on it, mouseenter compat event
        // is NOT fired) and after a timeout (to allow for mouse compatibility
        // events to fire) we explicitly restart cycling
Johann-S's avatar
Johann-S committed
323
324
325
326
327

        this.pause()
        if (this.touchTimeout) {
          clearTimeout(this.touchTimeout)
        }
XhmikosR's avatar
XhmikosR committed
328
329

        this.touchTimeout = setTimeout(event => this.cycle(event), TOUCHEVENT_COMPAT_WAIT + this._config.interval)
330
      }
Johann-S's avatar
Johann-S committed
331
332
    }

XhmikosR's avatar
XhmikosR committed
333
334
    makeArray(SelectorEngine.find(Selector.ITEM_IMG, this._element)).forEach(itemImg => {
      EventHandler.on(itemImg, Event.DRAG_START, e => e.preventDefault())
Johann-S's avatar
Johann-S committed
335
    })
336

Johann-S's avatar
Johann-S committed
337
    if (this._pointerEvent) {
XhmikosR's avatar
XhmikosR committed
338
339
      EventHandler.on(this._element, Event.POINTERDOWN, event => start(event))
      EventHandler.on(this._element, Event.POINTERUP, event => end(event))
Johann-S's avatar
Johann-S committed
340
341
342

      this._element.classList.add(ClassName.POINTER_EVENT)
    } else {
XhmikosR's avatar
XhmikosR committed
343
344
345
      EventHandler.on(this._element, Event.TOUCHSTART, event => start(event))
      EventHandler.on(this._element, Event.TOUCHMOVE, event => move(event))
      EventHandler.on(this._element, Event.TOUCHEND, event => end(event))
Johann-S's avatar
Johann-S committed
346
    }
Johann-S's avatar
Johann-S committed
347
  }
fat's avatar
fat committed
348

Johann-S's avatar
Johann-S committed
349
350
351
  _keydown(event) {
    if (/input|textarea/i.test(event.target.tagName)) {
      return
fat's avatar
fat committed
352
353
    }

Johann-S's avatar
Johann-S committed
354
355
356
357
358
359
360
361
362
363
    switch (event.which) {
      case ARROW_LEFT_KEYCODE:
        event.preventDefault()
        this.prev()
        break
      case ARROW_RIGHT_KEYCODE:
        event.preventDefault()
        this.next()
        break
      default:
fat's avatar
fat committed
364
    }
Johann-S's avatar
Johann-S committed
365
  }
fat's avatar
fat committed
366

Johann-S's avatar
Johann-S committed
367
  _getItemIndex(element) {
XhmikosR's avatar
XhmikosR committed
368
369
370
    this._items = element && element.parentNode ?
      makeArray(SelectorEngine.find(Selector.ITEM, element.parentNode)) :
      []
Johann-S's avatar
Johann-S committed
371

Johann-S's avatar
Johann-S committed
372
373
    return this._items.indexOf(element)
  }
fat's avatar
fat committed
374

Johann-S's avatar
Johann-S committed
375
376
377
  _getItemByDirection(direction, activeElement) {
    const isNextDirection = direction === Direction.NEXT
    const isPrevDirection = direction === Direction.PREV
XhmikosR's avatar
XhmikosR committed
378
379
    const activeIndex = this._getItemIndex(activeElement)
    const lastItemIndex = this._items.length - 1
380
381
    const isGoingToWrap = (isPrevDirection && activeIndex === 0) ||
                            (isNextDirection && activeIndex === lastItemIndex)
fat's avatar
fat committed
382

Johann-S's avatar
Johann-S committed
383
384
385
    if (isGoingToWrap && !this._config.wrap) {
      return activeElement
    }
fat's avatar
fat committed
386

XhmikosR's avatar
XhmikosR committed
387
    const delta = direction === Direction.PREV ? -1 : 1
Johann-S's avatar
Johann-S committed
388
    const itemIndex = (activeIndex + delta) % this._items.length
fat's avatar
fat committed
389

XhmikosR's avatar
XhmikosR committed
390
391
392
    return itemIndex === -1 ?
      this._items[this._items.length - 1] :
      this._items[itemIndex]
Johann-S's avatar
Johann-S committed
393
  }
fat's avatar
fat committed
394

Johann-S's avatar
Johann-S committed
395
396
  _triggerSlideEvent(relatedTarget, eventDirectionName) {
    const targetIndex = this._getItemIndex(relatedTarget)
Johann-S's avatar
Johann-S committed
397
398
399
    const fromIndex = this._getItemIndex(SelectorEngine.findOne(Selector.ACTIVE_ITEM, this._element))

    return EventHandler.trigger(this._element, Event.SLIDE, {
Johann-S's avatar
Johann-S committed
400
401
402
403
404
405
      relatedTarget,
      direction: eventDirectionName,
      from: fromIndex,
      to: targetIndex
    })
  }
fat's avatar
fat committed
406

Johann-S's avatar
Johann-S committed
407
408
  _setActiveIndicatorElement(element) {
    if (this._indicatorsElement) {
Johann-S's avatar
Johann-S committed
409
410
411
412
      const indicators = SelectorEngine.find(Selector.ACTIVE, this._indicatorsElement)
      for (let i = 0; i < indicators.length; i++) {
        indicators[i].classList.remove(ClassName.ACTIVE)
      }
fat's avatar
fat committed
413

Johann-S's avatar
Johann-S committed
414
415
416
      const nextIndicator = this._indicatorsElement.children[
        this._getItemIndex(element)
      ]
fat's avatar
fat committed
417

Johann-S's avatar
Johann-S committed
418
      if (nextIndicator) {
Johann-S's avatar
Johann-S committed
419
        nextIndicator.classList.add(ClassName.ACTIVE)
fat's avatar
fat committed
420
421
      }
    }
Johann-S's avatar
Johann-S committed
422
  }
fat's avatar
fat committed
423

Johann-S's avatar
Johann-S committed
424
  _slide(direction, element) {
Johann-S's avatar
Johann-S committed
425
    const activeElement = SelectorEngine.findOne(Selector.ACTIVE_ITEM, this._element)
Johann-S's avatar
Johann-S committed
426
    const activeElementIndex = this._getItemIndex(activeElement)
427
428
    const nextElement = element || (activeElement &&
      this._getItemByDirection(direction, activeElement))
Johann-S's avatar
Johann-S committed
429

Johann-S's avatar
Johann-S committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
    const nextElementIndex = this._getItemIndex(nextElement)
    const isCycling = Boolean(this._interval)

    let directionalClassName
    let orderClassName
    let eventDirectionName

    if (direction === Direction.NEXT) {
      directionalClassName = ClassName.LEFT
      orderClassName = ClassName.NEXT
      eventDirectionName = Direction.LEFT
    } else {
      directionalClassName = ClassName.RIGHT
      orderClassName = ClassName.PREV
      eventDirectionName = Direction.RIGHT
    }
Mark Otto's avatar
Mark Otto committed
446

Johann-S's avatar
Johann-S committed
447
    if (nextElement && nextElement.classList.contains(ClassName.ACTIVE)) {
Johann-S's avatar
Johann-S committed
448
449
450
      this._isSliding = false
      return
    }
fat's avatar
fat committed
451

Johann-S's avatar
Johann-S committed
452
    const slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName)
Johann-S's avatar
Johann-S committed
453
    if (slideEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
454
455
      return
    }
fat's avatar
fat committed
456

Johann-S's avatar
Johann-S committed
457
458
459
460
    if (!activeElement || !nextElement) {
      // Some weirdness is happening, so we bail
      return
    }
fat's avatar
fat committed
461

Johann-S's avatar
Johann-S committed
462
    this._isSliding = true
fat's avatar
fat committed
463

Johann-S's avatar
Johann-S committed
464
465
466
    if (isCycling) {
      this.pause()
    }
fat's avatar
fat committed
467

Johann-S's avatar
Johann-S committed
468
    this._setActiveIndicatorElement(nextElement)
fat's avatar
fat committed
469

Johann-S's avatar
Johann-S committed
470
471
    if (this._element.classList.contains(ClassName.SLIDE)) {
      nextElement.classList.add(orderClassName)
fat's avatar
fat committed
472

473
      reflow(nextElement)
fat's avatar
fat committed
474

Johann-S's avatar
Johann-S committed
475
476
      activeElement.classList.add(directionalClassName)
      nextElement.classList.add(directionalClassName)
477

Johann-S's avatar
Johann-S committed
478
479
480
481
482
483
484
485
      const nextElementInterval = parseInt(nextElement.getAttribute('data-interval'), 10)
      if (nextElementInterval) {
        this._config.defaultInterval = this._config.defaultInterval || this._config.interval
        this._config.interval = nextElementInterval
      } else {
        this._config.interval = this._config.defaultInterval || this._config.interval
      }

486
      const transitionDuration = getTransitionDurationFromElement(activeElement)
487

Johann-S's avatar
Johann-S committed
488
      EventHandler
489
        .one(activeElement, TRANSITION_END, () => {
Johann-S's avatar
Johann-S committed
490
491
492
          nextElement.classList.remove(directionalClassName)
          nextElement.classList.remove(orderClassName)
          nextElement.classList.add(ClassName.ACTIVE)
fat's avatar
fat committed
493

Johann-S's avatar
Johann-S committed
494
495
496
          activeElement.classList.remove(ClassName.ACTIVE)
          activeElement.classList.remove(orderClassName)
          activeElement.classList.remove(directionalClassName)
fat's avatar
fat committed
497

Johann-S's avatar
Johann-S committed
498
          this._isSliding = false
fat's avatar
fat committed
499

Johann-S's avatar
Johann-S committed
500
501
502
503
504
505
506
507
          setTimeout(() => {
            EventHandler.trigger(this._element, Event.SLID, {
              relatedTarget: nextElement,
              direction: eventDirectionName,
              from: activeElementIndex,
              to: nextElementIndex
            })
          }, 0)
Johann-S's avatar
Johann-S committed
508
        })
Johann-S's avatar
Johann-S committed
509

510
      emulateTransitionEnd(activeElement, transitionDuration)
Johann-S's avatar
Johann-S committed
511
    } else {
Johann-S's avatar
Johann-S committed
512
513
      activeElement.classList.remove(ClassName.ACTIVE)
      nextElement.classList.add(ClassName.ACTIVE)
fat's avatar
fat committed
514

Johann-S's avatar
Johann-S committed
515
      this._isSliding = false
Johann-S's avatar
Johann-S committed
516
517
518
519
520
521
      EventHandler.trigger(this._element, Event.SLID, {
        relatedTarget: nextElement,
        direction: eventDirectionName,
        from: activeElementIndex,
        to: nextElementIndex
      })
Johann-S's avatar
Johann-S committed
522
    }
fat's avatar
fat committed
523

Johann-S's avatar
Johann-S committed
524
525
    if (isCycling) {
      this.cycle()
fat's avatar
fat committed
526
    }
Johann-S's avatar
Johann-S committed
527
  }
fat's avatar
fat committed
528

Johann-S's avatar
Johann-S committed
529
  // Static
fat's avatar
fat committed
530

531
  static carouselInterface(element, config) {
XhmikosR's avatar
XhmikosR committed
532
    let data = Data.getData(element, DATA_KEY)
533
534
535
536
    let _config = {
      ...Default,
      ...Manipulator.getDataAttributes(element)
    }
fat's avatar
fat committed
537

538
539
540
541
    if (typeof config === 'object') {
      _config = {
        ..._config,
        ...config
Johann-S's avatar
Johann-S committed
542
      }
543
    }
fat's avatar
fat committed
544

545
    const action = typeof config === 'string' ? config : _config.slide
fat's avatar
fat committed
546

547
548
549
    if (!data) {
      data = new Carousel(element, _config)
    }
fat's avatar
fat committed
550

551
552
553
554
    if (typeof config === 'number') {
      data.to(config)
    } else if (typeof action === 'string') {
      if (typeof data[action] === 'undefined') {
XhmikosR's avatar
XhmikosR committed
555
        throw new TypeError(`No method named "${action}"`)
fat's avatar
fat committed
556
      }
XhmikosR's avatar
XhmikosR committed
557

558
559
560
561
562
563
564
      data[action]()
    } else if (_config.interval && _config.ride) {
      data.pause()
      data.cycle()
    }
  }

565
  static jQueryInterface(config) {
566
    return this.each(function () {
567
      Carousel.carouselInterface(this, config)
Johann-S's avatar
Johann-S committed
568
569
    })
  }
fat's avatar
fat committed
570

571
  static dataApiClickHandler(event) {
572
    const target = getElementFromSelector(this)
Jacob Thornton's avatar
Jacob Thornton committed
573

Johann-S's avatar
Johann-S committed
574
    if (!target || !target.classList.contains(ClassName.CAROUSEL)) {
Johann-S's avatar
Johann-S committed
575
576
      return
    }
fat's avatar
fat committed
577

Johann-S's avatar
Johann-S committed
578
    const config = {
579
580
      ...Manipulator.getDataAttributes(target),
      ...Manipulator.getDataAttributes(this)
Johann-S's avatar
Johann-S committed
581
582
    }
    const slideIndex = this.getAttribute('data-slide-to')
fat's avatar
fat committed
583

Johann-S's avatar
Johann-S committed
584
585
586
587
    if (slideIndex) {
      config.interval = false
    }

588
    Carousel.carouselInterface(target, config)
fat's avatar
fat committed
589

Johann-S's avatar
Johann-S committed
590
    if (slideIndex) {
Johann-S's avatar
Johann-S committed
591
      Data.getData(target, DATA_KEY).to(slideIndex)
fat's avatar
fat committed
592
    }
Johann-S's avatar
Johann-S committed
593
594

    event.preventDefault()
fat's avatar
fat committed
595
  }
596

597
  static getInstance(element) {
598
599
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
600
}
fat's avatar
fat committed
601

Johann-S's avatar
Johann-S committed
602
603
604
605
606
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
607

Johann-S's avatar
Johann-S committed
608
EventHandler
609
  .on(document, Event.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel.dataApiClickHandler)
fat's avatar
fat committed
610

Johann-S's avatar
Johann-S committed
611
EventHandler.on(window, Event.LOAD_DATA_API, () => {
612
  const carousels = makeArray(SelectorEngine.find(Selector.DATA_RIDE))
Johann-S's avatar
Johann-S committed
613
  for (let i = 0, len = carousels.length; i < len; i++) {
614
    Carousel.carouselInterface(carousels[i], Data.getData(carousels[i], DATA_KEY))
fat's avatar
fat committed
615
  }
Johann-S's avatar
Johann-S committed
616
617
})

618
619
const $ = getjQuery()

Johann-S's avatar
Johann-S committed
620
621
622
623
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
624
 * add .carousel to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
625
 */
Johann-S's avatar
Johann-S committed
626
/* istanbul ignore if */
627
if ($) {
Johann-S's avatar
Johann-S committed
628
  const JQUERY_NO_CONFLICT = $.fn[NAME]
629
  $.fn[NAME] = Carousel.jQueryInterface
XhmikosR's avatar
XhmikosR committed
630
631
  $.fn[NAME].Constructor = Carousel
  $.fn[NAME].noConflict = () => {
Johann-S's avatar
Johann-S committed
632
    $.fn[NAME] = JQUERY_NO_CONFLICT
633
    return Carousel.jQueryInterface
Johann-S's avatar
Johann-S committed
634
  }
Johann-S's avatar
Johann-S committed
635
}
fat's avatar
fat committed
636
637

export default Carousel