Skip to content

Commit b6d9458

Browse files
committed
minIndex/maxIndex implementation
1 parent 8205c73 commit b6d9458

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

src/ui-scroll.coffee

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ angular.module('ui.scroll', [])
6969
wrapper.scope.$destroy()
7070
]
7171

72-
Buffer = (itemName, $scope, linker)->
72+
Buffer = (itemName, $scope, linker, datasource)->
7373

7474
buffer = Object.create Array.prototype
7575

@@ -116,15 +116,11 @@ angular.module('ui.scroll', [])
116116
buffer.first = 1
117117
buffer.next = 1
118118

119-
buffer.eof = false
120-
121-
buffer.bof = false
122-
123-
buffer.first = 1
124-
125-
buffer.next = 1
119+
buffer.clear()
126120

121+
buffer.minIndex = -> datasource.minIndex || 0
127122

123+
buffer.maxIndex = -> datasource.maxIndex || 0
128124

129125
buffer
130126

@@ -159,11 +155,11 @@ angular.module('ui.scroll', [])
159155

160156
topPadding = new Padding template
161157
element.before topPadding
162-
viewport.topPadding = -> topPadding.height.apply(topPadding, arguments)
158+
#viewport.topPadding = -> topPadding.height.apply(topPadding, arguments)
163159

164160
bottomPadding = new Padding template
165161
element.after bottomPadding
166-
viewport.bottomPadding = -> bottomPadding.height.apply(bottomPadding, arguments)
162+
#viewport.bottomPadding = -> bottomPadding.height.apply(bottomPadding, arguments)
167163

168164
viewport.bottomDataPos = ->
169165
(viewport[0].scrollHeight ? viewport[0].document.documentElement.scrollHeight) - bottomPadding.height()
@@ -203,7 +199,7 @@ angular.module('ui.scroll', [])
203199
overage++
204200

205201
if overage > 0
206-
viewport.bottomPadding(viewport.bottomPadding() + bottomHeight)
202+
#viewport.bottomPadding(viewport.bottomPadding() + bottomHeight)
207203
buffer.remove(buffer.length - overage, buffer.length)
208204
buffer.next -= overage
209205

@@ -227,10 +223,34 @@ angular.module('ui.scroll', [])
227223
break if newRow
228224
overage++
229225
if overage > 0
230-
viewport.topPadding(viewport.topPadding() + topHeight)
226+
#viewport.topPadding(viewport.topPadding() + topHeight)
231227
buffer.remove(0, overage)
232228
buffer.first += overage
233229

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+
234254
viewport
235255

236256
Adapter = ($attr, viewport, buffer, adjustBuffer) ->
@@ -304,10 +324,6 @@ angular.module('ui.scroll', [])
304324
setIsLoading viewportScope, value
305325

306326
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
311327
topHeight = 0
312328
for item in buffer
313329
itemTop = item.element.offset().top
@@ -359,7 +375,7 @@ angular.module('ui.scroll', [])
359375

360376
pending = []
361377

362-
buffer = new Buffer(itemName, $scope, linker)
378+
buffer = new Buffer(itemName, $scope, linker, datasource)
363379

364380
viewport = new Viewport(buffer, element, controllers, $attr.padding)
365381

@@ -395,8 +411,8 @@ angular.module('ui.scroll', [])
395411
reload = ->
396412
dismissPendingRequests()
397413
buffer.clear()
398-
viewport.topPadding(0)
399-
viewport.bottomPadding(0)
414+
#viewport.topPadding(0)
415+
#viewport.bottomPadding(0)
400416
adjustBuffer ridActual
401417

402418
adapter.reload = reload
@@ -450,23 +466,25 @@ angular.module('ui.scroll', [])
450466
promises = promises.concat (buffer.remove wrapper)
451467

452468
# 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)))
454470

455471
if toBePrepended.length
456472
bottomPos = viewport.bottomDataPos()
457473
for wrapper in toBePrepended
458474
keepFetching = insertWrapperContent(wrapper) || keepFetching
459475
wrapper.op = 'none'
460476

461-
heightIncrement = viewport.bottomDataPos() - bottomPos
477+
viewport.adjustPadding viewport.bottomDataPos() - bottomPos
462478

479+
###
463480
# adjust padding to prevent it from visually pushing everything down
464481
if viewport.topPadding() >= heightIncrement
465482
# if possible, reduce topPadding
466483
viewport.topPadding(viewport.topPadding() - heightIncrement)
467484
else
468485
# if not, increment scrollTop
469486
viewport.scrollTop(viewport.scrollTop() + heightIncrement)
487+
###
470488

471489
# re-index the buffer
472490
item.scope.$index = buffer.first + i for item,i in buffer
@@ -490,6 +508,7 @@ angular.module('ui.scroll', [])
490508
enqueueFetch(rid, false)
491509
if pending.length == 0
492510
adapter.calculateProperties()
511+
viewport.adjustPadding()
493512

494513
adjustBufferAfterFetch = (rid) ->
495514
# We need the item bindings to be processed before we can do adjustment
@@ -509,6 +528,7 @@ angular.module('ui.scroll', [])
509528
adapter.calculateProperties()
510529
else
511530
fetch(rid)
531+
viewport.adjustPadding()
512532

513533
fetch = (rid) ->
514534
#log "Running fetch... #{{true:'bottom', false: 'top'}[direction]} pending #{pending.length}"
@@ -522,7 +542,7 @@ angular.module('ui.scroll', [])
522542
return if (rid and rid isnt ridActual) or $scope.$$destroyed
523543
if result.length < bufferSize
524544
buffer.eof = true
525-
viewport.bottomPadding(0)
545+
#viewport.bottomPadding(0)
526546
#log 'eof is reached'
527547
if result.length > 0
528548
viewport.clipTop()
@@ -545,7 +565,7 @@ angular.module('ui.scroll', [])
545565
return if (rid and rid isnt ridActual) or $scope.$$destroyed
546566
if result.length < bufferSize
547567
buffer.bof = true
548-
viewport.topPadding(0)
568+
#viewport.topPadding(0)
549569
#log 'bof is reached'
550570
if result.length > 0
551571
viewport.clipBottom() if buffer.length

0 commit comments

Comments
 (0)