carousel.js 17.2 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
9
10
11
12
13
14
15
16
17
18
19
import {
  jQuery as $,
  TRANSITION_END,
  emulateTransitionEnd,
  getSelectorFromElement,
  getTransitionDurationFromElement,
  isVisible,
  makeArray,
  reflow,
  triggerTransitionEnd,
  typeCheckConfig
} from './util/index'
Johann-S's avatar
Johann-S committed
20
21
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
22
import Manipulator from './dom/manipulator'
Johann-S's avatar
Johann-S committed
23
import SelectorEngine from './dom/selectorEngine'
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

Johann-S's avatar
Johann-S committed
31
const NAME                   = 'carousel'
XhmikosR's avatar
XhmikosR committed
32
const VERSION                = '4.3.1'
Johann-S's avatar
Johann-S committed
33
34
35
36
37
38
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
const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
Johann-S's avatar
Johann-S committed
39
const SWIPE_THRESHOLD        = 40
Johann-S's avatar
Johann-S committed
40
41
42
43
44
45

const Default = {
  interval : 5000,
  keyboard : true,
  slide    : false,
  pause    : 'hover',
46
47
  wrap     : true,
  touch    : true
Johann-S's avatar
Johann-S committed
48
49
50
51
52
53
54
}

const DefaultType = {
  interval : '(number|boolean)',
  keyboard : 'boolean',
  slide    : '(boolean|string)',
  pause    : '(string|boolean)',
55
56
  wrap     : 'boolean',
  touch    : 'boolean'
Johann-S's avatar
Johann-S committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
}

const Direction = {
  NEXT     : 'next',
  PREV     : 'prev',
  LEFT     : 'left',
  RIGHT    : 'right'
}

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

const ClassName = {
Johann-S's avatar
Johann-S 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
95
96
97
}

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

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

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

    this._config            = this._getConfig(config)
    this._element           = element
Johann-S's avatar
Johann-S committed
128
    this._indicatorsElement = SelectorEngine.findOne(Selector.INDICATORS, this._element)
Johann-S's avatar
Johann-S 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

Johann-S's avatar
Johann-S 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
230
231
232
233
234
235
236
237
238

    this._items             = null
    this._config            = null
    this._element           = null
    this._interval          = null
    this._isPaused          = null
    this._isSliding         = null
    this._activeElement     = null
    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
260
261
262
263
264
265
266
267
268
269
270
  _handleSwipe() {
    const absDeltax = Math.abs(this.touchDeltaX)

    if (absDeltax <= SWIPE_THRESHOLD) {
      return
    }

    const direction = absDeltax / this.touchDeltaX

    // swipe left
    if (direction > 0) {
      this.prev()
    }

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

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

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

284
285
286
    if (this._config.touch) {
      this._addTouchEventListeners()
    }
Johann-S's avatar
Johann-S committed
287
288
289
290
291
292
293
  }

  _addTouchEventListeners() {
    if (!this._touchSupported) {
      return
    }

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

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

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

      this._handleSwipe()
      if (this._config.pause === 'hover') {
Johann-S's avatar
Johann-S committed
318
319
320
321
322
323
324
        // 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
325
326
327
328
329
330

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

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

Johann-S's avatar
Johann-S committed
338
    if (this._pointerEvent) {
Johann-S's avatar
Johann-S committed
339
340
      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
341
342
343

      this._element.classList.add(ClassName.POINTER_EVENT)
    } else {
Johann-S's avatar
Johann-S committed
344
345
346
      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
347
    }
Johann-S's avatar
Johann-S committed
348
  }
fat's avatar
fat committed
349

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

Johann-S's avatar
Johann-S committed
355
356
357
358
359
360
361
362
363
364
    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
365
    }
Johann-S's avatar
Johann-S committed
366
  }
fat's avatar
fat committed
367

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

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

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

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

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

