diff options
| author | Mark Otto <[email protected]> | 2016-12-28 15:52:28 -0800 |
|---|---|---|
| committer | Mark Otto <[email protected]> | 2016-12-28 15:52:28 -0800 |
| commit | 047d4a77da5af8b59fd562935669c550272f57a6 (patch) | |
| tree | 7a2daf4d737693826c6d264113adc6e987c3ae2a /js/src/carousel.js | |
| parent | 11d52ba9498990483d822a5a42d371393a110080 (diff) | |
| parent | e1e621be046a4541a2fd36e445015ee44de3c67e (diff) | |
| download | bootstrap-047d4a77da5af8b59fd562935669c550272f57a6.tar.xz bootstrap-047d4a77da5af8b59fd562935669c550272f57a6.zip | |
Merge branch 'v4-dev' into v4-docs-streamlined
Diffstat (limited to 'js/src/carousel.js')
| -rw-r--r-- | js/src/carousel.js | 124 |
1 files changed, 68 insertions, 56 deletions
diff --git a/js/src/carousel.js b/js/src/carousel.js index ff00d8aaf..9a1a668b2 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -3,7 +3,7 @@ import Util from './util' /** * -------------------------------------------------------------------------- - * Bootstrap (v4.0.0-alpha.4): carousel.js + * Bootstrap (v4.0.0-alpha.5): carousel.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * -------------------------------------------------------------------------- */ @@ -18,7 +18,7 @@ const Carousel = (($) => { */ const NAME = 'carousel' - const VERSION = '4.0.0-alpha.4' + const VERSION = '4.0.0-alpha.5' const DATA_KEY = 'bs.carousel' const EVENT_KEY = `.${DATA_KEY}` const DATA_API_KEY = '.data-api' @@ -45,7 +45,9 @@ const Carousel = (($) => { const Direction = { NEXT : 'next', - PREVIOUS : 'prev' + PREV : 'prev', + LEFT : 'left', + RIGHT : 'right' } const Event = { @@ -62,8 +64,10 @@ const Carousel = (($) => { CAROUSEL : 'carousel', ACTIVE : 'active', SLIDE : 'slide', - RIGHT : 'right', - LEFT : 'left', + RIGHT : 'carousel-item-right', + LEFT : 'carousel-item-left', + NEXT : 'carousel-item-next', + PREV : 'carousel-item-prev', ITEM : 'carousel-item' } @@ -71,7 +75,7 @@ const Carousel = (($) => { ACTIVE : '.active', ACTIVE_ITEM : '.active.carousel-item', ITEM : '.carousel-item', - NEXT_PREV : '.next, .prev', + NEXT_PREV : '.carousel-item-next, .carousel-item-prev', INDICATORS : '.carousel-indicators', DATA_SLIDE : '[data-slide], [data-slide-to]', DATA_RIDE : '[data-ride="carousel"]' @@ -116,9 +120,10 @@ const Carousel = (($) => { // public next() { - if (!this._isSliding) { - this._slide(Direction.NEXT) + if (this._isSliding) { + throw new Error('Carousel is sliding') } + this._slide(Direction.NEXT) } nextWhenVisible() { @@ -129,9 +134,10 @@ const Carousel = (($) => { } prev() { - if (!this._isSliding) { - this._slide(Direction.PREVIOUS) + if (this._isSliding) { + throw new Error('Carousel is sliding') } + this._slide(Direction.PREVIOUS) } pause(event) { @@ -161,7 +167,8 @@ const Carousel = (($) => { if (this._config.interval && !this._isPaused) { this._interval = setInterval( - $.proxy(document.visibilityState ? this.nextWhenVisible : this.next, this), this._config.interval + (document.visibilityState ? this.nextWhenVisible : this.next).bind(this), + this._config.interval ) } } @@ -169,9 +176,9 @@ const Carousel = (($) => { to(index) { this._activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0] - let activeIndex = this._getItemIndex(this._activeElement) + const activeIndex = this._getItemIndex(this._activeElement) - if (index > (this._items.length - 1) || index < 0) { + if (index > this._items.length - 1 || index < 0) { return } @@ -186,7 +193,7 @@ const Carousel = (($) => { return } - let direction = index > activeIndex ? + const direction = index > activeIndex ? Direction.NEXT : Direction.PREVIOUS @@ -219,29 +226,29 @@ const Carousel = (($) => { _addEventListeners() { if (this._config.keyboard) { $(this._element) - .on(Event.KEYDOWN, $.proxy(this._keydown, this)) + .on(Event.KEYDOWN, (event) => this._keydown(event)) } if (this._config.pause === 'hover' && !('ontouchstart' in document.documentElement)) { $(this._element) - .on(Event.MOUSEENTER, $.proxy(this.pause, this)) - .on(Event.MOUSELEAVE, $.proxy(this.cycle, this)) + .on(Event.MOUSEENTER, (event) => this.pause(event)) + .on(Event.MOUSELEAVE, (event) => this.cycle(event)) } } _keydown(event) { - event.preventDefault() - if (/input|textarea/i.test(event.target.tagName)) { return } switch (event.which) { case ARROW_LEFT_KEYCODE: + event.preventDefault() this.prev() break case ARROW_RIGHT_KEYCODE: + event.preventDefault() this.next() break default: @@ -255,29 +262,29 @@ const Carousel = (($) => { } _getItemByDirection(direction, activeElement) { - let isNextDirection = direction === Direction.NEXT - let isPrevDirection = direction === Direction.PREVIOUS - let activeIndex = this._getItemIndex(activeElement) - let lastItemIndex = (this._items.length - 1) - let isGoingToWrap = (isPrevDirection && activeIndex === 0) || - (isNextDirection && activeIndex === lastItemIndex) + const isNextDirection = direction === Direction.NEXT + const isPrevDirection = direction === Direction.PREVIOUS + const activeIndex = this._getItemIndex(activeElement) + const lastItemIndex = this._items.length - 1 + const isGoingToWrap = isPrevDirection && activeIndex === 0 || + isNextDirection && activeIndex === lastItemIndex if (isGoingToWrap && !this._config.wrap) { return activeElement } - let delta = direction === Direction.PREVIOUS ? -1 : 1 - let itemIndex = (activeIndex + delta) % this._items.length + const delta = direction === Direction.PREVIOUS ? -1 : 1 + const itemIndex = (activeIndex + delta) % this._items.length return itemIndex === -1 ? this._items[this._items.length - 1] : this._items[itemIndex] } - _triggerSlideEvent(relatedTarget, directionalClassname) { - let slideEvent = $.Event(Event.SLIDE, { + _triggerSlideEvent(relatedTarget, eventDirectionName) { + const slideEvent = $.Event(Event.SLIDE, { relatedTarget, - direction: directionalClassname + direction: eventDirectionName }) $(this._element).trigger(slideEvent) @@ -291,7 +298,7 @@ const Carousel = (($) => { .find(Selector.ACTIVE) .removeClass(ClassName.ACTIVE) - let nextIndicator = this._indicatorsElement.children[ + const nextIndicator = this._indicatorsElement.children[ this._getItemIndex(element) ] @@ -302,22 +309,32 @@ const Carousel = (($) => { } _slide(direction, element) { - let activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0] - let nextElement = element || activeElement && + const activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0] + const nextElement = element || activeElement && this._getItemByDirection(direction, activeElement) - let isCycling = Boolean(this._interval) + const isCycling = Boolean(this._interval) - let directionalClassName = direction === Direction.NEXT ? - ClassName.LEFT : - ClassName.RIGHT + let directionalClassName + let orderClassName + let eventDirectionName + + if (direction === Direction.NEXT) { + directionalClassName = ClassName.LEFT + orderClassName = ClassName.NEXT + eventDirectionName = Direction.LEFT + } else { + directionalClassName = ClassName.RIGHT + orderClassName = ClassName.PREV + eventDirectionName = Direction.RIGHT + } if (nextElement && $(nextElement).hasClass(ClassName.ACTIVE)) { this._isSliding = false return } - let slideEvent = this._triggerSlideEvent(nextElement, directionalClassName) + const slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName) if (slideEvent.isDefaultPrevented()) { return } @@ -335,15 +352,15 @@ const Carousel = (($) => { this._setActiveIndicatorElement(nextElement) - let slidEvent = $.Event(Event.SLID, { + const slidEvent = $.Event(Event.SLID, { relatedTarget: nextElement, - direction: directionalClassName + direction: eventDirectionName }) if (Util.supportsTransitionEnd() && $(this._element).hasClass(ClassName.SLIDE)) { - $(nextElement).addClass(direction) + $(nextElement).addClass(orderClassName) Util.reflow(nextElement) @@ -353,15 +370,10 @@ const Carousel = (($) => { $(activeElement) .one(Util.TRANSITION_END, () => { $(nextElement) - .removeClass(directionalClassName) - .removeClass(direction) - - $(nextElement).addClass(ClassName.ACTIVE) + .removeClass(`${directionalClassName} ${orderClassName}`) + .addClass(ClassName.ACTIVE) - $(activeElement) - .removeClass(ClassName.ACTIVE) - .removeClass(direction) - .removeClass(directionalClassName) + $(activeElement).removeClass(`${ClassName.ACTIVE} ${orderClassName} ${directionalClassName}`) this._isSliding = false @@ -389,13 +401,13 @@ const Carousel = (($) => { static _jQueryInterface(config) { return this.each(function () { let data = $(this).data(DATA_KEY) - let _config = $.extend({}, Default, $(this).data()) + const _config = $.extend({}, Default, $(this).data()) if (typeof config === 'object') { $.extend(_config, config) } - let action = typeof config === 'string' ? config : _config.slide + const action = typeof config === 'string' ? config : _config.slide if (!data) { data = new Carousel(this, _config) @@ -417,20 +429,20 @@ const Carousel = (($) => { } static _dataApiClickHandler(event) { - let selector = Util.getSelectorFromElement(this) + const selector = Util.getSelectorFromElement(this) if (!selector) { return } - let target = $(selector)[0] + const target = $(selector)[0] if (!target || !$(target).hasClass(ClassName.CAROUSEL)) { return } - let config = $.extend({}, $(target).data(), $(this).data()) - let slideIndex = this.getAttribute('data-slide-to') + const config = $.extend({}, $(target).data(), $(this).data()) + const slideIndex = this.getAttribute('data-slide-to') if (slideIndex) { config.interval = false @@ -459,7 +471,7 @@ const Carousel = (($) => { $(window).on(Event.LOAD_DATA_API, () => { $(Selector.DATA_RIDE).each(function () { - let $carousel = $(this) + const $carousel = $(this) Carousel._jQueryInterface.call($carousel, $carousel.data()) }) }) |
