Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 533a9f0

Browse files
wesleychoFoxandxss
authored andcommitted
feat(collapse): convert to use $animateCss
- Change to use `$animateCss` when available Closes #4257
1 parent 0a69580 commit 533a9f0

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/accordion/test/accordion.spec.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
describe('accordion', function() {
2-
var $scope;
2+
var $animate, $scope;
33

44
beforeEach(module('ui.bootstrap.accordion'));
55
beforeEach(module('ui.bootstrap.collapse'));
6+
beforeEach(module('ngAnimateMock'));
67
beforeEach(module('template/accordion/accordion.html'));
78
beforeEach(module('template/accordion/accordion-group.html'));
89

9-
beforeEach(inject(function($rootScope) {
10+
beforeEach(inject(function(_$animate_, $rootScope) {
11+
$animate = _$animate_;
1012
$scope = $rootScope;
1113
}));
1214

@@ -358,6 +360,7 @@ describe('accordion', function() {
358360
angular.element(document.body).append(element);
359361
$compile(element)(scope);
360362
scope.$digest();
363+
$animate.flush();
361364
groups = element.find('.panel');
362365
});
363366

src/collapse/collapse.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
angular.module('ui.bootstrap.collapse', [])
22

3-
.directive('collapse', ['$animate', function($animate) {
3+
.directive('collapse', ['$animate', '$injector', function($animate, $injector) {
4+
var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
45
return {
56
link: function(scope, element, attrs) {
67
function expand() {
@@ -9,14 +10,23 @@ angular.module('ui.bootstrap.collapse', [])
910
.attr('aria-expanded', true)
1011
.attr('aria-hidden', false);
1112

12-
$animate.addClass(element, 'in', {
13-
to: { height: element[0].scrollHeight + 'px' }
14-
}).then(expandDone);
13+
if ($animateCss) {
14+
$animateCss(element, {
15+
addClass: 'in',
16+
easing: 'ease',
17+
to: { height: element[0].scrollHeight + 'px' }
18+
}).start().done(expandDone);
19+
} else {
20+
$animate.addClass(element, 'in', {
21+
to: { height: element[0].scrollHeight + 'px' }
22+
}).then(expandDone);
23+
}
1524
}
1625

1726
function expandDone() {
18-
element.removeClass('collapsing');
19-
element.css({height: 'auto'});
27+
element.removeClass('collapsing')
28+
.addClass('collapse')
29+
.css({height: 'auto'});
2030
}
2131

2232
function collapse() {
@@ -36,15 +46,22 @@ angular.module('ui.bootstrap.collapse', [])
3646
.attr('aria-expanded', false)
3747
.attr('aria-hidden', true);
3848

39-
$animate.removeClass(element, 'in', {
40-
to: {height: '0'}
41-
}).then(collapseDone);
49+
if ($animateCss) {
50+
$animateCss(element, {
51+
removeClass: 'in',
52+
to: {height: '0'}
53+
}).start().done(collapseDone);
54+
} else {
55+
$animate.removeClass(element, 'in', {
56+
to: {height: '0'}
57+
}).then(collapseDone);
58+
}
4259
}
4360

4461
function collapseDone() {
4562
element.css({height: '0'}); // Required so that collapse works when animation is disabled
46-
element.removeClass('collapsing');
47-
element.addClass('collapse');
63+
element.removeClass('collapsing')
64+
.addClass('collapse');
4865
}
4966

5067
scope.$watch(attrs.collapse, function(shouldCollapse) {

src/collapse/test/collapse.spec.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -28,56 +28,70 @@ describe('collapse directive', function() {
2828
it('should collapse if isCollapsed = true with animation on subsequent use', function() {
2929
scope.isCollapsed = false;
3030
scope.$digest();
31+
$animate.flush();
3132
scope.isCollapsed = true;
3233
scope.$digest();
34+
$animate.flush();
3335
expect(element.height()).toBe(0);
3436
});
3537

3638
it('should be shown on initialization if isCollapsed = false without transition', function() {
3739
scope.isCollapsed = false;
3840
scope.$digest();
41+
$animate.flush();
3942
expect(element.height()).not.toBe(0);
4043
});
4144

4245
it('should expand if isCollapsed = false with animation on subsequent use', function() {
4346
scope.isCollapsed = false;
4447
scope.$digest();
48+
$animate.flush();
4549
scope.isCollapsed = true;
4650
scope.$digest();
51+
$animate.flush();
4752
scope.isCollapsed = false;
4853
scope.$digest();
54+
$animate.flush();
4955
expect(element.height()).not.toBe(0);
5056
});
5157

5258
it('should expand if isCollapsed = true with animation on subsequent uses', function() {
5359
scope.isCollapsed = false;
5460
scope.$digest();
61+
$animate.flush();
5562
scope.isCollapsed = true;
5663
scope.$digest();
64+
$animate.flush();
5765
scope.isCollapsed = false;
5866
scope.$digest();
67+
$animate.flush();
5968
scope.isCollapsed = true;
6069
scope.$digest();
70+
$animate.flush();
6171
expect(element.height()).toBe(0);
6272
});
6373

6474
it('should change aria-expanded attribute', function() {
6575
scope.isCollapsed = false;
6676
scope.$digest();
77+
$animate.flush();
6778
expect(element.attr('aria-expanded')).toBe('true');
6879

6980
scope.isCollapsed = true;
7081
scope.$digest();
82+
$animate.flush();
7183
expect(element.attr('aria-expanded')).toBe('false');
7284
});
7385

7486
it('should change aria-hidden attribute', function() {
7587
scope.isCollapsed = false;
7688
scope.$digest();
89+
$animate.flush();
7790
expect(element.attr('aria-hidden')).toBe('false');
7891

7992
scope.isCollapsed = true;
8093
scope.$digest();
94+
$animate.flush();
8195
expect(element.attr('aria-hidden')).toBe('true');
8296
});
8397

@@ -98,10 +112,9 @@ describe('collapse directive', function() {
98112
scope.exp = false;
99113
scope.isCollapsed = false;
100114
scope.$digest();
101-
$animate.flush();
102-
scope.$digest();
103115
var collapseHeight = element.height();
104116
scope.exp = true;
117+
$animate.flush();
105118
scope.$digest();
106119
expect(element.height()).toBeGreaterThan(collapseHeight);
107120
});
@@ -110,7 +123,6 @@ describe('collapse directive', function() {
110123
scope.exp = true;
111124
scope.isCollapsed = false;
112125
scope.$digest();
113-
$animate.flush();
114126
var collapseHeight = element.height();
115127
scope.exp = false;
116128
scope.$digest();

0 commit comments

Comments
 (0)