import angular from 'angular';
import lodash from 'lodash';

const isScrollingDown = wheelEvent => {
    if (!wheelEvent) return true;

    return wheelEvent.deltaY > 0;
};

export default function maDatagridInfinitePagination($window, $document) {
    const body = $document[0].body;

    return {
        restrict: 'E',
        scope: {
            perPage: '@',
            totalItems: '@',
            nextPage: '&',
            options: '=',
        },
        link(scope) {
            let offset = 400;
            if(scope.options && scope.options.offset){
                offset = scope.options.offset
            }
            scope.processing = false;
            const perPage = parseInt(scope.perPage, 10) || 1;
            const totalItems = parseInt(scope.totalItems, 10);
            const nbPages = Math.ceil(totalItems / perPage) || 1;
            const loadedPages = [];
            let page = 1;

            const loadNextPage = lodash.debounce(() => {
                if (page >= nbPages) {
                    return;
                }

                page++;

                if (page in loadedPages) {
                    return;
                }

                scope.processing = true;

                loadedPages.push(page);
                scope.nextPage()(page);
                scope.processing = false;
            }, 500, { maxWait: 1000 });

            const isNearBottom = () =>
                body.offsetHeight - $window.innerHeight - $window.scrollY < offset;

            const shouldLoadNextPage = (wheelEvent) =>
                isScrollingDown(wheelEvent) &&
                !scope.processing &&
                isNearBottom()
            ;

            const shouldPreloadNextPage = () => {
                const list = document.getElementsByClassName("list-view");
                if(!list.length){
                    return;
                }
                const { bottom } = list[0].getBoundingClientRect();
                return bottom < $window.innerHeight;
            };

            const handler = (wheelEvent) => {
                if(!shouldLoadNextPage(wheelEvent)){
                    return;
                }
                loadNextPage();
            };

            // Trigger the load only if necessary (as many times as needed)
            // Necessary = the bottom of the table doesn't reach the end of the page
            const shouldPreloadInterval = setInterval(() => {
                if(shouldPreloadNextPage()){
                    loadNextPage();
                    return;
                }
                clearInterval(shouldPreloadInterval);
            }, 100);

            $window.addEventListener('wheel', handler);
            scope.$on('$destroy', () => {
                $window.removeEventListener('wheel', handler);
            });
        }
    };
}

maDatagridInfinitePagination.$inject = ['$window', '$document'];

@@ -1724,32 +1724,124 @@
 tPage = () =%3E %7B%0A
+                if (page %3E= nbPages) %7B%0A                    return false;%0A                %7D%0A%0A
                 
@@ -1951,32 +1951,38 @@
           return
+ false
 ;%0A              
@@ -1977,32 +1977,33 @@
               %7D%0A
+%0A
                 
@@ -2895,32 +2895,151 @@
 eel', handler);%0A
+                if(shouldPreloadInterval)%7B%0A                    clearInterval(shouldPreloadInterval);%0A                %7D%0A
             %7D);%0A

Make sure interval is cleared in all cases