Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 7fb3840

Browse files
ChewTeaYeahwesleycho
authored andcommitted
feat(carousel): add noWrap option to prevent re-cycling of slides
- Adds `noWrap` option on carousel element for breaking the wrapping of the carousel Closes #3462 Resolves #3397
1 parent eb3b32e commit 7fb3840

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

src/carousel/carousel.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,22 @@ angular.module('ui.bootstrap.carousel', [])
8383
$scope.next = function() {
8484
var newIndex = (self.getCurrentIndex() + 1) % slides.length;
8585

86+
if (newIndex === 0 && $scope.noWrap()) {
87+
$scope.pause();
88+
return;
89+
}
90+
8691
return self.select(getSlideByIndex(newIndex), 'next');
8792
};
8893

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

97+
if ($scope.noWrap() && newIndex === slides.length - 1){
98+
$scope.pause();
99+
return;
100+
}
101+
92102
return self.select(getSlideByIndex(newIndex), 'prev');
93103
};
94104

@@ -225,7 +235,8 @@ angular.module('ui.bootstrap.carousel', [])
225235
scope: {
226236
interval: '=',
227237
noTransition: '=',
228-
noPause: '='
238+
noPause: '=',
239+
noWrap: '&'
229240
}
230241
};
231242
}])

src/carousel/docs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ Carousel creates a carousel similar to bootstrap's image carousel.
33
The carousel also offers support for touchscreen devices in the form of swiping. To enable swiping, load the `ngTouch` module as a dependency.
44

55
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.
6+
7+
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

src/carousel/docs/demo.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div ng-controller="CarouselDemoCtrl">
22
<div style="height: 305px">
3-
<carousel interval="myInterval">
3+
<carousel interval="myInterval" no-wrap="noWrapSlides">
44
<slide ng-repeat="slide in slides" active="slide.active">
55
<img ng-src="{{slide.image}}" style="margin:auto;">
66
<div class="carousel-caption">
@@ -13,6 +13,12 @@ <h4>Slide {{$index}}</h4>
1313
<div class="row">
1414
<div class="col-md-6">
1515
<button type="button" class="btn btn-info" ng-click="addSlide()">Add Slide</button>
16+
<div class="checkbox">
17+
<label>
18+
<input type="checkbox" ng-model="noWrapSlides">
19+
Disable Slide Looping
20+
</label>
21+
</div>
1622
</div>
1723
<div class="col-md-6">
1824
Interval, in milliseconds: <input type="number" class="form-control" ng-model="myInterval">

src/carousel/docs/demo.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
angular.module('ui.bootstrap.demo').controller('CarouselDemoCtrl', function ($scope) {
22
$scope.myInterval = 5000;
3+
$scope.noWrapSlides = false;
34
var slides = $scope.slides = [];
45
$scope.addSlide = function() {
56
var newWidth = 600 + slides.length + 1;

src/carousel/test/carousel.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,51 @@ describe('carousel', function() {
7373
expect(indicators.length).toBe(3);
7474
});
7575

76+
it('should stop cycling slides forward when noWrap is truthy', function () {
77+
elm = $compile(
78+
'<carousel interval="interval" no-wrap="noWrap">' +
79+
'<slide ng-repeat="slide in slides" active="slide.active">' +
80+
'{{slide.content}}' +
81+
'</slide>' +
82+
'</carousel>'
83+
)(scope);
84+
85+
scope.noWrap = true;
86+
scope.$apply();
87+
88+
scope = elm.isolateScope();
89+
spyOn(scope, 'pause');
90+
91+
for (var i = 0; i < scope.slides.length - 1; ++i) {
92+
scope.next();
93+
}
94+
testSlideActive(scope.slides.length - 1);
95+
scope.next();
96+
testSlideActive(scope.slides.length - 1);
97+
expect(scope.pause).toHaveBeenCalled();
98+
});
99+
100+
it('should stop cycling slides backward when noWrap is truthy', function () {
101+
elm = $compile(
102+
'<carousel interval="interval" no-wrap="noWrap">' +
103+
'<slide ng-repeat="slide in slides" active="slide.active">' +
104+
'{{slide.content}}' +
105+
'</slide>' +
106+
'</carousel>'
107+
)(scope);
108+
109+
scope.noWrap = true;
110+
scope.$apply();
111+
112+
scope = elm.isolateScope();
113+
spyOn(scope, 'pause');
114+
115+
testSlideActive(0);
116+
scope.prev();
117+
testSlideActive(0);
118+
expect(scope.pause).toHaveBeenCalled();
119+
});
120+
76121
it('should hide navigation when only one slide', function () {
77122
scope.slides=[{active:false,content:'one'}];
78123
scope.$apply();

0 commit comments

Comments
 (0)