carousel.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*!
  2. * Bootstrap carousel.js v4.3.1 (https://getbootstrap.com/)
  3. * Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
  8. typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
  9. (global = global || self, global.Carousel = factory(global.jQuery, global.Util));
  10. }(this, function ($, Util) { 'use strict';
  11. $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  12. Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util;
  13. function _defineProperties(target, props) {
  14. for (var i = 0; i < props.length; i++) {
  15. var descriptor = props[i];
  16. descriptor.enumerable = descriptor.enumerable || false;
  17. descriptor.configurable = true;
  18. if ("value" in descriptor) descriptor.writable = true;
  19. Object.defineProperty(target, descriptor.key, descriptor);
  20. }
  21. }
  22. function _createClass(Constructor, protoProps, staticProps) {
  23. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  24. if (staticProps) _defineProperties(Constructor, staticProps);
  25. return Constructor;
  26. }
  27. function _defineProperty(obj, key, value) {
  28. if (key in obj) {
  29. Object.defineProperty(obj, key, {
  30. value: value,
  31. enumerable: true,
  32. configurable: true,
  33. writable: true
  34. });
  35. } else {
  36. obj[key] = value;
  37. }
  38. return obj;
  39. }
  40. function _objectSpread(target) {
  41. for (var i = 1; i < arguments.length; i++) {
  42. var source = arguments[i] != null ? arguments[i] : {};
  43. var ownKeys = Object.keys(source);
  44. if (typeof Object.getOwnPropertySymbols === 'function') {
  45. ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
  46. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  47. }));
  48. }
  49. ownKeys.forEach(function (key) {
  50. _defineProperty(target, key, source[key]);
  51. });
  52. }
  53. return target;
  54. }
  55. /**
  56. * ------------------------------------------------------------------------
  57. * Constants
  58. * ------------------------------------------------------------------------
  59. */
  60. var NAME = 'carousel';
  61. var VERSION = '4.3.1';
  62. var DATA_KEY = 'bs.carousel';
  63. var EVENT_KEY = "." + DATA_KEY;
  64. var DATA_API_KEY = '.data-api';
  65. var JQUERY_NO_CONFLICT = $.fn[NAME];
  66. var ARROW_LEFT_KEYCODE = 37; // KeyboardEvent.which value for left arrow key
  67. var ARROW_RIGHT_KEYCODE = 39; // KeyboardEvent.which value for right arrow key
  68. var TOUCHEVENT_COMPAT_WAIT = 500; // Time for mouse compat events to fire after touch
  69. var SWIPE_THRESHOLD = 40;
  70. var Default = {
  71. interval: 5000,
  72. keyboard: true,
  73. slide: false,
  74. pause: 'hover',
  75. wrap: true,
  76. touch: true
  77. };
  78. var DefaultType = {
  79. interval: '(number|boolean)',
  80. keyboard: 'boolean',
  81. slide: '(boolean|string)',
  82. pause: '(string|boolean)',
  83. wrap: 'boolean',
  84. touch: 'boolean'
  85. };
  86. var Direction = {
  87. NEXT: 'next',
  88. PREV: 'prev',
  89. LEFT: 'left',
  90. RIGHT: 'right'
  91. };
  92. var Event = {
  93. SLIDE: "slide" + EVENT_KEY,
  94. SLID: "slid" + EVENT_KEY,
  95. KEYDOWN: "keydown" + EVENT_KEY,
  96. MOUSEENTER: "mouseenter" + EVENT_KEY,
  97. MOUSELEAVE: "mouseleave" + EVENT_KEY,
  98. TOUCHSTART: "touchstart" + EVENT_KEY,
  99. TOUCHMOVE: "touchmove" + EVENT_KEY,
  100. TOUCHEND: "touchend" + EVENT_KEY,
  101. POINTERDOWN: "pointerdown" + EVENT_KEY,
  102. POINTERUP: "pointerup" + EVENT_KEY,
  103. DRAG_START: "dragstart" + EVENT_KEY,
  104. LOAD_DATA_API: "load" + EVENT_KEY + DATA_API_KEY,
  105. CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
  106. };
  107. var ClassName = {
  108. CAROUSEL: 'carousel',
  109. ACTIVE: 'active',
  110. SLIDE: 'slide',
  111. RIGHT: 'carousel-item-right',
  112. LEFT: 'carousel-item-left',
  113. NEXT: 'carousel-item-next',
  114. PREV: 'carousel-item-prev',
  115. ITEM: 'carousel-item',
  116. POINTER_EVENT: 'pointer-event'
  117. };
  118. var Selector = {
  119. ACTIVE: '.active',
  120. ACTIVE_ITEM: '.active.carousel-item',
  121. ITEM: '.carousel-item',
  122. ITEM_IMG: '.carousel-item img',
  123. NEXT_PREV: '.carousel-item-next, .carousel-item-prev',
  124. INDICATORS: '.carousel-indicators',
  125. DATA_SLIDE: '[data-slide], [data-slide-to]',
  126. DATA_RIDE: '[data-ride="carousel"]'
  127. };
  128. var PointerType = {
  129. TOUCH: 'touch',
  130. PEN: 'pen'
  131. /**
  132. * ------------------------------------------------------------------------
  133. * Class Definition
  134. * ------------------------------------------------------------------------
  135. */
  136. };
  137. var Carousel =
  138. /*#__PURE__*/
  139. function () {
  140. function Carousel(element, config) {
  141. this._items = null;
  142. this._interval = null;
  143. this._activeElement = null;
  144. this._isPaused = false;
  145. this._isSliding = false;
  146. this.touchTimeout = null;
  147. this.touchStartX = 0;
  148. this.touchDeltaX = 0;
  149. this._config = this._getConfig(config);
  150. this._element = element;
  151. this._indicatorsElement = this._element.querySelector(Selector.INDICATORS);
  152. this._touchSupported = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0;
  153. this._pointerEvent = Boolean(window.PointerEvent || window.MSPointerEvent);
  154. this._addEventListeners();
  155. } // Getters
  156. var _proto = Carousel.prototype;
  157. // Public
  158. _proto.next = function next() {
  159. if (!this._isSliding) {
  160. this._slide(Direction.NEXT);
  161. }
  162. };
  163. _proto.nextWhenVisible = function nextWhenVisible() {
  164. // Don't call next when the page isn't visible
  165. // or the carousel or its parent isn't visible
  166. if (!document.hidden && $(this._element).is(':visible') && $(this._element).css('visibility') !== 'hidden') {
  167. this.next();
  168. }
  169. };
  170. _proto.prev = function prev() {
  171. if (!this._isSliding) {
  172. this._slide(Direction.PREV);
  173. }
  174. };
  175. _proto.pause = function pause(event) {
  176. if (!event) {
  177. this._isPaused = true;
  178. }
  179. if (this._element.querySelector(Selector.NEXT_PREV)) {
  180. Util.triggerTransitionEnd(this._element);
  181. this.cycle(true);
  182. }
  183. clearInterval(this._interval);
  184. this._interval = null;
  185. };
  186. _proto.cycle = function cycle(event) {
  187. if (!event) {
  188. this._isPaused = false;
  189. }
  190. if (this._interval) {
  191. clearInterval(this._interval);
  192. this._interval = null;
  193. }
  194. if (this._config.interval && !this._isPaused) {
  195. this._interval = setInterval((document.visibilityState ? this.nextWhenVisible : this.next).bind(this), this._config.interval);
  196. }
  197. };
  198. _proto.to = function to(index) {
  199. var _this = this;
  200. this._activeElement = this._element.querySelector(Selector.ACTIVE_ITEM);
  201. var activeIndex = this._getItemIndex(this._activeElement);
  202. if (index > this._items.length - 1 || index < 0) {
  203. return;
  204. }
  205. if (this._isSliding) {
  206. $(this._element).one(Event.SLID, function () {
  207. return _this.to(index);
  208. });
  209. return;
  210. }
  211. if (activeIndex === index) {
  212. this.pause();
  213. this.cycle();
  214. return;
  215. }
  216. var direction = index > activeIndex ? Direction.NEXT : Direction.PREV;
  217. this._slide(direction, this._items[index]);
  218. };
  219. _proto.dispose = function dispose() {
  220. $(this._element).off(EVENT_KEY);
  221. $.removeData(this._element, DATA_KEY);
  222. this._items = null;
  223. this._config = null;
  224. this._element = null;
  225. this._interval = null;
  226. this._isPaused = null;
  227. this._isSliding = null;
  228. this._activeElement = null;
  229. this._indicatorsElement = null;
  230. } // Private
  231. ;
  232. _proto._getConfig = function _getConfig(config) {
  233. config = _objectSpread({}, Default, config);
  234. Util.typeCheckConfig(NAME, config, DefaultType);
  235. return config;
  236. };
  237. _proto._handleSwipe = function _handleSwipe() {
  238. var absDeltax = Math.abs(this.touchDeltaX);
  239. if (absDeltax <= SWIPE_THRESHOLD) {
  240. return;
  241. }
  242. var direction = absDeltax / this.touchDeltaX; // swipe left
  243. if (direction > 0) {
  244. this.prev();
  245. } // swipe right
  246. if (direction < 0) {
  247. this.next();
  248. }
  249. };
  250. _proto._addEventListeners = function _addEventListeners() {
  251. var _this2 = this;
  252. if (this._config.keyboard) {
  253. $(this._element).on(Event.KEYDOWN, function (event) {
  254. return _this2._keydown(event);
  255. });
  256. }
  257. if (this._config.pause === 'hover') {
  258. $(this._element).on(Event.MOUSEENTER, function (event) {
  259. return _this2.pause(event);
  260. }).on(Event.MOUSELEAVE, function (event) {
  261. return _this2.cycle(event);
  262. });
  263. }
  264. if (this._config.touch) {
  265. this._addTouchEventListeners();
  266. }
  267. };
  268. _proto._addTouchEventListeners = function _addTouchEventListeners() {
  269. var _this3 = this;
  270. if (!this._touchSupported) {
  271. return;
  272. }
  273. var start = function start(event) {
  274. if (_this3._pointerEvent && PointerType[event.originalEvent.pointerType.toUpperCase()]) {
  275. _this3.touchStartX = event.originalEvent.clientX;
  276. } else if (!_this3._pointerEvent) {
  277. _this3.touchStartX = event.originalEvent.touches[0].clientX;
  278. }
  279. };
  280. var move = function move(event) {
  281. // ensure swiping with one touch and not pinching
  282. if (event.originalEvent.touches && event.originalEvent.touches.length > 1) {
  283. _this3.touchDeltaX = 0;
  284. } else {
  285. _this3.touchDeltaX = event.originalEvent.touches[0].clientX - _this3.touchStartX;
  286. }
  287. };
  288. var end = function end(event) {
  289. if (_this3._pointerEvent && PointerType[event.originalEvent.pointerType.toUpperCase()]) {
  290. _this3.touchDeltaX = event.originalEvent.clientX - _this3.touchStartX;
  291. }
  292. _this3._handleSwipe();
  293. if (_this3._config.pause === 'hover') {
  294. // If it's a touch-enabled device, mouseenter/leave are fired as
  295. // part of the mouse compatibility events on first tap - the carousel
  296. // would stop cycling until user tapped out of it;
  297. // here, we listen for touchend, explicitly pause the carousel
  298. // (as if it's the second time we tap on it, mouseenter compat event
  299. // is NOT fired) and after a timeout (to allow for mouse compatibility
  300. // events to fire) we explicitly restart cycling
  301. _this3.pause();
  302. if (_this3.touchTimeout) {
  303. clearTimeout(_this3.touchTimeout);
  304. }
  305. _this3.touchTimeout = setTimeout(function (event) {
  306. return _this3.cycle(event);
  307. }, TOUCHEVENT_COMPAT_WAIT + _this3._config.interval);
  308. }
  309. };
  310. $(this._element.querySelectorAll(Selector.ITEM_IMG)).on(Event.DRAG_START, function (e) {
  311. return e.preventDefault();
  312. });
  313. if (this._pointerEvent) {
  314. $(this._element).on(Event.POINTERDOWN, function (event) {
  315. return start(event);
  316. });
  317. $(this._element).on(Event.POINTERUP, function (event) {
  318. return end(event);
  319. });
  320. this._element.classList.add(ClassName.POINTER_EVENT);
  321. } else {
  322. $(this._element).on(Event.TOUCHSTART, function (event) {
  323. return start(event);
  324. });
  325. $(this._element).on(Event.TOUCHMOVE, function (event) {
  326. return move(event);
  327. });
  328. $(this._element).on(Event.TOUCHEND, function (event) {
  329. return end(event);
  330. });
  331. }
  332. };
  333. _proto._keydown = function _keydown(event) {
  334. if (/input|textarea/i.test(event.target.tagName)) {
  335. return;
  336. }
  337. switch (event.which) {
  338. case ARROW_LEFT_KEYCODE:
  339. event.preventDefault();
  340. this.prev();
  341. break;
  342. case ARROW_RIGHT_KEYCODE:
  343. event.preventDefault();
  344. this.next();
  345. break;
  346. default:
  347. }
  348. };
  349. _proto._getItemIndex = function _getItemIndex(element) {
  350. this._items = element && element.parentNode ? [].slice.call(element.parentNode.querySelectorAll(Selector.ITEM)) : [];
  351. return this._items.indexOf(element);
  352. };
  353. _proto._getItemByDirection = function _getItemByDirection(direction, activeElement) {
  354. var isNextDirection = direction === Direction.NEXT;
  355. var isPrevDirection = direction === Direction.PREV;
  356. var activeIndex = this._getItemIndex(activeElement);
  357. var lastItemIndex = this._items.length - 1;
  358. var isGoingToWrap = isPrevDirection && activeIndex === 0 || isNextDirection && activeIndex === lastItemIndex;
  359. if (isGoingToWrap && !this._config.wrap) {
  360. return activeElement;
  361. }
  362. var delta = direction === Direction.PREV ? -1 : 1;
  363. var itemIndex = (activeIndex + delta) % this._items.length;
  364. return itemIndex === -1 ? this._items[this._items.length - 1] : this._items[itemIndex];
  365. };
  366. _proto._triggerSlideEvent = function _triggerSlideEvent(relatedTarget, eventDirectionName) {
  367. var targetIndex = this._getItemIndex(relatedTarget);
  368. var fromIndex = this._getItemIndex(this._element.querySelector(Selector.ACTIVE_ITEM));
  369. var slideEvent = $.Event(Event.SLIDE, {
  370. relatedTarget: relatedTarget,
  371. direction: eventDirectionName,
  372. from: fromIndex,
  373. to: targetIndex
  374. });
  375. $(this._element).trigger(slideEvent);
  376. return slideEvent;
  377. };
  378. _proto._setActiveIndicatorElement = function _setActiveIndicatorElement(element) {
  379. if (this._indicatorsElement) {
  380. var indicators = [].slice.call(this._indicatorsElement.querySelectorAll(Selector.ACTIVE));
  381. $(indicators).removeClass(ClassName.ACTIVE);
  382. var nextIndicator = this._indicatorsElement.children[this._getItemIndex(element)];
  383. if (nextIndicator) {
  384. $(nextIndicator).addClass(ClassName.ACTIVE);
  385. }
  386. }
  387. };
  388. _proto._slide = function _slide(direction, element) {
  389. var _this4 = this;
  390. var activeElement = this._element.querySelector(Selector.ACTIVE_ITEM);
  391. var activeElementIndex = this._getItemIndex(activeElement);
  392. var nextElement = element || activeElement && this._getItemByDirection(direction, activeElement);
  393. var nextElementIndex = this._getItemIndex(nextElement);
  394. var isCycling = Boolean(this._interval);
  395. var directionalClassName;
  396. var orderClassName;
  397. var eventDirectionName;
  398. if (direction === Direction.NEXT) {
  399. directionalClassName = ClassName.LEFT;
  400. orderClassName = ClassName.NEXT;
  401. eventDirectionName = Direction.LEFT;
  402. } else {
  403. directionalClassName = ClassName.RIGHT;
  404. orderClassName = ClassName.PREV;
  405. eventDirectionName = Direction.RIGHT;
  406. }
  407. if (nextElement && $(nextElement).hasClass(ClassName.ACTIVE)) {
  408. this._isSliding = false;
  409. return;
  410. }
  411. var slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName);
  412. if (slideEvent.isDefaultPrevented()) {
  413. return;
  414. }
  415. if (!activeElement || !nextElement) {
  416. // Some weirdness is happening, so we bail
  417. return;
  418. }
  419. this._isSliding = true;
  420. if (isCycling) {
  421. this.pause();
  422. }
  423. this._setActiveIndicatorElement(nextElement);
  424. var slidEvent = $.Event(Event.SLID, {
  425. relatedTarget: nextElement,
  426. direction: eventDirectionName,
  427. from: activeElementIndex,
  428. to: nextElementIndex
  429. });
  430. if ($(this._element).hasClass(ClassName.SLIDE)) {
  431. $(nextElement).addClass(orderClassName);
  432. Util.reflow(nextElement);
  433. $(activeElement).addClass(directionalClassName);
  434. $(nextElement).addClass(directionalClassName);
  435. var nextElementInterval = parseInt(nextElement.getAttribute('data-interval'), 10);
  436. if (nextElementInterval) {
  437. this._config.defaultInterval = this._config.defaultInterval || this._config.interval;
  438. this._config.interval = nextElementInterval;
  439. } else {
  440. this._config.interval = this._config.defaultInterval || this._config.interval;
  441. }
  442. var transitionDuration = Util.getTransitionDurationFromElement(activeElement);
  443. $(activeElement).one(Util.TRANSITION_END, function () {
  444. $(nextElement).removeClass(directionalClassName + " " + orderClassName).addClass(ClassName.ACTIVE);
  445. $(activeElement).removeClass(ClassName.ACTIVE + " " + orderClassName + " " + directionalClassName);
  446. _this4._isSliding = false;
  447. setTimeout(function () {
  448. return $(_this4._element).trigger(slidEvent);
  449. }, 0);
  450. }).emulateTransitionEnd(transitionDuration);
  451. } else {
  452. $(activeElement).removeClass(ClassName.ACTIVE);
  453. $(nextElement).addClass(ClassName.ACTIVE);
  454. this._isSliding = false;
  455. $(this._element).trigger(slidEvent);
  456. }
  457. if (isCycling) {
  458. this.cycle();
  459. }
  460. } // Static
  461. ;
  462. Carousel._jQueryInterface = function _jQueryInterface(config) {
  463. return this.each(function () {
  464. var data = $(this).data(DATA_KEY);
  465. var _config = _objectSpread({}, Default, $(this).data());
  466. if (typeof config === 'object') {
  467. _config = _objectSpread({}, _config, config);
  468. }
  469. var action = typeof config === 'string' ? config : _config.slide;
  470. if (!data) {
  471. data = new Carousel(this, _config);
  472. $(this).data(DATA_KEY, data);
  473. }
  474. if (typeof config === 'number') {
  475. data.to(config);
  476. } else if (typeof action === 'string') {
  477. if (typeof data[action] === 'undefined') {
  478. throw new TypeError("No method named \"" + action + "\"");
  479. }
  480. data[action]();
  481. } else if (_config.interval && _config.ride) {
  482. data.pause();
  483. data.cycle();
  484. }
  485. });
  486. };
  487. Carousel._dataApiClickHandler = function _dataApiClickHandler(event) {
  488. var selector = Util.getSelectorFromElement(this);
  489. if (!selector) {
  490. return;
  491. }
  492. var target = $(selector)[0];
  493. if (!target || !$(target).hasClass(ClassName.CAROUSEL)) {
  494. return;
  495. }
  496. var config = _objectSpread({}, $(target).data(), $(this).data());
  497. var slideIndex = this.getAttribute('data-slide-to');
  498. if (slideIndex) {
  499. config.interval = false;
  500. }
  501. Carousel._jQueryInterface.call($(target), config);
  502. if (slideIndex) {
  503. $(target).data(DATA_KEY).to(slideIndex);
  504. }
  505. event.preventDefault();
  506. };
  507. _createClass(Carousel, null, [{
  508. key: "VERSION",
  509. get: function get() {
  510. return VERSION;
  511. }
  512. }, {
  513. key: "Default",
  514. get: function get() {
  515. return Default;
  516. }
  517. }]);
  518. return Carousel;
  519. }();
  520. /**
  521. * ------------------------------------------------------------------------
  522. * Data Api implementation
  523. * ------------------------------------------------------------------------
  524. */
  525. $(document).on(Event.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel._dataApiClickHandler);
  526. $(window).on(Event.LOAD_DATA_API, function () {
  527. var carousels = [].slice.call(document.querySelectorAll(Selector.DATA_RIDE));
  528. for (var i = 0, len = carousels.length; i < len; i++) {
  529. var $carousel = $(carousels[i]);
  530. Carousel._jQueryInterface.call($carousel, $carousel.data());
  531. }
  532. });
  533. /**
  534. * ------------------------------------------------------------------------
  535. * jQuery
  536. * ------------------------------------------------------------------------
  537. */
  538. $.fn[NAME] = Carousel._jQueryInterface;
  539. $.fn[NAME].Constructor = Carousel;
  540. $.fn[NAME].noConflict = function () {
  541. $.fn[NAME] = JQUERY_NO_CONFLICT;
  542. return Carousel._jQueryInterface;
  543. };
  544. return Carousel;
  545. }));
  546. //# sourceMappingURL=carousel.js.map