@@ -17,10 +17,10 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
17
17
var destroyed = false ;
18
18
/* direction: "prev" or "next" */
19
19
self . select = $scope . select = function ( nextSlide , direction ) {
20
- var nextIndex = slides . indexOf ( nextSlide ) ;
20
+ var nextIndex = self . indexOfSlide ( nextSlide ) ;
21
21
//Decide direction if it's not given
22
22
if ( direction === undefined ) {
23
- direction = nextIndex > currentIndex ? 'next' : 'prev' ;
23
+ direction = nextIndex > self . getCurrentIndex ( ) ? 'next' : 'prev' ;
24
24
}
25
25
if ( nextSlide && nextSlide !== self . currentSlide ) {
26
26
goNext ( ) ;
@@ -31,7 +31,6 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
31
31
32
32
angular . extend ( nextSlide , { direction : direction , active : true } ) ;
33
33
angular . extend ( self . currentSlide || { } , { direction : direction , active : false } ) ;
34
-
35
34
if ( $animate . enabled ( ) && ! $scope . noTransition && nextSlide . $element ) {
36
35
$scope . $currentTransition = true ;
37
36
// TODO: Switch to use .one when upgrading beyond 1.2.21
@@ -52,26 +51,45 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
52
51
destroyed = true ;
53
52
} ) ;
54
53
54
+ function getSlideByIndex ( index ) {
55
+ if ( angular . isUndefined ( slides [ index ] . index ) ) {
56
+ return slides [ index ] ;
57
+ }
58
+ var i , len = slides . length ;
59
+ for ( i = 0 ; i < slides . length ; ++ i ) {
60
+ if ( slides [ i ] . index == index ) {
61
+ return slides [ i ] ;
62
+ }
63
+ }
64
+ }
65
+
66
+ self . getCurrentIndex = function ( ) {
67
+ if ( self . currentSlide && angular . isDefined ( self . currentSlide . index ) ) {
68
+ return + self . currentSlide . index ;
69
+ }
70
+ return currentIndex ;
71
+ } ;
72
+
55
73
/* Allow outside people to call indexOf on slides array */
56
74
self . indexOfSlide = function ( slide ) {
57
- return slides . indexOf ( slide ) ;
75
+ return angular . isDefined ( slide . index ) ? + slide . index : slides . indexOf ( slide ) ;
58
76
} ;
59
77
60
78
$scope . next = function ( ) {
61
- var newIndex = ( currentIndex + 1 ) % slides . length ;
79
+ var newIndex = ( self . getCurrentIndex ( ) + 1 ) % slides . length ;
62
80
63
81
//Prevent this user-triggered transition from occurring if there is already one in progress
64
82
if ( ! $scope . $currentTransition ) {
65
- return self . select ( slides [ newIndex ] , 'next' ) ;
83
+ return self . select ( getSlideByIndex ( newIndex ) , 'next' ) ;
66
84
}
67
85
} ;
68
86
69
87
$scope . prev = function ( ) {
70
- var newIndex = currentIndex - 1 < 0 ? slides . length - 1 : currentIndex - 1 ;
88
+ var newIndex = self . getCurrentIndex ( ) - 1 < 0 ? slides . length - 1 : self . getCurrentIndex ( ) - 1 ;
71
89
72
90
//Prevent this user-triggered transition from occurring if there is already one in progress
73
91
if ( ! $scope . $currentTransition ) {
74
- return self . select ( slides [ newIndex ] , 'prev' ) ;
92
+ return self . select ( getSlideByIndex ( newIndex ) , 'prev' ) ;
75
93
}
76
94
} ;
77
95
@@ -134,6 +152,11 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
134
152
} ;
135
153
136
154
self . removeSlide = function ( slide ) {
155
+ if ( angular . isDefined ( slide . index ) ) {
156
+ slides . sort ( function ( a , b ) {
157
+ return + a . index > + b . index ;
158
+ } ) ;
159
+ }
137
160
//get the index of the slide inside the carousel
138
161
var index = slides . indexOf ( slide ) ;
139
162
slides . splice ( index , 1 ) ;
@@ -213,13 +236,14 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
213
236
* Creates a slide inside a {@link ui.bootstrap.carousel.directive:carousel carousel}. Must be placed as a child of a carousel element.
214
237
*
215
238
* @param {boolean= } active Model binding, whether or not this slide is currently active.
239
+ * @param {number= } index The index of the slide. The slides will be sorted by this parameter.
216
240
*
217
241
* @example
218
242
<example module="ui.bootstrap">
219
243
<file name="index.html">
220
244
<div ng-controller="CarouselDemoCtrl">
221
245
<carousel>
222
- <slide ng-repeat="slide in slides" active="slide.active">
246
+ <slide ng-repeat="slide in slides" active="slide.active" index="$index" >
223
247
<img ng-src="{{slide.image}}" style="margin:auto;">
224
248
<div class="carousel-caption">
225
249
<h4>Slide {{$index}}</h4>
@@ -253,7 +277,8 @@ function CarouselDemoCtrl($scope) {
253
277
replace : true ,
254
278
templateUrl : 'template/carousel/slide.html' ,
255
279
scope : {
256
- active : '=?'
280
+ active : '=?' ,
281
+ index : '=?'
257
282
} ,
258
283
link : function ( scope , element , attrs , carouselCtrl ) {
259
284
carouselCtrl . addSlide ( scope , element ) ;
0 commit comments