source: trip-planner-front/node_modules/bootstrap/js/src/carousel.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 16.0 KB
Line 
1/**
2 * --------------------------------------------------------------------------
3 * Bootstrap (v5.1.3): carousel.js
4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5 * --------------------------------------------------------------------------
6 */
7
8import {
9 defineJQueryPlugin,
10 getElementFromSelector,
11 isRTL,
12 isVisible,
13 getNextActiveElement,
14 reflow,
15 triggerTransitionEnd,
16 typeCheckConfig
17} from './util/index'
18import EventHandler from './dom/event-handler'
19import Manipulator from './dom/manipulator'
20import SelectorEngine from './dom/selector-engine'
21import BaseComponent from './base-component'
22
23/**
24 * ------------------------------------------------------------------------
25 * Constants
26 * ------------------------------------------------------------------------
27 */
28
29const NAME = 'carousel'
30const DATA_KEY = 'bs.carousel'
31const EVENT_KEY = `.${DATA_KEY}`
32const DATA_API_KEY = '.data-api'
33
34const ARROW_LEFT_KEY = 'ArrowLeft'
35const ARROW_RIGHT_KEY = 'ArrowRight'
36const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
37const SWIPE_THRESHOLD = 40
38
39const Default = {
40 interval: 5000,
41 keyboard: true,
42 slide: false,
43 pause: 'hover',
44 wrap: true,
45 touch: true
46}
47
48const DefaultType = {
49 interval: '(number|boolean)',
50 keyboard: 'boolean',
51 slide: '(boolean|string)',
52 pause: '(string|boolean)',
53 wrap: 'boolean',
54 touch: 'boolean'
55}
56
57const ORDER_NEXT = 'next'
58const ORDER_PREV = 'prev'
59const DIRECTION_LEFT = 'left'
60const DIRECTION_RIGHT = 'right'
61
62const KEY_TO_DIRECTION = {
63 [ARROW_LEFT_KEY]: DIRECTION_RIGHT,
64 [ARROW_RIGHT_KEY]: DIRECTION_LEFT
65}
66
67const EVENT_SLIDE = `slide${EVENT_KEY}`
68const EVENT_SLID = `slid${EVENT_KEY}`
69const EVENT_KEYDOWN = `keydown${EVENT_KEY}`
70const EVENT_MOUSEENTER = `mouseenter${EVENT_KEY}`
71const EVENT_MOUSELEAVE = `mouseleave${EVENT_KEY}`
72const EVENT_TOUCHSTART = `touchstart${EVENT_KEY}`
73const EVENT_TOUCHMOVE = `touchmove${EVENT_KEY}`
74const EVENT_TOUCHEND = `touchend${EVENT_KEY}`
75const EVENT_POINTERDOWN = `pointerdown${EVENT_KEY}`
76const EVENT_POINTERUP = `pointerup${EVENT_KEY}`
77const EVENT_DRAG_START = `dragstart${EVENT_KEY}`
78const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
79const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
80
81const CLASS_NAME_CAROUSEL = 'carousel'
82const CLASS_NAME_ACTIVE = 'active'
83const CLASS_NAME_SLIDE = 'slide'
84const CLASS_NAME_END = 'carousel-item-end'
85const CLASS_NAME_START = 'carousel-item-start'
86const CLASS_NAME_NEXT = 'carousel-item-next'
87const CLASS_NAME_PREV = 'carousel-item-prev'
88const CLASS_NAME_POINTER_EVENT = 'pointer-event'
89
90const SELECTOR_ACTIVE = '.active'
91const SELECTOR_ACTIVE_ITEM = '.active.carousel-item'
92const SELECTOR_ITEM = '.carousel-item'
93const SELECTOR_ITEM_IMG = '.carousel-item img'
94const SELECTOR_NEXT_PREV = '.carousel-item-next, .carousel-item-prev'
95const SELECTOR_INDICATORS = '.carousel-indicators'
96const SELECTOR_INDICATOR = '[data-bs-target]'
97const SELECTOR_DATA_SLIDE = '[data-bs-slide], [data-bs-slide-to]'
98const SELECTOR_DATA_RIDE = '[data-bs-ride="carousel"]'
99
100const POINTER_TYPE_TOUCH = 'touch'
101const POINTER_TYPE_PEN = 'pen'
102
103/**
104 * ------------------------------------------------------------------------
105 * Class Definition
106 * ------------------------------------------------------------------------
107 */
108class Carousel extends BaseComponent {
109 constructor(element, config) {
110 super(element)
111
112 this._items = null
113 this._interval = null
114 this._activeElement = null
115 this._isPaused = false
116 this._isSliding = false
117 this.touchTimeout = null
118 this.touchStartX = 0
119 this.touchDeltaX = 0
120
121 this._config = this._getConfig(config)
122 this._indicatorsElement = SelectorEngine.findOne(SELECTOR_INDICATORS, this._element)
123 this._touchSupported = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0
124 this._pointerEvent = Boolean(window.PointerEvent)
125
126 this._addEventListeners()
127 }
128
129 // Getters
130
131 static get Default() {
132 return Default
133 }
134
135 static get NAME() {
136 return NAME
137 }
138
139 // Public
140
141 next() {
142 this._slide(ORDER_NEXT)
143 }
144
145 nextWhenVisible() {
146 // Don't call next when the page isn't visible
147 // or the carousel or its parent isn't visible
148 if (!document.hidden && isVisible(this._element)) {
149 this.next()
150 }
151 }
152
153 prev() {
154 this._slide(ORDER_PREV)
155 }
156
157 pause(event) {
158 if (!event) {
159 this._isPaused = true
160 }
161
162 if (SelectorEngine.findOne(SELECTOR_NEXT_PREV, this._element)) {
163 triggerTransitionEnd(this._element)
164 this.cycle(true)
165 }
166
167 clearInterval(this._interval)
168 this._interval = null
169 }
170
171 cycle(event) {
172 if (!event) {
173 this._isPaused = false
174 }
175
176 if (this._interval) {
177 clearInterval(this._interval)
178 this._interval = null
179 }
180
181 if (this._config && this._config.interval && !this._isPaused) {
182 this._updateInterval()
183
184 this._interval = setInterval(
185 (document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
186 this._config.interval
187 )
188 }
189 }
190
191 to(index) {
192 this._activeElement = SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
193 const activeIndex = this._getItemIndex(this._activeElement)
194
195 if (index > this._items.length - 1 || index < 0) {
196 return
197 }
198
199 if (this._isSliding) {
200 EventHandler.one(this._element, EVENT_SLID, () => this.to(index))
201 return
202 }
203
204 if (activeIndex === index) {
205 this.pause()
206 this.cycle()
207 return
208 }
209
210 const order = index > activeIndex ?
211 ORDER_NEXT :
212 ORDER_PREV
213
214 this._slide(order, this._items[index])
215 }
216
217 // Private
218
219 _getConfig(config) {
220 config = {
221 ...Default,
222 ...Manipulator.getDataAttributes(this._element),
223 ...(typeof config === 'object' ? config : {})
224 }
225 typeCheckConfig(NAME, config, DefaultType)
226 return config
227 }
228
229 _handleSwipe() {
230 const absDeltax = Math.abs(this.touchDeltaX)
231
232 if (absDeltax <= SWIPE_THRESHOLD) {
233 return
234 }
235
236 const direction = absDeltax / this.touchDeltaX
237
238 this.touchDeltaX = 0
239
240 if (!direction) {
241 return
242 }
243
244 this._slide(direction > 0 ? DIRECTION_RIGHT : DIRECTION_LEFT)
245 }
246
247 _addEventListeners() {
248 if (this._config.keyboard) {
249 EventHandler.on(this._element, EVENT_KEYDOWN, event => this._keydown(event))
250 }
251
252 if (this._config.pause === 'hover') {
253 EventHandler.on(this._element, EVENT_MOUSEENTER, event => this.pause(event))
254 EventHandler.on(this._element, EVENT_MOUSELEAVE, event => this.cycle(event))
255 }
256
257 if (this._config.touch && this._touchSupported) {
258 this._addTouchEventListeners()
259 }
260 }
261
262 _addTouchEventListeners() {
263 const hasPointerPenTouch = event => {
264 return this._pointerEvent &&
265 (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH)
266 }
267
268 const start = event => {
269 if (hasPointerPenTouch(event)) {
270 this.touchStartX = event.clientX
271 } else if (!this._pointerEvent) {
272 this.touchStartX = event.touches[0].clientX
273 }
274 }
275
276 const move = event => {
277 // ensure swiping with one touch and not pinching
278 this.touchDeltaX = event.touches && event.touches.length > 1 ?
279 0 :
280 event.touches[0].clientX - this.touchStartX
281 }
282
283 const end = event => {
284 if (hasPointerPenTouch(event)) {
285 this.touchDeltaX = event.clientX - this.touchStartX
286 }
287
288 this._handleSwipe()
289 if (this._config.pause === 'hover') {
290 // If it's a touch-enabled device, mouseenter/leave are fired as
291 // part of the mouse compatibility events on first tap - the carousel
292 // would stop cycling until user tapped out of it;
293 // here, we listen for touchend, explicitly pause the carousel
294 // (as if it's the second time we tap on it, mouseenter compat event
295 // is NOT fired) and after a timeout (to allow for mouse compatibility
296 // events to fire) we explicitly restart cycling
297
298 this.pause()
299 if (this.touchTimeout) {
300 clearTimeout(this.touchTimeout)
301 }
302
303 this.touchTimeout = setTimeout(event => this.cycle(event), TOUCHEVENT_COMPAT_WAIT + this._config.interval)
304 }
305 }
306
307 SelectorEngine.find(SELECTOR_ITEM_IMG, this._element).forEach(itemImg => {
308 EventHandler.on(itemImg, EVENT_DRAG_START, event => event.preventDefault())
309 })
310
311 if (this._pointerEvent) {
312 EventHandler.on(this._element, EVENT_POINTERDOWN, event => start(event))
313 EventHandler.on(this._element, EVENT_POINTERUP, event => end(event))
314
315 this._element.classList.add(CLASS_NAME_POINTER_EVENT)
316 } else {
317 EventHandler.on(this._element, EVENT_TOUCHSTART, event => start(event))
318 EventHandler.on(this._element, EVENT_TOUCHMOVE, event => move(event))
319 EventHandler.on(this._element, EVENT_TOUCHEND, event => end(event))
320 }
321 }
322
323 _keydown(event) {
324 if (/input|textarea/i.test(event.target.tagName)) {
325 return
326 }
327
328 const direction = KEY_TO_DIRECTION[event.key]
329 if (direction) {
330 event.preventDefault()
331 this._slide(direction)
332 }
333 }
334
335 _getItemIndex(element) {
336 this._items = element && element.parentNode ?
337 SelectorEngine.find(SELECTOR_ITEM, element.parentNode) :
338 []
339
340 return this._items.indexOf(element)
341 }
342
343 _getItemByOrder(order, activeElement) {
344 const isNext = order === ORDER_NEXT
345 return getNextActiveElement(this._items, activeElement, isNext, this._config.wrap)
346 }
347
348 _triggerSlideEvent(relatedTarget, eventDirectionName) {
349 const targetIndex = this._getItemIndex(relatedTarget)
350 const fromIndex = this._getItemIndex(SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element))
351
352 return EventHandler.trigger(this._element, EVENT_SLIDE, {
353 relatedTarget,
354 direction: eventDirectionName,
355 from: fromIndex,
356 to: targetIndex
357 })
358 }
359
360 _setActiveIndicatorElement(element) {
361 if (this._indicatorsElement) {
362 const activeIndicator = SelectorEngine.findOne(SELECTOR_ACTIVE, this._indicatorsElement)
363
364 activeIndicator.classList.remove(CLASS_NAME_ACTIVE)
365 activeIndicator.removeAttribute('aria-current')
366
367 const indicators = SelectorEngine.find(SELECTOR_INDICATOR, this._indicatorsElement)
368
369 for (let i = 0; i < indicators.length; i++) {
370 if (Number.parseInt(indicators[i].getAttribute('data-bs-slide-to'), 10) === this._getItemIndex(element)) {
371 indicators[i].classList.add(CLASS_NAME_ACTIVE)
372 indicators[i].setAttribute('aria-current', 'true')
373 break
374 }
375 }
376 }
377 }
378
379 _updateInterval() {
380 const element = this._activeElement || SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
381
382 if (!element) {
383 return
384 }
385
386 const elementInterval = Number.parseInt(element.getAttribute('data-bs-interval'), 10)
387
388 if (elementInterval) {
389 this._config.defaultInterval = this._config.defaultInterval || this._config.interval
390 this._config.interval = elementInterval
391 } else {
392 this._config.interval = this._config.defaultInterval || this._config.interval
393 }
394 }
395
396 _slide(directionOrOrder, element) {
397 const order = this._directionToOrder(directionOrOrder)
398 const activeElement = SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
399 const activeElementIndex = this._getItemIndex(activeElement)
400 const nextElement = element || this._getItemByOrder(order, activeElement)
401
402 const nextElementIndex = this._getItemIndex(nextElement)
403 const isCycling = Boolean(this._interval)
404
405 const isNext = order === ORDER_NEXT
406 const directionalClassName = isNext ? CLASS_NAME_START : CLASS_NAME_END
407 const orderClassName = isNext ? CLASS_NAME_NEXT : CLASS_NAME_PREV
408 const eventDirectionName = this._orderToDirection(order)
409
410 if (nextElement && nextElement.classList.contains(CLASS_NAME_ACTIVE)) {
411 this._isSliding = false
412 return
413 }
414
415 if (this._isSliding) {
416 return
417 }
418
419 const slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName)
420 if (slideEvent.defaultPrevented) {
421 return
422 }
423
424 if (!activeElement || !nextElement) {
425 // Some weirdness is happening, so we bail
426 return
427 }
428
429 this._isSliding = true
430
431 if (isCycling) {
432 this.pause()
433 }
434
435 this._setActiveIndicatorElement(nextElement)
436 this._activeElement = nextElement
437
438 const triggerSlidEvent = () => {
439 EventHandler.trigger(this._element, EVENT_SLID, {
440 relatedTarget: nextElement,
441 direction: eventDirectionName,
442 from: activeElementIndex,
443 to: nextElementIndex
444 })
445 }
446
447 if (this._element.classList.contains(CLASS_NAME_SLIDE)) {
448 nextElement.classList.add(orderClassName)
449
450 reflow(nextElement)
451
452 activeElement.classList.add(directionalClassName)
453 nextElement.classList.add(directionalClassName)
454
455 const completeCallBack = () => {
456 nextElement.classList.remove(directionalClassName, orderClassName)
457 nextElement.classList.add(CLASS_NAME_ACTIVE)
458
459 activeElement.classList.remove(CLASS_NAME_ACTIVE, orderClassName, directionalClassName)
460
461 this._isSliding = false
462
463 setTimeout(triggerSlidEvent, 0)
464 }
465
466 this._queueCallback(completeCallBack, activeElement, true)
467 } else {
468 activeElement.classList.remove(CLASS_NAME_ACTIVE)
469 nextElement.classList.add(CLASS_NAME_ACTIVE)
470
471 this._isSliding = false
472 triggerSlidEvent()
473 }
474
475 if (isCycling) {
476 this.cycle()
477 }
478 }
479
480 _directionToOrder(direction) {
481 if (![DIRECTION_RIGHT, DIRECTION_LEFT].includes(direction)) {
482 return direction
483 }
484
485 if (isRTL()) {
486 return direction === DIRECTION_LEFT ? ORDER_PREV : ORDER_NEXT
487 }
488
489 return direction === DIRECTION_LEFT ? ORDER_NEXT : ORDER_PREV
490 }
491
492 _orderToDirection(order) {
493 if (![ORDER_NEXT, ORDER_PREV].includes(order)) {
494 return order
495 }
496
497 if (isRTL()) {
498 return order === ORDER_PREV ? DIRECTION_LEFT : DIRECTION_RIGHT
499 }
500
501 return order === ORDER_PREV ? DIRECTION_RIGHT : DIRECTION_LEFT
502 }
503
504 // Static
505
506 static carouselInterface(element, config) {
507 const data = Carousel.getOrCreateInstance(element, config)
508
509 let { _config } = data
510 if (typeof config === 'object') {
511 _config = {
512 ..._config,
513 ...config
514 }
515 }
516
517 const action = typeof config === 'string' ? config : _config.slide
518
519 if (typeof config === 'number') {
520 data.to(config)
521 } else if (typeof action === 'string') {
522 if (typeof data[action] === 'undefined') {
523 throw new TypeError(`No method named "${action}"`)
524 }
525
526 data[action]()
527 } else if (_config.interval && _config.ride) {
528 data.pause()
529 data.cycle()
530 }
531 }
532
533 static jQueryInterface(config) {
534 return this.each(function () {
535 Carousel.carouselInterface(this, config)
536 })
537 }
538
539 static dataApiClickHandler(event) {
540 const target = getElementFromSelector(this)
541
542 if (!target || !target.classList.contains(CLASS_NAME_CAROUSEL)) {
543 return
544 }
545
546 const config = {
547 ...Manipulator.getDataAttributes(target),
548 ...Manipulator.getDataAttributes(this)
549 }
550 const slideIndex = this.getAttribute('data-bs-slide-to')
551
552 if (slideIndex) {
553 config.interval = false
554 }
555
556 Carousel.carouselInterface(target, config)
557
558 if (slideIndex) {
559 Carousel.getInstance(target).to(slideIndex)
560 }
561
562 event.preventDefault()
563 }
564}
565
566/**
567 * ------------------------------------------------------------------------
568 * Data Api implementation
569 * ------------------------------------------------------------------------
570 */
571
572EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_SLIDE, Carousel.dataApiClickHandler)
573
574EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
575 const carousels = SelectorEngine.find(SELECTOR_DATA_RIDE)
576
577 for (let i = 0, len = carousels.length; i < len; i++) {
578 Carousel.carouselInterface(carousels[i], Carousel.getInstance(carousels[i]))
579 }
580})
581
582/**
583 * ------------------------------------------------------------------------
584 * jQuery
585 * ------------------------------------------------------------------------
586 * add .Carousel to jQuery only if jQuery is present
587 */
588
589defineJQueryPlugin(Carousel)
590
591export default Carousel
Note: See TracBrowser for help on using the repository browser.