@@ -69,7 +69,7 @@ angular.module('ui.scroll', [])
69
69
wrapper .scope .$destroy ()
70
70
]
71
71
72
- Buffer = (itemName , $scope , linker )->
72
+ Buffer = (itemName , $scope , linker , datasource )->
73
73
74
74
buffer = Object .create Array .prototype
75
75
@@ -116,15 +116,11 @@ angular.module('ui.scroll', [])
116
116
buffer .first = 1
117
117
buffer .next = 1
118
118
119
- buffer .eof = false
120
-
121
- buffer .bof = false
122
-
123
- buffer .first = 1
124
-
125
- buffer .next = 1
119
+ buffer .clear ()
126
120
121
+ buffer .minIndex = -> datasource .minIndex || 0
127
122
123
+ buffer .maxIndex = -> datasource .maxIndex || 0
128
124
129
125
buffer
130
126
@@ -159,11 +155,11 @@ angular.module('ui.scroll', [])
159
155
160
156
topPadding = new Padding template
161
157
element .before topPadding
162
- viewport .topPadding = -> topPadding .height .apply (topPadding, arguments )
158
+ # viewport.topPadding = -> topPadding.height.apply(topPadding, arguments)
163
159
164
160
bottomPadding = new Padding template
165
161
element .after bottomPadding
166
- viewport .bottomPadding = -> bottomPadding .height .apply (bottomPadding, arguments )
162
+ # viewport.bottomPadding = -> bottomPadding.height.apply(bottomPadding, arguments)
167
163
168
164
viewport .bottomDataPos = ->
169
165
(viewport[0 ].scrollHeight ? viewport[0 ].document .documentElement .scrollHeight ) - bottomPadding .height ()
@@ -203,7 +199,7 @@ angular.module('ui.scroll', [])
203
199
overage++
204
200
205
201
if overage > 0
206
- viewport .bottomPadding (viewport .bottomPadding () + bottomHeight)
202
+ # viewport.bottomPadding(viewport.bottomPadding() + bottomHeight)
207
203
buffer .remove (buffer .length - overage, buffer .length )
208
204
buffer .next -= overage
209
205
@@ -227,10 +223,34 @@ angular.module('ui.scroll', [])
227
223
break if newRow
228
224
overage++
229
225
if overage > 0
230
- viewport .topPadding (viewport .topPadding () + topHeight)
226
+ # viewport.topPadding(viewport.topPadding() + topHeight)
231
227
buffer .remove (0 , overage)
232
228
buffer .first += overage
233
229
230
+ viewport .adjustPadding = (heightIncrement ) ->
231
+ if (buffer .length > 0 )
232
+ elementHeight = (buffer[buffer .length - 1 ].element .offset ().top +
233
+ buffer[buffer .length - 1 ].element .outerHeight (true ) -
234
+ buffer[0 ].element .offset ().top ) / buffer .length
235
+ topPaddingHeight = (buffer .first - buffer .minIndex ()) * elementHeight
236
+ # if !buffer.bof
237
+ # topPaddingHeight += bufferPadding()
238
+ topPadding .height topPaddingHeight
239
+ bottomPaddingHeight = (buffer .maxIndex () - buffer .next ) * elementHeight
240
+ if ! buffer .eof
241
+ bottomPaddingHeight += bufferPadding ()
242
+ bottomPadding .height bottomPaddingHeight
243
+ console .log " top=#{ topPaddingHeight} bottom=#{ bottomPaddingHeight} "
244
+ if (heightIncrement)
245
+ # adjust padding to prevent it from visually pushing everything down
246
+ if topPadding .height () >= heightIncrement
247
+ # if possible, reduce topPadding
248
+ topPadding .height (topPadding .height () - heightIncrement)
249
+ else
250
+ # if not, increment scrollTop
251
+ viewport .scrollTop (viewport .scrollTop () + heightIncrement)
252
+
253
+
234
254
viewport
235
255
236
256
Adapter = ($attr , viewport , buffer , adjustBuffer ) ->
@@ -304,10 +324,6 @@ angular.module('ui.scroll', [])
304
324
setIsLoading viewportScope, value
305
325
306
326
this .calculateProperties = ->
307
- if (buffer .length > 0 )
308
- buffer .elementHeight = (buffer[buffer .length - 1 ].element .offset ().top +
309
- buffer[buffer .length - 1 ].element .outerHeight (true ) -
310
- buffer[0 ].element .offset ().top ) / buffer .length
311
327
topHeight = 0
312
328
for item in buffer
313
329
itemTop = item .element .offset ().top
@@ -359,7 +375,7 @@ angular.module('ui.scroll', [])
359
375
360
376
pending = []
361
377
362
- buffer = new Buffer (itemName, $scope, linker)
378
+ buffer = new Buffer (itemName, $scope, linker, datasource )
363
379
364
380
viewport = new Viewport (buffer, element, controllers, $attr .padding )
365
381
@@ -395,8 +411,8 @@ angular.module('ui.scroll', [])
395
411
reload = ->
396
412
dismissPendingRequests ()
397
413
buffer .clear ()
398
- viewport .topPadding (0 )
399
- viewport .bottomPadding (0 )
414
+ # viewport.topPadding(0)
415
+ # viewport.bottomPadding(0)
400
416
adjustBuffer ridActual
401
417
402
418
adapter .reload = reload
@@ -450,23 +466,25 @@ angular.module('ui.scroll', [])
450
466
promises = promises .concat (buffer .remove wrapper)
451
467
452
468
# for anything other than prepend adjust the bottomPadding height
453
- viewport .bottomPadding (Math .max (0 ,viewport .bottomPadding () - (viewport .bottomDataPos () - bottomPos)))
469
+ # viewport.bottomPadding(Math.max(0,viewport.bottomPadding() - (viewport.bottomDataPos() - bottomPos)))
454
470
455
471
if toBePrepended .length
456
472
bottomPos = viewport .bottomDataPos ()
457
473
for wrapper in toBePrepended
458
474
keepFetching = insertWrapperContent (wrapper) || keepFetching
459
475
wrapper .op = ' none'
460
476
461
- heightIncrement = viewport .bottomDataPos () - bottomPos
477
+ viewport . adjustPadding viewport .bottomDataPos () - bottomPos
462
478
479
+ ###
463
480
# adjust padding to prevent it from visually pushing everything down
464
481
if viewport.topPadding() >= heightIncrement
465
482
# if possible, reduce topPadding
466
483
viewport.topPadding(viewport.topPadding() - heightIncrement)
467
484
else
468
485
# if not, increment scrollTop
469
486
viewport.scrollTop(viewport.scrollTop() + heightIncrement)
487
+ ###
470
488
471
489
# re-index the buffer
472
490
item .scope .$index = buffer .first + i for item,i in buffer
@@ -490,6 +508,7 @@ angular.module('ui.scroll', [])
490
508
enqueueFetch (rid, false )
491
509
if pending .length == 0
492
510
adapter .calculateProperties ()
511
+ viewport .adjustPadding ()
493
512
494
513
adjustBufferAfterFetch = (rid ) ->
495
514
# We need the item bindings to be processed before we can do adjustment
@@ -509,6 +528,7 @@ angular.module('ui.scroll', [])
509
528
adapter .calculateProperties ()
510
529
else
511
530
fetch (rid)
531
+ viewport .adjustPadding ()
512
532
513
533
fetch = (rid ) ->
514
534
# log "Running fetch... #{{true:'bottom', false: 'top'}[direction]} pending #{pending.length}"
@@ -522,7 +542,7 @@ angular.module('ui.scroll', [])
522
542
return if (rid and rid isnt ridActual) or $scope .$$destroyed
523
543
if result .length < bufferSize
524
544
buffer .eof = true
525
- viewport .bottomPadding (0 )
545
+ # viewport.bottomPadding(0)
526
546
# log 'eof is reached'
527
547
if result .length > 0
528
548
viewport .clipTop ()
@@ -545,7 +565,7 @@ angular.module('ui.scroll', [])
545
565
return if (rid and rid isnt ridActual) or $scope .$$destroyed
546
566
if result .length < bufferSize
547
567
buffer .bof = true
548
- viewport .topPadding (0 )
568
+ # viewport.topPadding(0)
549
569
# log 'bof is reached'
550
570
if result .length > 0
551
571
viewport .clipBottom () if buffer .length
0 commit comments