From cbba8e53df2cfea3273072d94408fd2981dce460 Mon Sep 17 00:00:00 2001 From: Chris Rebert Date: Mon, 7 Jul 2014 19:05:48 -0700 Subject: [PATCH] Fix #13818 by using more robust "find next carousel item" logic Also adds another test for #9461: carousel next/prev should ignore non-items --- js/carousel.js | 11 +++++++++-- js/tests/unit/carousel.js | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/js/carousel.js b/js/carousel.js index b7da1ba53e..86383edae8 100644 --- a/js/carousel.js +++ b/js/carousel.js @@ -63,6 +63,13 @@ return this.$items.index(item || this.$active) } + Carousel.prototype.getItemForDirection = function (direction, active) { + var delta = direction == 'prev' ? -1 : 1 + var activeIndex = this.getItemIndex(active) + var itemIndex = (activeIndex + delta) % this.$items.length + return this.$items.eq(itemIndex) + } + Carousel.prototype.to = function (pos) { var that = this var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item.active')) @@ -72,7 +79,7 @@ if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid" if (activeIndex == pos) return this.pause().cycle() - return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos])) + return this.slide(pos > activeIndex ? 'next' : 'prev', this.$items.eq(pos)) } Carousel.prototype.pause = function (e) { @@ -100,7 +107,7 @@ Carousel.prototype.slide = function (type, next) { var $active = this.$element.find('.item.active') - var $next = next || $active[type]() + var $next = next || this.getItemForDirection(type, $active) var isCycling = this.interval var direction = type == 'next' ? 'left' : 'right' var fallback = type == 'next' ? 'first' : 'last' diff --git a/js/tests/unit/carousel.js b/js/tests/unit/carousel.js index 51872c57b4..3f9e61a349 100644 --- a/js/tests/unit/carousel.js +++ b/js/tests/unit/carousel.js @@ -349,7 +349,7 @@ $(function () { $carousel.remove() }) - test('should skip over non-items', function () { + test('should skip over non-items when using item indices', function () { var templateHTML = '