Skip to content

feat(uiView): add autoscroll option #714

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions src/viewDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
var viewScope, viewLocals,
name = attr[directive.name] || attr.name || '',
onloadExp = attr.onload || '',
autoScrollExp = attr.autoscroll,
animate = $animator && $animator(scope, attr),
initialView = transclude(scope);

Expand Down Expand Up @@ -104,9 +105,10 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
viewScope.$emit('$viewContentLoaded');
if (onloadExp) viewScope.$eval(onloadExp);

// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
// $anchorScroll might listen on event...
$anchorScroll();
if (angular.isDefined(autoScrollExp)
&& (!autoScrollExp || scope.$eval(autoScrollExp))) {
$anchorScroll();
}
}
};
}
Expand Down
74 changes: 73 additions & 1 deletion test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ describe('uiView', function () {
template: '<span ng-class="test">jState</span>'
};

beforeEach(module(function ($stateProvider) {
var count;
var $anchorScroll = function() {
count++;
};

beforeEach(module(function ($stateProvider, $provide) {
$stateProvider
.state('a', aState)
.state('b', bState)
Expand All @@ -81,6 +86,8 @@ describe('uiView', function () {
.state('g.h', hState)
.state('i', iState)
.state('j', jState);

$provide.factory('$anchorScroll', function(){ return $anchorScroll; });
}));

beforeEach(inject(function ($rootScope, _$compile_) {
Expand Down Expand Up @@ -209,4 +216,69 @@ describe('uiView', function () {
}));
});

describe('autoscroll', function() {
beforeEach(function() {
count = 0;
});

it('should call anchor scroll if autoscroll is specified without a value', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll></div>')(scope));

$state.transitionTo(aState);
$q.flush();

expect(count).toBe(1);
}));

it('should call anchor scroll if autoscroll is true', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="true"></div>')(scope));

$state.transitionTo(aState);
$q.flush();

expect(count).toBe(1);
}));

it('should not call anchor scroll if autoscroll is false', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="false"></div>')(scope));

$state.transitionTo(aState);
$q.flush();

expect(count).toBe(0);
}));

it('should correctly evaluate the expression', inject(function ($state, $q, $anchorScroll) {
scope.thing = true;
scope.$apply();
elem.append($compile('<div ui-view autoscroll="thing"></div>')(scope));

$state.transitionTo(aState);
$q.flush();

scope.thing = true;
scope.$apply();

expect(count).toBe(1);
count = 0;

scope.thing = false;
scope.$apply();

$state.transitionTo(bState);
$q.flush();

expect(count).toBe(0);
}));

it('should not call anchor scroll if autoscroll is not set', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view></div>')(scope));

$state.transitionTo(aState);
$q.flush();

expect(count).toBe(0);
}));
});

});