Skip to content

Commit e7a3481

Browse files
cherry-pick 2523bbd from 0.2.17
1 parent bec736a commit e7a3481

File tree

2 files changed

+71
-29
lines changed

2 files changed

+71
-29
lines changed

src/ng1/viewDirective.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var ngMajorVer = angular.version.major;
2+
var ngMinorVer = angular.version.minor;
13
/** @module view */ /** for typedoc */
24
/// <reference path='../../typings/angularjs/angular.d.ts' />
35

@@ -31,6 +33,9 @@ import {UIViewData} from "../view/interface";
3133
* service, {@link ui.router.state.$uiViewScroll}. This custom service let's you
3234
* scroll ui-view elements into view when they are populated during a state activation.
3335
*
36+
* @param {string=} noanimation If truthy, the non-animated renderer will be selected (no animations
37+
* will be applied to the ui-view)
38+
*
3439
* *Note: To revert back to old [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll)
3540
* functionality, call `$uiViewScrollProvider.useAnchorScroll()`.*
3641
*
@@ -124,16 +129,26 @@ $ViewDirective.$inject = ['$view', '$animate', '$uiViewScroll', '$interpolate',
124129
function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate, $q) {
125130

126131
function getRenderer(attrs, scope) {
132+
133+
function animEnabled(element) {
134+
if (!!attrs.noanimation) return false;
135+
return (ngMajorVer === 1 && ngMinorVer >= 4) ? !!$animate.enabled(element) : !!$animate.enabled();
136+
}
137+
127138
return {
128139
enter: function(element, target, cb) {
129-
if (angular.version.minor > 2) {
140+
if (!animEnabled(element)) {
141+
target.after(element); cb();
142+
} else if (angular.version.minor > 2) {
130143
$animate.enter(element, null, target).then(cb);
131144
} else {
132145
$animate.enter(element, null, target, cb);
133146
}
134147
},
135148
leave: function(element, cb) {
136-
if (angular.version.minor > 2) {
149+
if (!animEnabled(element)) {
150+
element.remove(); cb();
151+
} else if (angular.version.minor > 2) {
137152
$animate.leave(element).then(cb);
138153
} else {
139154
$animate.leave(element, cb);

test/viewDirectiveSpec.js

+54-27
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('uiView', function () {
1212
var log, scope, $compile, elem;
1313

1414
beforeEach(function() {
15-
var depends = ['ui.router'];
15+
var depends = ['ui.router', 'ui.router.state.events'];
1616

1717
try {
1818
angular.module('ngAnimate');
@@ -26,7 +26,8 @@ describe('uiView', function () {
2626
module('ui.router.test');
2727
});
2828

29-
beforeEach(module(function ($provide) {
29+
beforeEach(module(function ($provide, $stateEventsProvider) {
30+
$stateEventsProvider.enable();
3031
$provide.decorator('$uiViewScroll', function () {
3132
return jasmine.createSpy('$uiViewScroll');
3233
});
@@ -132,17 +133,13 @@ describe('uiView', function () {
132133
.state('m', {
133134
template: 'mState',
134135
controller: function($scope) {
135-
log += 'm;';
136-
$scope.$on('$destroy', function() {
137-
log += '$destroy(m);';
138-
});
139-
},
136+
log += 'ctrl(m);';
137+
$scope.$on('$destroy', function() { log += '$destroy(m);'; });
138+
}
140139
})
141140
.state('n', {
142141
template: 'nState',
143-
controller: function($scope) {
144-
log += 'n;';
145-
},
142+
controller: function($scope) { log += 'ctrl(n);'; }
146143
})
147144
.state('o', oState)
148145
}));
@@ -155,23 +152,6 @@ describe('uiView', function () {
155152

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

158-
it('$destroy event is triggered after animation ends', inject(function($state, $q, $animate) {
159-
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
160-
161-
$state.transitionTo('m');
162-
$q.flush();
163-
expect(log).toBe('m;');
164-
$state.transitionTo('n');
165-
$q.flush();
166-
if ($animate) {
167-
expect(log).toBe('m;n;');
168-
animateFlush($animate);
169-
expect(log).toBe('m;n;$destroy(m);');
170-
} else {
171-
expect(log).toBe('m;$destroy(m);n;');
172-
}
173-
}));
174-
175155
it('anonymous ui-view should be replaced with the template of the current $state', inject(function ($state, $q) {
176156
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
177157

@@ -610,6 +590,53 @@ describe('uiView', function () {
610590
// No more animations
611591
expect($animate.queue.length).toBe(0);
612592
}));
593+
594+
it ('should disable animations if noanimation="true" is present', inject(function($state, $q, $compile, $animate) {
595+
var content = 'Initial Content', animation;
596+
elem.append($compile('<div><ui-view noanimation="true">' + content + '</ui-view></div>')(scope));
597+
598+
animation = $animate.queue.shift();
599+
expect(animation).toBeUndefined();
600+
601+
$state.transitionTo(aState);
602+
$q.flush();
603+
animation = $animate.queue.shift();
604+
expect(animation).toBeUndefined();
605+
expect(elem.text()).toBe(aState.template);
606+
607+
$state.transitionTo(bState);
608+
$q.flush();
609+
animation = $animate.queue.shift();
610+
expect(animation).toBeUndefined();
611+
expect(elem.text()).toBe(bState.template);
612+
}));
613+
614+
describe('$destroy event', function() {
615+
it('is triggered after animation ends', inject(function($state, $q, $animate, $rootScope) {
616+
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
617+
618+
$state.transitionTo('m');
619+
$q.flush();
620+
expect(log).toBe('ctrl(m);');
621+
$state.transitionTo('n');
622+
$q.flush();
623+
624+
expect(log).toBe('ctrl(m);ctrl(n);');
625+
animateFlush($animate);
626+
expect(log).toBe('ctrl(m);ctrl(n);$destroy(m);');
627+
}));
628+
629+
it('is triggered before $stateChangeSuccess if noanimation is present', inject(function($state, $q, $animate, $rootScope) {
630+
elem.append($compile('<div><ui-view noanimation="true"></ui-view></div>')(scope));
631+
632+
$state.transitionTo('m');
633+
$q.flush();
634+
expect(log).toBe('ctrl(m);');
635+
$state.transitionTo('n');
636+
$q.flush();
637+
expect(log).toBe('ctrl(m);$destroy(m);ctrl(n);');
638+
}));
639+
});
613640
});
614641
});
615642

0 commit comments

Comments
 (0)