import $ from 'jquery' ; interface LazyScope extends ng . IScope { [ key ] : any ; } export function createInfiniteScrollDirective ( ) { return { restrict : 'E' , scope : { more : '=' , } , link : ( $scope , $element ) => { const $window = $ ( window ) ; let checkTimer ; function onScroll ( ) { if ( ! $scope . more ) return ; const winHeight = Number ( $window . height ( ) ) ; const winBottom = Number ( winHeight ) + Number ( $window . scrollTop ( ) ) ; const offset = $element . offset ( ) ; const elTop = offset ? offset . top : 0 ; const remaining = elTop - winBottom ; if ( remaining <= winHeight * 0.5 ) { $scope [ $scope . $$phase ? '$eval' : '$apply' ] ( function ( ) { $scope . more ( ) ; } ) ; } } function scheduleCheck ( ) { if ( checkTimer ) return ; checkTimer = setTimeout ( function ( ) { checkTimer = null ; onScroll ( ) ; } , 50 ) ; } $window . on ( 'scroll' , scheduleCheck ) ; $scope . $on ( '$destroy' , function ( ) { clearTimeout ( checkTimer ) ; $window . off ( 'scroll' , scheduleCheck ) ; } ) ; scheduleCheck ( ) ; } , } ; }