@@ -9,14 +9,13 @@ function($rootScope, $timeout) {
9
9
this . dataSource = options . dataSource ;
10
10
this . element = options . element ;
11
11
this . scrollView = options . scrollView ;
12
- this . itemSizePrimary = options . itemSizePrimary ;
13
- this . itemSizeSecondary = options . itemSizeSecondary ;
14
12
15
13
this . isVertical = ! ! this . scrollView . options . scrollingY ;
16
14
this . renderedItems = { } ;
17
15
18
16
this . lastRenderScrollValue = this . bufferTransformOffset = this . hasBufferStartIndex =
19
17
this . hasBufferEndIndex = this . bufferItemsLength = 0 ;
18
+ this . setCurrentIndex ( 0 ) ;
20
19
21
20
this . scrollView . __$callback = this . scrollView . __callback ;
22
21
this . scrollView . __callback = angular . bind ( this , this . renderScroll ) ;
@@ -34,12 +33,18 @@ function($rootScope, $timeout) {
34
33
this . scrollSize = function ( ) {
35
34
return this . scrollView . __clientHeight ;
36
35
} ;
37
- this . getSecondaryScrollSize = function ( ) {
36
+ this . secondaryScrollSize = function ( ) {
38
37
return this . scrollView . __clientWidth ;
39
38
} ;
40
39
this . transformString = function ( y , x ) {
41
40
return 'translate3d(' + x + 'px,' + y + 'px,0)' ;
42
41
} ;
42
+ this . primaryDimension = function ( dim ) {
43
+ return dim . height ;
44
+ } ;
45
+ this . secondaryDimension = function ( dim ) {
46
+ return dim . width ;
47
+ } ;
43
48
} else {
44
49
this . scrollView . options . getContentWidth = getViewportSize ;
45
50
@@ -52,12 +57,18 @@ function($rootScope, $timeout) {
52
57
this . scrollSize = function ( ) {
53
58
return this . scrollView . __clientWidth ;
54
59
} ;
55
- this . getSecondaryScrollSize = function ( ) {
60
+ this . secondaryScrollSize = function ( ) {
56
61
return this . scrollView . __clientHeight ;
57
62
} ;
58
63
this . transformString = function ( x , y ) {
59
64
return 'translate3d(' + x + 'px,' + y + 'px,0)' ;
60
65
} ;
66
+ this . primaryDimension = function ( dim ) {
67
+ return dim . width ;
68
+ } ;
69
+ this . secondaryDimension = function ( dim ) {
70
+ return dim . height ;
71
+ } ;
61
72
}
62
73
}
63
74
@@ -67,36 +78,41 @@ function($rootScope, $timeout) {
67
78
this . removeItem ( i ) ;
68
79
}
69
80
} ,
70
- resize : function ( ) {
81
+ calculateDimensions : function ( ) {
71
82
var primaryPos = 0 ;
72
83
var secondaryPos = 0 ;
73
- var itemsPerSpace = 0 ;
74
84
var len = this . dataSource . dimensions . length ;
75
- this . dimensions = this . dataSource . dimensions . map ( function ( dimensions , index ) {
85
+ var secondaryScrollSize = this . secondaryScrollSize ( ) ;
86
+ var previous ;
87
+
88
+ return this . dataSource . dimensions . map ( function ( dim ) {
76
89
var rect = {
77
- primarySize : this . isVertical ? dimensions . height : dimensions . width ,
78
- secondarySize : this . isVertical ? dimensions . width : dimensions . height ,
79
- primaryPos : primaryPos ,
80
- secondaryPos : secondaryPos
90
+ primarySize : this . primaryDimension ( dim ) ,
91
+ secondarySize : Math . min ( this . secondaryDimension ( dim ) , secondaryScrollSize )
81
92
} ;
82
93
83
- itemsPerSpace ++ ;
84
- secondaryPos += rect . secondarySize ;
85
- if ( secondaryPos >= this . getSecondaryScrollSize ( ) ) {
86
- secondaryPos = 0 ;
87
- primaryPos += rect . primarySize ;
88
-
89
- if ( ! this . itemsPerSpace ) {
90
- this . itemsPerSpace = itemsPerSpace ;
94
+ if ( previous ) {
95
+ secondaryPos += previous . secondarySize ;
96
+ if ( previous . primaryPos === primaryPos &&
97
+ secondaryPos + rect . secondarySize > secondaryScrollSize ) {
98
+ secondaryPos = 0 ;
99
+ primaryPos += previous . primarySize ;
100
+ } else {
91
101
}
92
102
}
93
103
104
+ rect . primaryPos = primaryPos ;
105
+ rect . secondaryPos = secondaryPos ;
106
+
107
+ previous = rect ;
94
108
return rect ;
95
109
} , this ) ;
96
-
97
- this . viewportSize = primaryPos ;
110
+ } ,
111
+ resize : function ( ) {
112
+ this . dimensions = this . calculateDimensions ( ) ;
113
+ var last = this . dimensions [ this . dimensions . length - 1 ] ;
114
+ this . viewportSize = last ? last . primaryPos + last . primarySize : 0 ;
98
115
this . setCurrentIndex ( 0 ) ;
99
- this . lastRenderScrollValue = 0 ;
100
116
this . render ( true ) ;
101
117
} ,
102
118
setCurrentIndex : function ( index , height ) {
@@ -129,40 +145,45 @@ function($rootScope, $timeout) {
129
145
} ,
130
146
getIndexForScrollValue : function ( i , scrollValue ) {
131
147
var rect ;
132
- //Scrolling down
148
+ //Scrolling up
133
149
if ( scrollValue <= this . dimensions [ i ] . primaryPos ) {
134
150
while ( ( rect = this . dimensions [ i - 1 ] ) && rect . primaryPos > scrollValue ) {
135
- i -= this . itemsPerSpace ;
151
+ i -- ;
136
152
}
137
- //Scrolling up
153
+ //Scrolling down
138
154
} else {
139
155
while ( ( rect = this . dimensions [ i + 1 ] ) && rect . primaryPos < scrollValue ) {
140
- i += this . itemsPerSpace ;
156
+ i ++ ;
141
157
}
142
158
}
143
159
return i ;
144
160
} ,
145
161
render : function ( shouldRedrawAll ) {
146
- if ( this . currentIndex >= this . dataSource . getLength ( ) ) {
147
- return ;
148
- }
149
-
150
162
var i ;
151
- if ( shouldRedrawAll ) {
163
+ if ( this . currentIndex >= this . dataSource . getLength ( ) || shouldRedrawAll ) {
152
164
for ( i in this . renderedItems ) {
153
165
this . removeItem ( i ) ;
154
166
}
167
+ if ( this . currentIndex >= this . dataSource . getLength ( ) ) return null ;
155
168
}
169
+
170
+ var rect ;
156
171
var scrollValue = this . scrollValue ( ) ;
157
172
var scrollDelta = scrollValue - this . lastRenderScrollValue ;
158
173
var scrollSize = this . scrollSize ( ) ;
159
174
var scrollSizeEnd = scrollSize + scrollValue ;
160
175
var startIndex = this . getIndexForScrollValue ( this . currentIndex , scrollValue ) ;
161
- var bufferStartIndex = Math . max ( 0 , startIndex - this . itemsPerSpace ) ;
176
+
177
+ //Make buffer start on previous row
178
+ var bufferStartIndex = Math . max ( startIndex - 1 , 0 ) ;
179
+ while ( bufferStartIndex > 0 &&
180
+ ( rect = this . dimensions [ bufferStartIndex ] ) &&
181
+ rect . primaryPos === this . dimensions [ startIndex - 1 ] . primaryPos ) {
182
+ bufferStartIndex -- ;
183
+ }
162
184
var startPos = this . dimensions [ bufferStartIndex ] . primaryPos ;
163
185
164
186
i = bufferStartIndex ;
165
- var rect ;
166
187
while ( ( rect = this . dimensions [ i ] ) && ( rect . primaryPos - rect . primarySize < scrollSizeEnd ) ) {
167
188
this . renderItem ( i , rect . primaryPos - startPos , rect . secondaryPos ) ;
168
189
i ++ ;
@@ -183,7 +204,6 @@ function($rootScope, $timeout) {
183
204
}
184
205
} ,
185
206
renderItem : function ( dataIndex , primaryPos , secondaryPos ) {
186
- var self = this ;
187
207
var item = this . dataSource . getItem ( dataIndex ) ;
188
208
if ( item ) {
189
209
this . dataSource . attachItem ( item ) ;
0 commit comments