Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more robust "find next carousel item" logic #14069

Merged
merged 1 commit into from
Jul 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions js/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down
27 changes: 26 additions & 1 deletion js/tests/unit/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div id="myCarousel" class="carousel" data-interval="1814">'
+ '<div class="carousel-inner">'
+ '<div class="item active">'
Expand All @@ -373,4 +373,29 @@ $(function () {

strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
})

test('should skip over non-items when using next/prev methods', function () {
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">'
+ '<div class="carousel-inner">'
+ '<div class="item active">'
+ '<img alt="">'
+ '</div>'
+ '<script type="text/x-metamorph" id="thingy"/>'
+ '<div class="item">'
+ '<img alt="">'
+ '</div>'
+ '<div class="item">'
+ '</div>'
+ '</div>'
+ '</div>'
var $template = $(templateHTML)

$template.bootstrapCarousel()

strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')

$template.bootstrapCarousel('next')

strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
})
})