0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00
Bootstrap/js/dist/carousel.js

808 lines
25 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
2019-02-13 17:01:40 +01:00
* Bootstrap carousel.js v4.3.1 (https://getbootstrap.com/)
2019-01-04 17:29:45 +01:00
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2018-11-13 07:41:12 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
2019-03-01 17:31:34 +01:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/eventHandler.js'), require('./dom/manipulator.js'), require('./dom/selectorEngine.js')) :
typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/eventHandler.js', './dom/manipulator.js', './dom/selectorEngine.js'], factory) :
(global = global || self, global.Carousel = factory(global.Data, global.EventHandler, global.Manipulator, global.SelectorEngine));
}(this, function (Data, EventHandler, Manipulator, SelectorEngine) { 'use strict';
2018-07-24 02:51:14 +02:00
2019-03-01 17:31:34 +01:00
Data = Data && Data.hasOwnProperty('default') ? Data['default'] : Data;
EventHandler = EventHandler && EventHandler.hasOwnProperty('default') ? EventHandler['default'] : EventHandler;
Manipulator = Manipulator && Manipulator.hasOwnProperty('default') ? Manipulator['default'] : Manipulator;
SelectorEngine = SelectorEngine && SelectorEngine.hasOwnProperty('default') ? SelectorEngine['default'] : SelectorEngine;
2018-07-24 02:51:14 +02:00
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
2018-03-31 22:59:37 +02:00
2018-07-24 02:51:14 +02:00
return obj;
}
2017-12-23 01:21:54 +01:00
2018-07-24 02:51:14 +02:00
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
2017-09-06 06:05:12 +02:00
2018-07-24 02:51:14 +02:00
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
var MILLISECONDS_MULTIPLIER = 1000;
var TRANSITION_END = 'transitionend';
2019-03-11 16:13:30 +01:00
var _window = window,
jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 17:31:34 +01:00
var toType = function toType(obj) {
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
};
var getSelectorFromElement = function getSelectorFromElement(element) {
var selector = element.getAttribute('data-target');
if (!selector || selector === '#') {
var hrefAttr = element.getAttribute('href');
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
}
try {
return document.querySelector(selector) ? selector : null;
2019-03-11 16:13:30 +01:00
} catch (error) {
2019-03-01 17:31:34 +01:00
return null;
}
};
var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
if (!element) {
return 0;
} // Get transition-duration of the element
var _window$getComputedSt = window.getComputedStyle(element),
transitionDuration = _window$getComputedSt.transitionDuration,
transitionDelay = _window$getComputedSt.transitionDelay;
var floatTransitionDuration = parseFloat(transitionDuration);
var floatTransitionDelay = parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
if (!floatTransitionDuration && !floatTransitionDelay) {
return 0;
} // If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0];
transitionDelay = transitionDelay.split(',')[0];
return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
};
var triggerTransitionEnd = function triggerTransitionEnd(element) {
element.dispatchEvent(new Event(TRANSITION_END));
};
var isElement = function isElement(obj) {
return (obj[0] || obj).nodeType;
};
var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
var called = false;
var durationPadding = 5;
var emulatedDuration = duration + durationPadding;
function listener() {
called = true;
element.removeEventListener(TRANSITION_END, listener);
}
element.addEventListener(TRANSITION_END, listener);
setTimeout(function () {
if (!called) {
triggerTransitionEnd(element);
}
}, emulatedDuration);
};
var typeCheckConfig = function typeCheckConfig(componentName, config, configTypes) {
Object.keys(configTypes).forEach(function (property) {
var expectedTypes = configTypes[property];
var value = config[property];
var valueType = value && isElement(value) ? 'element' : toType(value);
if (!new RegExp(expectedTypes).test(valueType)) {
throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
}
});
};
var makeArray = function makeArray(nodeList) {
if (!nodeList) {
return [];
}
return [].slice.call(nodeList);
};
var isVisible = function isVisible(element) {
if (!element) {
return false;
}
if (element.style && element.parentNode && element.parentNode.style) {
return element.style.display !== 'none' && element.parentNode.style.display !== 'none' && element.style.visibility !== 'hidden';
}
return false;
};
var reflow = function reflow(element) {
return element.offsetHeight;
};
2015-05-08 07:26:40 +02:00
/**
2018-11-13 07:41:12 +01:00
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
2015-05-08 07:26:40 +02:00
*/
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
var NAME = 'carousel';
2019-02-13 17:01:40 +01:00
var VERSION = '4.3.1';
2018-11-13 07:41:12 +01:00
var DATA_KEY = 'bs.carousel';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
var ARROW_LEFT_KEYCODE = 37; // KeyboardEvent.which value for left arrow key
var ARROW_RIGHT_KEYCODE = 39; // KeyboardEvent.which value for right arrow key
var TOUCHEVENT_COMPAT_WAIT = 500; // Time for mouse compat events to fire after touch
var SWIPE_THRESHOLD = 40;
var Default = {
interval: 5000,
keyboard: true,
slide: false,
pause: 'hover',
wrap: true,
touch: true
};
var DefaultType = {
interval: '(number|boolean)',
keyboard: 'boolean',
slide: '(boolean|string)',
pause: '(string|boolean)',
wrap: 'boolean',
touch: 'boolean'
};
var Direction = {
NEXT: 'next',
PREV: 'prev',
LEFT: 'left',
RIGHT: 'right'
};
2019-03-01 17:31:34 +01:00
var Event$1 = {
2018-11-13 07:41:12 +01:00
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
};
var ClassName = {
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'
};
var Selector = {
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"]'
};
var PointerType = {
TOUCH: 'touch',
PEN: 'pen'
2017-09-13 07:24:15 +02:00
/**
* ------------------------------------------------------------------------
2018-11-13 07:41:12 +01:00
* Class Definition
2017-09-13 07:24:15 +02:00
* ------------------------------------------------------------------------
*/
2018-11-13 07:41:12 +01:00
};
var Carousel =
/*#__PURE__*/
function () {
function Carousel(element, config) {
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._element = element;
2019-03-01 17:31:34 +01:00
this._indicatorsElement = SelectorEngine.findOne(Selector.INDICATORS, this._element);
2018-11-13 07:41:12 +01:00
this._touchSupported = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0;
this._pointerEvent = Boolean(window.PointerEvent || window.MSPointerEvent);
this._addEventListeners();
2019-03-01 17:31:34 +01:00
Data.setData(element, DATA_KEY, this);
2018-11-13 07:41:12 +01:00
} // Getters
var _proto = Carousel.prototype;
// Public
_proto.next = function next() {
if (!this._isSliding) {
this._slide(Direction.NEXT);
}
2018-07-24 02:51:14 +02:00
};
2018-11-13 07:41:12 +01:00
_proto.nextWhenVisible = function nextWhenVisible() {
// 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();
}
2018-07-24 02:51:14 +02:00
};
2018-11-13 07:41:12 +01:00
_proto.prev = function prev() {
if (!this._isSliding) {
this._slide(Direction.PREV);
}
2018-07-24 02:51:14 +02:00
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto.pause = function pause(event) {
if (!event) {
this._isPaused = true;
}
2019-03-01 17:31:34 +01:00
if (SelectorEngine.findOne(Selector.NEXT_PREV, this._element)) {
triggerTransitionEnd(this._element);
2018-11-13 07:41:12 +01:00
this.cycle(true);
}
clearInterval(this._interval);
this._interval = null;
2018-07-24 02:51:14 +02:00
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto.cycle = function cycle(event) {
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) {
2018-11-13 07:41:12 +01:00
this._interval = setInterval((document.visibilityState ? this.nextWhenVisible : this.next).bind(this), this._config.interval);
}
};
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
_proto.to = function to(index) {
var _this = this;
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
this._activeElement = SelectorEngine.findOne(Selector.ACTIVE_ITEM, this._element);
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
var 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) {
2019-03-01 17:31:34 +01:00
EventHandler.one(this._element, Event$1.SLID, function () {
2018-11-13 07:41:12 +01:00
return _this.to(index);
});
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
2018-11-13 07:41:12 +01:00
var direction = index > activeIndex ? Direction.NEXT : Direction.PREV;
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
this._slide(direction, this._items[index]);
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto.dispose = function dispose() {
2019-03-01 17:31:34 +01:00
EventHandler.off(this._element, EVENT_KEY);
Data.removeData(this._element, DATA_KEY);
2018-11-13 07:41:12 +01:00
this._items = null;
this._config = null;
this._element = null;
this._interval = null;
this._isPaused = null;
this._isSliding = null;
this._activeElement = null;
this._indicatorsElement = null;
2019-01-04 17:29:45 +01:00
} // Private
;
2018-11-13 07:41:12 +01:00
_proto._getConfig = function _getConfig(config) {
config = _objectSpread({}, Default, config);
2019-03-01 17:31:34 +01:00
typeCheckConfig(NAME, config, DefaultType);
2018-11-13 07:41:12 +01:00
return config;
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto._handleSwipe = function _handleSwipe() {
var 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
2018-11-13 07:41:12 +01:00
var direction = absDeltax / this.touchDeltaX; // swipe left
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (direction > 0) {
this.prev();
} // swipe right
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (direction < 0) {
this.next();
}
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._addEventListeners = function _addEventListeners() {
var _this2 = this;
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (this._config.keyboard) {
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.KEYDOWN, function (event) {
2018-11-13 07:41:12 +01:00
return _this2._keydown(event);
});
}
2016-11-01 05:14:23 +01:00
2018-11-13 07:41:12 +01:00
if (this._config.pause === 'hover') {
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.MOUSEENTER, function (event) {
2018-11-13 07:41:12 +01:00
return _this2.pause(event);
2019-03-01 17:31:34 +01:00
});
EventHandler.on(this._element, Event$1.MOUSELEAVE, function (event) {
2018-11-13 07:41:12 +01:00
return _this2.cycle(event);
});
}
2015-05-08 07:26:40 +02:00
if (this._config.touch) {
this._addTouchEventListeners();
}
2018-11-13 07:41:12 +01:00
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto._addTouchEventListeners = function _addTouchEventListeners() {
var _this3 = this;
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
if (!this._touchSupported) {
return;
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var start = function start(event) {
2019-03-01 17:31:34 +01:00
if (_this3._pointerEvent && PointerType[event.pointerType.toUpperCase()]) {
_this3.touchStartX = event.clientX;
2018-11-13 07:41:12 +01:00
} else if (!_this3._pointerEvent) {
2019-03-01 17:31:34 +01:00
_this3.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
2018-11-13 07:41:12 +01:00
var move = function move(event) {
// ensure swiping with one touch and not pinching
2019-03-01 17:31:34 +01:00
if (event.touches && event.touches.length > 1) {
2018-11-13 07:41:12 +01:00
_this3.touchDeltaX = 0;
} else {
2019-03-01 17:31:34 +01:00
_this3.touchDeltaX = event.touches[0].clientX - _this3.touchStartX;
2018-07-24 02:51:14 +02:00
}
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var end = function end(event) {
2019-03-01 17:31:34 +01:00
if (_this3._pointerEvent && PointerType[event.pointerType.toUpperCase()]) {
_this3.touchDeltaX = event.clientX - _this3.touchStartX;
2018-07-24 02:51:14 +02:00
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_this3._handleSwipe();
if (_this3._config.pause === 'hover') {
// 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
_this3.pause();
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (_this3.touchTimeout) {
clearTimeout(_this3.touchTimeout);
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_this3.touchTimeout = setTimeout(function (event) {
return _this3.cycle(event);
}, TOUCHEVENT_COMPAT_WAIT + _this3._config.interval);
2018-07-24 02:51:14 +02:00
}
};
2016-10-10 02:26:51 +02:00
2019-03-01 17:31:34 +01:00
makeArray(SelectorEngine.find(Selector.ITEM_IMG, this._element)).forEach(function (itemImg) {
EventHandler.on(itemImg, Event$1.DRAG_START, function (e) {
return 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) {
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.POINTERDOWN, function (event) {
2018-11-13 07:41:12 +01:00
return start(event);
});
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.POINTERUP, function (event) {
2018-11-13 07:41:12 +01:00
return end(event);
});
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._element.classList.add(ClassName.POINTER_EVENT);
} else {
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.TOUCHSTART, function (event) {
2018-11-13 07:41:12 +01:00
return start(event);
});
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.TOUCHMOVE, function (event) {
2018-11-13 07:41:12 +01:00
return move(event);
});
2019-03-01 17:31:34 +01:00
EventHandler.on(this._element, Event$1.TOUCHEND, function (event) {
2018-11-13 07:41:12 +01:00
return end(event);
});
}
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._keydown = function _keydown(event) {
if (/input|textarea/i.test(event.target.tagName)) {
return;
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
switch (event.which) {
case ARROW_LEFT_KEYCODE:
event.preventDefault();
this.prev();
break;
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
case ARROW_RIGHT_KEYCODE:
event.preventDefault();
this.next();
break;
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
default:
}
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._getItemIndex = function _getItemIndex(element) {
2019-03-01 17:31:34 +01:00
this._items = element && element.parentNode ? makeArray(SelectorEngine.find(Selector.ITEM, element.parentNode)) : [];
2018-11-13 07:41:12 +01:00
return this._items.indexOf(element);
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._getItemByDirection = function _getItemByDirection(direction, activeElement) {
var isNextDirection = direction === Direction.NEXT;
var isPrevDirection = direction === Direction.PREV;
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
var activeIndex = this._getItemIndex(activeElement);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var lastItemIndex = this._items.length - 1;
var isGoingToWrap = isPrevDirection && activeIndex === 0 || isNextDirection && activeIndex === lastItemIndex;
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
if (isGoingToWrap && !this._config.wrap) {
return activeElement;
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var delta = direction === Direction.PREV ? -1 : 1;
var itemIndex = (activeIndex + delta) % this._items.length;
return itemIndex === -1 ? this._items[this._items.length - 1] : this._items[itemIndex];
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._triggerSlideEvent = function _triggerSlideEvent(relatedTarget, eventDirectionName) {
var targetIndex = this._getItemIndex(relatedTarget);
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
var fromIndex = this._getItemIndex(SelectorEngine.findOne(Selector.ACTIVE_ITEM, this._element));
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
return EventHandler.trigger(this._element, Event$1.SLIDE, {
2018-11-13 07:41:12 +01:00
relatedTarget: relatedTarget,
direction: eventDirectionName,
from: fromIndex,
to: targetIndex
});
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._setActiveIndicatorElement = function _setActiveIndicatorElement(element) {
if (this._indicatorsElement) {
2019-03-01 17:31:34 +01:00
var indicators = SelectorEngine.find(Selector.ACTIVE, this._indicatorsElement);
for (var i = 0; i < indicators.length; i++) {
indicators[i].classList.remove(ClassName.ACTIVE);
}
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var nextIndicator = this._indicatorsElement.children[this._getItemIndex(element)];
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (nextIndicator) {
2019-03-01 17:31:34 +01:00
nextIndicator.classList.add(ClassName.ACTIVE);
2018-07-24 02:51:14 +02:00
}
2018-11-13 07:41:12 +01:00
}
};
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
_proto._slide = function _slide(direction, element) {
var _this4 = this;
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
var activeElement = SelectorEngine.findOne(Selector.ACTIVE_ITEM, this._element);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var activeElementIndex = this._getItemIndex(activeElement);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var nextElement = element || activeElement && this._getItemByDirection(direction, activeElement);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var nextElementIndex = this._getItemIndex(nextElement);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var isCycling = Boolean(this._interval);
var directionalClassName;
var orderClassName;
var eventDirectionName;
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
if (direction === Direction.NEXT) {
directionalClassName = ClassName.LEFT;
orderClassName = ClassName.NEXT;
eventDirectionName = Direction.LEFT;
} else {
directionalClassName = ClassName.RIGHT;
orderClassName = ClassName.PREV;
eventDirectionName = Direction.RIGHT;
}
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
if (nextElement && nextElement.classList.contains(ClassName.ACTIVE)) {
2018-11-13 07:41:12 +01:00
this._isSliding = false;
return;
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
var 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
2019-03-01 17:31:34 +01:00
if (this._element.classList.contains(ClassName.SLIDE)) {
nextElement.classList.add(orderClassName);
reflow(nextElement);
activeElement.classList.add(directionalClassName);
nextElement.classList.add(directionalClassName);
2018-11-13 07:41:12 +01:00
var nextElementInterval = parseInt(nextElement.getAttribute('data-interval'), 10);
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
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;
}
2018-07-24 02:51:14 +02:00
2019-03-01 17:31:34 +01:00
var transitionDuration = getTransitionDurationFromElement(activeElement);
EventHandler.one(activeElement, TRANSITION_END, function () {
nextElement.classList.remove(directionalClassName);
nextElement.classList.remove(orderClassName);
nextElement.classList.add(ClassName.ACTIVE);
activeElement.classList.remove(ClassName.ACTIVE);
activeElement.classList.remove(orderClassName);
activeElement.classList.remove(directionalClassName);
2018-11-13 07:41:12 +01:00
_this4._isSliding = false;
setTimeout(function () {
2019-03-01 17:31:34 +01:00
EventHandler.trigger(_this4._element, Event$1.SLID, {
relatedTarget: nextElement,
direction: eventDirectionName,
from: activeElementIndex,
to: nextElementIndex
});
2018-11-13 07:41:12 +01:00
}, 0);
2019-03-01 17:31:34 +01:00
});
emulateTransitionEnd(activeElement, transitionDuration);
2018-11-13 07:41:12 +01:00
} else {
2019-03-01 17:31:34 +01:00
activeElement.classList.remove(ClassName.ACTIVE);
nextElement.classList.add(ClassName.ACTIVE);
2018-11-13 07:41:12 +01:00
this._isSliding = false;
2019-03-01 17:31:34 +01:00
EventHandler.trigger(this._element, Event$1.SLID, {
relatedTarget: nextElement,
direction: eventDirectionName,
from: activeElementIndex,
to: nextElementIndex
});
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();
}
2019-01-04 17:29:45 +01:00
} // Static
;
2016-10-10 02:26:51 +02:00
2019-03-01 17:31:34 +01:00
Carousel._carouselInterface = function _carouselInterface(element, config) {
var data = Data.getData(element, DATA_KEY);
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
var _config = _objectSpread({}, Default, Manipulator.getDataAttributes(element));
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
if (typeof config === 'object') {
_config = _objectSpread({}, _config, config);
}
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
var action = typeof config === 'string' ? config : _config.slide;
2017-12-23 01:21:54 +01:00
2019-03-01 17:31:34 +01:00
if (!data) {
data = new Carousel(element, _config);
}
if (typeof config === 'number') {
data.to(config);
} else if (typeof action === 'string') {
if (typeof data[action] === 'undefined') {
2019-03-11 16:13:30 +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();
}
};
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
Carousel._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
Carousel._carouselInterface(this, config);
2018-11-13 07:41:12 +01:00
});
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
Carousel._dataApiClickHandler = function _dataApiClickHandler(event) {
2019-03-01 17:31:34 +01:00
var selector = getSelectorFromElement(this);
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (!selector) {
return;
}
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
var target = SelectorEngine.findOne(selector);
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
if (!target || !target.classList.contains(ClassName.CAROUSEL)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-08 07:26:40 +02:00
2019-03-01 17:31:34 +01:00
var config = _objectSpread({}, Manipulator.getDataAttributes(target), Manipulator.getDataAttributes(this));
2015-05-08 07:26:40 +02:00
2018-11-13 07:41:12 +01:00
var slideIndex = this.getAttribute('data-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-03-01 17:31:34 +01:00
Carousel._carouselInterface(target, config);
2018-11-13 07:41:12 +01:00
if (slideIndex) {
2019-03-01 17:31:34 +01:00
Data.getData(target, DATA_KEY).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();
2018-07-24 02:51:14 +02:00
};
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
Carousel._getInstance = function _getInstance(element) {
return Data.getData(element, DATA_KEY);
};
2018-11-13 07:41:12 +01:00
_createClass(Carousel, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}, {
key: "Default",
get: function get() {
return Default;
}
}]);
2018-07-24 02:51:14 +02:00
return Carousel;
2018-11-13 07:41:12 +01:00
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2019-03-01 17:31:34 +01:00
EventHandler.on(document, Event$1.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel._dataApiClickHandler);
EventHandler.on(window, Event$1.LOAD_DATA_API, function () {
var carousels = makeArray(SelectorEngine.find(Selector.DATA_RIDE));
2018-11-13 07:41:12 +01:00
for (var i = 0, len = carousels.length; i < len; i++) {
2019-03-01 17:31:34 +01:00
Carousel._carouselInterface(carousels[i], Data.getData(carousels[i], DATA_KEY));
2018-11-13 07:41:12 +01:00
}
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2019-03-01 17:31:34 +01:00
* add .carousel to jQuery only if jQuery is present
2018-11-13 07:41:12 +01:00
*/
2019-03-01 17:31:34 +01:00
if (typeof jQuery !== 'undefined') {
var JQUERY_NO_CONFLICT = jQuery.fn[NAME];
jQuery.fn[NAME] = Carousel._jQueryInterface;
jQuery.fn[NAME].Constructor = Carousel;
2018-11-13 07:41:12 +01:00
2019-03-01 17:31:34 +01:00
jQuery.fn[NAME].noConflict = function () {
jQuery.fn[NAME] = JQUERY_NO_CONFLICT;
return Carousel._jQueryInterface;
};
}
2015-05-08 07:26:40 +02:00
return Carousel;
2018-07-24 02:51:14 +02:00
2019-01-04 17:29:45 +01:00
}));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=carousel.js.map