Skip to content

Commit

Permalink
feat(uiView) autoscroll attribute
Browse files Browse the repository at this point in the history
Adds an `autoscroll="expr"` attribute to `uiView` just like
`ngInclude` has, with the difference that if the `autoscroll`
attribute isn't defined then the scroll will happen in order to not
break the current behavior.
  • Loading branch information
ysbaddaden committed Dec 23, 2013
1 parent 8ee2afb commit 5fd1efc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/viewDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an

// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
// $anchorScroll might listen on event...
$anchorScroll();
if (!angular.isDefined(attr.autoscroll) || scope.$eval(attr.autoscroll)) {
$anchorScroll();
}
}
};
}
Expand Down
29 changes: 29 additions & 0 deletions test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe('uiView', function () {

beforeEach(module('ui.router'));

beforeEach(module(function ($provide) {
$provide.decorator('$anchorScroll', function ($delegate) {
return jasmine.createSpy('$anchorScroll');
});
}));

var aState = {
template: 'aState template'
},
Expand Down Expand Up @@ -209,4 +215,27 @@ describe('uiView', function () {
}));
});

describe('autoscroll attribute', function () {
it('should autoscroll when missing', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view></div>')(scope));
$state.transitionTo(gState);
$q.flush();
expect($anchorScroll).toHaveBeenCalled();
}));

it('should autoscroll when truthy', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="true"></div>')(scope));
$state.transitionTo(gState);
$q.flush();
expect($anchorScroll).toHaveBeenCalled();
}));

it('should not autoscroll when falsy', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="false"></div>')(scope));
$state.transitionTo(gState);
$q.flush();
expect($anchorScroll).not.toHaveBeenCalled();
}));
});

});

0 comments on commit 5fd1efc

Please sign in to comment.