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

honor wrap option in Carousel.getItemForDirection #15152

Merged
merged 2 commits into from
Nov 17, 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: 4 additions & 7 deletions js/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@
}

Carousel.prototype.getItemForDirection = function (direction, active) {
var delta = direction == 'prev' ? -1 : 1
var activeIndex = this.getItemIndex(active)
var willWrap = (direction == 'prev' && activeIndex === 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the strict comparison here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSCS check complains otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess because of false == 0. Never mind then.

|| (direction == 'next' && activeIndex == (this.$items.length - 1))
if (willWrap && !this.options.wrap) return active
var delta = direction == 'prev' ? -1 : 1
var itemIndex = (activeIndex + delta) % this.$items.length
return this.$items.eq(itemIndex)
}
Expand Down Expand Up @@ -116,14 +119,8 @@
var $next = next || this.getItemForDirection(type, $active)
var isCycling = this.interval
var direction = type == 'next' ? 'left' : 'right'
var fallback = type == 'next' ? 'first' : 'last'
var that = this

if (!$next.length) {
if (!this.options.wrap) return
$next = this.$element.find('.item')[fallback]()
}

if ($next.hasClass('active')) return (this.sliding = false)

var relatedTarget = $next[0]
Expand Down
153 changes: 153 additions & 0 deletions js/tests/unit/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,157 @@ $(function () {
strictEqual(type in $._data($template[0], 'events'), !isMobile, 'does' + (isMobile ? ' not' : '') + ' listen for ' + type + ' events')
})
})

test('should wrap around from end to start when wrap option is true', function () {
var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="true">'
+ '<ol class="carousel-indicators">'
+ '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="1"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="2"/>'
+ '</ol>'
+ '<div class="carousel-inner">'
+ '<div class="item active" id="one">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="two">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="three">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '</div>'
+ '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>'
+ '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>'
+ '</div>'
var $carousel = $(carouselHTML)
var getActiveId = function () { return $carousel.find('.item.active').attr('id') }

stop()

$carousel
.one('slid.bs.carousel', function () {
strictEqual(getActiveId(), 'two', 'carousel slid from 1st to 2nd slide')
$carousel
.one('slid.bs.carousel', function () {
strictEqual(getActiveId(), 'three', 'carousel slid from 2nd to 3rd slide')
$carousel
.one('slid.bs.carousel', function () {
strictEqual(getActiveId(), 'one', 'carousel wrapped around and slid from 3rd to 1st slide')
start()
})
.bootstrapCarousel('next')
})
.bootstrapCarousel('next')
})
.bootstrapCarousel('next')
})

test('should wrap around from start to end when wrap option is true', function () {
var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="true">'
+ '<ol class="carousel-indicators">'
+ '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="1"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="2"/>'
+ '</ol>'
+ '<div class="carousel-inner">'
+ '<div class="item active" id="one">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="two">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="three">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '</div>'
+ '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>'
+ '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>'
+ '</div>'
var $carousel = $(carouselHTML)

stop()

$carousel
.on('slid.bs.carousel', function () {
strictEqual($carousel.find('.item.active').attr('id'), 'three', 'carousel wrapped around and slid from 1st to 3rd slide')
start()
})
.bootstrapCarousel('prev')
})

test('should stay at the end when the next method is called and wrap is false', function () {
var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="false">'
+ '<ol class="carousel-indicators">'
+ '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="1"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="2"/>'
+ '</ol>'
+ '<div class="carousel-inner">'
+ '<div class="item active" id="one">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="two">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="three">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '</div>'
+ '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>'
+ '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>'
+ '</div>'
var $carousel = $(carouselHTML)
var getActiveId = function () { return $carousel.find('.item.active').attr('id') }

stop()

$carousel
.one('slid.bs.carousel', function () {
strictEqual(getActiveId(), 'two', 'carousel slid from 1st to 2nd slide')
$carousel
.one('slid.bs.carousel', function () {
strictEqual(getActiveId(), 'three', 'carousel slid from 2nd to 3rd slide')
$carousel
.one('slid.bs.carousel', function () {
ok(false, 'carousel slid when it should not have slid')
})
.bootstrapCarousel('next')
strictEqual(getActiveId(), 'three', 'carousel did not wrap around and stayed on 3rd slide')
start()
})
.bootstrapCarousel('next')
})
.bootstrapCarousel('next')
})

test('should stay at the start when the prev method is called and wrap is false', function () {
var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="false">'
+ '<ol class="carousel-indicators">'
+ '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="1"/>'
+ '<li data-target="#carousel-example-generic" data-slide-to="2"/>'
+ '</ol>'
+ '<div class="carousel-inner">'
+ '<div class="item active" id="one">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="two">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item" id="three">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '</div>'
+ '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>'
+ '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>'
+ '</div>'
var $carousel = $(carouselHTML)

$carousel
.on('slid.bs.carousel', function () {
ok(false, 'carousel slid when it should not have slid')
})
.bootstrapCarousel('prev')
strictEqual($carousel.find('.item.active').attr('id'), 'one', 'carousel did not wrap around and stayed on 1st slide')
})
})