mirror of
https://github.com/ansible/awx.git
synced 2026-01-23 15:38:06 -03:30
45 lines
1.7 KiB
JavaScript
45 lines
1.7 KiB
JavaScript
(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,
|
|
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 = element[0].scrollHeight - (element[0].clientHeight + element[0].scrollTop);
|
|
|
|
//if we have reached the threshold and we scroll down
|
|
if (remaining < lengthThreshold && (remaining - lastRemaining) < 0) {
|
|
|
|
//if there is already a timer running which has no 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);
|