aboutsummaryrefslogtreecommitdiff
path: root/js/src/carousel.js
diff options
context:
space:
mode:
authorGijs Boddeus <[email protected]>2017-08-15 23:43:36 +0200
committerGitHub <[email protected]>2017-08-15 23:43:36 +0200
commit06d4c6d273daf3eb84c9c5bb6306cecc9209304f (patch)
tree695d8fb5b2f414eae0bac032369f77285f594803 /js/src/carousel.js
parent1fb6d8c46a560e2e35295440721ba2929f9721b6 (diff)
parent7b873fa0a15c0fb62671f95e966656967c6fd9b4 (diff)
downloadbootstrap-06d4c6d273daf3eb84c9c5bb6306cecc9209304f.tar.xz
bootstrap-06d4c6d273daf3eb84c9c5bb6306cecc9209304f.zip
Merge pull request #1 from twbs/v4-dev
updating fork of BS to v4-beta1
Diffstat (limited to 'js/src/carousel.js')
-rw-r--r--js/src/carousel.js105
1 files changed, 73 insertions, 32 deletions
diff --git a/js/src/carousel.js b/js/src/carousel.js
index e56d4f0f2..a5d5f143a 100644
--- a/js/src/carousel.js
+++ b/js/src/carousel.js
@@ -3,7 +3,7 @@ import Util from './util'
/**
* --------------------------------------------------------------------------
- * Bootstrap (v4.0.0-alpha.5): carousel.js
+ * Bootstrap (v4.0.0-beta): carousel.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
@@ -17,15 +17,16 @@ const Carousel = (($) => {
* ------------------------------------------------------------------------
*/
- const NAME = 'carousel'
- const VERSION = '4.0.0-alpha.5'
- const DATA_KEY = 'bs.carousel'
- const EVENT_KEY = `.${DATA_KEY}`
- const DATA_API_KEY = '.data-api'
- const JQUERY_NO_CONFLICT = $.fn[NAME]
- const TRANSITION_DURATION = 600
- const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key
- const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
+ const NAME = 'carousel'
+ const VERSION = '4.0.0-beta'
+ const DATA_KEY = 'bs.carousel'
+ const EVENT_KEY = `.${DATA_KEY}`
+ const DATA_API_KEY = '.data-api'
+ const JQUERY_NO_CONFLICT = $.fn[NAME]
+ const TRANSITION_DURATION = 600
+ const ARROW_LEFT_KEYCODE = 37 // KeyboardEvent.which value for left arrow key
+ const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
+ const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
const Default = {
interval : 5000,
@@ -45,7 +46,9 @@ const Carousel = (($) => {
const Direction = {
NEXT : 'next',
- PREVIOUS : 'prev'
+ PREV : 'prev',
+ LEFT : 'left',
+ RIGHT : 'right'
}
const Event = {
@@ -54,6 +57,7 @@ const Carousel = (($) => {
KEYDOWN : `keydown${EVENT_KEY}`,
MOUSEENTER : `mouseenter${EVENT_KEY}`,
MOUSELEAVE : `mouseleave${EVENT_KEY}`,
+ TOUCHEND : `touchend${EVENT_KEY}`,
LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`,
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
}
@@ -62,8 +66,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 +77,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"]'
@@ -94,6 +100,8 @@ const Carousel = (($) => {
this._isPaused = false
this._isSliding = false
+ this.touchTimeout = null
+
this._config = this._getConfig(config)
this._element = $(element)[0]
this._indicatorsElement = $(this._element).find(Selector.INDICATORS)[0]
@@ -130,7 +138,7 @@ const Carousel = (($) => {
prev() {
if (!this._isSliding) {
- this._slide(Direction.PREVIOUS)
+ this._slide(Direction.PREV)
}
}
@@ -189,7 +197,7 @@ const Carousel = (($) => {
const direction = index > activeIndex ?
Direction.NEXT :
- Direction.PREVIOUS
+ Direction.PREV
this._slide(direction, this._items[index])
}
@@ -223,11 +231,26 @@ const Carousel = (($) => {
.on(Event.KEYDOWN, (event) => this._keydown(event))
}
- if (this._config.pause === 'hover' &&
- !('ontouchstart' in document.documentElement)) {
+ if (this._config.pause === 'hover') {
$(this._element)
.on(Event.MOUSEENTER, (event) => this.pause(event))
.on(Event.MOUSELEAVE, (event) => this.cycle(event))
+ if ('ontouchstart' in document.documentElement) {
+ // if it's a touch-enabled device, mouseenter/leave are fired as
+ // part of the mouse compatibility events on first tap - the carousel
+ // would stop cycling until user tapped out of it;
+ // here, we listen for touchend, explicitly pause the carousel
+ // (as if it's the second time we tap on it, mouseenter compat event
+ // is NOT fired) and after a timeout (to allow for mouse compatibility
+ // events to fire) we explicitly restart cycling
+ $(this._element).on(Event.TOUCHEND, () => {
+ this.pause()
+ if (this.touchTimeout) {
+ clearTimeout(this.touchTimeout)
+ }
+ this.touchTimeout = setTimeout((event) => this.cycle(event), TOUCHEVENT_COMPAT_WAIT + this._config.interval)
+ })
+ }
}
}
@@ -235,13 +258,14 @@ const Carousel = (($) => {
if (/input|textarea/i.test(event.target.tagName)) {
return
}
- event.preventDefault()
switch (event.which) {
case ARROW_LEFT_KEYCODE:
+ event.preventDefault()
this.prev()
break
case ARROW_RIGHT_KEYCODE:
+ event.preventDefault()
this.next()
break
default:
@@ -256,7 +280,7 @@ const Carousel = (($) => {
_getItemByDirection(direction, activeElement) {
const isNextDirection = direction === Direction.NEXT
- const isPrevDirection = direction === Direction.PREVIOUS
+ const isPrevDirection = direction === Direction.PREV
const activeIndex = this._getItemIndex(activeElement)
const lastItemIndex = this._items.length - 1
const isGoingToWrap = isPrevDirection && activeIndex === 0 ||
@@ -266,7 +290,7 @@ const Carousel = (($) => {
return activeElement
}
- const delta = direction === Direction.PREVIOUS ? -1 : 1
+ const delta = direction === Direction.PREV ? -1 : 1
const itemIndex = (activeIndex + delta) % this._items.length
return itemIndex === -1 ?
@@ -274,10 +298,14 @@ const Carousel = (($) => {
}
- _triggerSlideEvent(relatedTarget, directionalClassname) {
+ _triggerSlideEvent(relatedTarget, eventDirectionName) {
+ const targetIndex = this._getItemIndex(relatedTarget)
+ const fromIndex = this._getItemIndex($(this._element).find(Selector.ACTIVE_ITEM)[0])
const slideEvent = $.Event(Event.SLIDE, {
relatedTarget,
- direction: directionalClassname
+ direction: eventDirectionName,
+ from: fromIndex,
+ to: targetIndex
})
$(this._element).trigger(slideEvent)
@@ -303,21 +331,32 @@ const Carousel = (($) => {
_slide(direction, element) {
const activeElement = $(this._element).find(Selector.ACTIVE_ITEM)[0]
+ const activeElementIndex = this._getItemIndex(activeElement)
const nextElement = element || activeElement &&
this._getItemByDirection(direction, activeElement)
-
+ const nextElementIndex = this._getItemIndex(nextElement)
const isCycling = Boolean(this._interval)
- const 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
}
- const slideEvent = this._triggerSlideEvent(nextElement, directionalClassName)
+ const slideEvent = this._triggerSlideEvent(nextElement, eventDirectionName)
if (slideEvent.isDefaultPrevented()) {
return
}
@@ -337,13 +376,15 @@ const Carousel = (($) => {
const slidEvent = $.Event(Event.SLID, {
relatedTarget: nextElement,
- direction: directionalClassName
+ direction: eventDirectionName,
+ from: activeElementIndex,
+ to: nextElementIndex
})
if (Util.supportsTransitionEnd() &&
$(this._element).hasClass(ClassName.SLIDE)) {
- $(nextElement).addClass(direction)
+ $(nextElement).addClass(orderClassName)
Util.reflow(nextElement)
@@ -353,10 +394,10 @@ const Carousel = (($) => {
$(activeElement)
.one(Util.TRANSITION_END, () => {
$(nextElement)
- .removeClass(`${directionalClassName} ${direction}`)
+ .removeClass(`${directionalClassName} ${orderClassName}`)
.addClass(ClassName.ACTIVE)
- $(activeElement).removeClass(`${ClassName.ACTIVE} ${direction} ${directionalClassName}`)
+ $(activeElement).removeClass(`${ClassName.ACTIVE} ${orderClassName} ${directionalClassName}`)
this._isSliding = false