UNPKG

6.62 kBJavaScriptView Raw
1import { style, animate, AnimationBuilder } from '@angular/animations';
2import { EventEmitter, Directive, ElementRef, Renderer2, Output, HostBinding, Input, NgModule } from '@angular/core';
3
4const COLLAPSE_ANIMATION_TIMING = '400ms cubic-bezier(0.4,0.0,0.2,1)';
5const expandAnimation = [
6 style({ height: 0, visibility: 'hidden' }),
7 animate(COLLAPSE_ANIMATION_TIMING, style({ height: '*', visibility: 'visible' }))
8];
9const collapseAnimation = [
10 style({ height: '*', visibility: 'visible' }),
11 animate(COLLAPSE_ANIMATION_TIMING, style({ height: 0, visibility: 'hidden' }))
12];
13
14class CollapseDirective {
15 constructor(_el, _renderer, _builder) {
16 this._el = _el;
17 this._renderer = _renderer;
18 /** This event fires as soon as content collapses */
19 this.collapsed = new EventEmitter();
20 /** This event fires when collapsing is started */
21 this.collapses = new EventEmitter();
22 /** This event fires as soon as content becomes visible */
23 this.expanded = new EventEmitter();
24 /** This event fires when expansion is started */
25 this.expands = new EventEmitter();
26 // shown
27 this.isExpanded = true;
28 this.collapseNewValue = true;
29 // hidden
30 this.isCollapsed = false;
31 // stale state
32 this.isCollapse = true;
33 // animation state
34 this.isCollapsing = false;
35 /** turn on/off animation */
36 this.isAnimated = false;
37 this._display = 'block';
38 this._stylesLoaded = false;
39 this._COLLAPSE_ACTION_NAME = 'collapse';
40 this._EXPAND_ACTION_NAME = 'expand';
41 this._factoryCollapseAnimation = _builder.build(collapseAnimation);
42 this._factoryExpandAnimation = _builder.build(expandAnimation);
43 }
44 set display(value) {
45 if (!this.isAnimated) {
46 this._renderer.setStyle(this._el.nativeElement, 'display', value);
47 return;
48 }
49 this._display = value;
50 if (value === 'none') {
51 this.hide();
52 return;
53 }
54 this.show();
55 }
56 /** A flag indicating visibility of content (shown or hidden) */
57 set collapse(value) {
58 this.collapseNewValue = value;
59 if (!this._player || this._isAnimationDone) {
60 this.isExpanded = value;
61 this.toggle();
62 }
63 }
64 get collapse() {
65 return this.isExpanded;
66 }
67 ngAfterViewChecked() {
68 this._stylesLoaded = true;
69 if (!this._player || !this._isAnimationDone) {
70 return;
71 }
72 this._player.reset();
73 this._renderer.setStyle(this._el.nativeElement, 'height', '*');
74 }
75 /** allows to manually toggle content visibility */
76 toggle() {
77 if (this.isExpanded) {
78 this.hide();
79 }
80 else {
81 this.show();
82 }
83 }
84 /** allows to manually hide content */
85 hide() {
86 this.isCollapsing = true;
87 this.isExpanded = false;
88 this.isCollapsed = true;
89 this.isCollapsing = false;
90 this.collapses.emit(this);
91 this._isAnimationDone = false;
92 this.animationRun(this.isAnimated, this._COLLAPSE_ACTION_NAME)(() => {
93 this._isAnimationDone = true;
94 if (this.collapseNewValue !== this.isCollapsed && this.isAnimated) {
95 this.show();
96 return;
97 }
98 this.collapsed.emit(this);
99 this._renderer.setStyle(this._el.nativeElement, 'display', 'none');
100 });
101 }
102 /** allows to manually show collapsed content */
103 show() {
104 this._renderer.setStyle(this._el.nativeElement, 'display', this._display);
105 this.isCollapsing = true;
106 this.isExpanded = true;
107 this.isCollapsed = false;
108 this.isCollapsing = false;
109 this.expands.emit(this);
110 this._isAnimationDone = false;
111 this.animationRun(this.isAnimated, this._EXPAND_ACTION_NAME)(() => {
112 this._isAnimationDone = true;
113 if (this.collapseNewValue !== this.isCollapsed && this.isAnimated) {
114 this.hide();
115 return;
116 }
117 this.expanded.emit(this);
118 this._renderer.removeStyle(this._el.nativeElement, 'overflow');
119 });
120 }
121 animationRun(isAnimated, action) {
122 if (!isAnimated || !this._stylesLoaded) {
123 return (callback) => callback();
124 }
125 this._renderer.setStyle(this._el.nativeElement, 'overflow', 'hidden');
126 this._renderer.addClass(this._el.nativeElement, 'collapse');
127 const factoryAnimation = (action === this._EXPAND_ACTION_NAME)
128 ? this._factoryExpandAnimation
129 : this._factoryCollapseAnimation;
130 if (this._player) {
131 this._player.destroy();
132 }
133 this._player = factoryAnimation.create(this._el.nativeElement);
134 this._player.play();
135 return (callback) => { var _a; return (_a = this._player) === null || _a === void 0 ? void 0 : _a.onDone(callback); };
136 }
137}
138CollapseDirective.decorators = [
139 { type: Directive, args: [{
140 selector: '[collapse]',
141 exportAs: 'bs-collapse',
142 // eslint-disable-next-line @angular-eslint/no-host-metadata-property
143 host: {
144 '[class.collapse]': 'true'
145 }
146 },] }
147];
148CollapseDirective.ctorParameters = () => [
149 { type: ElementRef },
150 { type: Renderer2 },
151 { type: AnimationBuilder }
152];
153CollapseDirective.propDecorators = {
154 collapsed: [{ type: Output }],
155 collapses: [{ type: Output }],
156 expanded: [{ type: Output }],
157 expands: [{ type: Output }],
158 isExpanded: [{ type: HostBinding, args: ['class.in',] }, { type: HostBinding, args: ['class.show',] }, { type: HostBinding, args: ['attr.aria-expanded',] }],
159 isCollapsed: [{ type: HostBinding, args: ['attr.aria-hidden',] }],
160 isCollapse: [{ type: HostBinding, args: ['class.collapse',] }],
161 isCollapsing: [{ type: HostBinding, args: ['class.collapsing',] }],
162 display: [{ type: Input }],
163 isAnimated: [{ type: Input }],
164 collapse: [{ type: Input }]
165};
166
167class CollapseModule {
168 static forRoot() {
169 return { ngModule: CollapseModule, providers: [] };
170 }
171}
172CollapseModule.decorators = [
173 { type: NgModule, args: [{
174 declarations: [CollapseDirective],
175 exports: [CollapseDirective]
176 },] }
177];
178
179/**
180 * Generated bundle index. Do not edit.
181 */
182
183export { CollapseDirective, CollapseModule };
184//# sourceMappingURL=ngx-bootstrap-collapse.js.map