carousel.js 20 KB

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