forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.js
115 lines (102 loc) · 5.14 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
angular.module('ui.bootstrap.collapse',['ui.bootstrap.transition'])
// The collapsible directive indicates a block of html that will expand and collapse
.directive('collapse', ['$transition', function($transition) {
// CSS transitions don't work with height: auto, so we have to manually change the height to a
// specific value and then once the animation completes, we can reset the height to auto.
// Unfortunately if you do this while the CSS transitions are specified (i.e. in the CSS class
// "collapse") then you trigger a change to height 0 in between.
// The fix is to remove the "collapse" CSS class while changing the height back to auto - phew!
// NOTE:
// Removed the fixUpHeight function, for now, as more class manipulation was needed than before.
return {
link: function(scope, element, attrs) {
var isCollapsed;
var initialAnimSkip = true;
scope.$watch(function (){ return element[0].scrollHeight; }, function (value) {
//The listener is called when scrollHeight changes
//It actually does on 2 scenarios:
// 1. Parent is set to display none
// 2. angular bindings inside are resolved
//When we have a change of scrollHeight we are setting again the correct height if the group is opened
if (element[0].scrollHeight !== 0) {
if (!isCollapsed) {
if (initialAnimSkip) {
element.css({height: 'auto'});
element.removeClass('collapse in collapsing');
var x = element[0].offsetWidth;
element.addClass('collapse in');
} else {
element.css({height: element[0].scrollHeight + 'px'});
element.removeClass('collapse in collapsing');
var x = element[0].offsetWidth;
element.addClass('collapse in');
}
}
}
});
scope.$watch(attrs.collapse, function(value) {
if (value) {
collapse();
} else {
expand();
}
});
var currentTransition;
var doTransition = function(change) {
if ( currentTransition ) {
currentTransition.cancel();
}
currentTransition = $transition(element,change);
currentTransition.then(
function() { currentTransition = undefined;},
function() { currentTransition = undefined;}
).then(
function() { animationComplete(); }
);
};
var animationComplete = function() {
element.removeClass('collapsing');
var x = element[0].offsetWidth;
if (isCollapsed) {
element.addClass('collapse');
console.log('Animation Complete isCollapsed Fired');
}
else {
element.addClass('collapse in');
element.css({height: 'auto'});
console.log('Animation Complete Not isCollapsed Fired');
}
};
var expand = function() {
if (initialAnimSkip) {
initialAnimSkip = false;
animationComplete();
} else {
// Switch to collapsing for the transition
element.removeClass('collapse in');
var x = element[0].offsetWidth;
element.addClass('collapsing');
doTransition({ height : element[0].scrollHeight + 'px' });
}
isCollapsed = false;
};
var collapse = function() {
isCollapsed = true;
if (initialAnimSkip) {
initialAnimSkip = false;
animationComplete();
} else {
// Switch to collapsing for the transition
element.removeClass('collapse in');
var x = element[0].offsetWidth;
element.addClass('collapsing');
var scrollHeight = element[0].scrollHeight + 'px';
console.log('Height: ' + element[0].style.cssText);
element.css({height: scrollHeight});
console.log('Height: ' + element[0].style.cssText);
doTransition({'height':'0px'});
}
};
}
};
}]);