Skip to content

Commit 3409732

Browse files
committed
SetTopVisible properties moved to the adapter
1 parent 04d21d9 commit 3409732

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

src/ui-scroll.coffee

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ angular.module('ui.scroll', [])
231231

232232
viewport
233233

234-
Adapter = (buffer, adjustBuffer) ->
234+
Adapter = ($attr, viewportScope, buffer, adjustBuffer) ->
235235
this.isLoading = false
236236

237237
applyUpdate = (wrapper, newItems) ->
@@ -274,6 +274,29 @@ angular.module('ui.scroll', [])
274274
buffer.insert 'prepend', item
275275
adjustBuffer()
276276

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+
277300
return
278301

279302
require: ['?^uiScrollViewport']
@@ -312,7 +335,7 @@ angular.module('ui.scroll', [])
312335

313336
viewport = new Viewport(buffer, element, controllers, $attr.padding)
314337

315-
adapter = new Adapter buffer,
338+
adapter = new Adapter $attr, viewport.scope() || $rootScope, buffer,
316339
->
317340
dismissPendingRequests()
318341
adjustBuffer ridActual
@@ -324,6 +347,35 @@ angular.module('ui.scroll', [])
324347
adapterOnScope = $parse($attr.adapter)($scope)
325348
angular.extend(adapterOnScope, adapter)
326349
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
327379

328380
# Padding element builder
329381
#
@@ -339,17 +391,6 @@ angular.module('ui.scroll', [])
339391
# also remove the template when the directive scope is destroyed
340392
$scope.$on '$destroy', -> template.remove()
341393

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-
353394
loading = (value) ->
354395
adapter.isLoading = value
355396
$parse($attr.isLoading).assign($scope, value) if $attr.isLoading
@@ -446,19 +487,6 @@ angular.module('ui.scroll', [])
446487

447488
keepFetching
448489

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-
462490
adjustBuffer = (rid) ->
463491

464492
# We need the item bindings to be processed before we can do adjustment
@@ -473,7 +501,7 @@ angular.module('ui.scroll', [])
473501
enqueueFetch(rid, false)
474502

475503
if pending.length == 0
476-
calculateTopProperties()
504+
calculateProperties()
477505

478506
adjustBufferAfterFetch = (rid) ->
479507

@@ -494,7 +522,7 @@ angular.module('ui.scroll', [])
494522
pending.shift()
495523
if pending.length == 0
496524
loading(false)
497-
calculateTopProperties()
525+
calculateProperties()
498526
else
499527
fetch(rid)
500528

0 commit comments

Comments
 (0)