-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scrolling): Allow native scrolling to be configurable, add infin…
…ite scroll support for native scrolling
- Loading branch information
1 parent
d24ac30
commit 54c27ff
Showing
7 changed files
with
305 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
IonicModule | ||
.controller('$ionInfiniteScroll', [ | ||
'$scope', | ||
'$attrs', | ||
'$element', | ||
'$timeout', | ||
function($scope, $attrs, $element, $timeout) { | ||
var self = this; | ||
self.isLoading = false; | ||
|
||
$scope.icon = function() { | ||
return angular.isDefined($attrs.icon) ? $attrs.icon : 'ion-loading-d'; | ||
}; | ||
|
||
$scope.$on('scroll.infiniteScrollComplete', function() { | ||
finishInfiniteScroll(); | ||
}); | ||
|
||
$scope.$on('$destroy', function() { | ||
if (self.scrollCtrl && self.scrollCtrl.$element) self.scrollCtrl.$element.off('scroll', self.checkBounds); | ||
if (self.scrollEl && self.scrollEl.removeEventListener) { | ||
self.scrollEl.removeEventListener('scroll', self.checkBounds); | ||
} | ||
}); | ||
|
||
// debounce checking infinite scroll events | ||
self.checkBounds = ionic.Utils.throttle(checkInfiniteBounds, 300); | ||
|
||
function onInfinite() { | ||
ionic.requestAnimationFrame(function() { | ||
$element[0].classList.add('active'); | ||
}); | ||
self.isLoading = true; | ||
$scope.$parent && $scope.$parent.$apply($attrs.onInfinite || ''); | ||
} | ||
|
||
function finishInfiniteScroll() { | ||
ionic.requestAnimationFrame(function() { | ||
$element[0].classList.remove('active'); | ||
}); | ||
$timeout(function() { | ||
if (self.jsScrolling) self.scrollView.resize(); | ||
self.checkBounds(); | ||
}, 30, false); | ||
self.isLoading = false; | ||
} | ||
|
||
// check if we've scrolled far enough to trigger an infinite scroll | ||
function checkInfiniteBounds() { | ||
if (self.isLoading) return; | ||
var maxScroll = {}; | ||
|
||
if (self.jsScrolling) { | ||
maxScroll = self.getJSMaxScroll(); | ||
var scrollValues = self.scrollView.getValues(); | ||
if ((maxScroll.left !== -1 && scrollValues.left >= maxScroll.left) || | ||
(maxScroll.top !== -1 && scrollValues.top >= maxScroll.top)) { | ||
onInfinite(); | ||
} | ||
} else { | ||
maxScroll = self.getNativeMaxScroll(); | ||
if (( | ||
maxScroll.left !== -1 && | ||
self.scrollEl.scrollLeft >= maxScroll.left - self.scrollEl.clientWidth | ||
) || ( | ||
maxScroll.top !== -1 && | ||
self.scrollEl.scrollTop >= maxScroll.top - self.scrollEl.clientHeight | ||
)) { | ||
onInfinite(); | ||
} | ||
} | ||
} | ||
|
||
// determine the threshold at which we should fire an infinite scroll | ||
// note: this gets processed every scroll event, can it be cached? | ||
self.getJSMaxScroll = function() { | ||
var maxValues = self.scrollView.getScrollMax(); | ||
return { | ||
left: self.scrollView.options.scrollingX ? | ||
calculateMaxValue(maxValues.left) : | ||
-1, | ||
top: self.scrollView.options.scrollingY ? | ||
calculateMaxValue(maxValues.top) : | ||
-1 | ||
}; | ||
}; | ||
|
||
self.getNativeMaxScroll = function() { | ||
var maxValues = { | ||
left: self.scrollEl.scrollWidth, | ||
top: self.scrollEl.scrollHeight | ||
}; | ||
var computedStyle = window.getComputedStyle(self.scrollEl) || {}; | ||
return { | ||
left: computedStyle.overflowX === 'scroll' || | ||
computedStyle.overflowX === 'auto' || | ||
self.scrollEl.style['overflow-x'] === 'scroll' ? | ||
calculateMaxValue(maxValues.left) : -1, | ||
top: computedStyle.overflowY === 'scroll' || | ||
computedStyle.overflowY === 'auto' || | ||
self.scrollEl.style['overflow-y'] === 'scroll' ? | ||
calculateMaxValue(maxValues.top) : -1 | ||
}; | ||
}; | ||
|
||
// determine pixel refresh distance based on % or value | ||
function calculateMaxValue(maximum) { | ||
distance = ($attrs.distance || '2.5%').trim(); | ||
isPercent = distance.indexOf('%') !== -1; | ||
return isPercent ? | ||
maximum * (1 - parseFloat(distance) / 100) : | ||
maximum - parseFloat(distance); | ||
} | ||
|
||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
54c27ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This + Crosswalk = win
54c27ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
54c27ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great news!
Do you plan to have all of the JS scrolling functionnalities work with Android native scrolling (like collection-repeat)?
54c27ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@perrygovier Thanks! Infinite scrolling is available for overflow / native scrolling. I had a really bad hacky solution previously!