forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.js
116 lines (100 loc) · 3.35 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
angular.module('ui.bootstrap.collapse', [])
.directive('collapse', ['$animate', function ($animate) {
return {
link: function (scope, element, attrs) {
var horizontal = element.hasClass('width');
var contents;
if(horizontal){
// get all .content children so we can fix their width
var children = element.children();
for(var i=0; i < children.length; i++){
var child = angular.element(children[i]);
if(child.hasClass('content')){
if(!contents){ contents = []; }
contents.push(child);
}
}
}
function fixContentWidth(unfix){
angular.forEach(contents, function(content){
content.css({ width: unfix?'auto':content[0].scrollWidth+'px' });
});
}
function expand() {
element.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', true)
.attr('aria-hidden', false);
var animate = { to: {} };
if(horizontal){
animate.to.width = element[0].scrollWidth + 'px';
fixContentWidth();
}
else{
animate.to.height = element[0].scrollHeight + 'px';
}
$animate.addClass(element, 'in', animate).then(expandDone);
}
function expandDone() {
var css = {};
if(horizontal){
css.width = 'auto';
fixContentWidth(true);
}
else{
css.height = 'auto';
}
element.removeClass('collapsing');
element.css(css);
}
function collapse() {
if(! element.hasClass('collapse') && ! element.hasClass('in')) {
return collapseDone();
}
var css = {};
var animate = { to: {} };
if(horizontal){
css.width = element[0].scrollWidth + 'px';
animate.to.width = '0';
fixContentWidth();
}
else{
css.height = element[0].scrollHeight + 'px';
animate.to.height = '0';
}
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(css)
// 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);
$animate.removeClass(element, 'in', animate).then(collapseDone);
}
function collapseDone() {
var css = {};
if(horizontal){
css.width = '0';
fixContentWidth();
}
else{
css.height = '0';
}
element.css(css); // Required so that collapse works when animation is disabled
element.removeClass('collapsing');
element.addClass('collapse');
}
scope.$watch(attrs.collapse, function (shouldCollapse) {
if (shouldCollapse) {
collapse();
} else {
expand();
}
});
}
};
}]);