From 896e444895614538ec7b2dc689ddd051fb97b99e Mon Sep 17 00:00:00 2001 From: Mitchell Bryson Date: Thu, 12 Nov 2020 07:34:17 +0000 Subject: [PATCH] Check for data-interval on the first slide of carousel - v4 (#31820) When starting a cycle for a carousel, it only checks for a default interval, and not an interval defined on the slide element via data props. This adds a check in before creating the interval to move to the next slide. Co-authored-by: XhmikosR --- .bundlewatch.config.json | 2 +- js/src/carousel.js | 28 ++++++++++++++++++++-------- js/tests/unit/carousel.js | 15 ++++++++++++++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/.bundlewatch.config.json b/.bundlewatch.config.json index fb76d8dd83b8..1b4444728788 100644 --- a/.bundlewatch.config.json +++ b/.bundlewatch.config.json @@ -26,7 +26,7 @@ }, { "path": "./dist/js/bootstrap.bundle.js", - "maxSize": "47.5 kB" + "maxSize": "48 kB" }, { "path": "./dist/js/bootstrap.bundle.min.js", diff --git a/js/src/carousel.js b/js/src/carousel.js index 5f738fd92758..cc6ae44079f2 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -169,6 +169,8 @@ class Carousel { } if (this._config.interval && !this._isPaused) { + this._updateInterval() + this._interval = setInterval( (document.visibilityState ? this.nextWhenVisible : this.next).bind(this), this._config.interval @@ -401,6 +403,23 @@ class Carousel { } } + _updateInterval() { + const element = this._activeElement || this._element.querySelector(SELECTOR_ACTIVE_ITEM) + + 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) { const activeElement = this._element.querySelector(SELECTOR_ACTIVE_ITEM) const activeElementIndex = this._getItemIndex(activeElement) @@ -445,6 +464,7 @@ class Carousel { } this._setActiveIndicatorElement(nextElement) + this._activeElement = nextElement const slidEvent = $.Event(EVENT_SLID, { relatedTarget: nextElement, @@ -461,14 +481,6 @@ class Carousel { $(activeElement).addClass(directionalClassName) $(nextElement).addClass(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 = Util.getTransitionDurationFromElement(activeElement) $(activeElement) diff --git a/js/tests/unit/carousel.js b/js/tests/unit/carousel.js index 757461575b90..3c3e203e538a 100644 --- a/js/tests/unit/carousel.js +++ b/js/tests/unit/carousel.js @@ -480,7 +480,7 @@ $(function () { }) QUnit.test('should set interval from data attribute on individual carousel-item', function (assert) { - assert.expect(2) + assert.expect(4) var templateHTML = '