@@ -366,106 +366,99 @@ angular.module('ui.scroll', [])
366
366
}
367
367
368
368
function Adapter ( $attr , viewport , buffer , adjustBuffer ) {
369
- var applyUpdate , setIsLoading , setTopVisible , setTopVisibleElement , setTopVisibleScope , viewportScope ;
369
+ const viewportScope = viewport . scope ( ) || $rootScope ;
370
+ const setTopVisible = $attr . topVisible ? $parse ( $attr . topVisible ) . assign : angular . noop ;
371
+ const setTopVisibleElement = $attr . topVisibleElement ? $parse ( $attr . topVisibleElement ) . assign : angular . noop ;
372
+ const setTopVisibleScope = $attr . topVisibleScope ? $parse ( $attr . topVisibleScope ) . assign : angular . noop ;
373
+ const setIsLoading = $attr . isLoading ? $parse ( $attr . isLoading ) . assign : angular . noop ;
374
+
370
375
this . isLoading = false ;
371
376
372
- viewportScope = viewport . scope ( ) || $rootScope ;
373
-
374
- applyUpdate = function ( wrapper , newItems ) {
375
- var i , j , keepIt , len , newItem , pos , ref ;
376
- if ( angular . isArray ( newItems ) ) {
377
- pos = ( buffer . indexOf ( wrapper ) ) + 1 ;
378
- ref = newItems . reverse ( ) ;
379
- for ( i = j = 0 , len = ref . length ; j < len ; i = ++ j ) {
380
- newItem = ref [ i ] ;
381
- if ( newItem === wrapper . item ) {
382
- keepIt = true ;
383
- pos -- ;
384
- } else {
385
- buffer . insert ( pos , newItem ) ;
386
- }
387
- }
388
- if ( ! keepIt ) {
389
- return wrapper . op = 'remove' ;
377
+ function applyUpdate ( wrapper , newItems ) {
378
+ if ( ! angular . isArray ( newItems ) ) {
379
+ return ;
380
+ }
381
+
382
+ let keepIt ;
383
+ let pos = ( buffer . indexOf ( wrapper ) ) + 1 ;
384
+
385
+ newItems . reverse ( ) . forEach ( ( newItem ) => {
386
+ if ( newItem === wrapper . item ) {
387
+ keepIt = true ;
388
+ pos -- ;
389
+ } else {
390
+ buffer . insert ( pos , newItem ) ;
390
391
}
392
+ } ) ;
393
+
394
+ if ( ! keepIt ) {
395
+ wrapper . op = 'remove' ;
391
396
}
392
- } ;
397
+ }
393
398
394
- this . applyUpdates = function ( arg1 , arg2 ) {
395
- var bufferClone , i , j , len , ref , wrapper ;
399
+ this . applyUpdates = ( arg1 , arg2 ) => {
396
400
if ( angular . isFunction ( arg1 ) ) {
397
401
// arg1 is the updater function, arg2 is ignored
398
- bufferClone = buffer . slice ( 0 ) ;
399
- for ( i = j = 0 , len = bufferClone . length ; j < len ; i = ++ j ) {
402
+ buffer . slice ( 0 ) . forEach ( ( wrapper ) => {
400
403
// we need to do it on the buffer clone, because buffer content
401
404
// may change as we iterate through
402
- wrapper = bufferClone [ i ] ;
403
405
applyUpdate ( wrapper , arg1 ( wrapper . item , wrapper . scope , wrapper . element ) ) ;
404
- }
406
+ } ) ;
405
407
} else {
406
408
// arg1 is item index, arg2 is the newItems array
407
- if ( arg1 % 1 === 0 ) { // checking if it is an integer
408
- if ( ( 0 <= ( ref = arg1 - buffer . first ) && ref < buffer . length ) ) {
409
- applyUpdate ( buffer [ arg1 - buffer . first ] , arg2 ) ;
410
- }
411
- } else {
409
+ if ( arg1 % 1 !== 0 ) { // checking if it is an integer
412
410
throw new Error ( 'applyUpdates - ' + arg1 + ' is not a valid index' ) ;
413
411
}
412
+
413
+ const index = arg1 - buffer . first ;
414
+ if ( ( index >= 0 && index < buffer . length ) ) {
415
+ applyUpdate ( buffer [ index ] , arg2 ) ;
416
+ }
414
417
}
415
- return adjustBuffer ( ) ;
418
+
419
+ adjustBuffer ( ) ;
416
420
} ;
417
421
418
- this . append = function ( newItems ) {
422
+ this . append = ( newItems ) => {
419
423
buffer . append ( newItems ) ;
420
- return adjustBuffer ( ) ;
424
+ adjustBuffer ( ) ;
421
425
} ;
422
426
423
- this . prepend = function ( newItems ) {
427
+ this . prepend = ( newItems ) => {
424
428
buffer . prepend ( newItems ) ;
425
- return adjustBuffer ( ) ;
426
- } ;
427
-
428
- setTopVisible = $attr . topVisible ? $parse ( $attr . topVisible ) . assign : function ( ) {
429
- } ;
430
- setTopVisibleElement = $attr . topVisibleElement ? $parse ( $attr . topVisibleElement ) . assign : function ( ) {
431
- } ;
432
- setTopVisibleScope = $attr . topVisibleScope ? $parse ( $attr . topVisibleScope ) . assign : function ( ) {
433
- } ;
434
- setIsLoading = $attr . isLoading ? $parse ( $attr . isLoading ) . assign : function ( ) {
429
+ adjustBuffer ( ) ;
435
430
} ;
436
431
437
432
this . loading = function ( value ) {
438
433
this . isLoading = value ;
439
- return setIsLoading ( viewportScope , value ) ;
434
+ setIsLoading ( viewportScope , value ) ;
440
435
} ;
441
436
442
437
this . calculateProperties = function ( ) {
443
- var item , itemHeight , itemTop , j , len , newRow , results , rowTop , topHeight ;
438
+ let itemHeight , itemTop , isNewRow , rowTop , topHeight ;
444
439
topHeight = 0 ;
445
- results = [ ] ;
446
- for ( j = 0 , len = buffer . length ; j < len ; j ++ ) {
447
- item = buffer [ j ] ;
440
+ buffer . some ( ( item ) => {
448
441
itemTop = item . element . offset ( ) . top ;
449
- newRow = rowTop !== itemTop ;
442
+ isNewRow = rowTop !== itemTop ;
450
443
rowTop = itemTop ;
451
- if ( newRow ) {
444
+ if ( isNewRow ) {
452
445
itemHeight = item . element . outerHeight ( true ) ;
453
446
}
454
- if ( newRow && ( viewport . topDataPos ( ) + topHeight + itemHeight <= viewport . topVisiblePos ( ) ) ) {
455
- results . push ( topHeight += itemHeight ) ;
447
+ if ( isNewRow && ( viewport . topDataPos ( ) + topHeight + itemHeight <= viewport . topVisiblePos ( ) ) ) {
448
+ topHeight += itemHeight ;
456
449
} else {
457
- if ( newRow ) {
450
+ if ( isNewRow ) {
458
451
this . topVisible = item . item ;
459
452
this . topVisibleElement = item . element ;
460
453
this . topVisibleScope = item . scope ;
461
454
setTopVisible ( viewportScope , item . item ) ;
462
455
setTopVisibleElement ( viewportScope , item . element ) ;
463
456
setTopVisibleScope ( viewportScope , item . scope ) ;
464
457
}
465
- break ;
458
+
459
+ return true ; // Break the loop
466
460
}
467
- }
468
- return results ;
461
+ } ) ;
469
462
} ;
470
463
}
471
464
0 commit comments