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

Commit 13df4eb

Browse files
SidhNorpkozlowski-opensource
authored andcommitted
fix(collapsible): Fixed #94 by skipping any transitions on initialization
1 parent 1687aa7 commit 13df4eb

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/collapse/collapse.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('ui.bootstrap.collapse',['ui.bootstrap.transition'])
2121
link: function(scope, element, attrs) {
2222

2323
var isCollapsed;
24-
24+
var initialAnimSkip = true;
2525
scope.$watch(function (){ return element[0].scrollHeight; }, function (value) {
2626
//The listener is called when scollHeight changes
2727
//It actually does on 2 scenarios:
@@ -30,7 +30,7 @@ angular.module('ui.bootstrap.collapse',['ui.bootstrap.transition'])
3030
//When we have a change of scrollHeight we are setting again the correct height if the group is opened
3131
if (element[0].scrollHeight !== 0) {
3232
if (!isCollapsed) {
33-
doTransition({ height : element[0].scrollHeight + 'px' });
33+
fixUpHeight(scope, element, element[0].scrollHeight + 'px');
3434
}
3535
}
3636
});
@@ -58,21 +58,33 @@ angular.module('ui.bootstrap.collapse',['ui.bootstrap.transition'])
5858
};
5959

6060
var expand = function() {
61-
doTransition({ height : element[0].scrollHeight + 'px' })
62-
.then(function() {
63-
// This check ensures that we don't accidentally update the height if the user has closed
64-
// the group while the animation was still running
61+
if (initialAnimSkip) {
62+
initialAnimSkip = false;
6563
if ( !isCollapsed ) {
6664
fixUpHeight(scope, element, 'auto');
6765
}
68-
});
66+
} else {
67+
doTransition({ height : element[0].scrollHeight + 'px' })
68+
.then(function() {
69+
// This check ensures that we don't accidentally update the height if the user has closed
70+
// the group while the animation was still running
71+
if ( !isCollapsed ) {
72+
fixUpHeight(scope, element, 'auto');
73+
}
74+
});
75+
}
6976
isCollapsed = false;
7077
};
7178

7279
var collapse = function() {
7380
isCollapsed = true;
74-
fixUpHeight(scope, element, element[0].scrollHeight + 'px');
75-
doTransition({'height':'0'});
81+
if (initialAnimSkip) {
82+
initialAnimSkip = false;
83+
fixUpHeight(scope, element, 0);
84+
} else {
85+
fixUpHeight(scope, element, element[0].scrollHeight + 'px');
86+
doTransition({'height':'0'});
87+
}
7688
};
7789
}
7890
};

src/collapse/test/collapse.spec.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,35 @@ describe('collapse directive', function () {
1717
angular.element(document.body).append(element);
1818
});
1919

20-
it('should collapse if isCollapsed = true', function() {
20+
it('should be hidden on initialization if isCollapsed = true without transition', function() {
21+
scope.isCollapsed = true;
22+
scope.$digest();
23+
//No animation timeout here
24+
expect(element.height()).toBe(0);
25+
});
26+
27+
it('should collapse if isCollapsed = true with animation on subsequent use', function() {
28+
scope.isCollapsed = false;
29+
scope.$digest();
2130
scope.isCollapsed = true;
2231
scope.$digest();
2332
$timeout.flush();
2433
expect(element.height()).toBe(0);
2534
});
2635

27-
it('should expand if isCollapsed = false', function() {
36+
it('should be shown on initialization if isCollapsed = false without transition', function() {
37+
scope.isCollapsed = false;
38+
scope.$digest();
39+
//No animation timeout here
40+
expect(element.height()).not.toBe(0);
41+
});
42+
43+
it('should expand if isCollapsed = false on subsequent use', function() {
44+
scope.isCollapsed = true;
45+
scope.$digest();
2846
scope.isCollapsed = false;
2947
scope.$digest();
3048
$timeout.flush();
3149
expect(element.height()).not.toBe(0);
32-
});
50+
});
3351
});

0 commit comments

Comments
 (0)