carousel.js 17 KB

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