Blame view
resources/js/vendor/ng-infinite-scroll.js
5.67 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
/* ng-infinite-scroll - v1.1.2 - 2014-05-21 */ var mod; mod = angular.module('infinite-scroll', []); mod.value('THROTTLE_MILLISECONDS', null); mod.directive('infiniteScroll', [ '$rootScope', '$window', '$timeout', 'THROTTLE_MILLISECONDS', function($rootScope, $window, $timeout, THROTTLE_MILLISECONDS) { return { scope: { infiniteScroll: '&', infiniteScrollContainer: '=', infiniteScrollDistance: '=', infiniteScrollDisabled: '=', infiniteScrollUseDocumentBottom: '=' }, link: function(scope, elem, attrs) { var changeContainer, checkWhenEnabled, container, handleInfiniteScrollContainer, handleInfiniteScrollDisabled, handleInfiniteScrollDistance, handleInfiniteScrollUseDocumentBottom, handler, immediateCheck, scrollDistance, scrollEnabled, throttle, useDocumentBottom; $window = angular.element($window); scrollDistance = null; scrollEnabled = null; checkWhenEnabled = null; container = null; immediateCheck = true; useDocumentBottom = false; handler = function() { var containerBottom, containerTopOffset, elementBottom, remaining, shouldScroll; if (container === $window) { containerBottom = container.height() + container.scrollTop(); elementBottom = elem.offset().top + elem.height(); } else { containerBottom = container.height(); containerTopOffset = 0; if (container.offset() !== void 0) { containerTopOffset = container.offset().top; } elementBottom = elem.offset().top - containerTopOffset + elem.height(); } if (useDocumentBottom) { elementBottom = $(document).height(); } remaining = elementBottom - containerBottom; shouldScroll = remaining <= container.height() * scrollDistance + 1; if (shouldScroll) { checkWhenEnabled = true; if (scrollEnabled) { if (scope.$$phase || $rootScope.$$phase) { return scope.infiniteScroll(); } else { return scope.$apply(scope.infiniteScroll); } } } else { return checkWhenEnabled = false; } }; throttle = function(func, wait) { var later, previous, timeout; timeout = null; previous = 0; later = function() { var context; previous = new Date().getTime(); $timeout.cancel(timeout); timeout = null; func.call(); return context = null; }; return function() { var now, remaining; now = new Date().getTime(); remaining = wait - (now - previous); if (remaining <= 0) { clearTimeout(timeout); $timeout.cancel(timeout); timeout = null; previous = now; return func.call(); } else { if (!timeout) { return timeout = $timeout(later, remaining); } } }; }; if (THROTTLE_MILLISECONDS != null) { handler = throttle(handler, THROTTLE_MILLISECONDS); } scope.$on('$destroy', function() { return container.off('scroll', handler); }); handleInfiniteScrollDistance = function(v) { return scrollDistance = parseInt(v, 10) || 0; }; scope.$watch('infiniteScrollDistance', handleInfiniteScrollDistance); handleInfiniteScrollDistance(scope.infiniteScrollDistance); handleInfiniteScrollDisabled = function(v) { scrollEnabled = !v; if (scrollEnabled && checkWhenEnabled) { checkWhenEnabled = false; return handler(); } }; scope.$watch('infiniteScrollDisabled', handleInfiniteScrollDisabled); handleInfiniteScrollDisabled(scope.infiniteScrollDisabled); handleInfiniteScrollUseDocumentBottom = function(v) { return useDocumentBottom = v; }; scope.$watch('infiniteScrollUseDocumentBottom', handleInfiniteScrollUseDocumentBottom); handleInfiniteScrollUseDocumentBottom(scope.infiniteScrollUseDocumentBottom); changeContainer = function(newContainer) { if (container != null) { container.off('scroll', handler); } container = typeof newContainer.last === 'function' && newContainer !== $window ? newContainer.last() : newContainer; if (newContainer != null) { return container.on('scroll', handler); } }; changeContainer($window); handleInfiniteScrollContainer = function(newContainer) { if ((!(newContainer != null)) || newContainer.length === 0) { return; } newContainer = angular.element(newContainer); if (newContainer != null) { return changeContainer(newContainer); } else { throw new Exception("invalid infinite-scroll-container attribute."); } }; scope.$watch('infiniteScrollContainer', handleInfiniteScrollContainer); handleInfiniteScrollContainer(scope.infiniteScrollContainer || []); if (attrs.infiniteScrollParent != null) { changeContainer(angular.element(elem.parent())); } if (attrs.infiniteScrollImmediateCheck != null) { immediateCheck = scope.$eval(attrs.infiniteScrollImmediateCheck); } return $timeout((function() { if (immediateCheck) { return handler(); } }), 0); } }; } ]); |