Skip to content

Commit

Permalink
feat(carousel): add noWrap option to prevent re-cycling of slides
Browse files Browse the repository at this point in the history
- Adds `noWrap` option on carousel element for breaking the wrapping of the carousel

Closes angular-ui#3462
Resolves angular-ui#3397
  • Loading branch information
ChewTeaYeah authored and wesleycho committed Jun 30, 2015
1 parent eb3b32e commit 7fb3840
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/carousel/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,22 @@ angular.module('ui.bootstrap.carousel', [])
$scope.next = function() {
var newIndex = (self.getCurrentIndex() + 1) % slides.length;

if (newIndex === 0 && $scope.noWrap()) {
$scope.pause();
return;
}

return self.select(getSlideByIndex(newIndex), 'next');
};

$scope.prev = function() {
var newIndex = self.getCurrentIndex() - 1 < 0 ? slides.length - 1 : self.getCurrentIndex() - 1;

if ($scope.noWrap() && newIndex === slides.length - 1){
$scope.pause();
return;
}

return self.select(getSlideByIndex(newIndex), 'prev');
};

Expand Down Expand Up @@ -225,7 +235,8 @@ angular.module('ui.bootstrap.carousel', [])
scope: {
interval: '=',
noTransition: '=',
noPause: '='
noPause: '=',
noWrap: '&'
}
};
}])
Expand Down
2 changes: 2 additions & 0 deletions src/carousel/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Carousel creates a carousel similar to bootstrap's image carousel.
The carousel also offers support for touchscreen devices in the form of swiping. To enable swiping, load the `ngTouch` module as a dependency.

Use a `<carousel>` element with `<slide>` elements inside it. It will automatically cycle through the slides at a given rate, and a current-index variable will be kept in sync with the currently visible slide.

Use the no-wrap attribute on a `<carousel>` element to control the looping of slides; setting no-wrap to an expression which evaluates to a truthy value will prevent looping
8 changes: 7 additions & 1 deletion src/carousel/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div ng-controller="CarouselDemoCtrl">
<div style="height: 305px">
<carousel interval="myInterval">
<carousel interval="myInterval" no-wrap="noWrapSlides">
<slide ng-repeat="slide in slides" active="slide.active">
<img ng-src="{{slide.image}}" style="margin:auto;">
<div class="carousel-caption">
Expand All @@ -13,6 +13,12 @@ <h4>Slide {{$index}}</h4>
<div class="row">
<div class="col-md-6">
<button type="button" class="btn btn-info" ng-click="addSlide()">Add Slide</button>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="noWrapSlides">
Disable Slide Looping
</label>
</div>
</div>
<div class="col-md-6">
Interval, in milliseconds: <input type="number" class="form-control" ng-model="myInterval">
Expand Down
1 change: 1 addition & 0 deletions src/carousel/docs/demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
angular.module('ui.bootstrap.demo').controller('CarouselDemoCtrl', function ($scope) {
$scope.myInterval = 5000;
$scope.noWrapSlides = false;
var slides = $scope.slides = [];
$scope.addSlide = function() {
var newWidth = 600 + slides.length + 1;
Expand Down
45 changes: 45 additions & 0 deletions src/carousel/test/carousel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,51 @@ describe('carousel', function() {
expect(indicators.length).toBe(3);
});

it('should stop cycling slides forward when noWrap is truthy', function () {
elm = $compile(
'<carousel interval="interval" no-wrap="noWrap">' +
'<slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</slide>' +
'</carousel>'
)(scope);

scope.noWrap = true;
scope.$apply();

scope = elm.isolateScope();
spyOn(scope, 'pause');

for (var i = 0; i < scope.slides.length - 1; ++i) {
scope.next();
}
testSlideActive(scope.slides.length - 1);
scope.next();
testSlideActive(scope.slides.length - 1);
expect(scope.pause).toHaveBeenCalled();
});

it('should stop cycling slides backward when noWrap is truthy', function () {
elm = $compile(
'<carousel interval="interval" no-wrap="noWrap">' +
'<slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</slide>' +
'</carousel>'
)(scope);

scope.noWrap = true;
scope.$apply();

scope = elm.isolateScope();
spyOn(scope, 'pause');

testSlideActive(0);
scope.prev();
testSlideActive(0);
expect(scope.pause).toHaveBeenCalled();
});

it('should hide navigation when only one slide', function () {
scope.slides=[{active:false,content:'one'}];
scope.$apply();
Expand Down

0 comments on commit 7fb3840

Please sign in to comment.