carousel.js 14 KB

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