@@ -39,9 +39,10 @@ angular.module('ui.scroll', [])
39
39
'$injector' ,
40
40
'$rootScope' ,
41
41
'$timeout' ,
42
+ '$interval' ,
42
43
'$q' ,
43
44
'$parse' ,
44
- function ( console , $injector , $rootScope , $timeout , $q , $parse ) {
45
+ function ( console , $injector , $rootScope , $timeout , $interval , $ q, $parse ) {
45
46
46
47
return {
47
48
require : [ '?^uiScrollViewport' ] ,
@@ -59,14 +60,16 @@ angular.module('ui.scroll', [])
59
60
}
60
61
61
62
function parseNumericAttr ( value , defaultValue ) {
62
- let result = $parse ( value ) ( $scope ) ;
63
+ const result = $parse ( value ) ( $scope ) ;
63
64
return isNaN ( result ) ? defaultValue : result ;
64
65
}
65
66
66
67
const BUFFER_MIN = 3 ;
67
68
const BUFFER_DEFAULT = 10 ;
68
69
const PADDING_MIN = 0.3 ;
69
70
const PADDING_DEFAULT = 0.5 ;
71
+ const MAX_VIEWPORT_DELAY = 500 ;
72
+ const VIEWPORT_POLLING_INTERVAL = 50 ;
70
73
71
74
let datasource = null ;
72
75
const itemName = match [ 1 ] ;
@@ -78,16 +81,16 @@ angular.module('ui.scroll', [])
78
81
let ridActual = 0 ; // current data revision id
79
82
let pending = [ ] ;
80
83
81
- let elementRoutines = new ElementRoutines ( $injector , $q ) ;
82
- let buffer = new ScrollBuffer ( elementRoutines , bufferSize ) ;
83
- let viewport = new Viewport ( elementRoutines , buffer , element , viewportController , $rootScope , padding ) ;
84
- let adapter = new Adapter ( viewport , buffer , adjustBuffer , reload , $attr , $parse , $scope ) ;
84
+ const elementRoutines = new ElementRoutines ( $injector , $q ) ;
85
+ const buffer = new ScrollBuffer ( elementRoutines , bufferSize ) ;
86
+ const viewport = new Viewport ( elementRoutines , buffer , element , viewportController , $rootScope , padding ) ;
87
+ const adapter = new Adapter ( viewport , buffer , adjustBuffer , reload , $attr , $parse , $scope ) ;
85
88
86
89
if ( viewportController ) {
87
90
viewportController . adapter = adapter ;
88
91
}
89
92
90
- let isDatasourceValid = ( ) => angular . isObject ( datasource ) && angular . isFunction ( datasource . get ) ;
93
+ const isDatasourceValid = ( ) => angular . isObject ( datasource ) && angular . isFunction ( datasource . get ) ;
91
94
datasource = $parse ( datasourceName ) ( $scope ) ; // try to get datasource on scope
92
95
if ( ! isDatasourceValid ( ) ) {
93
96
datasource = $injector . get ( datasourceName ) ; // try to inject datasource as service
@@ -114,7 +117,7 @@ angular.module('ui.scroll', [])
114
117
}
115
118
116
119
function defineIndexProperty ( datasource , propName , propUserName ) {
117
- let descriptor = Object . getOwnPropertyDescriptor ( datasource , propName ) ;
120
+ const descriptor = Object . getOwnPropertyDescriptor ( datasource , propName ) ;
118
121
if ( descriptor && ( descriptor . set || descriptor . get ) ) {
119
122
return ;
120
123
}
@@ -124,7 +127,7 @@ angular.module('ui.scroll', [])
124
127
set : ( value ) => {
125
128
getter = value ;
126
129
buffer [ propUserName ] = value ;
127
- let topPaddingHeightOld = viewport . topDataPos ( ) ;
130
+ const topPaddingHeightOld = viewport . topDataPos ( ) ;
128
131
viewport . adjustPaddings ( ) ;
129
132
if ( propName === 'minIndex' ) {
130
133
viewport . onAfterMinIndexSet ( topPaddingHeightOld ) ;
@@ -157,6 +160,26 @@ angular.module('ui.scroll', [])
157
160
} , success ) ;
158
161
} ;
159
162
163
+ const run = ( ) => {
164
+ let tryCount = 0 ;
165
+ if ( ! viewport . applyContainerStyle ( ) ) {
166
+ const timer = $interval ( ( ) => {
167
+ tryCount ++ ;
168
+ if ( viewport . applyContainerStyle ( ) ) {
169
+ $interval . cancel ( timer ) ;
170
+ reload ( ) ;
171
+ }
172
+ if ( tryCount * VIEWPORT_POLLING_INTERVAL >= MAX_VIEWPORT_DELAY ) {
173
+ $interval . cancel ( timer ) ;
174
+ throw Error ( `ui-scroll directive requires a viewport with non-zero height in ${ MAX_VIEWPORT_DELAY } ms` ) ;
175
+ }
176
+ } , VIEWPORT_POLLING_INTERVAL ) ;
177
+ }
178
+ else {
179
+ reload ( ) ;
180
+ }
181
+ } ;
182
+
160
183
/**
161
184
* Build padding elements
162
185
*
@@ -180,10 +203,7 @@ angular.module('ui.scroll', [])
180
203
181
204
viewport . bind ( 'mousewheel' , wheelHandler ) ;
182
205
183
- $timeout ( ( ) => {
184
- viewport . applyContainerStyle ( ) ;
185
- reload ( ) ;
186
- } ) ;
206
+ run ( ) ;
187
207
188
208
/* Private function definitions */
189
209
@@ -239,7 +259,7 @@ angular.module('ui.scroll', [])
239
259
240
260
function createElement ( wrapper , insertAfter , insertElement ) {
241
261
let promises = null ;
242
- let sibling = ( insertAfter > 0 ) ? buffer [ insertAfter - 1 ] . element : undefined ;
262
+ const sibling = ( insertAfter > 0 ) ? buffer [ insertAfter - 1 ] . element : undefined ;
243
263
linker ( ( clone , scope ) => {
244
264
promises = insertElement ( clone , sibling ) ;
245
265
wrapper . element = clone ;
@@ -248,7 +268,7 @@ angular.module('ui.scroll', [])
248
268
} ) ;
249
269
// ui-scroll-grid apply
250
270
if ( adapter . transform ) {
251
- let tdInitializer = wrapper . scope . uiScrollTdInitializer ;
271
+ const tdInitializer = wrapper . scope . uiScrollTdInitializer ;
252
272
if ( tdInitializer && tdInitializer . linking ) {
253
273
adapter . transform ( wrapper . scope , wrapper . element ) ;
254
274
} else {
@@ -459,8 +479,8 @@ angular.module('ui.scroll', [])
459
479
460
480
function wheelHandler ( event ) {
461
481
if ( ! adapter . disabled ) {
462
- let scrollTop = viewport [ 0 ] . scrollTop ;
463
- let yMax = viewport [ 0 ] . scrollHeight - viewport [ 0 ] . clientHeight ;
482
+ const scrollTop = viewport [ 0 ] . scrollTop ;
483
+ const yMax = viewport [ 0 ] . scrollHeight - viewport [ 0 ] . clientHeight ;
464
484
465
485
if ( ( scrollTop === 0 && ! buffer . bof ) || ( scrollTop === yMax && ! buffer . eof ) ) {
466
486
event . preventDefault ( ) ;
0 commit comments