-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
collapse.directive.ts
181 lines (149 loc) · 4.51 KB
/
collapse.directive.ts
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
import {
AnimationBuilder,
AnimationFactory,
AnimationPlayer
} from '@angular/animations';
import {
AfterViewChecked,
Directive,
ElementRef,
EventEmitter,
HostBinding,
Input,
Output,
Renderer2
} from '@angular/core';
import {
collapseAnimation,
expandAnimation
} from './collapse-animations';
@Directive({
selector: '[collapse]',
exportAs: 'bs-collapse',
host: {
'[class.collapse]': 'true'
}
})
export class CollapseDirective implements AfterViewChecked {
/** This event fires as soon as content collapses */
@Output() collapsed: EventEmitter<CollapseDirective> = new EventEmitter();
/** This event fires when collapsing is started */
@Output() collapses: EventEmitter<CollapseDirective> = new EventEmitter();
/** This event fires as soon as content becomes visible */
@Output() expanded: EventEmitter<CollapseDirective> = new EventEmitter();
/** This event fires when expansion is started */
@Output() expands: EventEmitter<CollapseDirective> = new EventEmitter();
// shown
@HostBinding('class.in')
@HostBinding('class.show')
@HostBinding('attr.aria-expanded')
isExpanded = true;
// hidden
@HostBinding('attr.aria-hidden') isCollapsed = false;
// stale state
@HostBinding('class.collapse') isCollapse = true;
// animation state
@HostBinding('class.collapsing') isCollapsing = false;
@Input()
set display(value: string) {
if (!this.isAnimated) {
this._renderer.setStyle(this._el.nativeElement, 'display', value);
return;
}
this._display = value;
if (value === 'none') {
this.hide();
return;
}
this.show();
}
/** turn on/off animation */
@Input() isAnimated = false;
/** A flag indicating visibility of content (shown or hidden) */
@Input()
set collapse(value: boolean) {
if (!this._player || this._isAnimationDone) {
this.isExpanded = value;
this.toggle();
}
}
get collapse(): boolean {
return this.isExpanded;
}
private _display = 'block';
private _factoryCollapseAnimation: AnimationFactory;
private _factoryExpandAnimation: AnimationFactory;
private _isAnimationDone: boolean;
private _player: AnimationPlayer;
private _stylesLoaded = false;
private _COLLAPSE_ACTION_NAME = 'collapse';
private _EXPAND_ACTION_NAME = 'expand';
constructor(
private _el: ElementRef,
private _renderer: Renderer2,
_builder: AnimationBuilder
) {
this._factoryCollapseAnimation = _builder.build(collapseAnimation);
this._factoryExpandAnimation = _builder.build(expandAnimation);
}
ngAfterViewChecked(): void {
this._stylesLoaded = true;
if (!this._player || !this._isAnimationDone) {
return;
}
this._player.reset();
this._renderer.setStyle(this._el.nativeElement, 'height', '*');
}
/** allows to manually toggle content visibility */
toggle(): void {
if (this.isExpanded) {
this.hide();
} else {
this.show();
}
}
/** allows to manually hide content */
hide(): void {
this.isCollapsing = true;
this.isExpanded = false;
this.isCollapsed = true;
this.isCollapsing = false;
this.collapses.emit(this);
this._isAnimationDone = false;
this.animationRun(this.isAnimated, this._COLLAPSE_ACTION_NAME)(() => {
this._isAnimationDone = true;
this.collapsed.emit(this);
this._renderer.setStyle(this._el.nativeElement, 'display', 'none');
});
}
/** allows to manually show collapsed content */
show(): void {
this._renderer.setStyle(this._el.nativeElement, 'display', this._display);
this.isCollapsing = true;
this.isExpanded = true;
this.isCollapsed = false;
this.isCollapsing = false;
this.expands.emit(this);
this._isAnimationDone = false;
this.animationRun(this.isAnimated, this._EXPAND_ACTION_NAME)(() => {
this._isAnimationDone = true;
this.expanded.emit(this);
});
}
animationRun(isAnimated: boolean, action: string) {
if (!isAnimated || !this._stylesLoaded) {
return (callback: () => void) => callback();
}
this._renderer.setStyle(this._el.nativeElement, 'overflow', 'hidden');
this._renderer.addClass(this._el.nativeElement, 'collapse');
const factoryAnimation = (action === this._EXPAND_ACTION_NAME)
? this._factoryExpandAnimation
: this._factoryCollapseAnimation;
if (this._player) {
this._player.destroy();
}
this._player = factoryAnimation.create(this._el.nativeElement);
this._player.play();
return (callback: () => void) => this._player.onDone(callback);
}
}