carousel.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * --------------------------------------------------------------------------
  6. * Bootstrap (v4.0.0-alpha.4): carousel.js
  7. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  8. * --------------------------------------------------------------------------
  9. */
  10. var Carousel = function ($) {
  11. /**
  12. * ------------------------------------------------------------------------
  13. * Constants
  14. * ------------------------------------------------------------------------
  15. */
  16. var NAME = 'carousel';
  17. var VERSION = '4.0.0-alpha.4';
  18. var DATA_KEY = 'bs.carousel';
  19. var EVENT_KEY = '.' + DATA_KEY;
  20. var DATA_API_KEY = '.data-api';
  21. var JQUERY_NO_CONFLICT = $.fn[NAME];
  22. var TRANSITION_DURATION = 600;
  23. var ARROW_LEFT_KEYCODE = 37; // KeyboardEvent.which value for left arrow key
  24. var ARROW_RIGHT_KEYCODE = 39; // KeyboardEvent.which value for right arrow key
  25. var Default = {
  26. interval: 5000,
  27. keyboard: true,
  28. slide: false,
  29. pause: 'hover',
  30. wrap: true
  31. };
  32. var DefaultType = {
  33. interval: '(number|boolean)',
  34. keyboard: 'boolean',
  35. slide: '(boolean|string)',
  36. pause: '(string|boolean)',
  37. wrap: 'boolean'
  38. };
  39. var Direction = {
  40. NEXT: 'next',
  41. PREVIOUS: 'prev'
  42. };
  43. var Event = {
  44. SLIDE: 'slide' + EVENT_KEY,
  45. SLID: 'slid' + EVENT_KEY,
  46. KEYDOWN: 'keydown' + EVENT_KEY,
  47. MOUSEENTER: 'mouseenter' + EVENT_KEY,
  48. MOUSELEAVE: 'mouseleave' + EVENT_KEY,
  49. LOAD_DATA_API: 'load' + EVENT_KEY + DATA_API_KEY,
  50. CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY
  51. };
  52. var ClassName = {
  53. CAROUSEL: 'carousel',
  54. ACTIVE: 'active',
  55. SLIDE: 'slide',
  56. RIGHT: 'right',
  57. LEFT: 'left',
  58. ITEM: 'carousel-item'
  59. };
  60. var Selector = {
  61. ACTIVE: '.active',
  62. ACTIVE_ITEM: '.active.carousel-item',
  63. ITEM: '.carousel-item',
  64. NEXT_PREV: '.next, .prev',
  65. INDICATORS: '.carousel-indicators',
  66. DATA_SLIDE: '[data-slide], [data-slide-to]',
  67. DATA_RIDE: '[data-ride="carousel"]'
  68. };
  69. /**
  70. * ------------------------------------------------------------------------
  71. * Class Definition
  72. * ------------------------------------------------------------------------
  73. */
  74. var Carousel = function () {
  75. function Carousel(element, config) {
  76. _classCallCheck(this, Carousel);
  77. this._items = null;
  78. this._interval = null;
  79. this._activeElement = null;
  80. this._isPaused = false;
  81. this._isSliding = false;
  82. this._config = this._getConfig(config);
  83. this._element = $(element)[0];
  84. this._indicatorsElement = $(this._element).find(Selector.INDICATORS)[0];
  85. this._addEventListeners();
  86. }
  87. // getters
  88. // public
  89. Carousel.prototype.next = function next() {
  90. if (!this._isSliding) {
  91. this._slide(Direction.NEXT);
  92. }
  93. };
  94. Carousel.prototype.nextWhenVisible = function nextWhenVisible() {
  95. // Don't call next when the page isn't visible
  96. if (!document.hidden) {
  97. this.next();
  98. }
  99. };
  100. Carousel.prototype.prev = function prev() {
  101. if (!this._isSliding) {
  102. this._slide(Direction.PREVIOUS);
  103. }
  104. };
  105. Carousel.prototype.pause = function pause(event) {
  106. if (!event) {
  107. this._isPaused = true;
  108. }
  109. if ($(this._element).find(Selector.NEXT_PREV)[0] && Util.supportsTransitionEnd()) {
  110. Util.triggerTransitionEnd(this._element);
  111. this.cycle(true);
  112. }
  113. clearInterval(this._interval);
  114. this._interval = null;
  115. };
  116. Carousel.prototype.cycle = function cycle(event) {
  117. if (!event) {
  118. this._isPaused = false;
  119. }
  120. if (this._interval) {
  121. clearInterval(this._interval);
  122. this._interval = null;
  123. }
  124. if (this._config.interval && !this._isPaused) {
  125. this._interval = setInterval($.proxy(document.visibilityState ? this.nextWhenVisible : this.next, this), this._config.interval);
  126. }
  127. };
  128. Carousel.prototype.to = function to(index) {
  129. var _this = this;
  130. this._activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0];
  131. var activeIndex = this._getItemIndex(this._activeElement);
  132. if (index > this._items.length - 1 || index < 0) {
  133. return;
  134. }
  135. if (this._isSliding) {
  136. $(this._element).one(Event.SLID, function () {
  137. return _this.to(index);
  138. });
  139. return;
  140. }
  141. if (activeIndex === index) {
  142. this.pause();
  143. this.cycle();
  144. return;
  145. }
  146. var direction = index > activeIndex ? Direction.NEXT : Direction.PREVIOUS;
  147. this._slide(direction, this._items[index]);
  148. };
  149. Carousel.prototype.dispose = function dispose() {
  150. $(this._element).off(EVENT_KEY);
  151. $.removeData(this._element, DATA_KEY);
  152. this._items = null;
  153. this._config = null;
  154. this._element = null;
  155. this._interval = null;
  156. this._isPaused = null;
  157. this._isSliding = null;
  158. this._activeElement = null;
  159. this._indicatorsElement = null;
  160. };
  161. // private
  162. Carousel.prototype._getConfig = function _getConfig(config) {
  163. config = $.extend({}, Default, config);
  164. Util.typeCheckConfig(NAME, config, DefaultType);
  165. return config;
  166. };
  167. Carousel.prototype._addEventListeners = function _addEventListeners() {
  168. if (this._config.keyboard) {
  169. $(this._element).on(Event.KEYDOWN, $.proxy(this._keydown, this));
  170. }
  171. if (this._config.pause === 'hover' && !('ontouchstart' in document.documentElement)) {
  172. $(this._element).on(Event.MOUSEENTER, $.proxy(this.pause, this)).on(Event.MOUSELEAVE, $.proxy(this.cycle, this));
  173. }
  174. };
  175. Carousel.prototype._keydown = function _keydown(event) {
  176. event.preventDefault();
  177. if (/input|textarea/i.test(event.target.tagName)) {
  178. return;
  179. }
  180. switch (event.which) {
  181. case ARROW_LEFT_KEYCODE:
  182. this.prev();
  183. break;
  184. case ARROW_RIGHT_KEYCODE:
  185. this.next();
  186. break;
  187. default:
  188. return;
  189. }
  190. };
  191. Carousel.prototype._getItemIndex = function _getItemIndex(element) {
  192. this._items = $.makeArray($(element).parent().find(Selector.ITEM));
  193. return this._items.indexOf(element);
  194. };
  195. Carousel.prototype._getItemByDirection = function _getItemByDirection(direction, activeElement) {
  196. var isNextDirection = direction === Direction.NEXT;
  197. var isPrevDirection = direction === Direction.PREVIOUS;
  198. var activeIndex = this._getItemIndex(activeElement);
  199. var lastItemIndex = this._items.length - 1;
  200. var isGoingToWrap = isPrevDirection && activeIndex === 0 || isNextDirection && activeIndex === lastItemIndex;
  201. if (isGoingToWrap && !this._config.wrap) {
  202. return activeElement;
  203. }
  204. var delta = direction === Direction.PREVIOUS ? -1 : 1;
  205. var itemIndex = (activeIndex + delta) % this._items.length;
  206. return itemIndex === -1 ? this._items[this._items.length - 1] : this._items[itemIndex];
  207. };
  208. Carousel.prototype._triggerSlideEvent = function _triggerSlideEvent(relatedTarget, directionalClassname) {
  209. var slideEvent = $.Event(Event.SLIDE, {
  210. relatedTarget: relatedTarget,
  211. direction: directionalClassname
  212. });
  213. $(this._element).trigger(slideEvent);
  214. return slideEvent;
  215. };
  216. Carousel.prototype._setActiveIndicatorElement = function _setActiveIndicatorElement(element) {
  217. if (this._indicatorsElement) {
  218. $(this._indicatorsElement).find(Selector.ACTIVE).removeClass(ClassName.ACTIVE);
  219. var nextIndicator = this._indicatorsElement.children[this._getItemIndex(element)];
  220. if (nextIndicator) {
  221. $(nextIndicator).addClass(ClassName.ACTIVE);
  222. }
  223. }
  224. };
  225. Carousel.prototype._slide = function _slide(direction, element) {
  226. var _this2 = this;
  227. var activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0];
  228. var nextElement = element || activeElement && this._getItemByDirection(direction, activeElement);
  229. var isCycling = Boolean(this._interval);
  230. var directionalClassName = direction === Direction.NEXT ? ClassName.LEFT : ClassName.RIGHT;
  231. if (nextElement && $(nextElement).hasClass(ClassName.ACTIVE)) {
  232. this._isSliding = false;
  233. return;
  234. }
  235. var slideEvent = this._triggerSlideEvent(nextElement, directionalClassName);
  236. if (slideEvent.isDefaultPrevented()) {
  237. return;
  238. }
  239. if (!activeElement || !nextElement) {
  240. // some weirdness is happening, so we bail
  241. return;
  242. }
  243. this._isSliding = true;
  244. if (isCycling) {
  245. this.pause();
  246. }
  247. this._setActiveIndicatorElement(nextElement);
  248. var slidEvent = $.Event(Event.SLID, {
  249. relatedTarget: nextElement,
  250. direction: directionalClassName
  251. });
  252. if (Util.supportsTransitionEnd() && $(this._element).hasClass(ClassName.SLIDE)) {
  253. $(nextElement).addClass(direction);
  254. Util.reflow(nextElement);
  255. $(activeElement).addClass(directionalClassName);
  256. $(nextElement).addClass(directionalClassName);
  257. $(activeElement).one(Util.TRANSITION_END, function () {
  258. $(nextElement).removeClass(directionalClassName).removeClass(direction);
  259. $(nextElement).addClass(ClassName.ACTIVE);
  260. $(activeElement).removeClass(ClassName.ACTIVE).removeClass(direction).removeClass(directionalClassName);
  261. _this2._isSliding = false;
  262. setTimeout(function () {
  263. return $(_this2._element).trigger(slidEvent);
  264. }, 0);
  265. }).emulateTransitionEnd(TRANSITION_DURATION);
  266. } else {
  267. $(activeElement).removeClass(ClassName.ACTIVE);
  268. $(nextElement).addClass(ClassName.ACTIVE);
  269. this._isSliding = false;
  270. $(this._element).trigger(slidEvent);
  271. }
  272. if (isCycling) {
  273. this.cycle();
  274. }
  275. };
  276. // static
  277. Carousel._jQueryInterface = function _jQueryInterface(config) {
  278. return this.each(function () {
  279. var data = $(this).data(DATA_KEY);
  280. var _config = $.extend({}, Default, $(this).data());
  281. if ((typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object') {
  282. $.extend(_config, config);
  283. }
  284. var action = typeof config === 'string' ? config : _config.slide;
  285. if (!data) {
  286. data = new Carousel(this, _config);
  287. $(this).data(DATA_KEY, data);
  288. }
  289. if (typeof config === 'number') {
  290. data.to(config);
  291. } else if (typeof action === 'string') {
  292. if (data[action] === undefined) {
  293. throw new Error('No method named "' + action + '"');
  294. }
  295. data[action]();
  296. } else if (_config.interval) {
  297. data.pause();
  298. data.cycle();
  299. }
  300. });
  301. };
  302. Carousel._dataApiClickHandler = function _dataApiClickHandler(event) {
  303. var selector = Util.getSelectorFromElement(this);
  304. if (!selector) {
  305. return;
  306. }
  307. var target = $(selector)[0];
  308. if (!target || !$(target).hasClass(ClassName.CAROUSEL)) {
  309. return;
  310. }
  311. var config = $.extend({}, $(target).data(), $(this).data());
  312. var slideIndex = this.getAttribute('data-slide-to');
  313. if (slideIndex) {
  314. config.interval = false;
  315. }
  316. Carousel._jQueryInterface.call($(target), config);
  317. if (slideIndex) {
  318. $(target).data(DATA_KEY).to(slideIndex);
  319. }
  320. event.preventDefault();
  321. };
  322. _createClass(Carousel, null, [{
  323. key: 'VERSION',
  324. get: function get() {
  325. return VERSION;
  326. }
  327. }, {
  328. key: 'Default',
  329. get: function get() {
  330. return Default;
  331. }
  332. }]);
  333. return Carousel;
  334. }();
  335. /**
  336. * ------------------------------------------------------------------------
  337. * Data Api implementation
  338. * ------------------------------------------------------------------------
  339. */
  340. $(document).on(Event.CLICK_DATA_API, Selector.DATA_SLIDE, Carousel._dataApiClickHandler);
  341. $(window).on(Event.LOAD_DATA_API, function () {
  342. $(Selector.DATA_RIDE).each(function () {
  343. var $carousel = $(this);
  344. Carousel._jQueryInterface.call($carousel, $carousel.data());
  345. });
  346. });
  347. /**
  348. * ------------------------------------------------------------------------
  349. * jQuery
  350. * ------------------------------------------------------------------------
  351. */
  352. $.fn[NAME] = Carousel._jQueryInterface;
  353. $.fn[NAME].Constructor = Carousel;
  354. $.fn[NAME].noConflict = function () {
  355. $.fn[NAME] = JQUERY_NO_CONFLICT;
  356. return Carousel._jQueryInterface;
  357. };
  358. return Carousel;
  359. }(jQuery);
  360. //# sourceMappingURL=carousel.js.map