-
Notifications
You must be signed in to change notification settings - Fork 3
/
angular-collapse.js
277 lines (226 loc) · 7.25 KB
/
angular-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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
angular.module('angular-collapse', [])
.directive('ngCollapse', ['$animate', '$timeout', function($animate, $timeout){
return {
link: function(scope, element, attrs){
// main collapse class, use collapse-options to override transition settings
element.addClass('collapse');
var init = true;
var config;
var animator;
// read in class="content" children so we can fix the size
var contents;
var children = element.children();
for(var i=0; i < children.length; i++){
var content = angular.element(children[i]);
if(content.hasClass('content')){
if(!contents){ contents = []; }
contents.push({ element: content,
originalWidth: content[0].style.width || (content.css('width') != '0px'?content.css('width'):'')
});
}
}
function fixContentSize(reset){
angular.forEach(contents, function(content){
if(!content.originalWidth || content.originalWidth == 'auto'){
if(!reset){
var size = getRenderSize(content.element);
content.renderWidth = size.width+'px';
content.element.css( { width: content.renderWidth } );
}
else{
content.element.css( { width: content.originalWidth } );
}
}
});
}
function getRenderSize(element){
var size = {};
var clone = element.clone();
clone.css({"visibility":"visible", "display":"table"});
element[0].parentElement.appendChild(clone[0]);
size.width = clone[0].clientWidth;
size.height = clone[0].clientHeight;
element[0].parentElement.removeChild(clone[0]);
return size;
}
function setTransitionProperties(properties){
var css = {};
if(angular.isUndefined(properties)){
// reset transition properties
css['-webkit-transition-property'] = css['transition-property'] = '';
css['-webkit-transition-duration'] = css['transition-duration'] = '';
css['-webkit-transition-timing-function'] = css['transition-timing-function'] = '';
}
else{
// configure transition properties
css['-webkit-transition-property'] = css['transition-property'] = properties.join(',');
if(config && config.duration){
css['-webkit-transition-duration'] = css['transition-duration'] = config.duration;
}
if(config && config.timing){
css['-webkit-transition-timing-function'] = css['transition-timing-function'] = config.timing;
}
}
element.css(css);
}
function expand(){
if(init){ return expandDone(); }
// before setting up the animation, add the in class so the element is displayed (and we can read its rendered dimensions)
element.addClass('in')
.attr('aria-expanded', true)
.attr('aria-hidden', false);
// fix inner content element sizes
fixContentSize();
var css = {};
var animate = { to: {}, from: {} };
// build the animation
var size = getRenderSize(element);
// vertical
if(!config || config.vertical){
animate.from.height = '0';
animate.to.height = size.height+'px';
if(config && config.vertical == 'bottom'){
css.position = 'relative';
animate.from.top = size.height+'px';
animate.to.top = '0';
}
}
// horizontal
if(config && config.horizontal){
animate.from.width = '0';
animate.to.width = size.width+'px';
if(config.horizontal == 'right'){
css.position = 'relative';
animate.from.left = size.width+'px';
animate.to.left = '0';
}
}
// set the appropriate transition properties
var animate_props = Object.keys(animate.to);
animate_props.push('visibility');
setTransitionProperties(animate_props);
// apply the css needed for the transition
element.css(css)
// cancel any existing animations
if(animator){
$animate.cancel(animator);
}
// animate with the collapsing class applied
animator = $animate.animate(element, animate.from, animate.to, 'collapsing');
animator.then(expandDone);
}
function expandDone(){
if(animator){ animator = null; }
// reset element's style for expanded state
var css = {};
// reset inner content element sizes
fixContentSize(true);
// vetical
if(!config || config.vertical){
css.height = '';
if(config && config.vertical == 'bottom'){
css.position = '';
css.top = '';
}
}
// horizontal
if(config && config.horizontal){
css.width = '';
if(config.horizontal == 'right'){
css.position = '';
css.left = '';
}
}
// reset transition properties
setTransitionProperties();
// apply post-expand css
element.css(css)
.addClass('in');
}
function collapse(){
if(init){ return collapseDone(); }
var css = {};
var animate = { to: {}, from: {} };
var size = getRenderSize(element);
if(!config || config.vertical){
animate.from.height = size.height+'px';
animate.to.height = '0';
if(config && config.vertical == 'bottom'){
css.position = 'relative';
animate.from.top = '0';
animate.to.top = size.height+'px';
}
}
if(config && config.horizontal){
animate.from.width = size.width+'px';
animate.to.width = '0';
if(config.horizontal == 'right'){
css.position = 'relative';
animate.from.left = '0';
animate.to.left = size.width+'px';
}
}
// fix inner content element sizes
fixContentSize();
// set the appropriate transition properties
var animate_props = Object.keys(animate.to);
animate_props.push('visibility');
setTransitionProperties(animate_props);
// apply the css needed for the transition
element.css(css)
.removeClass('in') // remove the in class before animating to prevent flicker at end (collapsing class has display: block)
.attr('aria-expanded', false)
.attr('aria-hidden', true);
// cancel any existing animations
if(animator){
$animate.cancel(animator);
}
// animate with the collapsing class applied
animator = $animate.animate(element, animate.from, animate.to, 'collapsing');
animator.then(collapseDone);
}
function collapseDone(){
if(animator){ animator = null; }
// reset element's style for collapsed state
var css = {};
// reset inner content element sizes
fixContentSize(true);
// vertical
if(!config || config.vertical){
css.height = '0';
if(config && config.vertical == 'bottom'){
css.position = '';
css.top = '';
}
}
// horizontal
if(config && config.horizontal){
css.width = '0';
if(config.horizontal == 'right'){
css.position = '';
css.left = '';
}
}
// reset transition properties
setTransitionProperties();
// apply post-collapse css
element.css(css);
}
// watch the ng-collapse toggler
scope.$watch(attrs.ngCollapse, function(shouldCollapse){
$timeout(function(){
if (shouldCollapse) {
collapse();
} else {
expand();
}
if(init){ init = false; } // initialization done
});
});
// watch the collapse-options, reading them into config
scope.$watchCollection(attrs.collapseOptions, function(options){
config = options;
});
}
};
}]);