Skip to content

feat(ui-view): Add noanimation attribute to specify static renderer. #2497

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

Merged
merged 1 commit into from
Feb 6, 2016
Merged
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
33 changes: 25 additions & 8 deletions src/viewDirective.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var ngMajorVer = angular.version.major;
var ngMinorVer = angular.version.minor;
/**
* @ngdoc directive
* @name ui.router.state.directive:ui-view
Expand All @@ -22,6 +24,9 @@
* service, {@link ui.router.state.$uiViewScroll}. This custom service let's you
* scroll ui-view elements into view when they are populated during a state activation.
*
* @param {string=} noanimation If truthy, the non-animated renderer will be selected (no animations
* will be applied to the ui-view)
*
* *Note: To revert back to old [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll)
* functionality, call `$uiViewScrollProvider.useAnchorScroll()`.*
*
Expand Down Expand Up @@ -133,24 +138,35 @@ function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate)
// Returns a set of DOM manipulation functions based on which Angular version
// it should use
function getRenderer(attrs, scope) {
var statics = function() {
return {
enter: function (element, target, cb) { target.after(element); cb(); },
leave: function (element, cb) { element.remove(); cb(); }
};
var statics = {
enter: function (element, target, cb) { target.after(element); cb(); },
leave: function (element, cb) { element.remove(); cb(); }
};

if (!!attrs.noanimation) return statics;

function animEnabled(element) {
if (ngMajorVer === 1 && ngMinorVer >= 4) return !!$animate.enabled(element);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disabled anim on a per-element basis

if (ngMajorVer === 1 && ngMinorVer >= 2) return !!$animate.enabled();
return (!!$animator);
}

// ng 1.2+
if ($animate) {
return {
enter: function(element, target, cb) {
if (angular.version.minor > 2) {
if (!animEnabled(element)) {
statics.enter(element, target, cb);
} else if (angular.version.minor > 2) {
$animate.enter(element, null, target).then(cb);
} else {
$animate.enter(element, null, target, cb);
}
},
leave: function(element, cb) {
if (angular.version.minor > 2) {
if (!animEnabled(element)) {
statics.leave(element, cb);
} else if (angular.version.minor > 2) {
$animate.leave(element).then(cb);
} else {
$animate.leave(element, cb);
Expand All @@ -159,6 +175,7 @@ function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate)
};
}

// ng 1.1.5
if ($animator) {
var animate = $animator && $animator(scope, attrs);

Expand All @@ -168,7 +185,7 @@ function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate)
};
}

return statics();
return statics;
}

var directive = {
Expand Down
80 changes: 55 additions & 25 deletions test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,12 @@ describe('uiView', function () {
.state('l', lState)
.state('m', {
controller: function($scope) {
log += 'm;';
$scope.$on('$destroy', function() {
log += '$destroy(m);';
});
},
log += 'ctrl(m);';
$scope.$on('$destroy', function() { log += '$destroy(m);'; });
}
})
.state('n', {
controller: function($scope) {
log += 'n;';
},
controller: function($scope) { log += 'ctrl(n);'; }
});
}));

Expand All @@ -144,23 +140,6 @@ describe('uiView', function () {

describe('linking ui-directive', function () {

it('$destroy event is triggered after animation ends', inject(function($state, $q, $animate) {
elem.append($compile('<div><ui-view></ui-view></div>')(scope));

$state.transitionTo('m');
$q.flush();
expect(log).toBe('m;');
$state.transitionTo('n');
$q.flush();
if ($animate) {
expect(log).toBe('m;n;');
animateFlush($animate);
expect(log).toBe('m;n;$destroy(m);');
} else {
expect(log).toBe('m;$destroy(m);n;');
}
}));

it('anonymous ui-view should be replaced with the template of the current $state', inject(function ($state, $q) {
elem.append($compile('<div><ui-view></ui-view></div>')(scope));

Expand Down Expand Up @@ -594,5 +573,56 @@ describe('uiView', function () {
// No more animations
expect($animate.queue.length).toBe(0);
}));

it ('should disable animations if noanimation="true" is present', inject(function($state, $q, $compile, $animate) {
var content = 'Initial Content', animation;
elem.append($compile('<div><ui-view noanimation="true">' + content + '</ui-view></div>')(scope));

animation = $animate.queue.shift();
expect(animation).toBeUndefined();

$state.transitionTo(aState);
$q.flush();
animation = $animate.queue.shift();
expect(animation).toBeUndefined();
expect(elem.text()).toBe(aState.template);

$state.transitionTo(bState);
$q.flush();
animation = $animate.queue.shift();
expect(animation).toBeUndefined();
expect(elem.text()).toBe(bState.template);
}));

it('$destroy event is triggered after animation ends', inject(function($state, $q, $animate, $rootScope) {
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
$rootScope.$on('$stateChangeSuccess', function(evt, toState) { log += 'success(' + toState.name + ');'; });

$state.transitionTo('m');
$q.flush();
expect(log).toBe('success(m);ctrl(m);');
$state.transitionTo('n');
$q.flush();
if ($animate) {
expect(log).toBe('success(m);ctrl(m);success(n);ctrl(n);');
animateFlush($animate);
expect(log).toBe('success(m);ctrl(m);success(n);ctrl(n);$destroy(m);');
} else {
expect(log).toBe('success(m);ctrl(m);$destroy(m);success(n);ctrl(n);');
}
}));

it('$destroy event is triggered before $stateChangeSuccess if noanimation is present', inject(function($state, $q, $animate, $rootScope) {
elem.append($compile('<div><ui-view noanimation="true"></ui-view></div>')(scope));
$rootScope.$on('$stateChangeSuccess', function(evt, toState) { log += 'success(' + toState.name + ');'; });

$state.transitionTo('m');
$q.flush();
expect(log).toBe('success(m);ctrl(m);');
$state.transitionTo('n');
$q.flush();
expect(log).toBe('success(m);ctrl(m);success(n);$destroy(m);ctrl(n);');
}));

});
});