]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Check for data-interval on the first slide of carousel - v4 (#31820)
authorMitchell Bryson <mitchelljbryson@gmail.com>
Thu, 12 Nov 2020 07:34:17 +0000 (07:34 +0000)
committerGitHub <noreply@github.com>
Thu, 12 Nov 2020 07:34:17 +0000 (09:34 +0200)
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 <xhmikosr@gmail.com>
.bundlewatch.config.json
js/src/carousel.js
js/tests/unit/carousel.js

index fb76d8dd83b870250c944eab71d661e09cf15f84..1b44447287881b059764c62c18080c09de17a9cc 100644 (file)
@@ -26,7 +26,7 @@
     },
     {
       "path": "./dist/js/bootstrap.bundle.js",
-      "maxSize": "47.5 kB"
+      "maxSize": "48 kB"
     },
     {
       "path": "./dist/js/bootstrap.bundle.min.js",
index 5f738fd92758ed5c16e22b8591466dc2c04c8894..cc6ae44079f2cc5796423bda5ff06d12aad5279c 100644 (file)
@@ -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)
index 757461575b90dd780ef80dc469e98446f6f7db2e..3c3e203e538a975775ee1badedc0e784c5ef6907 100644 (file)
@@ -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 = '<div id="myCarousel" class="carousel slide" data-interval="1814">' +
         '<div class="carousel-inner">' +
         '<div class="carousel-item active" data-interval="2814">' +
@@ -516,13 +516,26 @@ $(function () {
         '</div>'
     var $carousel = $(templateHTML)
 
+    $carousel.appendTo('body')
+    $carousel.bootstrapCarousel()
+    assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814)
+    $carousel.remove()
+
+    $carousel.appendTo('body')
+    $carousel.bootstrapCarousel(0)
+    $carousel.data('bs.carousel').cycle()
+    assert.strictEqual($carousel.data('bs.carousel')._config.interval, 2814)
+    $carousel.remove()
+
     $carousel.appendTo('body')
     $carousel.bootstrapCarousel(1)
+    $carousel.data('bs.carousel').cycle()
     assert.strictEqual($carousel.data('bs.carousel')._config.interval, 3814)
     $carousel.remove()
 
     $carousel.appendTo('body')
     $carousel.bootstrapCarousel(2)
+    $carousel.data('bs.carousel').cycle()
     assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'reverts to default interval if no data-interval is set')
     $carousel.remove()
   })