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

Commit bca2e25

Browse files
committed
feat(collapse): optimize attribute parsing and standardize promises
1 parent c3c43b2 commit bca2e25

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

Diff for: src/collapse/collapse.js

+50-43
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,86 @@
11
angular.module('ui.bootstrap.collapse', [])
22

3-
.directive('uibCollapse', ['$animate', '$q', '$injector', function($animate, $q, $injector) {
3+
.directive('uibCollapse', ['$animate', '$q', '$parse', '$injector', function($animate, $q, $parse, $injector) {
44
var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
55
return {
66
link: function(scope, element, attrs) {
7+
var expandingExpr = $parse(attrs.expanding),
8+
expandedExpr = $parse(attrs.expanded),
9+
collapsingExpr = $parse(attrs.collapsing),
10+
collapsedExpr = $parse(attrs.collapsed);
11+
712
if (!scope.$eval(attrs.uibCollapse)) {
813
element.addClass('in')
914
.addClass('collapse')
1015
.css({height: 'auto'});
1116
}
1217

1318
function expand() {
14-
$q.when(scope.$eval(attrs.expanding), function() {
15-
element.removeClass('collapse')
16-
.addClass('collapsing')
17-
.attr('aria-expanded', true)
18-
.attr('aria-hidden', false);
19+
$q.resolve(expandingExpr(scope))
20+
.then(function() {
21+
element.removeClass('collapse')
22+
.addClass('collapsing')
23+
.attr('aria-expanded', true)
24+
.attr('aria-hidden', false);
1925

20-
if ($animateCss) {
21-
$animateCss(element, {
22-
addClass: 'in',
23-
easing: 'ease',
24-
to: { height: element[0].scrollHeight + 'px' }
25-
}).start()['finally'](expandDone);
26-
} else {
27-
$animate.addClass(element, 'in', {
28-
to: { height: element[0].scrollHeight + 'px' }
29-
}).then(expandDone);
30-
}
31-
});
26+
if ($animateCss) {
27+
$animateCss(element, {
28+
addClass: 'in',
29+
easing: 'ease',
30+
to: { height: element[0].scrollHeight + 'px' }
31+
}).start()['finally'](expandDone);
32+
} else {
33+
$animate.addClass(element, 'in', {
34+
to: { height: element[0].scrollHeight + 'px' }
35+
}).then(expandDone);
36+
}
37+
});
3238
}
3339

3440
function expandDone() {
3541
element.removeClass('collapsing')
3642
.addClass('collapse')
3743
.css({height: 'auto'});
38-
scope.$eval(attrs.expanded);
44+
expandedExpr(scope);
3945
}
4046

4147
function collapse() {
4248
if (!element.hasClass('collapse') && !element.hasClass('in')) {
4349
return collapseDone();
4450
}
4551

46-
$q.when(scope.$eval(attrs.collapsing), function() {
47-
element
48-
// IMPORTANT: The height must be set before adding "collapsing" class.
49-
// Otherwise, the browser attempts to animate from height 0 (in
50-
// collapsing class) to the given height here.
51-
.css({height: element[0].scrollHeight + 'px'})
52-
// initially all panel collapse have the collapse class, this removal
53-
// prevents the animation from jumping to collapsed state
54-
.removeClass('collapse')
55-
.addClass('collapsing')
56-
.attr('aria-expanded', false)
57-
.attr('aria-hidden', true);
52+
$q.resolve(collapsingExpr(scope))
53+
.then(function() {
54+
element
55+
// IMPORTANT: The height must be set before adding "collapsing" class.
56+
// Otherwise, the browser attempts to animate from height 0 (in
57+
// collapsing class) to the given height here.
58+
.css({height: element[0].scrollHeight + 'px'})
59+
// initially all panel collapse have the collapse class, this removal
60+
// prevents the animation from jumping to collapsed state
61+
.removeClass('collapse')
62+
.addClass('collapsing')
63+
.attr('aria-expanded', false)
64+
.attr('aria-hidden', true);
5865

59-
if ($animateCss) {
60-
$animateCss(element, {
61-
removeClass: 'in',
62-
to: {height: '0'}
63-
}).start()['finally'](collapseDone);
64-
} else {
65-
$animate.removeClass(element, 'in', {
66-
to: {height: '0'}
67-
}).then(collapseDone);
68-
}
69-
});
66+
if ($animateCss) {
67+
$animateCss(element, {
68+
removeClass: 'in',
69+
to: {height: '0'}
70+
}).start()['finally'](collapseDone);
71+
} else {
72+
$animate.removeClass(element, 'in', {
73+
to: {height: '0'}
74+
}).then(collapseDone);
75+
}
76+
});
7077
}
7178

7279
function collapseDone() {
7380
element.css({height: '0'}); // Required so that collapse works when animation is disabled
7481
element.removeClass('collapsing')
7582
.addClass('collapse');
76-
scope.$eval(attrs.collapsed);
83+
collapsedExpr(scope);
7784
}
7885

7986
scope.$watch(attrs.uibCollapse, function(shouldCollapse) {

0 commit comments

Comments
 (0)