This repository was archived by the owner on Oct 14, 2023. It is now read-only.
forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.js
162 lines (149 loc) · 5.83 KB
/
collapse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
angular.module('ui.bootstrap.collapse', [])
.directive('uibCollapse', ['$animate', '$q', '$parse', '$injector', function($animate, $q, $parse, $injector) {
var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
return {
link: function(scope, element, attrs) {
var expandingExpr = $parse(attrs.expanding),
expandedExpr = $parse(attrs.expanded),
collapsingExpr = $parse(attrs.collapsing),
collapsedExpr = $parse(attrs.collapsed),
horizontal = false;
horizontal = !!('horizontal' in attrs);
if (!scope.$eval(attrs.uibCollapse)) {
if (horizontal) {
element.addClass('in')
.addClass('collapse')
.attr('aria-expanded', true)
.attr('aria-hidden', false)
.css({width: 'auto'})
.css({height: 'inherit'});
} else {
element.addClass('in')
.addClass('collapse')
.attr('aria-expanded', true)
.attr('aria-hidden', false)
.css({height: 'auto'});
}
}
function expand() {
if (element.hasClass('collapse') && element.hasClass('in')) {
return;
}
$q.resolve(expandingExpr(scope))
.then(function() {
element.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', true)
.attr('aria-hidden', false);
if (horizontal) {
if ($animateCss) {
$animateCss(element, {
addClass: 'in',
easing: 'ease',
to: {width: element[0].scrollWidth + 'px'}
}).start()['finally'](expandDone);
} else {
$animate.addClass(element, 'in', {
to: {width: element[0].scrollWidth + 'px'}
}).then(expandDone);
}
} else {
if ($animateCss) {
$animateCss(element, {
addClass: 'in',
easing: 'ease',
to: {height: element[0].scrollHeight + 'px'}
}).start()['finally'](expandDone);
} else {
$animate.addClass(element, 'in', {
to: {height: element[0].scrollHeight + 'px'}
}).then(expandDone);
}
}
});
}
function expandDone() {
if (horizontal) {
element.removeClass('collapsing')
.addClass('collapse')
.css({width: 'auto'});
} else {
element.removeClass('collapsing')
.addClass('collapse')
.css({height: 'auto'});
}
expandedExpr(scope);
}
function collapse() {
if (!element.hasClass('collapse') && !element.hasClass('in')) {
return collapseDone();
}
$q.resolve(collapsingExpr(scope))
.then(function() {
if (horizontal) {
element
// IMPORTANT: The width must be set before adding "collapsing" class.
// Otherwise, the browser attempts to animate from width 0 (in
// collapsing class) to the given width here.
.css({width: element[0].scrollWidth + 'px'})
// initially all panel collapse have the collapse class, this removal
// prevents the animation from jumping to collapsed state
.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', false)
.attr('aria-hidden', true);
if ($animateCss) {
$animateCss(element, {
removeClass: 'in',
to: {width: '0'}
}).start()['finally'](collapseDone);
} else {
$animate.removeClass(element, 'in', {
to: {width: '0'}
}).then(collapseDone);
}
} else {
element
// IMPORTANT: The height must be set before adding "collapsing" class.
// Otherwise, the browser attempts to animate from height 0 (in
// collapsing class) to the given height here.
.css({height: element[0].scrollHeight + 'px'})
// initially all panel collapse have the collapse class, this removal
// prevents the animation from jumping to collapsed state
.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', false)
.attr('aria-hidden', true);
if ($animateCss) {
$animateCss(element, {
removeClass: 'in',
to: {height: '0'}
}).start()['finally'](collapseDone);
} else {
$animate.removeClass(element, 'in', {
to: {height: '0'}
}).then(collapseDone);
}
}
});
}
function collapseDone() {
if (horizontal) {
element.css({width: '0'}); // Required so that collapse works when animation is disabled
} else {
element.css({height: '0'}); // Required so that collapse works when animation is disabled
}
element.removeClass('collapsing')
.addClass('collapse');
collapsedExpr(scope);
}
scope.$watch(attrs.uibCollapse, function(shouldCollapse) {
if (shouldCollapse) {
collapse();
} else {
expand();
}
});
}
};
}]);