|
1 | 1 | angular.module('ui.bootstrap.collapse', [])
|
2 | 2 |
|
3 |
| - .directive('uibCollapse', ['$animate', '$q', '$injector', function($animate, $q, $injector) { |
| 3 | + .directive('uibCollapse', ['$animate', '$q', '$parse', '$injector', function($animate, $q, $parse, $injector) { |
4 | 4 | var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
|
5 | 5 | return {
|
6 | 6 | 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 | + |
7 | 12 | if (!scope.$eval(attrs.uibCollapse)) {
|
8 | 13 | element.addClass('in')
|
9 | 14 | .addClass('collapse')
|
10 | 15 | .css({height: 'auto'});
|
11 | 16 | }
|
12 | 17 |
|
13 | 18 | 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); |
19 | 25 |
|
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 | + }); |
32 | 38 | }
|
33 | 39 |
|
34 | 40 | function expandDone() {
|
35 | 41 | element.removeClass('collapsing')
|
36 | 42 | .addClass('collapse')
|
37 | 43 | .css({height: 'auto'});
|
38 |
| - scope.$eval(attrs.expanded); |
| 44 | + expandedExpr(scope); |
39 | 45 | }
|
40 | 46 |
|
41 | 47 | function collapse() {
|
42 | 48 | if (!element.hasClass('collapse') && !element.hasClass('in')) {
|
43 | 49 | return collapseDone();
|
44 | 50 | }
|
45 | 51 |
|
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); |
58 | 65 |
|
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 | + }); |
70 | 77 | }
|
71 | 78 |
|
72 | 79 | function collapseDone() {
|
73 | 80 | element.css({height: '0'}); // Required so that collapse works when animation is disabled
|
74 | 81 | element.removeClass('collapsing')
|
75 | 82 | .addClass('collapse');
|
76 |
| - scope.$eval(attrs.collapsed); |
| 83 | + collapsedExpr(scope); |
77 | 84 | }
|
78 | 85 |
|
79 | 86 | scope.$watch(attrs.uibCollapse, function(shouldCollapse) {
|
|
0 commit comments