0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/js/dist/carousel.js

722 lines
23 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap carousel.js v5.0.2 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 07:41:12 +01:00
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/selector-engine.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['./dom/selector-engine', './dom/event-handler', './dom/manipulator', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Carousel = factory(global.SelectorEngine, global.EventHandler, global.Manipulator, global.Base));
}(this, (function (SelectorEngine, EventHandler, Manipulator, BaseComponent) { 'use strict';
2018-07-24 02:51:14 +02:00
2020-09-14 17:12:06 +02:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
2020-09-14 17:12:06 +02:00
var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2021-03-23 17:26:54 +01:00
const TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 17:31:34 +01:00
2021-03-23 17:26:54 +01:00
const toType = obj => {
2020-03-28 11:29:08 +01:00
if (obj === null || obj === undefined) {
2021-03-23 17:26:54 +01:00
return `${obj}`;
2020-03-28 11:29:08 +01:00
}
2019-03-01 17:31:34 +01:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
};
2021-03-23 17:26:54 +01:00
const getSelector = element => {
let selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
2021-03-23 17:26:54 +01:00
let hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
return null;
} // Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
hrefAttr = `#${hrefAttr.split('#')[1]}`;
}
2019-08-27 15:03:21 +02:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 17:31:34 +01:00
}
2019-08-27 15:03:21 +02:00
return selector;
};
2021-03-23 17:26:54 +01:00
const getElementFromSelector = element => {
const selector = getSelector(element);
2019-08-27 15:03:21 +02:00
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01:00
};
2021-03-23 17:26:54 +01:00
const triggerTransitionEnd = element => {
2020-03-28 11:29:08 +01:00
element.dispatchEvent(new Event(TRANSITION_END));
2019-03-01 17:31:34 +01:00
};
const isElement = obj => {
if (!obj || typeof obj !== 'object') {
return false;
}
2019-03-01 17:31:34 +01:00
if (typeof obj.jquery !== 'undefined') {
obj = obj[0];
2019-03-01 17:31:34 +01:00
}
return typeof obj.nodeType !== 'undefined';
2019-03-01 17:31:34 +01:00
};
2021-03-23 17:26:54 +01:00
const typeCheckConfig = (componentName, config, configTypes) => {
Object.keys(configTypes).forEach(property => {
const expectedTypes = configTypes[property];
const value = config[property];
const valueType = value && isElement(value) ? 'element' : toType(value);
2019-03-01 17:31:34 +01:00
if (!new RegExp(expectedTypes).test(valueType)) {
throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`);
2019-03-01 17:31:34 +01:00
}
});
};
2021-03-23 17:26:54 +01:00
const isVisible = element => {
if (!isElement(element) || element.getClientRects().length === 0) {
2019-03-01 17:31:34 +01:00
return false;
}
return getComputedStyle(element).getPropertyValue('visibility') === 'visible';
2019-03-01 17:31:34 +01:00
};
2021-03-23 17:26:54 +01:00
const reflow = element => element.offsetHeight;
2019-03-01 17:31:34 +01:00
2021-03-23 17:26:54 +01:00
const getjQuery = () => {
const {
jQuery
} = window;
2019-08-27 15:03:21 +02:00
2020-11-23 14:17:16 +01:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 15:03:21 +02:00
return jQuery;
}
return null;
};
const DOMContentLoadedCallbacks = [];
2021-03-23 17:26:54 +01:00
const onDOMContentLoaded = callback => {
2020-11-11 18:07:37 +01:00
if (document.readyState === 'loading') {
// add listener on the first call when the document is in loading state
if (!DOMContentLoadedCallbacks.length) {
document.addEventListener('DOMContentLoaded', () => {
DOMContentLoadedCallbacks.forEach(callback => callback());
});
}
DOMContentLoadedCallbacks.push(callback);
2020-11-11 18:07:37 +01:00
} else {
callback();
}
};
2021-03-23 17:26:54 +01:00
const isRTL = () => document.documentElement.dir === 'rtl';
2020-12-03 14:08:31 +01:00
const defineJQueryPlugin = plugin => {
2021-03-23 17:26:54 +01:00
onDOMContentLoaded(() => {
const $ = getjQuery();
/* istanbul ignore if */
2020-12-03 15:18:59 +01:00
if ($) {
const name = plugin.NAME;
2021-03-23 17:26:54 +01:00
const JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 15:18:59 +01:00
}
});
};
/**
* Return the previous/next element of a list.
*
* @param {array} list The list of elements
* @param activeElement The active element
* @param shouldGetNext Choose to get next or previous element
* @param isCycleAllowed
* @return {Element|elem} The proper element
*/
const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed) => {
let index = list.indexOf(activeElement); // if the element does not exist in the list return an element depending on the direction and if cycle is allowed
if (index === -1) {
return list[!shouldGetNext && isCycleAllowed ? list.length - 1 : 0];
}
const listLength = list.length;
index += shouldGetNext ? 1 : -1;
if (isCycleAllowed) {
index = (index + listLength) % listLength;
}
return list[Math.max(0, Math.min(index, listLength - 1))];
};
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.2): carousel.js
2021-03-23 17:26:54 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2020-12-03 15:18:59 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2021-03-23 17:26:54 +01:00
const NAME = 'carousel';
const DATA_KEY = 'bs.carousel';
const EVENT_KEY = `.${DATA_KEY}`;
const DATA_API_KEY = '.data-api';
const ARROW_LEFT_KEY = 'ArrowLeft';
const ARROW_RIGHT_KEY = 'ArrowRight';
const TOUCHEVENT_COMPAT_WAIT = 500; // Time for mouse compat events to fire after touch
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
const SWIPE_THRESHOLD = 40;
const Default = {
2018-11-13 07:41:12 +01:00
interval: 5000,
keyboard: true,
slide: false,
pause: 'hover',
wrap: true,
touch: true
};
2021-03-23 17:26:54 +01:00
const DefaultType = {
2018-11-13 07:41:12 +01:00
interval: '(number|boolean)',
keyboard: 'boolean',
slide: '(boolean|string)',
pause: '(string|boolean)',
wrap: 'boolean',
touch: 'boolean'
};
2021-03-23 17:26:54 +01:00
const ORDER_NEXT = 'next';
const ORDER_PREV = 'prev';
const DIRECTION_LEFT = 'left';
const DIRECTION_RIGHT = 'right';
const KEY_TO_DIRECTION = {
[ARROW_LEFT_KEY]: DIRECTION_RIGHT,
[ARROW_RIGHT_KEY]: DIRECTION_LEFT
};
2021-03-23 17:26:54 +01:00
const EVENT_SLIDE = `slide${EVENT_KEY}`;
const EVENT_SLID = `slid${EVENT_KEY}`;
const EVENT_KEYDOWN = `keydown${EVENT_KEY}`;
const EVENT_MOUSEENTER = `mouseenter${EVENT_KEY}`;
const EVENT_MOUSELEAVE = `mouseleave${EVENT_KEY}`;
const EVENT_TOUCHSTART = `touchstart${EVENT_KEY}`;
const EVENT_TOUCHMOVE = `touchmove${EVENT_KEY}`;
const EVENT_TOUCHEND = `touchend${EVENT_KEY}`;
const EVENT_POINTERDOWN = `pointerdown${EVENT_KEY}`;
const EVENT_POINTERUP = `pointerup${EVENT_KEY}`;
const EVENT_DRAG_START = `dragstart${EVENT_KEY}`;
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`;
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
const CLASS_NAME_CAROUSEL = 'carousel';
const CLASS_NAME_ACTIVE = 'active';
const CLASS_NAME_SLIDE = 'slide';
const CLASS_NAME_END = 'carousel-item-end';
const CLASS_NAME_START = 'carousel-item-start';
const CLASS_NAME_NEXT = 'carousel-item-next';
const CLASS_NAME_PREV = 'carousel-item-prev';
const CLASS_NAME_POINTER_EVENT = 'pointer-event';
const SELECTOR_ACTIVE = '.active';
const SELECTOR_ACTIVE_ITEM = '.active.carousel-item';
const SELECTOR_ITEM = '.carousel-item';
const SELECTOR_ITEM_IMG = '.carousel-item img';
const SELECTOR_NEXT_PREV = '.carousel-item-next, .carousel-item-prev';
const SELECTOR_INDICATORS = '.carousel-indicators';
const SELECTOR_INDICATOR = '[data-bs-target]';
const SELECTOR_DATA_SLIDE = '[data-bs-slide], [data-bs-slide-to]';
const SELECTOR_DATA_RIDE = '[data-bs-ride="carousel"]';
const POINTER_TYPE_TOUCH = 'touch';
const POINTER_TYPE_PEN = 'pen';
2019-10-08 08:39:10 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
class Carousel extends BaseComponent__default['default'] {
constructor(element, config) {
super(element);
this._items = null;
this._interval = null;
this._activeElement = null;
this._isPaused = false;
this._isSliding = false;
this.touchTimeout = null;
this.touchStartX = 0;
this.touchDeltaX = 0;
this._config = this._getConfig(config);
this._indicatorsElement = SelectorEngine__default['default'].findOne(SELECTOR_INDICATORS, this._element);
this._touchSupported = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0;
this._pointerEvent = Boolean(window.PointerEvent);
this._addEventListeners();
2018-11-13 07:41:12 +01:00
} // Getters
2021-03-23 17:26:54 +01:00
static get Default() {
return Default;
}
static get NAME() {
return NAME;
2021-03-23 17:26:54 +01:00
} // Public
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
next() {
this._slide(ORDER_NEXT);
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
nextWhenVisible() {
2018-11-13 07:41:12 +01:00
// Don't call next when the page isn't visible
// or the carousel or its parent isn't visible
2019-03-01 17:31:34 +01:00
if (!document.hidden && isVisible(this._element)) {
2018-11-13 07:41:12 +01:00
this.next();
}
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
prev() {
this._slide(ORDER_PREV);
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
pause(event) {
2018-11-13 07:41:12 +01:00
if (!event) {
this._isPaused = true;
}
2020-09-14 17:12:06 +02:00
if (SelectorEngine__default['default'].findOne(SELECTOR_NEXT_PREV, this._element)) {
2019-03-01 17:31:34 +01:00
triggerTransitionEnd(this._element);
2018-11-13 07:41:12 +01:00
this.cycle(true);
}
clearInterval(this._interval);
this._interval = null;
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
cycle(event) {
2018-11-13 07:41:12 +01:00
if (!event) {
2018-07-24 02:51:14 +02:00
this._isPaused = false;
2018-11-13 07:41:12 +01:00
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (this._interval) {
clearInterval(this._interval);
this._interval = null;
}
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
if (this._config && this._config.interval && !this._isPaused) {
2020-11-11 18:07:37 +01:00
this._updateInterval();
2018-11-13 07:41:12 +01:00
this._interval = setInterval((document.visibilityState ? this.nextWhenVisible : this.next).bind(this), this._config.interval);
}
2021-03-23 17:26:54 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
to(index) {
2020-09-14 17:12:06 +02:00
this._activeElement = SelectorEngine__default['default'].findOne(SELECTOR_ACTIVE_ITEM, this._element);
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
const activeIndex = this._getItemIndex(this._activeElement);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (index > this._items.length - 1 || index < 0) {
return;
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (this._isSliding) {
2021-03-23 17:26:54 +01:00
EventHandler__default['default'].one(this._element, EVENT_SLID, () => this.to(index));
2018-11-13 07:41:12 +01:00
return;
}
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
if (activeIndex === index) {
this.pause();
this.cycle();
return;
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const order = index > activeIndex ? ORDER_NEXT : ORDER_PREV;
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
this._slide(order, this._items[index]);
2019-01-04 17:29:45 +01:00
} // Private
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
_getConfig(config) {
config = { ...Default,
...Manipulator__default['default'].getDataAttributes(this._element),
...(typeof config === 'object' ? config : {})
2021-03-23 17:26:54 +01:00
};
2019-03-01 17:31:34 +01:00
typeCheckConfig(NAME, config, DefaultType);
2018-11-13 07:41:12 +01:00
return config;
2021-03-23 17:26:54 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
_handleSwipe() {
const absDeltax = Math.abs(this.touchDeltaX);
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (absDeltax <= SWIPE_THRESHOLD) {
return;
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const direction = absDeltax / this.touchDeltaX;
this.touchDeltaX = 0;
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
if (!direction) {
return;
2018-11-13 07:41:12 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
this._slide(direction > 0 ? DIRECTION_RIGHT : DIRECTION_LEFT);
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_addEventListeners() {
2018-11-13 07:41:12 +01:00
if (this._config.keyboard) {
2021-03-23 17:26:54 +01:00
EventHandler__default['default'].on(this._element, EVENT_KEYDOWN, event => this._keydown(event));
2018-11-13 07:41:12 +01:00
}
2016-11-01 05:14:23 +01:00
2018-11-13 07:41:12 +01:00
if (this._config.pause === 'hover') {
2021-03-23 17:26:54 +01:00
EventHandler__default['default'].on(this._element, EVENT_MOUSEENTER, event => this.pause(event));
EventHandler__default['default'].on(this._element, EVENT_MOUSELEAVE, event => this.cycle(event));
2018-11-13 07:41:12 +01:00
}
2015-05-08 07:26:40 +02:00
if (this._config.touch && this._touchSupported) {
this._addTouchEventListeners();
}
2021-03-23 17:26:54 +01:00
}
2018-07-24 02:51:14 +02:00
2021-03-23 17:26:54 +01:00
_addTouchEventListeners() {
const start = event => {
if (this._pointerEvent && (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH)) {
this.touchStartX = event.clientX;
} else if (!this._pointerEvent) {
this.touchStartX = event.touches[0].clientX;
2017-09-06 06:05:12 +02:00
}
2018-11-13 07:41:12 +01:00
};
2017-09-06 06:05:12 +02:00
2021-03-23 17:26:54 +01:00
const move = event => {
2018-11-13 07:41:12 +01:00
// ensure swiping with one touch and not pinching
2021-03-23 17:26:54 +01:00
this.touchDeltaX = event.touches && event.touches.length > 1 ? 0 : event.touches[0].clientX - this.touchStartX;
2018-07-24 02:51:14 +02:00
};
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const end = event => {
if (this._pointerEvent && (event.pointerType === POINTER_TYPE_PEN || event.pointerType === POINTER_TYPE_TOUCH)) {
this.touchDeltaX = event.clientX - this.touchStartX;
2018-07-24 02:51:14 +02:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
this._handleSwipe();
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
if (this._config.pause === 'hover') {
2018-11-13 07:41:12 +01:00
// 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
2021-03-23 17:26:54 +01:00
this.pause();
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
if (this.touchTimeout) {
clearTimeout(this.touchTimeout);
2018-11-13 07:41:12 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
this.touchTimeout = setTimeout(event => this.cycle(event), TOUCHEVENT_COMPAT_WAIT + this._config.interval);
2018-07-24 02:51:14 +02:00
}
};
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
SelectorEngine__default['default'].find(SELECTOR_ITEM_IMG, this._element).forEach(itemImg => {
EventHandler__default['default'].on(itemImg, EVENT_DRAG_START, e => e.preventDefault());
2018-11-13 07:41:12 +01:00
});
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
if (this._pointerEvent) {
2021-03-23 17:26:54 +01:00
EventHandler__default['default'].on(this._element, EVENT_POINTERDOWN, event => start(event));
EventHandler__default['default'].on(this._element, EVENT_POINTERUP, event => end(event));
2017-09-30 23:28:03 +02:00
2020-03-28 11:29:08 +01:00
this._element.classList.add(CLASS_NAME_POINTER_EVENT);
2018-11-13 07:41:12 +01:00
} else {
2021-03-23 17:26:54 +01:00
EventHandler__default['default'].on(this._element, EVENT_TOUCHSTART, event => start(event));
EventHandler__default['default'].on(this._element, EVENT_TOUCHMOVE, event => move(event));
EventHandler__default['default'].on(this._element, EVENT_TOUCHEND, event => end(event));
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_keydown(event) {
2018-11-13 07:41:12 +01:00
if (/input|textarea/i.test(event.target.tagName)) {
return;
}
2015-05-08 07:26:40 +02:00
const direction = KEY_TO_DIRECTION[event.key];
if (direction) {
event.preventDefault();
2015-05-08 07:26:40 +02:00
this._slide(direction);
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_getItemIndex(element) {
2020-09-14 17:12:06 +02:00
this._items = element && element.parentNode ? SelectorEngine__default['default'].find(SELECTOR_ITEM, element.parentNode) : [];
2018-11-13 07:41:12 +01:00
return this._items.indexOf(element);
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_getItemByOrder(order, activeElement) {
const isNext = order === ORDER_NEXT;
return getNextActiveElement(this._items, activeElement, isNext, this._config.wrap);
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_triggerSlideEvent(relatedTarget, eventDirectionName) {
const targetIndex = this._getItemIndex(relatedTarget);
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const fromIndex = this._getItemIndex(SelectorEngine__default['default'].findOne(SELECTOR_ACTIVE_ITEM, this._element));
2015-05-08 07:26:40 +02:00
2020-09-14 17:12:06 +02:00
return EventHandler__default['default'].trigger(this._element, EVENT_SLIDE, {
2021-03-23 17:26:54 +01:00
relatedTarget,
2018-11-13 07:41:12 +01:00
direction: eventDirectionName,
from: fromIndex,
to: targetIndex
});
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_setActiveIndicatorElement(element) {
2018-11-13 07:41:12 +01:00
if (this._indicatorsElement) {
2021-03-23 17:26:54 +01:00
const activeIndicator = SelectorEngine__default['default'].findOne(SELECTOR_ACTIVE, this._indicatorsElement);
activeIndicator.classList.remove(CLASS_NAME_ACTIVE);
activeIndicator.removeAttribute('aria-current');
2021-03-23 17:26:54 +01:00
const indicators = SelectorEngine__default['default'].find(SELECTOR_INDICATOR, this._indicatorsElement);
2019-03-01 17:31:34 +01:00
2021-03-23 17:26:54 +01:00
for (let i = 0; i < indicators.length; i++) {
if (Number.parseInt(indicators[i].getAttribute('data-bs-slide-to'), 10) === this._getItemIndex(element)) {
indicators[i].classList.add(CLASS_NAME_ACTIVE);
indicators[i].setAttribute('aria-current', 'true');
break;
}
2018-07-24 02:51:14 +02:00
}
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
_updateInterval() {
const element = this._activeElement || SelectorEngine__default['default'].findOne(SELECTOR_ACTIVE_ITEM, this._element);
2020-11-11 18:07:37 +01:00
if (!element) {
return;
}
2021-03-23 17:26:54 +01:00
const elementInterval = Number.parseInt(element.getAttribute('data-bs-interval'), 10);
2020-11-11 18:07:37 +01:00
if (elementInterval) {
this._config.defaultInterval = this._config.defaultInterval || this._config.interval;
this._config.interval = elementInterval;
} else {
this._config.interval = this._config.defaultInterval || this._config.interval;
}
2021-03-23 17:26:54 +01:00
}
2020-11-11 18:07:37 +01:00
2021-03-23 17:26:54 +01:00
_slide(directionOrOrder, element) {
const order = this._directionToOrder(directionOrOrder);
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const activeElement = SelectorEngine__default['default'].findOne(SELECTOR_ACTIVE_ITEM, this._element);
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const activeElementIndex = this._getItemIndex(activeElement);
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const nextElement = element || this._getItemByOrder(order, activeElement);
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const nextElementIndex = this._getItemIndex(nextElement);
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const isCycling = Boolean(this._interval);
const isNext = order === ORDER_NEXT;
const directionalClassName = isNext ? CLASS_NAME_START : CLASS_NAME_END;
const orderClassName = isNext ? CLASS_NAME_NEXT : CLASS_NAME_PREV;
const eventDirectionName = this._orderToDirection(order);
2017-09-30 23:28:03 +02:00
2020-03-28 11:29:08 +01:00
if (nextElement && nextElement.classList.contains(CLASS_NAME_ACTIVE)) {
2018-11-13 07:41:12 +01:00
this._isSliding = false;
return;
}
2017-09-30 23:28:03 +02:00
if (this._isSliding) {
return;
}
2021-03-23 17:26:54 +01:00
const slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName);
2018-09-19 06:35:40 +02:00
2019-03-01 17:31:34 +01:00
if (slideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (!activeElement || !nextElement) {
// Some weirdness is happening, so we bail
return;
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
this._isSliding = true;
2015-08-19 05:28:28 +02:00
2018-11-13 07:41:12 +01:00
if (isCycling) {
this.pause();
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
this._setActiveIndicatorElement(nextElement);
2017-09-30 23:28:03 +02:00
2020-11-11 18:07:37 +01:00
this._activeElement = nextElement;
const triggerSlidEvent = () => {
EventHandler__default['default'].trigger(this._element, EVENT_SLID, {
relatedTarget: nextElement,
direction: eventDirectionName,
from: activeElementIndex,
to: nextElementIndex
});
};
2020-03-28 11:29:08 +01:00
if (this._element.classList.contains(CLASS_NAME_SLIDE)) {
2019-03-01 17:31:34 +01:00
nextElement.classList.add(orderClassName);
reflow(nextElement);
activeElement.classList.add(directionalClassName);
nextElement.classList.add(directionalClassName);
const completeCallBack = () => {
2020-05-13 20:53:43 +02:00
nextElement.classList.remove(directionalClassName, orderClassName);
2020-03-28 11:29:08 +01:00
nextElement.classList.add(CLASS_NAME_ACTIVE);
2020-05-13 20:53:43 +02:00
activeElement.classList.remove(CLASS_NAME_ACTIVE, orderClassName, directionalClassName);
2021-03-23 17:26:54 +01:00
this._isSliding = false;
setTimeout(triggerSlidEvent, 0);
};
this._queueCallback(completeCallBack, activeElement, true);
2018-11-13 07:41:12 +01:00
} else {
2020-03-28 11:29:08 +01:00
activeElement.classList.remove(CLASS_NAME_ACTIVE);
nextElement.classList.add(CLASS_NAME_ACTIVE);
2018-11-13 07:41:12 +01:00
this._isSliding = false;
triggerSlidEvent();
2018-11-13 07:41:12 +01:00
}
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
if (isCycling) {
this.cycle();
}
2021-03-23 17:26:54 +01:00
}
_directionToOrder(direction) {
if (![DIRECTION_RIGHT, DIRECTION_LEFT].includes(direction)) {
return direction;
}
if (isRTL()) {
return direction === DIRECTION_LEFT ? ORDER_PREV : ORDER_NEXT;
2021-03-23 17:26:54 +01:00
}
return direction === DIRECTION_LEFT ? ORDER_NEXT : ORDER_PREV;
2021-03-23 17:26:54 +01:00
}
_orderToDirection(order) {
if (![ORDER_NEXT, ORDER_PREV].includes(order)) {
return order;
}
if (isRTL()) {
return order === ORDER_PREV ? DIRECTION_LEFT : DIRECTION_RIGHT;
2021-03-23 17:26:54 +01:00
}
return order === ORDER_PREV ? DIRECTION_RIGHT : DIRECTION_LEFT;
2019-01-04 17:29:45 +01:00
} // Static
2016-10-10 02:26:51 +02:00
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
static carouselInterface(element, config) {
const data = Carousel.getOrCreateInstance(element, config);
let {
_config
} = data;
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
if (typeof config === 'object') {
2021-03-23 17:26:54 +01:00
_config = { ..._config,
...config
};
2019-03-01 17:31:34 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
const action = typeof config === 'string' ? config : _config.slide;
2017-12-23 01:21:54 +01:00
2019-03-01 17:31:34 +01:00
if (typeof config === 'number') {
data.to(config);
} else if (typeof action === 'string') {
if (typeof data[action] === 'undefined') {
2021-03-23 17:26:54 +01:00
throw new TypeError(`No method named "${action}"`);
2018-07-24 02:51:14 +02:00
}
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
data[action]();
} else if (_config.interval && _config.ride) {
data.pause();
data.cycle();
}
2021-03-23 17:26:54 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
static jQueryInterface(config) {
2019-03-01 17:31:34 +01:00
return this.each(function () {
2019-08-27 15:03:21 +02:00
Carousel.carouselInterface(this, config);
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
static dataApiClickHandler(event) {
const target = getElementFromSelector(this);
2015-05-08 07:26:40 +02:00
2020-03-28 11:29:08 +01:00
if (!target || !target.classList.contains(CLASS_NAME_CAROUSEL)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-08 07:26:40 +02:00
2021-03-23 17:26:54 +01:00
const config = { ...Manipulator__default['default'].getDataAttributes(target),
...Manipulator__default['default'].getDataAttributes(this)
};
const slideIndex = this.getAttribute('data-bs-slide-to');
2018-06-22 07:55:23 +02:00
2018-11-13 07:41:12 +01:00
if (slideIndex) {
config.interval = false;
2018-07-24 02:51:14 +02:00
}
2017-09-30 23:28:03 +02:00
2019-08-27 15:03:21 +02:00
Carousel.carouselInterface(target, config);
2018-11-13 07:41:12 +01:00
if (slideIndex) {
Carousel.getInstance(target).to(slideIndex);
2018-11-13 07:41:12 +01:00
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
event.preventDefault();
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_SLIDE, Carousel.dataApiClickHandler);
2021-03-23 17:26:54 +01:00
EventHandler__default['default'].on(window, EVENT_LOAD_DATA_API, () => {
const carousels = SelectorEngine__default['default'].find(SELECTOR_DATA_RIDE);
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
for (let i = 0, len = carousels.length; i < len; i++) {
Carousel.carouselInterface(carousels[i], Carousel.getInstance(carousels[i]));
2018-11-13 07:41:12 +01:00
}
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 18:07:37 +01:00
* add .Carousel to jQuery only if jQuery is present
2018-11-13 07:41:12 +01:00
*/
defineJQueryPlugin(Carousel);
2015-05-08 07:26:40 +02:00
return Carousel;
2018-07-24 02:51:14 +02:00
2019-11-08 09:11:23 +01:00
})));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=carousel.js.map