Skip to content

Commit bc896a2

Browse files
committed
moved buffer manipulation methods to the Buffer object
1 parent 88088ec commit bc896a2

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

demo/examples/animation.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ angular.module('application', ['ui.scroll', 'ui.scroll.jqlite', 'ngAnimate']).co
3535
if scope.$index % 2 is 0
3636
return []
3737

38+
$scope.refresh = ->
39+
$scope.adapterContainer.adapter.reload()
40+
3841
idList = 1000
3942

4043
$scope.addToList = ->

demo/examples/animation.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ <h2>
5555
<button ng-click="updateList()">update this list</button>
5656
<button ng-click="addToList()">add new 3d item</button>
5757
<button ng-click="removeFromList()">remove even items</button>
58+
<button ng-click="refresh()">refresh</button>
5859
</td>
5960
</tr>
6061
</table>

src/ui-scroll.coffee

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -55,63 +55,86 @@ angular.module('ui.scroll', [])
5555
bufferPadding = -> viewport.outerHeight() * Math.max(0.1, +$attr.padding || 0.1) # some extra space to initiate preload
5656
scrollHeight = (elem) -> elem[0].scrollHeight ? elem[0].document.documentElement.scrollHeight
5757

58-
# initial settings
59-
60-
builder = null
61-
ridActual = 0 # current data revision id
62-
first = 1
63-
next = 1
64-
buffer = []
65-
pending = []
66-
eof = false
67-
bof = false
58+
isAngularVersionLessThen1_3 = angular.version.major == 1 and angular.version.minor < 3
6859

6960
# Element manipulation routines
7061

71-
isAngularVersionLessThen1_3 = angular.version.major == 1 and angular.version.minor < 3
62+
insertElement =
63+
(newElement, previousElement) ->
64+
element.after.apply(previousElement, [newElement])
65+
[]
7266

73-
removeItem =
67+
insertElementAnimated =
7468
if $animate
7569
if isAngularVersionLessThen1_3
76-
(wrapper) ->
70+
(newElement, previousElement) ->
71+
deferred = $q.defer()
72+
$animate.enter newElement, element, previousElement, -> deferred.resolve()
73+
[deferred.promise]
74+
else
75+
(newElement, previousElement) ->
76+
[$animate.enter newElement, element, previousElement]
77+
78+
else insertElement
79+
80+
# buffer object setup
81+
82+
Buffer = ->
83+
buffer = Object.create Array.prototype
84+
85+
if $animate
86+
if isAngularVersionLessThen1_3
87+
remove = (wrapper) ->
7788
buffer.splice buffer.indexOf(wrapper), 1
7889
deferred = $q.defer()
7990
$animate.leave wrapper.element, ->
8091
wrapper.scope.$destroy()
8192
deferred.resolve()
8293
[deferred.promise]
8394
else
84-
(wrapper) ->
95+
remove = (wrapper) ->
8596
buffer.splice buffer.indexOf(wrapper), 1
8697
[($animate.leave wrapper.element).then ->
8798
wrapper.scope.$destroy()
8899
]
89100
else
90-
(wrapper) ->
101+
remove = (wrapper) ->
91102
buffer.splice buffer.indexOf(wrapper), 1
92103
wrapper.element.remove()
93104
wrapper.scope.$destroy()
94105
[]
95106

96-
insertElement =
97-
(newElement, previousElement) ->
98-
element.after.apply(previousElement, [newElement])
99-
[]
107+
# buffer object methods
100108

101-
insertElementAnimated =
102-
if $animate
103-
if isAngularVersionLessThen1_3
104-
(newElement, previousElement) ->
105-
deferred = $q.defer()
106-
$animate.enter newElement, element, previousElement, -> deferred.resolve()
107-
[deferred.promise]
109+
buffer.remove = (arg1, arg2) ->
110+
if angular.isNumber arg1
111+
#removes items from arg1 (including) through arg2 (excluding)
112+
for i in [arg1...arg2]
113+
buffer[i].scope.$destroy()
114+
buffer[i].element.remove()
115+
buffer.splice arg1, arg2 - arg1
108116
else
109-
(newElement, previousElement) ->
110-
[$animate.enter newElement, element, previousElement]
117+
# removes single item(wrapper) from the buffer
118+
remove arg1
111119

