Skip to content

Commit

Permalink
fix(scroll): anchor scroll should scroll to IDs that are multiple lev…
Browse files Browse the repository at this point in the history
…els beneath the scroll view. Closes #1804
  • Loading branch information
perrygovier committed Jul 21, 2014
1 parent 5b50e12 commit 3d0a46e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
15 changes: 11 additions & 4 deletions js/angular/controller/scrollController.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,19 @@ function($scope, scrollViewOptions, $timeout, $window, $$scrollValueCache, $loca
this.resize().then(function() {
var hash = $location.hash();
var elm = hash && $document[0].getElementById(hash);
if (hash && elm) {
var scroll = ionic.DomUtil.getPositionInParent(elm, self.$element);
scrollView.scrollTo(scroll.left, scroll.top, !!shouldAnimate);
} else {
if (!(hash && elm)) {
scrollView.scrollTo(0,0, !!shouldAnimate);
return;
}
var curElm = elm;
var scrollLeft = 0, scrollTop = 0, levelsClimbed = 0;
do {
if(curElm !== null)scrollLeft += curElm.offsetLeft;
if(curElm !== null)scrollTop += curElm.offsetTop;
curElm = curElm.offsetParent;
levelsClimbed++;
} while (curElm.attributes != self.element.attributes && curElm.offsetParent !== null);
scrollView.scrollTo(scrollLeft, scrollTop, !!shouldAnimate);
});
};

Expand Down
38 changes: 37 additions & 1 deletion test/unit/angular/controller/scrollController.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ describe('$ionicScroll Controller', function() {
}));
it('.anchorScroll with el matching hash should scroll to it', inject(function($location, $document) {
$document[0].getElementById = jasmine.createSpy('byId').andCallFake(function() {
return { offsetLeft: 8, offsetTop: 9 };
return {
offsetLeft: 8,
offsetTop: 9,
attributes:[],
offsetParent:{}
};
});
spyOn($location, 'hash').andCallFake(function() {
return 'foo';
Expand All @@ -305,6 +310,37 @@ describe('$ionicScroll Controller', function() {
});
});

it('should work', function() {
var ele = {
offsetLeft: 8,
offsetTop: 9,
attributes:[],
offsetParent:{
offsetLeft: 10,
offsetTop: 11,
attributes:[],
offsetParent:{}
}
};
module('ionic', function($provide) {
$provide.value('$document', [ { getElementById: function(){ return ele; } } ]);
});
inject(function($controller, $rootScope, $location, $timeout) {
var scrollCtrl = $controller('$ionicScroll', {
$scope: $rootScope.$new(),
$element: jqLite('<div><div></div></div>'),
scrollViewOptions: { el: jqLite('<div><div></div></div>')[0] }
});
spyOn($location, 'hash').andCallFake(function() {
return 'bar';
});
spyOn(scrollCtrl.scrollView, 'scrollTo')
scrollCtrl.anchorScroll()
$timeout.flush();
expect(scrollCtrl.scrollView.scrollTo.mostRecentCall.args).toEqual([18, 20, false]);
});
});

it('should not activatePullToRefresh if setRefresher is not called', function() {
setup();
timeout.flush();
Expand Down

0 comments on commit 3d0a46e

Please sign in to comment.