@@ -17,10 +17,10 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
1717 var destroyed = false ;
1818 /* direction: "prev" or "next" */
1919 self . select = $scope . select = function ( nextSlide , direction ) {
20- var nextIndex = slides . indexOf ( nextSlide ) ;
20+ var nextIndex = self . indexOfSlide ( nextSlide ) ;
2121 //Decide direction if it's not given
2222 if ( direction === undefined ) {
23- direction = nextIndex > currentIndex ? 'next' : 'prev' ;
23+ direction = nextIndex > self . getCurrentIndex ( ) ? 'next' : 'prev' ;
2424 }
2525 if ( nextSlide && nextSlide !== self . currentSlide ) {
2626 goNext ( ) ;
@@ -31,7 +31,6 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
3131
3232 angular . extend ( nextSlide , { direction : direction , active : true } ) ;
3333 angular . extend ( self . currentSlide || { } , { direction : direction , active : false } ) ;
34-
3534 if ( $animate . enabled ( ) && ! $scope . noTransition && nextSlide . $element ) {
3635 $scope . $currentTransition = true ;
3736 // TODO: Switch to use .one when upgrading beyond 1.2.21
@@ -52,26 +51,45 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
5251 destroyed = true ;
5352 } ) ;
5453
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+
5573 /* Allow outside people to call indexOf on slides array */
5674 self . indexOfSlide = function ( slide ) {
57- return slides . indexOf ( slide ) ;
75+ return angular . isDefined ( slide . index ) ? + slide . index : slides . indexOf ( slide ) ;
5876 } ;
5977
6078 $scope . next = function ( ) {
61- var newIndex = ( currentIndex + 1 ) % slides . length ;
79+ var newIndex = ( self . getCurrentIndex ( ) + 1 ) % slides . length ;
6280
6381 //Prevent this user-triggered transition from occurring if there is already one in progress
6482 if ( ! $scope . $currentTransition ) {
65- return self . select ( slides [ newIndex ] , 'next' ) ;
83+ return self . select ( getSlideByIndex ( newIndex ) , 'next' ) ;
6684 }
6785 } ;
6886
6987 $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 ;
7189
7290 //Prevent this user-triggered transition from occurring if there is already one in progress
7391 if ( ! $scope . $currentTransition ) {
74- return self . select ( slides [ newIndex ] , 'prev' ) ;
92+ return self . select ( getSlideByIndex ( newIndex ) , 'prev' ) ;
7593 }
7694 } ;
7795
@@ -134,6 +152,11 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
134152 } ;
135153
136154 self . removeSlide = function ( slide ) {
155+ if ( angular . isDefined ( slide . index ) ) {
156+ slides . sort ( function ( a , b ) {
157+ return + a . index > + b . index ;
158+ } ) ;
159+ }
137160 //get the index of the slide inside the carousel
138161 var index = slides . indexOf ( slide ) ;
139162 slides . splice ( index , 1 ) ;
@@ -213,13 +236,14 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
213236 * Creates a slide inside a {@link ui.bootstrap.carousel.directive:carousel carousel}. Must be placed as a child of a carousel element.
214237 *
215238 * @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.
216240 *
217241 * @example
218242<example module="ui.bootstrap">
219243 <file name="index.html">
220244<div ng-controller="CarouselDemoCtrl">
221245 <carousel>
222- <slide ng-repeat="slide in slides" active="slide.active">
246+ <slide ng-repeat="slide in slides" active="slide.active" index="$index" >
223247 <img ng-src="{{slide.image}}" style="margin:auto;">
224248 <div class="carousel-caption">
225249 <h4>Slide {{$index}}</h4>
@@ -253,7 +277,8 @@ function CarouselDemoCtrl($scope) {
253277 replace : true ,
254278 templateUrl : 'template/carousel/slide.html' ,
255279 scope : {
256- active : '=?'
280+ active : '=?' ,
281+ index : '=?'
257282 } ,
258283 link : function ( scope , element , attrs , carouselCtrl ) {
259284 carouselCtrl . addSlide ( scope , element ) ;
0 commit comments