@@ -11,7 +11,6 @@ IonicModule
1111function ( scope , element , $$ionicAttachDrag , $interval ) {
1212 var self = this ;
1313 var slideList = ionic . Utils . list ( [ ] ) ;
14- var selectedIndex = - 1 ;
1514 var slidesParent = angular . element ( element [ 0 ] . querySelector ( '.slider-slides' ) ) ;
1615
1716 // Successful slide requires velocity to be greater than this amount
@@ -54,24 +53,23 @@ function(scope, element, $$ionicAttachDrag, $interval) {
5453 // Gets whether the given index is relevant to selected
5554 // That is, whether the given index is previous, selected, or next
5655 function isRelevant ( index ) {
57- return slideList . isRelevant ( index , selectedIndex ) ;
56+ return slideList . isRelevant ( index , scope . selectedIndex ) ;
5857 }
5958
60- // Gets the index to the previous of the given slide, default selectedIndex
59+ // Gets the index to the previous of the given slide, default scope. selectedIndex
6160 function previous ( index ) {
62- index = arguments . length ? index : selectedIndex ;
61+ index = arguments . length ? index : scope . selectedIndex ;
6362 // If we only have two slides and loop is enabled, we cannot have a previous
6463 // because previous === next. In this case, return -1.
6564 if ( self . loop ( ) && self . count ( ) === 2 ) {
66- console . log ( 'lying about previous' ) ;
6765 return - 1 ;
6866 }
6967 return slideList . previous ( index ) ;
7068 }
7169
72- // Gets the index to the next of the given slide, default selectedIndex
70+ // Gets the index to the next of the given slide, default scope. selectedIndex
7371 function next ( index ) {
74- index = arguments . length ? index : selectedIndex ;
72+ index = arguments . length ? index : scope . selectedIndex ;
7573 return slideList . next ( index ) ;
7674 }
7775
@@ -99,7 +97,12 @@ function(scope, element, $$ionicAttachDrag, $interval) {
9997 var newIndex = slideList . add ( slide , index ) ;
10098 slide . onAdded ( slidesParent ) ;
10199
102- if ( selectedIndex === - 1 ) {
100+ // If we are waiting for a certain scope.selectedIndex and this is it,
101+ // select the slide
102+ if ( scope . selectedIndex === index ) {
103+ self . select ( newIndex ) ;
104+ // If we don't have a selectedIndex yet, select the first one available
105+ } else if ( ! isNumber ( scope . selectedIndex ) || scope . selectedIndex === - 1 ) {
103106 self . select ( newIndex ) ;
104107 } else if ( newIndex === self . previous ( ) || newIndex === self . next ( ) ) {
105108 // if the new slide is adjacent to selected, refresh the selection
@@ -115,7 +118,7 @@ function(scope, element, $$ionicAttachDrag, $interval) {
115118 slide . onRemoved ( ) ;
116119
117120 if ( isSelected ) {
118- self . select ( self . isInRange ( selectedIndex ) ? selectedIndex : selectedIndex - 1 ) ;
121+ self . select ( self . isInRange ( scope . selectedIndex ) ? scope . selectedIndex : scope . selectedIndex - 1 ) ;
119122 }
120123 }
121124 function move ( slide , targetIndex ) {
@@ -133,7 +136,7 @@ function(scope, element, $$ionicAttachDrag, $interval) {
133136 }
134137
135138 function selected ( ) {
136- return selectedIndex ;
139+ return self . isInRange ( scope . selectedIndex ) ? scope . selectedIndex : - 1 ;
137140 }
138141
139142 /*
@@ -142,15 +145,15 @@ function(scope, element, $$ionicAttachDrag, $interval) {
142145 function select ( newIndex , transitionDuration ) {
143146 if ( ! self . isInRange ( newIndex ) ) return ;
144147
145- var delta = self . delta ( selectedIndex , newIndex ) ;
148+ var delta = self . delta ( scope . selectedIndex , newIndex ) ;
146149
147150 slidesParent . css (
148151 ionic . CSS . TRANSITION_DURATION ,
149152 ( transitionDuration || SLIDE_TRANSITION_DURATION ) + 'ms'
150153 ) ;
151- selectedIndex = newIndex ;
154+ scope . selectedIndex = newIndex ;
152155
153- if ( self . isInRange ( selectedIndex ) && Math . abs ( delta ) > 1 ) {
156+ if ( self . isInRange ( scope . selectedIndex ) && Math . abs ( delta ) > 1 ) {
154157 // if the new slide is > 1 away, then it is currently not attached to the DOM.
155158 // Attach it in the position from which it will slide in.
156159 self . at ( newIndex ) . setState ( delta > 1 ? 'next' : 'previous' ) ;
@@ -163,9 +166,9 @@ function(scope, element, $$ionicAttachDrag, $interval) {
163166
164167 function doSelect ( ) {
165168 // If a new selection has happened before this frame, abort.
166- if ( selectedIndex !== newIndex ) return ;
169+ if ( scope . selectedIndex !== newIndex ) return ;
167170 scope . $evalAsync ( function ( ) {
168- if ( selectedIndex !== newIndex ) return ;
171+ if ( scope . selectedIndex !== newIndex ) return ;
169172 arrangeSlides ( newIndex ) ;
170173 } ) ;
171174 }
@@ -197,7 +200,7 @@ function(scope, element, $$ionicAttachDrag, $interval) {
197200 ) ;
198201 self . select ( nextIndex , transitionDuration ) ;
199202 } else {
200- self . select ( selectedIndex ) ;
203+ self . select ( scope . selectedIndex ) ;
201204 }
202205 }
203206
@@ -257,7 +260,7 @@ function(scope, element, $$ionicAttachDrag, $interval) {
257260 if ( ! enqueueRefresh . queued ) {
258261 enqueueRefresh . queued = true ;
259262 scope . $$postDigest ( function ( ) {
260- self . select ( selectedIndex ) ;
263+ self . select ( scope . selectedIndex ) ;
261264 enqueueRefresh . queued = false ;
262265 } ) ;
263266 }
0 commit comments