Johann-S's avatar
Johann-S committed
391
392
393
    return itemIndex === -1
      ? this._items[this._items.length - 1] : this._items[itemIndex]
  }
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
427
428
    const activeElementIndex = this._getItemIndex(activeElement)
    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
532
533
534
535
536
  static _carouselInterface(element, config) {
    let data    = Data.getData(element, DATA_KEY)
    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
555
    if (typeof config === 'number') {
      data.to(config)
    } else if (typeof action === 'string') {
      if (typeof data[action] === 'undefined') {
        throw new Error(`No method named "${action}"`)
fat's avatar
fat committed
556
      }
557
558
559
560
561
562
563
564
565
566
      data[action]()
    } else if (_config.interval && _config.ride) {
      data.pause()
      data.cycle()
    }
  }

  static _jQueryInterface(config) {
    return this.each(function () {
      Carousel._carouselInterface(this, config)
Johann-S's avatar
Johann-S committed
567
568
    })
  }
fat's avatar
fat committed
569

Johann-S's avatar
Johann-S committed
570
  static _dataApiClickHandler(event) {
571
    const selector = getSelectorFromElement(this)
fat's avatar
fat committed
572

Johann-S's avatar
Johann-S committed
573
574
575
    if (!selector) {
      return
    }
fat's avatar
fat committed
576

Johann-S's avatar
Johann-S committed
577
    const target = SelectorEngine.findOne(selector)
Jacob Thornton's avatar
Jacob Thornton committed
578

Johann-S's avatar
Johann-S committed
579
    if (!target || !target.classList.contains(ClassName.CAROUSEL)) {
Johann-S's avatar
Johann-S committed
580
581
      return
    }
fat's avatar
fat committed
582

Johann-S's avatar
Johann-S committed
583
    const config = {
584
585
      ...Manipulator.getDataAttributes(target),
      ...Manipulator.getDataAttributes(this)
Johann-S's avatar
Johann-S committed
586
587
    }
    const slideIndex = this.getAttribute('data-slide-to')
fat's avatar
fat committed
588

Johann-S's avatar
Johann-S committed
589
590
591
592
    if (slideIndex) {
      config.interval = false
    }

593
    Carousel._carouselInterface(target, config)
fat's avatar
fat committed
594

Johann-S's avatar
Johann-S committed
595
    if (slideIndex) {
Johann-S's avatar
Johann-S committed
596
      Data.getData(target, DATA_KEY).to(slideIndex)
fat's avatar
fat committed
597
    }
Johann-S's avatar
Johann-S committed
598
599

    event.preventDefault()
fat's avatar
fat committed
600
  }
601
602
603
604

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
605
}
fat's avatar
fat committed
606

Johann-S's avatar
Johann-S committed
607
608
609
610
611
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
612

Johann-S's avatar
Johann-S committed
613
614
EventHandler
  .on(document, Event.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel._dataApiClickHandler)
fat's avatar
fat committed
615

Johann-S's avatar
Johann-S committed
616
EventHandler.on(window, Event.LOAD_DATA_API, () => {
617
  const carousels = makeArray(SelectorEngine.find(Selector.DATA_RIDE))
Johann-S's avatar
Johann-S committed
618
  for (let i = 0, len = carousels.length; i < len; i++) {
619
    Carousel._carouselInterface(carousels[i], Data.getData(carousels[i], DATA_KEY))
fat's avatar
fat committed
620
  }
Johann-S's avatar
Johann-S committed
621
622
623
624
625
626
})

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
627
 * add .carousel to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
628
 */
fat's avatar
fat committed
629

630
if (typeof $ !== 'undefined') {
Johann-S's avatar
Johann-S committed
631
632
633
634
635
636
637
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Carousel._jQueryInterface
  $.fn[NAME].Constructor   = Carousel
  $.fn[NAME].noConflict    = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Carousel._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
638
}
fat's avatar
fat committed
639
640

export default Carousel