112-
else insertElement
120+
#clears the buffer
121+
buffer.clear = ->
122+
buffer.remove(0, buffer.length)
123+
124+
buffer
125+
126+
# initial settings
127+
128+
builder = null
129+
ridActual = 0 # current data revision id
130+
first = 1
131+
next = 1
132+
buffer = new Buffer
133+
pending = []
134+
eof = false
135+
bof = false
113136

114-
# Element builder
137+
# Padding element builder
115138
#
116139
# Calling linker is the only way I found to get access to the tag name of the template
117140
# to prevent the directive scope from pollution a new scope is created and destroyed
@@ -174,13 +197,6 @@ angular.module('ui.scroll', [])
174197
$parse($attr.isLoading).assign($scope, value) if $attr.isLoading
175198
datasource.loading(value) if angular.isFunction(datasource.loading)
176199

177-
#removes items from start (including) through stop (excluding)
178-
removeFromBuffer = (start, stop)->
179-
for i in [start...stop]
180-
buffer[i].scope.$destroy()
181-
buffer[i].element.remove()
182-
buffer.splice start, stop - start
183-
184200
dismissPendingRequests = () ->
185201
ridActual++
186202
pending = []
@@ -189,7 +205,7 @@ angular.module('ui.scroll', [])
189205
dismissPendingRequests()
190206
first = 1
191207
next = 1
192-
removeFromBuffer(0, buffer.length)
208+
buffer.clear()
193209
builder.topPadding(0)
194210
builder.bottomPadding(0)
195211
eof = false
@@ -227,7 +243,7 @@ angular.module('ui.scroll', [])
227243

228244
if overage > 0
229245
builder.bottomPadding(builder.bottomPadding() + bottomHeight)
230-
removeFromBuffer(buffer.length - overage, buffer.length)
246+
buffer.remove(buffer.length - overage, buffer.length)
231247
next -= overage
232248
#log 'clipped off bottom ' + overage + ' bottom padding ' + builder.bottomPadding()
233249

@@ -253,7 +269,7 @@ angular.module('ui.scroll', [])
253269
overage++
254270
if overage > 0
255271
builder.topPadding(builder.topPadding() + topHeight)
256-
removeFromBuffer(0, overage)
272+
buffer.remove(0, overage)
257273
first += overage
258274
#log 'clipped off top ' + overage + ' top padding ' + builder.topPadding()
259275

@@ -263,6 +279,9 @@ angular.module('ui.scroll', [])
263279
if pending.push(direction) == 1
264280
fetch(rid)
265281

282+
# the first argument is either operation keyword (see below) or a number for operation 'insert'
283+
# for insert the number is the index for the buffer element the new one have to be inserted after
284+
# operations: 'append', 'prepend', 'insert', 'remove', 'update', 'none'
266285
insertItem = (operation, item) ->
267286
itemScope = $scope.$new()
268287
itemScope[itemName] = item
@@ -272,7 +291,6 @@ angular.module('ui.scroll', [])
272291
linker itemScope, (clone) ->
273292
wrapper.element = clone
274293

275-
# operations: 'append', 'prepend', 'insert', 'remove', 'update', 'none'
276294
if operation % 1 == 0 # it is an insert
277295
wrapper.op = 'insert'
278296
buffer.splice operation, 0, wrapper
@@ -322,7 +340,7 @@ angular.module('ui.scroll', [])
322340
when 'remove' then toBeRemoved.push wrapper
323341

324342
for wrapper in toBeRemoved
325-
promises = promises.concat (removeItem wrapper)
343+
promises = promises.concat (buffer.remove wrapper)
326344

327345
# for anything other than prepend adjust the bottomPadding height
328346
builder.bottomPadding(Math.max(0,builder.bottomPadding() - (builder.bottomDataPos() - bottomPos)))
@@ -468,9 +486,8 @@ angular.module('ui.scroll', [])
468486
$scope.$watch datasource.revision, reload
469487

470488
$scope.$on '$destroy', ->
471-
for item in buffer
472-
item.scope.$destroy()
473-
item.element.remove()
489+
# clear the buffer. It is necessary to remove the elements and $destroy the scopes
490+
buffer.clear()
474491
viewport.unbind 'resize', resizeAndScrollHandler
475492
viewport.unbind 'scroll', resizeAndScrollHandler
476493
viewport.unbind 'mousewheel', wheelHandler

0 commit comments

Comments
 (0)