@@ -231,7 +231,7 @@ angular.module('ui.scroll', [])
231
231
232
232
viewport
233
233
234
- Adapter = (buffer , adjustBuffer ) ->
234
+ Adapter = ($attr , viewportScope , buffer , adjustBuffer ) ->
235
235
this .isLoading = false
236
236
237
237
applyUpdate = (wrapper , newItems ) ->
@@ -274,6 +274,29 @@ angular.module('ui.scroll', [])
274
274
buffer .insert ' prepend' , item
275
275
adjustBuffer ()
276
276
277
+ if $attr .topVisible
278
+ setTopVisible = $parse ($attr .topVisible ).assign
279
+ else
280
+ setTopVisible = ->
281
+
282
+ if $attr .topVisibleElement
283
+ setTopVisibleElement = $parse ($attr .topVisibleElement ).assign
284
+ else
285
+ setTopVisibleElement = ->
286
+
287
+ if $attr .topVisibleScope
288
+ setTopVisibleScope = $parse ($attr .topVisibleScope ).assign
289
+ else
290
+ setTopVisibleScope = ->
291
+
292
+ this .setTopVisible = (item ) ->
293
+ this .topVisible = item .item
294
+ this .topVisibleElement = item .element
295
+ this .topVisibleScope = item .scope
296
+ setTopVisible (viewportScope, item .item )
297
+ setTopVisibleElement (viewportScope, item .element )
298
+ setTopVisibleScope (viewportScope, item .scope )
299
+
277
300
return
278
301
279
302
require : [' ?^uiScrollViewport' ]
@@ -312,7 +335,7 @@ angular.module('ui.scroll', [])
312
335
313
336
viewport = new Viewport (buffer, element, controllers, $attr .padding )
314
337
315
- adapter = new Adapter buffer,
338
+ adapter = new Adapter $attr, viewport . scope () || $rootScope, buffer,
316
339
->
317
340
dismissPendingRequests ()
318
341
adjustBuffer ridActual
@@ -324,6 +347,35 @@ angular.module('ui.scroll', [])
324
347
adapterOnScope = $parse ($attr .adapter )($scope)
325
348
angular .extend (adapterOnScope, adapter)
326
349
adapter = adapterOnScope
350
+ ###
351
+ setTopVisible = (item) ->
352
+
353
+ viewportScope = viewport.scope() || $rootScope
354
+
355
+ adapter.topVisible = item.item
356
+ adapter.topVisibleElement = item.element
357
+ adapter.topVisibleScope = item.scope
358
+ $parse($attr.topVisible).assign(viewportScope, adapter.topVisible) if $attr.topVisible
359
+ $parse($attr.topVisibleElement).assign(viewportScope, adapter.topVisibleElement) if $attr.topVisibleElement
360
+ $parse($attr.topVisibleScope).assign(viewportScope, adapter.topVisibleScope) if $attr.topVisibleScope
361
+ datasource.topVisible(item) if angular.isFunction(datasource.topVisible)
362
+ ###
363
+ calculateProperties = ->
364
+ if (buffer .length > 0 )
365
+ buffer .elementHeight = (buffer[buffer .length - 1 ].element .offset ().top +
366
+ buffer[buffer .length - 1 ].element .outerHeight (true ) -
367
+ buffer[0 ].element .offset ().top ) / buffer .length
368
+ topHeight = 0
369
+ for item in buffer
370
+ itemTop = item .element .offset ().top
371
+ newRow = rowTop isnt itemTop
372
+ rowTop = itemTop
373
+ itemHeight = item .element .outerHeight (true ) if newRow
374
+ if newRow and (viewport .topDataPos () + topHeight + itemHeight < viewport .topVisiblePos ())
375
+ topHeight += itemHeight
376
+ else
377
+ adapter .setTopVisible (item) if newRow
378
+ break
327
379
328
380
# Padding element builder
329
381
#
@@ -339,17 +391,6 @@ angular.module('ui.scroll', [])
339
391
# also remove the template when the directive scope is destroyed
340
392
$scope .$on ' $destroy' , -> template .remove ()
341
393
342
- viewportScope = viewport .scope () || $rootScope
343
-
344
- topVisible = (item ) ->
345
- adapter .topVisible = item .item
346
- adapter .topVisibleElement = item .element
347
- adapter .topVisibleScope = item .scope
348
- $parse ($attr .topVisible ).assign (viewportScope, adapter .topVisible ) if $attr .topVisible
349
- $parse ($attr .topVisibleElement ).assign (viewportScope, adapter .topVisibleElement ) if $attr .topVisibleElement
350
- $parse ($attr .topVisibleScope ).assign (viewportScope, adapter .topVisibleScope ) if $attr .topVisibleScope
351
- datasource .topVisible (item) if angular .isFunction (datasource .topVisible )
352
-
353
394
loading = (value ) ->
354
395
adapter .isLoading = value
355
396
$parse ($attr .isLoading ).assign ($scope, value) if $attr .isLoading
@@ -446,19 +487,6 @@ angular.module('ui.scroll', [])
446
487
447
488
keepFetching
448
489
449
- calculateTopProperties = ->
450
- topHeight = 0
451
- for item in buffer
452
- itemTop = item .element .offset ().top
453
- newRow = rowTop isnt itemTop
454
- rowTop = itemTop
455
- itemHeight = item .element .outerHeight (true ) if newRow
456
- if newRow and (viewport .topDataPos () + topHeight + itemHeight < viewport .topVisiblePos ())
457
- topHeight += itemHeight
458
- else
459
- topVisible (item) if newRow
460
- break
461
-
462
490
adjustBuffer = (rid ) ->
463
491
464
492
# We need the item bindings to be processed before we can do adjustment
@@ -473,7 +501,7 @@ angular.module('ui.scroll', [])
473
501
enqueueFetch (rid, false )
474
502
475
503
if pending .length == 0
476
- calculateTopProperties ()
504
+ calculateProperties ()
477
505
478
506
adjustBufferAfterFetch = (rid ) ->
479
507
@@ -494,7 +522,7 @@ angular.module('ui.scroll', [])
494
522
pending .shift ()
495
523
if pending .length == 0
496
524
loading (false )
497
- calculateTopProperties ()
525
+ calculateProperties ()
498
526
else
499
527
fetch (rid)
500
528
0 commit comments