Skip to content

Commit 8205c73

Browse files
committed
calculate properties -> Adapter method
1 parent 812af28 commit 8205c73

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

src/ui-scroll.coffee

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,12 @@ angular.module('ui.scroll', [])
233233

234234
viewport
235235

236-
Adapter = ($attr, viewportScope, buffer, adjustBuffer) ->
236+
Adapter = ($attr, viewport, buffer, adjustBuffer) ->
237+
237238
this.isLoading = false
238239

240+
viewportScope = viewport.scope() || $rootScope
241+
239242
applyUpdate = (wrapper, newItems) ->
240243
if angular.isArray newItems
241244
pos = (buffer.indexOf wrapper) + 1
@@ -291,14 +294,6 @@ angular.module('ui.scroll', [])
291294
else
292295
setTopVisibleScope = ->
293296

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-
302297
if $attr.isLoading
303298
setIsLoading = $parse($attr.isLoading).assign
304299
else
@@ -308,6 +303,30 @@ angular.module('ui.scroll', [])
308303
this.isLoading = value
309304
setIsLoading viewportScope, value
310305

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+
311330
return
312331

313332
require: ['?^uiScrollViewport']
@@ -344,7 +363,7 @@ angular.module('ui.scroll', [])
344363

345364
viewport = new Viewport(buffer, element, controllers, $attr.padding)
346365

347-
adapter = new Adapter $attr, viewport.scope() || $rootScope, buffer,
366+
adapter = new Adapter $attr, viewport, buffer,
348367
->
349368
dismissPendingRequests()
350369
adjustBuffer ridActual
@@ -357,33 +376,13 @@ angular.module('ui.scroll', [])
357376
angular.extend(adapterOnScope, adapter)
358377
adapter = adapterOnScope
359378

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-
378379
# Build padding elements
379380
#
380381
# Calling linker is the only way I found to get access to the tag name of the template
381382
# to prevent the directive scope from pollution a new scope is created and destroyed
382383
# right after the builder creation is completed
383384
linker $scope.$new(), (template, scope) ->
384-
385385
viewport.createPaddingElements(template[0])
386-
387386
# Destroy template's scope to remove any watchers on it.
388387
scope.$destroy()
389388
# also remove the template when the directive scope is destroyed
@@ -481,28 +480,21 @@ angular.module('ui.scroll', [])
481480
keepFetching
482481

483482
adjustBuffer = (rid) ->
484-
485483
# We need the item bindings to be processed before we can do adjustment
486484
$timeout ->
487-
488485
processBufferedItems(rid)
489-
490486
if viewport.shouldLoadBottom()
491487
enqueueFetch(rid, true)
492488
else
493489
if viewport.shouldLoadTop()
494490
enqueueFetch(rid, false)
495-
496491
if pending.length == 0
497-
calculateProperties()
492+
adapter.calculateProperties()
498493

499494
adjustBufferAfterFetch = (rid) ->
500-
501495
# We need the item bindings to be processed before we can do adjustment
502496
$timeout ->
503-
504497
keepFetching = processBufferedItems(rid)
505-
506498
if viewport.shouldLoadBottom()
507499
# keepFetching = true means that at least one item app/prepended in the last batch had height > 0
508500
enqueueFetch(rid, true) if keepFetching
@@ -511,11 +503,10 @@ angular.module('ui.scroll', [])
511503
# pending[0] = true means that previous fetch was appending. We need to force at least one prepend
512504
# BTW there will always be at least 1 element in the pending array because bottom is fetched first
513505
enqueueFetch(rid, false) if keepFetching || pending[0]
514-
515506
pending.shift()
516507
if pending.length == 0
517508
adapter.loading(false)
518-
calculateProperties()
509+
adapter.calculateProperties()
519510
else
520511
fetch(rid)
521512

0 commit comments

Comments
 (0)