@@ -233,9 +233,12 @@ angular.module('ui.scroll', [])
233
233
234
234
viewport
235
235
236
- Adapter = ($attr , viewportScope , buffer , adjustBuffer ) ->
236
+ Adapter = ($attr , viewport , buffer , adjustBuffer ) ->
237
+
237
238
this .isLoading = false
238
239
240
+ viewportScope = viewport .scope () || $rootScope
241
+
239
242
applyUpdate = (wrapper , newItems ) ->
240
243
if angular .isArray newItems
241
244
pos = (buffer .indexOf wrapper) + 1
@@ -291,14 +294,6 @@ angular.module('ui.scroll', [])
291
294
else
292
295
setTopVisibleScope = ->
293
296
294
- this .setTopVisible = (item ) ->
295
- this .topVisible = item .item
296
- this .topVisibleElement = item .element
297
- this .topVisibleScope = item .scope
298
- setTopVisible (viewportScope, item .item )
299
- setTopVisibleElement (viewportScope, item .element )
300
- setTopVisibleScope (viewportScope, item .scope )
301
-
302
297
if $attr .isLoading
303
298
setIsLoading = $parse ($attr .isLoading ).assign
304
299
else
@@ -308,6 +303,30 @@ angular.module('ui.scroll', [])
308
303
this .isLoading = value
309
304
setIsLoading viewportScope, value
310
305
306
+ 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
+ topHeight = 0
312
+ for item in buffer
313
+ itemTop = item .element .offset ().top
314
+ newRow = rowTop isnt itemTop
315
+ rowTop = itemTop
316
+ itemHeight = item .element .outerHeight (true ) if newRow
317
+ if newRow and (viewport .topDataPos () + topHeight + itemHeight < viewport .topVisiblePos ())
318
+ topHeight += itemHeight
319
+ else
320
+ if newRow
321
+ this .topVisible = item .item
322
+ this .topVisibleElement = item .element
323
+ this .topVisibleScope = item .scope
324
+ setTopVisible (viewportScope, item .item )
325
+ setTopVisibleElement (viewportScope, item .element )
326
+ setTopVisibleScope (viewportScope, item .scope )
327
+ break
328
+
329
+
311
330
return
312
331
313
332
require : [' ?^uiScrollViewport' ]
@@ -344,7 +363,7 @@ angular.module('ui.scroll', [])
344
363
345
364
viewport = new Viewport (buffer, element, controllers, $attr .padding )
346
365
347
- adapter = new Adapter $attr, viewport . scope () || $rootScope , buffer,
366
+ adapter = new Adapter $attr, viewport, buffer,
348
367
->
349
368
dismissPendingRequests ()
350
369
adjustBuffer ridActual
@@ -357,33 +376,13 @@ angular.module('ui.scroll', [])
357
376
angular .extend (adapterOnScope, adapter)
358
377
adapter = adapterOnScope
359
378
360
- calculateProperties = ->
361
- if (buffer .length > 0 )
362
- buffer .elementHeight = (buffer[buffer .length - 1 ].element .offset ().top +
363
- buffer[buffer .length - 1 ].element .outerHeight (true ) -
364
- buffer[0 ].element .offset ().top ) / buffer .length
365
- topHeight = 0
366
- for item in buffer
367
- itemTop = item .element .offset ().top
368
- newRow = rowTop isnt itemTop
369
- rowTop = itemTop
370
- itemHeight = item .element .outerHeight (true ) if newRow
371
- if newRow and (viewport .topDataPos () + topHeight + itemHeight < viewport .topVisiblePos ())
372
- topHeight += itemHeight
373
- else
374
- adapter .setTopVisible (item) if newRow
375
- break
376
- console .log " min=#{ datasource .minIndex } max=#{ datasource .maxIndex } "
377
-
378
379
# Build padding elements
379
380
#
380
381
# Calling linker is the only way I found to get access to the tag name of the template
381
382
# to prevent the directive scope from pollution a new scope is created and destroyed
382
383
# right after the builder creation is completed
383
384
linker $scope .$new (), (template , scope ) ->
384
-
385
385
viewport .createPaddingElements (template[0 ])
386
-
387
386
# Destroy template's scope to remove any watchers on it.
388
387
scope .$destroy ()
389
388
# also remove the template when the directive scope is destroyed
@@ -481,28 +480,21 @@ angular.module('ui.scroll', [])
481
480
keepFetching
482
481
483
482
adjustBuffer = (rid ) ->
484
-
485
483
# We need the item bindings to be processed before we can do adjustment
486
484
$timeout ->
487
-
488
485
processBufferedItems (rid)
489
-
490
486
if viewport .shouldLoadBottom ()
491
487
enqueueFetch (rid, true )
492
488
else
493
489
if viewport .shouldLoadTop ()
494
490
enqueueFetch (rid, false )
495
-
496
491
if pending .length == 0
497
- calculateProperties ()
492
+ adapter . calculateProperties ()
498
493
499
494
adjustBufferAfterFetch = (rid ) ->
500
-
501
495
# We need the item bindings to be processed before we can do adjustment
502
496
$timeout ->
503
-
504
497
keepFetching = processBufferedItems (rid)
505
-
506
498
if viewport .shouldLoadBottom ()
507
499
# keepFetching = true means that at least one item app/prepended in the last batch had height > 0
508
500
enqueueFetch (rid, true ) if keepFetching
@@ -511,11 +503,10 @@ angular.module('ui.scroll', [])
511
503
# pending[0] = true means that previous fetch was appending. We need to force at least one prepend
512
504
# BTW there will always be at least 1 element in the pending array because bottom is fetched first
513
505
enqueueFetch (rid, false ) if keepFetching || pending[0 ]
514
-
515
506
pending .shift ()
516
507
if pending .length == 0
517
508
adapter .loading (false )
518
- calculateProperties ()
509
+ adapter . calculateProperties ()
519
510
else
520
511
fetch (rid)
521
512
0 commit comments