Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new infiniteScrollReset parameter to allow for easier list updates #277

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions build/ng-infinite-scroll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ng-infinite-scroll - v1.2.1 - 2016-02-09 */
/* ng-infinite-scroll - v1.2.1 - 2016-04-15 */
var mod;

mod = angular.module('infinite-scroll', []);
Expand All @@ -14,7 +14,8 @@ mod.directive('infiniteScroll', [
infiniteScrollDistance: '=',
infiniteScrollDisabled: '=',
infiniteScrollUseDocumentBottom: '=',
infiniteScrollListenForEvent: '@'
infiniteScrollListenForEvent: '@',
infiniteScrollReset: '='
},
link: function(scope, elem, attrs) {
var changeContainer, checkInterval, checkWhenEnabled, container, handleInfiniteScrollContainer, handleInfiniteScrollDisabled, handleInfiniteScrollDistance, handleInfiniteScrollUseDocumentBottom, handler, height, immediateCheck, offsetTop, pageYOffset, scrollDistance, scrollEnabled, throttle, unregisterEventListener, useDocumentBottom, windowElement;
Expand Down Expand Up @@ -49,8 +50,9 @@ mod.directive('infiniteScroll', [
return elem.ownerDocument.defaultView.pageYOffset;
}
};
handler = function() {
handler = function(event, reset) {
var containerBottom, containerTopOffset, elementBottom, remaining, shouldScroll;
reset = reset || false;
if (container === windowElement) {
containerBottom = height(container) + pageYOffset(container[0].document.documentElement);
elementBottom = offsetTop(elem) + height(elem);
Expand All @@ -66,9 +68,10 @@ mod.directive('infiniteScroll', [
elementBottom = height((elem[0].ownerDocument || elem[0].document).documentElement);
}
remaining = elementBottom - containerBottom;
shouldScroll = remaining <= height(container) * scrollDistance + 1;
shouldScroll = reset ? true : remaining <= height(container) * scrollDistance + 1;
if (shouldScroll) {
checkWhenEnabled = true;
scrollEnabled = reset ? true : scrollEnabled;
if (scrollEnabled) {
if (scope.$$phase || $rootScope.$$phase) {
return scope.infiniteScroll();
Expand Down Expand Up @@ -116,7 +119,10 @@ mod.directive('infiniteScroll', [
container.unbind('scroll', handler);
if (unregisterEventListener != null) {
unregisterEventListener();
return unregisterEventListener = null;
unregisterEventListener = null;
}
if (checkInterval) {
return $interval.cancel(checkInterval);
}
});
handleInfiniteScrollDistance = function(v) {
Expand Down Expand Up @@ -149,7 +155,9 @@ mod.directive('infiniteScroll', [
};
changeContainer(windowElement);
if (scope.infiniteScrollListenForEvent) {
unregisterEventListener = $rootScope.$on(scope.infiniteScrollListenForEvent, handler);
unregisterEventListener = $rootScope.$on(scope.infiniteScrollListenForEvent, function(event) {
handler(event, scope.infiniteScrollReset ? scope.infiniteScrollReset : false);
});
}
handleInfiniteScrollContainer = function(newContainer) {
if ((newContainer == null) || newContainer.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions build/ng-infinite-scroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/infinite-scroll.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
infiniteScrollDisabled: '='
infiniteScrollUseDocumentBottom: '=',
infiniteScrollListenForEvent: '@'
infiniteScrollReset: '='

link: (scope, elem, attrs) ->
windowElement = angular.element($window)
Expand Down Expand Up @@ -46,7 +47,8 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
# document. It is recommended to use infinite-scroll-disabled
# with a boolean that is set to true when the function is
# called in order to throttle the function call.
handler = ->
handler = (event, reset) ->
reset = reset || false
if container == windowElement
containerBottom = height(container) + pageYOffset(container[0].document.documentElement)
elementBottom = offsetTop(elem) + height(elem)
Expand All @@ -61,11 +63,13 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
elementBottom = height((elem[0].ownerDocument || elem[0].document).documentElement)

remaining = elementBottom - containerBottom
shouldScroll = remaining <= height(container) * scrollDistance + 1
shouldScroll = if reset then true else remaining <= height(container) * scrollDistance + 1

if shouldScroll
checkWhenEnabled = true

scrollEnabled = if reset then true else scrollEnabled

if scrollEnabled
if scope.$$phase || $rootScope.$$phase
scope.infiniteScroll()
Expand Down Expand Up @@ -108,7 +112,7 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
if unregisterEventListener?
unregisterEventListener()
unregisterEventListener = null
if checkInterval
if checkInterval
$interval.cancel checkInterval

# infinite-scroll-distance specifies how close to the bottom of the page
Expand Down Expand Up @@ -163,7 +167,9 @@ mod.directive 'infiniteScroll', ['$rootScope', '$window', '$interval', 'THROTTLE
changeContainer windowElement

if scope.infiniteScrollListenForEvent
unregisterEventListener = $rootScope.$on scope.infiniteScrollListenForEvent, handler
unregisterEventListener = $rootScope.$on scope.infiniteScrollListenForEvent, (event) ->
handler event, if scope.infiniteScrollReset then scope.infiniteScrollReset else false
return

handleInfiniteScrollContainer = (newContainer) ->
# TODO: For some reason newContainer is sometimes null instead
Expand Down