@@ -229,10 +229,52 @@ angular.module('ui.scroll', [])
229
229
buffer .remove (0 , overage)
230
230
buffer .first += overage
231
231
232
+ viewport
232
233
234
+ Adapter = (buffer , adjustBuffer ) ->
235
+ this .isLoading = false
233
236
234
- viewport
237
+ applyUpdate = (wrapper , newItems ) ->
238
+ if angular .isArray newItems
239
+ pos = (buffer .indexOf wrapper) + 1
240
+ for newItem,i in newItems .reverse ()
241
+ if newItem == wrapper .item
242
+ keepIt = true ;
243
+ pos--
244
+ else
245
+ buffer .insert pos, newItem
246
+ unless keepIt
247
+ wrapper .op = ' remove'
248
+
249
+ this .applyUpdates = (arg1 , arg2 ) ->
250
+ if angular .isFunction arg1
251
+ # arg1 is the updater function, arg2 is ignored
252
+ bufferClone = buffer .slice (0 )
253
+ for wrapper,i in bufferClone # we need to do it on the buffer clone, because buffer content
254
+ # may change as we iterate through
255
+ applyUpdate wrapper, arg1 (wrapper .item , wrapper .scope , wrapper .element )
256
+ else
257
+ # arg1 is item index, arg2 is the newItems array
258
+ if arg1% 1 == 0 # checking if it is an integer
259
+ if 0 <= arg1- buffer .first < buffer .length
260
+ applyUpdate buffer[arg1 - buffer .first ], arg2
261
+ else
262
+ throw new Error ' applyUpdates - ' + arg1 + ' is not a valid index'
263
+ adjustBuffer ()
264
+
265
+ this .append = (newItems ) ->
266
+ for item in newItems
267
+ ++ buffer .next
268
+ buffer .insert ' append' , item
269
+ adjustBuffer ()
235
270
271
+ this .prepend = (newItems ) ->
272
+ for item in newItems .reverse ()
273
+ -- buffer .first
274
+ buffer .insert ' prepend' , item
275
+ adjustBuffer ()
276
+
277
+ return
236
278
237
279
require : [' ?^uiScrollViewport' ]
238
280
transclude : ' element'
@@ -261,7 +303,6 @@ angular.module('ui.scroll', [])
261
303
throw new Error datasourceName + ' is not a valid datasource'
262
304
263
305
bufferSize = Math .max (3 , + $attr .bufferSize || 10 )
264
- bufferPadding = -> viewport .outerHeight () * Math .max (0.1 , + $attr .padding || 0.1 ) # some extra space to initiate preload
265
306
266
307
# initial settings
267
308
@@ -271,6 +312,19 @@ angular.module('ui.scroll', [])
271
312
272
313
viewport = new Viewport (buffer, element, controllers, $attr .padding )
273
314
315
+ adapter = new Adapter buffer,
316
+ ->
317
+ dismissPendingRequests ()
318
+ adjustBuffer ridActual
319
+
320
+ if $attr .adapter # so we have an adapter on $scope
321
+ adapterOnScope = $parse ($attr .adapter )($scope)
322
+ if not angular .isObject adapterOnScope
323
+ $parse ($attr .adapter ).assign ($scope, {})
324
+ adapterOnScope = $parse ($attr .adapter )($scope)
325
+ angular .extend (adapterOnScope, adapter)
326
+ adapter = adapterOnScope
327
+
274
328
# Padding element builder
275
329
#
276
330
# Calling linker is the only way I found to get access to the tag name of the template
@@ -305,13 +359,15 @@ angular.module('ui.scroll', [])
305
359
ridActual++
306
360
pending = []
307
361
308
- reloadImpl = ->
362
+ reload = ->
309
363
dismissPendingRequests ()
310
364
buffer .clear ()
311
365
viewport .topPadding (0 )
312
366
viewport .bottomPadding (0 )
313
367
adjustBuffer ridActual
314
368
369
+ adapter .reload = reload
370
+
315
371
enqueueFetch = (rid , direction )->
316
372
if ! adapter .isLoading
317
373
loading (true )
@@ -501,7 +557,7 @@ angular.module('ui.scroll', [])
501
557
viewport .bind ' scroll' , resizeAndScrollHandler
502
558
viewport .bind ' mousewheel' , wheelHandler
503
559
504
- $scope .$watch datasource .revision , reloadImpl
560
+ $scope .$watch datasource .revision , reload
505
561
506
562
$scope .$on ' $destroy' , ->
507
563
# clear the buffer. It is necessary to remove the elements and $destroy the scopes
@@ -510,68 +566,6 @@ angular.module('ui.scroll', [])
510
566
viewport .unbind ' scroll' , resizeAndScrollHandler
511
567
viewport .unbind ' mousewheel' , wheelHandler
512
568
513
-
514
- # adapter setup
515
-
516
- Adapter = (buffer , adjustBuffer ) ->
517
- this .isLoading = false
518
- this .reload = reloadImpl
519
-
520
- applyUpdate = (wrapper , newItems ) ->
521
- if angular .isArray newItems
522
- pos = (buffer .indexOf wrapper) + 1
523
- for newItem,i in newItems .reverse ()
524
- if newItem == wrapper .item
525
- keepIt = true ;
526
- pos--
527
- else
528
- buffer .insert pos, newItem
529
- unless keepIt
530
- wrapper .op = ' remove'
531
-
532
- this .applyUpdates = (arg1 , arg2 ) ->
533
- if angular .isFunction arg1
534
- # arg1 is the updater function, arg2 is ignored
535
- bufferClone = buffer .slice (0 )
536
- for wrapper,i in bufferClone # we need to do it on the buffer clone, because buffer content
537
- # may change as we iterate through
538
- applyUpdate wrapper, arg1 (wrapper .item , wrapper .scope , wrapper .element )
539
- else
540
- # arg1 is item index, arg2 is the newItems array
541
- if arg1% 1 == 0 # checking if it is an integer
542
- if 0 <= arg1- buffer .first < buffer .length
543
- applyUpdate buffer[arg1 - buffer .first ], arg2
544
- else
545
- throw new Error ' applyUpdates - ' + arg1 + ' is not a valid index'
546
- adjustBuffer ()
547
-
548
- this .append = (newItems ) ->
549
- for item in newItems
550
- ++ buffer .next
551
- buffer .insert ' append' , item
552
- adjustBuffer ()
553
-
554
- this .prepend = (newItems ) ->
555
- for item in newItems .reverse ()
556
- -- buffer .first
557
- buffer .insert ' prepend' , item
558
- adjustBuffer ()
559
-
560
- return
561
-
562
- adapter = new Adapter buffer,
563
- ->
564
- dismissPendingRequests ()
565
- adjustBuffer ridActual
566
-
567
- if $attr .adapter # so we have an adapter on $scope
568
- adapterOnScope = $parse ($attr .adapter )($scope)
569
- if not angular .isObject adapterOnScope
570
- $parse ($attr .adapter ).assign ($scope, {})
571
- adapterOnScope = $parse ($attr .adapter )($scope)
572
- angular .extend (adapterOnScope, adapter)
573
- adapter = adapterOnScope
574
-
575
569
# update events (deprecated since v1.1.0, unsupported since 1.2.0)
576
570
577
571
unsupportedMethod = (token ) ->
0 commit comments