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

feat(collapse): convert to use $animateCss #4257

Closed
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
7 changes: 5 additions & 2 deletions src/accordion/test/accordion.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
describe('accordion', function() {
var $scope;
var $animate, $scope;

beforeEach(module('ui.bootstrap.accordion'));
beforeEach(module('ui.bootstrap.collapse'));
beforeEach(module('ngAnimateMock'));
beforeEach(module('template/accordion/accordion.html'));
beforeEach(module('template/accordion/accordion-group.html'));

beforeEach(inject(function($rootScope) {
beforeEach(inject(function(_$animate_, $rootScope) {
$animate = _$animate_;
$scope = $rootScope;
}));

Expand Down Expand Up @@ -358,6 +360,7 @@ describe('accordion', function() {
angular.element(document.body).append(element);
$compile(element)(scope);
scope.$digest();
$animate.flush();
groups = element.find('.panel');
});

Expand Down
39 changes: 28 additions & 11 deletions src/collapse/collapse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
angular.module('ui.bootstrap.collapse', [])

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

$animate.addClass(element, 'in', {
to: { height: element[0].scrollHeight + 'px' }
}).then(expandDone);
if ($animateCss) {
$animateCss(element, {
addClass: 'in',
easing: 'ease',
to: { height: element[0].scrollHeight + 'px' }
}).start().done(expandDone);
} else {
$animate.addClass(element, 'in', {
to: { height: element[0].scrollHeight + 'px' }
}).then(expandDone);
}
}

function expandDone() {
element.removeClass('collapsing');
element.css({height: 'auto'});
element.removeClass('collapsing')
.addClass('collapse')
.css({height: 'auto'});
}

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

$animate.removeClass(element, 'in', {
to: {height: '0'}
}).then(collapseDone);
if ($animateCss) {
$animateCss(element, {
removeClass: 'in',
to: {height: '0'}
}).start().done(collapseDone);
} else {
$animate.removeClass(element, 'in', {
to: {height: '0'}
}).then(collapseDone);
}
}

function collapseDone() {
element.css({height: '0'}); // Required so that collapse works when animation is disabled
element.removeClass('collapsing');
element.addClass('collapse');
element.removeClass('collapsing')
.addClass('collapse');
}

scope.$watch(attrs.collapse, function(shouldCollapse) {
Expand Down
15 changes: 15 additions & 0 deletions src/collapse/test/collapse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,70 @@ describe('collapse directive', function() {
it('should collapse if isCollapsed = true with animation on subsequent use', function() {
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
scope.isCollapsed = true;
scope.$digest();
$animate.flush();
expect(element.height()).toBe(0);
});

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

it('should expand if isCollapsed = false with animation on subsequent use', function() {
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
scope.isCollapsed = true;
scope.$digest();
$animate.flush();
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
expect(element.height()).not.toBe(0);
});

it('should expand if isCollapsed = true with animation on subsequent uses', function() {
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
scope.isCollapsed = true;
scope.$digest();
$animate.flush();
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
scope.isCollapsed = true;
scope.$digest();
$animate.flush();
expect(element.height()).toBe(0);
});

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

scope.isCollapsed = true;
scope.$digest();
$animate.flush();
expect(element.attr('aria-expanded')).toBe('false');
});

it('should change aria-hidden attribute', function() {
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
expect(element.attr('aria-hidden')).toBe('false');

scope.isCollapsed = true;
scope.$digest();
$animate.flush();
expect(element.attr('aria-hidden')).toBe('true');
});

Expand Down Expand Up @@ -111,6 +125,7 @@ describe('collapse directive', function() {
scope.isCollapsed = false;
scope.$digest();
$animate.flush();
scope.$digest();
var collapseHeight = element.height();
scope.exp = false;
scope.$digest();
Expand Down