diff --git a/awx/ui/static/lib/lrInfiniteScroll/lrInfiniteScroll.js b/awx/ui/static/lib/lrInfiniteScroll/lrInfiniteScroll.js new file mode 100644 index 0000000000..efabf5b3eb --- /dev/null +++ b/awx/ui/static/lib/lrInfiniteScroll/lrInfiniteScroll.js @@ -0,0 +1,45 @@ +(function (ng) { + 'use strict'; + var module = ng.module('lrInfiniteScroll', []); + + module.directive('lrInfiniteScroll', ['$timeout', function (timeout) { + return{ + link: function (scope, element, attr) { + var + lengthThreshold = attr.scrollThreshold || 50, + timeThreshold = attr.timeThreshold || 400, + direction = attr.direction || 'down', + handler = scope.$eval(attr.lrInfiniteScroll), + promise = null, + lastRemaining = 9999; + + lengthThreshold = parseInt(lengthThreshold, 10); + timeThreshold = parseInt(timeThreshold, 10); + + if (!handler || !ng.isFunction(handler)) { + handler = ng.noop; + } + + element.bind('scroll', function () { + var remaining = + (direction === 'down') ? element[0].scrollHeight - (element[0].clientHeight + element[0].scrollTop) : element[0].scrollTop; + + //if we have reached the threshold and we scroll down + if ((direction === 'down' && remaining < lengthThreshold && (remaining - lastRemaining) < 0) || + direction === 'up' && remaining < lengthThreshold) { + //if there is already a timer running which has not expired yet we have to cancel it and restart the timer + if (promise !== null) { + timeout.cancel(promise); + } + promise = timeout(function () { + handler(); + promise = null; + }, timeThreshold); + } + lastRemaining = remaining; + }); + } + + }; + }]); +})(angular);