@@ -55,63 +55,86 @@ angular.module('ui.scroll', [])
55
55
bufferPadding = -> viewport .outerHeight () * Math .max (0.1 , + $attr .padding || 0.1 ) # some extra space to initiate preload
56
56
scrollHeight = (elem ) -> elem[0 ].scrollHeight ? elem[0 ].document .documentElement .scrollHeight
57
57
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
68
59
69
60
# Element manipulation routines
70
61
71
- isAngularVersionLessThen1_3 = angular .version .major == 1 and angular .version .minor < 3
62
+ insertElement =
63
+ (newElement , previousElement ) ->
64
+ element .after .apply (previousElement, [newElement])
65
+ []
72
66
73
- removeItem =
67
+ insertElementAnimated =
74
68
if $animate
75
69
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 ) ->
77
88
buffer .splice buffer .indexOf (wrapper), 1
78
89
deferred = $q .defer ()
79
90
$animate .leave wrapper .element , ->
80
91
wrapper .scope .$destroy ()
81
92
deferred .resolve ()
82
93
[deferred .promise ]
83
94
else
84
- (wrapper ) ->
95
+ remove = (wrapper ) ->
85
96
buffer .splice buffer .indexOf (wrapper), 1
86
97
[($animate .leave wrapper .element ).then ->
87
98
wrapper .scope .$destroy ()
88
99
]
89
100
else
90
- (wrapper ) ->
101
+ remove = (wrapper ) ->
91
102
buffer .splice buffer .indexOf (wrapper), 1
92
103
wrapper .element .remove ()
93
104
wrapper .scope .$destroy ()
94
105
[]
95
106
96
- insertElement =
97
- (newElement , previousElement ) ->
98
- element .after .apply (previousElement, [newElement])
99
- []
107
+ # buffer object methods
100
108
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
108
116
else
109
- ( newElement , previousElement ) ->
110
- [ $animate . enter newElement, element, previousElement]
117
+ # removes single item(wrapper) from the buffer
118
+ remove arg1
111
119
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
113
136
114
- # Element builder
137
+ # Padding element builder
115
138
#
116
139
# Calling linker is the only way I found to get access to the tag name of the template
117
140
# to prevent the directive scope from pollution a new scope is created and destroyed
@@ -174,13 +197,6 @@ angular.module('ui.scroll', [])
174
197
$parse ($attr .isLoading ).assign ($scope, value) if $attr .isLoading
175
198
datasource .loading (value) if angular .isFunction (datasource .loading )
176
199
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
-
184
200
dismissPendingRequests = () ->
185
201
ridActual++
186
202
pending = []
@@ -189,7 +205,7 @@ angular.module('ui.scroll', [])
189
205
dismissPendingRequests ()
190
206
first = 1
191
207
next = 1
192
- removeFromBuffer ( 0 , buffer .length )
208
+ buffer .clear ( )
193
209
builder .topPadding (0 )
194
210
builder .bottomPadding (0 )
195
211
eof = false
@@ -227,7 +243,7 @@ angular.module('ui.scroll', [])
227
243
228
244
if overage > 0
229
245
builder .bottomPadding (builder .bottomPadding () + bottomHeight)
230
- removeFromBuffer (buffer .length - overage, buffer .length )
246
+ buffer . remove (buffer .length - overage, buffer .length )
231
247
next -= overage
232
248
# log 'clipped off bottom ' + overage + ' bottom padding ' + builder.bottomPadding()
233
249
@@ -253,7 +269,7 @@ angular.module('ui.scroll', [])
253
269
overage++
254
270
if overage > 0
255
271
builder .topPadding (builder .topPadding () + topHeight)
256
- removeFromBuffer (0 , overage)
272
+ buffer . remove (0 , overage)
257
273
first += overage
258
274
# log 'clipped off top ' + overage + ' top padding ' + builder.topPadding()
259
275
@@ -263,6 +279,9 @@ angular.module('ui.scroll', [])
263
279
if pending .push (direction) == 1
264
280
fetch (rid)
265
281
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'
266
285
insertItem = (operation , item ) ->
267
286
itemScope = $scope .$new ()
268
287
itemScope[itemName] = item
@@ -272,7 +291,6 @@ angular.module('ui.scroll', [])
272
291
linker itemScope, (clone ) ->
273
292
wrapper .element = clone
274
293
275
- # operations: 'append', 'prepend', 'insert', 'remove', 'update', 'none'
276
294
if operation % 1 == 0 # it is an insert
277
295
wrapper .op = ' insert'
278
296
buffer .splice operation, 0 , wrapper
@@ -322,7 +340,7 @@ angular.module('ui.scroll', [])
322
340
when ' remove' then toBeRemoved .push wrapper
323
341
324
342
for wrapper in toBeRemoved
325
- promises = promises .concat (removeItem wrapper)
343
+ promises = promises .concat (buffer . remove wrapper)
326
344
327
345
# for anything other than prepend adjust the bottomPadding height
328
346
builder .bottomPadding (Math .max (0 ,builder .bottomPadding () - (builder .bottomDataPos () - bottomPos)))
@@ -468,9 +486,8 @@ angular.module('ui.scroll', [])
468
486
$scope .$watch datasource .revision , reload
469
487
470
488
$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 ()
474
491
viewport .unbind ' resize' , resizeAndScrollHandler
475
492
viewport .unbind ' scroll' , resizeAndScrollHandler
476
493
viewport .unbind ' mousewheel' , wheelHandler
0 commit comments