mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-09 15:16:51 +02:00
Check for data-interval on the first slide of carousel (#31818)
* check for data-interval on the first slide of carousel * add updateInterval method for elements of a carousel * add test for carousel interval being set during cycle * update activeElement as soon as slide has finished (before transition end) * only updateInterval before using it Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
@@ -181,6 +181,8 @@ class Carousel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this._config && this._config.interval && !this._isPaused) {
|
if (this._config && this._config.interval && !this._isPaused) {
|
||||||
|
this._updateInterval()
|
||||||
|
|
||||||
this._interval = setInterval(
|
this._interval = setInterval(
|
||||||
(document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
|
(document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
|
||||||
this._config.interval
|
this._config.interval
|
||||||
@@ -409,6 +411,23 @@ class Carousel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_updateInterval() {
|
||||||
|
const element = this._activeElement || SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
|
||||||
|
|
||||||
|
if (!element) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const elementInterval = parseInt(element.getAttribute('data-interval'), 10)
|
||||||
|
|
||||||
|
if (elementInterval) {
|
||||||
|
this._config.defaultInterval = this._config.defaultInterval || this._config.interval
|
||||||
|
this._config.interval = elementInterval
|
||||||
|
} else {
|
||||||
|
this._config.interval = this._config.defaultInterval || this._config.interval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_slide(direction, element) {
|
_slide(direction, element) {
|
||||||
const activeElement = SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
|
const activeElement = SelectorEngine.findOne(SELECTOR_ACTIVE_ITEM, this._element)
|
||||||
const activeElementIndex = this._getItemIndex(activeElement)
|
const activeElementIndex = this._getItemIndex(activeElement)
|
||||||
@@ -454,6 +473,7 @@ class Carousel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._setActiveIndicatorElement(nextElement)
|
this._setActiveIndicatorElement(nextElement)
|
||||||
|
this._activeElement = nextElement
|
||||||
|
|
||||||
if (this._element.classList.contains(CLASS_NAME_SLIDE)) {
|
if (this._element.classList.contains(CLASS_NAME_SLIDE)) {
|
||||||
nextElement.classList.add(orderClassName)
|
nextElement.classList.add(orderClassName)
|
||||||
@@ -463,14 +483,6 @@ class Carousel {
|
|||||||
activeElement.classList.add(directionalClassName)
|
activeElement.classList.add(directionalClassName)
|
||||||
nextElement.classList.add(directionalClassName)
|
nextElement.classList.add(directionalClassName)
|
||||||
|
|
||||||
const nextElementInterval = parseInt(nextElement.getAttribute('data-interval'), 10)
|
|
||||||
if (nextElementInterval) {
|
|
||||||
this._config.defaultInterval = this._config.defaultInterval || this._config.interval
|
|
||||||
this._config.interval = nextElementInterval
|
|
||||||
} else {
|
|
||||||
this._config.interval = this._config.defaultInterval || this._config.interval
|
|
||||||
}
|
|
||||||
|
|
||||||
const transitionDuration = getTransitionDurationFromElement(activeElement)
|
const transitionDuration = getTransitionDurationFromElement(activeElement)
|
||||||
|
|
||||||
EventHandler.one(activeElement, TRANSITION_END, () => {
|
EventHandler.one(activeElement, TRANSITION_END, () => {
|
||||||
|
@@ -636,27 +636,24 @@ describe('Carousel', () => {
|
|||||||
carousel.next()
|
carousel.next()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should get interval from data attribute in individual item', () => {
|
it('should update the active element to the next item before sliding', () => {
|
||||||
fixtureEl.innerHTML = [
|
fixtureEl.innerHTML = [
|
||||||
'<div id="myCarousel" class="carousel slide">',
|
'<div id="myCarousel" class="carousel slide">',
|
||||||
' <div class="carousel-inner">',
|
' <div class="carousel-inner">',
|
||||||
' <div class="carousel-item active">item 1</div>',
|
' <div class="carousel-item active">item 1</div>',
|
||||||
' <div class="carousel-item" data-interval="7">item 2</div>',
|
' <div id="secondItem" class="carousel-item">item 2</div>',
|
||||||
' <div class="carousel-item">item 3</div>',
|
' <div class="carousel-item">item 3</div>',
|
||||||
' </div>',
|
' </div>',
|
||||||
'</div>'
|
'</div>'
|
||||||
].join('')
|
].join('')
|
||||||
|
|
||||||
const carouselEl = fixtureEl.querySelector('#myCarousel')
|
const carouselEl = fixtureEl.querySelector('#myCarousel')
|
||||||
const carousel = new Carousel(carouselEl, {
|
const secondItemEl = fixtureEl.querySelector('#secondItem')
|
||||||
interval: 1814
|
const carousel = new Carousel(carouselEl)
|
||||||
})
|
|
||||||
|
|
||||||
expect(carousel._config.interval).toEqual(1814)
|
|
||||||
|
|
||||||
carousel.next()
|
carousel.next()
|
||||||
|
|
||||||
expect(carousel._config.interval).toEqual(7)
|
expect(carousel._activeElement).toEqual(secondItemEl)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should update indicators if present', done => {
|
it('should update indicators if present', done => {
|
||||||
@@ -876,6 +873,35 @@ describe('Carousel', () => {
|
|||||||
expect(window.setInterval).toHaveBeenCalled()
|
expect(window.setInterval).toHaveBeenCalled()
|
||||||
expect(window.clearInterval).toHaveBeenCalled()
|
expect(window.clearInterval).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should get interval from data attribute on the active item element', () => {
|
||||||
|
fixtureEl.innerHTML = [
|
||||||
|
'<div id="myCarousel" class="carousel slide">',
|
||||||
|
' <div class="carousel-inner">',
|
||||||
|
' <div class="carousel-item active" data-interval="7">item 1</div>',
|
||||||
|
' <div id="secondItem" class="carousel-item" data-interval="9385">item 2</div>',
|
||||||
|
' <div class="carousel-item">item 3</div>',
|
||||||
|
' </div>',
|
||||||
|
'</div>'
|
||||||
|
].join('')
|
||||||
|
|
||||||
|
const carouselEl = fixtureEl.querySelector('#myCarousel')
|
||||||
|
const secondItemEl = fixtureEl.querySelector('#secondItem')
|
||||||
|
const carousel = new Carousel(carouselEl, {
|
||||||
|
interval: 1814
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(carousel._config.interval).toEqual(1814)
|
||||||
|
|
||||||
|
carousel.cycle()
|
||||||
|
|
||||||
|
expect(carousel._config.interval).toEqual(7)
|
||||||
|
|
||||||
|
carousel._activeElement = secondItemEl
|
||||||
|
carousel.cycle()
|
||||||
|
|
||||||
|
expect(carousel._config.interval).toEqual(9385)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('to', () => {
|
describe('to', () => {
|
||||||
|
Reference in New Issue
Block a user