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

Do not call next when the carousel or the parent isn't visible #23524

Merged
merged 3 commits into from
Aug 17, 2017
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
4 changes: 3 additions & 1 deletion js/src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ const Carousel = (($) => {

nextWhenVisible() {
// Don't call next when the page isn't visible
if (!document.hidden) {
// or the carousel or it's parent isn't visible
if (!document.hidden &&
($(this._element).is(':visible') && $(this._element).css('visibility') !== 'hidden')) {
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure those 2 checks aren't identical?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes the first one just check if the parent or the carousel don't have display: none;

Copy link
Member Author

Choose a reason for hiding this comment

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

For more informations and precise explanations (with a better English 😆) : https://api.jquery.com/hidden-selector/

this.next()
}
}
Expand Down
76 changes: 76 additions & 0 deletions js/tests/unit/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,80 @@ $(function () {
})
$textArea.trigger(eventKeyDown)
})

QUnit.test('Should not go to the next item when the carousel is not visible', function (assert) {
assert.expect(2)
var done = assert.async()
var html = '<div id="myCarousel" class="carousel slide" data-interval="50" style="display: none;">'
+ ' <div class="carousel-inner">'
+ ' <div id="firstItem" class="carousel-item active">'
+ ' <img alt="">'
+ ' </div>'
+ ' <div class="carousel-item">'
+ ' <img alt="">'
+ ' </div>'
+ ' <div class="carousel-item">'
+ ' <img alt="">'
+ ' </div>'
+ ' <a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>'
+ ' <a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>'
+ '</div>'
var $html = $(html)
$html
.appendTo('#qunit-fixture')
.bootstrapCarousel()

var $firstItem = $('#firstItem')
setTimeout(function () {
assert.ok($firstItem.hasClass('active'))
$html
.bootstrapCarousel('dispose')
.attr('style', 'visibility: hidden;')
.bootstrapCarousel()

setTimeout(function () {
assert.ok($firstItem.hasClass('active'))
done()
}, 80)
}, 80)
})

QUnit.test('Should not go to the next item when the parent of the carousel is not visible', function (assert) {
assert.expect(2)
var done = assert.async()
var html = '<div id="parent" style="display: none;">'
+ ' <div id="myCarousel" class="carousel slide" data-interval="50" style="display: none;">'
+ ' <div class="carousel-inner">'
+ ' <div id="firstItem" class="carousel-item active">'
+ ' <img alt="">'
+ ' </div>'
+ ' <div class="carousel-item">'
+ ' <img alt="">'
+ ' </div>'
+ ' <div class="carousel-item">'
+ ' <img alt="">'
+ ' </div>'
+ ' <a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>'
+ ' <a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>'
+ ' </div>'
+ '</div>'
var $html = $(html)
$html.appendTo('#qunit-fixture')
var $parent = $html.find('#parent')
var $carousel = $html.find('#myCarousel')
$carousel.bootstrapCarousel()
var $firstItem = $('#firstItem')

setTimeout(function () {
assert.ok($firstItem.hasClass('active'))
$carousel.bootstrapCarousel('dispose')
$parent.attr('style', 'visibility: hidden;')
Copy link
Member

@XhmikosR XhmikosR Aug 17, 2017

Choose a reason for hiding this comment

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

This sets visibility:hidden but doesn't remove display:none, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

It remove the display: none; because we set the attribute value no matter what we had before that

Copy link
Member

Choose a reason for hiding this comment

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

OK, looks good to me then. If you can't think of anything else, please merge :)

$carousel.bootstrapCarousel()

setTimeout(function () {
assert.ok($firstItem.hasClass('active'))
done()
}, 80)
}, 80)
})
})