@@ -33,6 +33,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
33
33
34
34
static ngAcceptInputType_horizontal : BooleanInput ;
35
35
static ngAcceptInputType_navbar : BooleanInput ;
36
+ static ngAcceptInputType_visible : BooleanInput ;
36
37
37
38
/**
38
39
* @ignore
@@ -41,9 +42,11 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
41
42
set animate ( value : boolean ) {
42
43
this . _animate = value ;
43
44
}
45
+
44
46
get animate ( ) : boolean {
45
47
return this . _animate ;
46
48
}
49
+
47
50
private _animate = true ;
48
51
49
52
/**
@@ -53,21 +56,25 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
53
56
set horizontal ( value : boolean ) {
54
57
this . _horizontal = coerceBooleanProperty ( value ) ;
55
58
}
59
+
56
60
get horizontal ( ) : boolean {
57
61
return this . _horizontal ;
58
62
}
63
+
59
64
private _horizontal : boolean = false ;
60
65
61
66
/**
62
67
* Toggle the visibility of collapsible element.
63
68
*/
64
69
@Input ( )
65
70
set visible ( value ) {
66
- this . _visible = value ;
71
+ this . _visible = coerceBooleanProperty ( value ) ;
67
72
}
73
+
68
74
get visible ( ) : boolean {
69
75
return this . _visible ;
70
76
}
77
+
71
78
private _visible = false ;
72
79
73
80
/**
@@ -77,9 +84,11 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
77
84
set navbar ( value : boolean ) {
78
85
this . _navbar = coerceBooleanProperty ( value ) ;
79
86
} ;
87
+
80
88
get navbar ( ) {
81
89
return this . _navbar ;
82
90
}
91
+
83
92
private _navbar = false ;
84
93
85
94
/**
@@ -115,10 +124,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
115
124
get hostClasses ( ) : any {
116
125
return {
117
126
'navbar-collapse' : this . navbar ,
118
- show : this . visible ,
119
- 'collapse-horizontal' : this . horizontal ,
120
- collapsing : this . collapsing
121
- // collapse: !this.collapsing && !this.visible
127
+ 'collapse-horizontal' : this . horizontal
122
128
} ;
123
129
}
124
130
@@ -169,32 +175,58 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
169
175
const expand = this . horizontal ? expandHorizontalAnimation : expandAnimation ;
170
176
const collapse = this . horizontal ? collapseHorizontalAnimation : collapseAnimation ;
171
177
178
+ const dimension = this . horizontal ? 'width' : 'height' ;
179
+ const capitalizedDimension = dimension [ 0 ] . toUpperCase ( ) + dimension . slice ( 1 ) ;
180
+ const scrollSize = `scroll${ capitalizedDimension } ` ;
181
+
172
182
const animationFactory = this . animationBuilder . build (
173
183
useAnimation ( visible ? expand : collapse , { params : { time : duration , easing : this . transition } } )
174
184
) ;
175
185
176
186
this . player = animationFactory . create ( this . host ) ;
187
+
188
+ this . renderer . setStyle ( this . host , dimension , visible ? 0 : `${ this . host . getBoundingClientRect ( ) [ dimension ] } px` ) ;
189
+
190
+ ! visible && this . host . offsetHeight ;
191
+
177
192
this . player . onStart ( ( ) => {
178
193
this . setMaxSize ( ) ;
179
- this . visible = visible ;
194
+ this . renderer . removeClass ( this . host , 'collapse' ) ;
195
+ this . renderer . addClass ( this . host , 'collapsing' ) ;
196
+ this . renderer . removeClass ( this . host , 'show' ) ;
180
197
this . collapsing = true ;
198
+ if ( visible ) {
199
+ // @ts -ignore
200
+ this . renderer . setStyle ( this . host , dimension , `${ this . host [ scrollSize ] } px` ) ;
201
+ } else {
202
+ this . renderer . setStyle ( this . host , dimension , '' ) ;
203
+ }
181
204
this . collapseChange . emit ( visible ? 'opening' : 'collapsing' ) ;
182
205
} ) ;
183
206
this . player . onDone ( ( ) => {
207
+ this . visible = visible ;
184
208
this . collapsing = false ;
209
+ this . renderer . removeClass ( this . host , 'collapsing' ) ;
210
+ this . renderer . addClass ( this . host , 'collapse' ) ;
211
+ if ( visible ) {
212
+ this . renderer . addClass ( this . host , 'show' ) ;
213
+ this . renderer . setStyle ( this . host , dimension , '' ) ;
214
+ } else {
215
+ this . renderer . removeClass ( this . host , 'show' ) ;
216
+ }
185
217
this . collapseChange . emit ( visible ? 'open' : 'collapsed' ) ;
186
218
} ) ;
187
219
}
188
220
189
221
setMaxSize ( ) {
190
- setTimeout ( ( ) => {
191
- if ( this . horizontal ) {
192
- this . scrollWidth = this . host . scrollWidth ;
193
- this . scrollWidth > 0 && this . renderer . setStyle ( this . host , 'maxWidth' , `${ this . scrollWidth } px` ) ;
194
- // } else {
195
- // this.scrollHeight = this.host.scrollHeight;
196
- // this.scrollHeight > 0 && this.renderer.setStyle(this.host, 'maxHeight', `${this.scrollHeight}px`);
197
- }
198
- } ) ;
222
+ // setTimeout(() => {
223
+ if ( this . horizontal ) {
224
+ this . scrollWidth = this . host . scrollWidth ;
225
+ this . scrollWidth > 0 && this . renderer . setStyle ( this . host , 'maxWidth' , `${ this . scrollWidth } px` ) ;
226
+ // } else {
227
+ // this.scrollHeight = this.host.scrollHeight;
228
+ // this.scrollHeight > 0 && this.renderer.setStyle(this.host, 'maxHeight', `${this.scrollHeight}px`);
229
+ }
230
+ // });
199
231
}
200
232
}
0 commit comments