1 | import { style, animate, AnimationBuilder } from '@angular/animations';
|
2 | import { EventEmitter, Directive, ElementRef, Renderer2, Output, HostBinding, Input, NgModule } from '@angular/core';
|
3 |
|
4 | import * as ɵngcc0 from '@angular/core';
|
5 | import * as ɵngcc1 from '@angular/animations';
|
6 | const COLLAPSE_ANIMATION_TIMING = '400ms cubic-bezier(0.4,0.0,0.2,1)';
|
7 | const expandAnimation = [
|
8 | style({ height: 0, visibility: 'hidden' }),
|
9 | animate(COLLAPSE_ANIMATION_TIMING, style({ height: '*', visibility: 'visible' }))
|
10 | ];
|
11 | const collapseAnimation = [
|
12 | style({ height: '*', visibility: 'visible' }),
|
13 | animate(COLLAPSE_ANIMATION_TIMING, style({ height: 0, visibility: 'hidden' }))
|
14 | ];
|
15 |
|
16 | class CollapseDirective {
|
17 | constructor(_el, _renderer, _builder) {
|
18 | this._el = _el;
|
19 | this._renderer = _renderer;
|
20 |
|
21 | this.collapsed = new EventEmitter();
|
22 |
|
23 | this.collapses = new EventEmitter();
|
24 |
|
25 | this.expanded = new EventEmitter();
|
26 |
|
27 | this.expands = new EventEmitter();
|
28 |
|
29 | this.isExpanded = true;
|
30 | this.collapseNewValue = true;
|
31 |
|
32 | this.isCollapsed = false;
|
33 |
|
34 | this.isCollapse = true;
|
35 |
|
36 | this.isCollapsing = false;
|
37 |
|
38 | this.isAnimated = false;
|
39 | this._display = 'block';
|
40 | this._stylesLoaded = false;
|
41 | this._COLLAPSE_ACTION_NAME = 'collapse';
|
42 | this._EXPAND_ACTION_NAME = 'expand';
|
43 | this._factoryCollapseAnimation = _builder.build(collapseAnimation);
|
44 | this._factoryExpandAnimation = _builder.build(expandAnimation);
|
45 | }
|
46 | set display(value) {
|
47 | if (!this.isAnimated) {
|
48 | this._renderer.setStyle(this._el.nativeElement, 'display', value);
|
49 | return;
|
50 | }
|
51 | this._display = value;
|
52 | if (value === 'none') {
|
53 | this.hide();
|
54 | return;
|
55 | }
|
56 | this.show();
|
57 | }
|
58 |
|
59 | set collapse(value) {
|
60 | this.collapseNewValue = value;
|
61 | if (!this._player || this._isAnimationDone) {
|
62 | this.isExpanded = value;
|
63 | this.toggle();
|
64 | }
|
65 | }
|
66 | get collapse() {
|
67 | return this.isExpanded;
|
68 | }
|
69 | ngAfterViewChecked() {
|
70 | this._stylesLoaded = true;
|
71 | if (!this._player || !this._isAnimationDone) {
|
72 | return;
|
73 | }
|
74 | this._player.reset();
|
75 | this._renderer.setStyle(this._el.nativeElement, 'height', '*');
|
76 | }
|
77 |
|
78 | toggle() {
|
79 | if (this.isExpanded) {
|
80 | this.hide();
|
81 | }
|
82 | else {
|
83 | this.show();
|
84 | }
|
85 | }
|
86 |
|
87 | hide() {
|
88 | this.isCollapsing = true;
|
89 | this.isExpanded = false;
|
90 | this.isCollapsed = true;
|
91 | this.isCollapsing = false;
|
92 | this.collapses.emit(this);
|
93 | this._isAnimationDone = false;
|
94 | this.animationRun(this.isAnimated, this._COLLAPSE_ACTION_NAME)(() => {
|
95 | this._isAnimationDone = true;
|
96 | if (this.collapseNewValue !== this.isCollapsed && this.isAnimated) {
|
97 | this.show();
|
98 | return;
|
99 | }
|
100 | this.collapsed.emit(this);
|
101 | this._renderer.setStyle(this._el.nativeElement, 'display', 'none');
|
102 | });
|
103 | }
|
104 |
|
105 | show() {
|
106 | this._renderer.setStyle(this._el.nativeElement, 'display', this._display);
|
107 | this.isCollapsing = true;
|
108 | this.isExpanded = true;
|
109 | this.isCollapsed = false;
|
110 | this.isCollapsing = false;
|
111 | this.expands.emit(this);
|
112 | this._isAnimationDone = false;
|
113 | this.animationRun(this.isAnimated, this._EXPAND_ACTION_NAME)(() => {
|
114 | this._isAnimationDone = true;
|
115 | if (this.collapseNewValue !== this.isCollapsed && this.isAnimated) {
|
116 | this.hide();
|
117 | return;
|
118 | }
|
119 | this.expanded.emit(this);
|
120 | this._renderer.removeStyle(this._el.nativeElement, 'overflow');
|
121 | });
|
122 | }
|
123 | animationRun(isAnimated, action) {
|
124 | if (!isAnimated || !this._stylesLoaded) {
|
125 | return (callback) => callback();
|
126 | }
|
127 | this._renderer.setStyle(this._el.nativeElement, 'overflow', 'hidden');
|
128 | this._renderer.addClass(this._el.nativeElement, 'collapse');
|
129 | const factoryAnimation = (action === this._EXPAND_ACTION_NAME)
|
130 | ? this._factoryExpandAnimation
|
131 | : this._factoryCollapseAnimation;
|
132 | if (this._player) {
|
133 | this._player.destroy();
|
134 | }
|
135 | this._player = factoryAnimation.create(this._el.nativeElement);
|
136 | this._player.play();
|
137 | return (callback) => { var _a; return (_a = this._player) === null || _a === void 0 ? void 0 : _a.onDone(callback); };
|
138 | }
|
139 | }
|
140 | CollapseDirective.ɵfac = function CollapseDirective_Factory(t) { return new (t || CollapseDirective)(ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ElementRef), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.Renderer2), ɵngcc0.ɵɵdirectiveInject(ɵngcc1.AnimationBuilder)); };
|
141 | CollapseDirective.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: CollapseDirective, selectors: [["", "collapse", ""]], hostVars: 10, hostBindings: function CollapseDirective_HostBindings(rf, ctx) { if (rf & 2) {
|
142 | ɵngcc0.ɵɵattribute("aria-expanded", ctx.isExpanded)("aria-hidden", ctx.isCollapsed);
|
143 | ɵngcc0.ɵɵclassProp("collapse", ctx.isCollapse)("in", ctx.isExpanded)("show", ctx.isExpanded)("collapsing", ctx.isCollapsing);
|
144 | } }, inputs: { isAnimated: "isAnimated", display: "display", collapse: "collapse" }, outputs: { collapsed: "collapsed", collapses: "collapses", expanded: "expanded", expands: "expands" }, exportAs: ["bs-collapse"] });
|
145 | CollapseDirective.ctorParameters = () => [
|
146 | { type: ElementRef },
|
147 | { type: Renderer2 },
|
148 | { type: AnimationBuilder }
|
149 | ];
|
150 | CollapseDirective.propDecorators = {
|
151 | collapsed: [{ type: Output }],
|
152 | collapses: [{ type: Output }],
|
153 | expanded: [{ type: Output }],
|
154 | expands: [{ type: Output }],
|
155 | isExpanded: [{ type: HostBinding, args: ['class.in',] }, { type: HostBinding, args: ['class.show',] }, { type: HostBinding, args: ['attr.aria-expanded',] }],
|
156 | isCollapsed: [{ type: HostBinding, args: ['attr.aria-hidden',] }],
|
157 | isCollapse: [{ type: HostBinding, args: ['class.collapse',] }],
|
158 | isCollapsing: [{ type: HostBinding, args: ['class.collapsing',] }],
|
159 | display: [{ type: Input }],
|
160 | isAnimated: [{ type: Input }],
|
161 | collapse: [{ type: Input }]
|
162 | };
|
163 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(CollapseDirective, [{
|
164 | type: Directive,
|
165 | args: [{
|
166 | selector: '[collapse]',
|
167 | exportAs: 'bs-collapse',
|
168 |
|
169 | host: {
|
170 | '[class.collapse]': 'true'
|
171 | }
|
172 | }]
|
173 | }], function () { return [{ type: ɵngcc0.ElementRef }, { type: ɵngcc0.Renderer2 }, { type: ɵngcc1.AnimationBuilder }]; }, { collapsed: [{
|
174 | type: Output
|
175 | }], collapses: [{
|
176 | type: Output
|
177 | }], expanded: [{
|
178 | type: Output
|
179 | }], expands: [{
|
180 | type: Output
|
181 | }], isExpanded: [{
|
182 | type: HostBinding,
|
183 | args: ['class.in']
|
184 | }, {
|
185 | type: HostBinding,
|
186 | args: ['class.show']
|
187 | }, {
|
188 | type: HostBinding,
|
189 | args: ['attr.aria-expanded']
|
190 | }], isCollapsed: [{
|
191 | type: HostBinding,
|
192 | args: ['attr.aria-hidden']
|
193 | }], isCollapse: [{
|
194 | type: HostBinding,
|
195 | args: ['class.collapse']
|
196 | }], isCollapsing: [{
|
197 | type: HostBinding,
|
198 | args: ['class.collapsing']
|
199 | }], isAnimated: [{
|
200 | type: Input
|
201 | }], display: [{
|
202 | type: Input
|
203 | }], collapse: [{
|
204 | type: Input
|
205 | }] }); })();
|
206 |
|
207 | class CollapseModule {
|
208 | static forRoot() {
|
209 | return { ngModule: CollapseModule, providers: [] };
|
210 | }
|
211 | }
|
212 | CollapseModule.ɵmod = ɵngcc0.ɵɵdefineNgModule({ type: CollapseModule });
|
213 | CollapseModule.ɵinj = ɵngcc0.ɵɵdefineInjector({ factory: function CollapseModule_Factory(t) { return new (t || CollapseModule)(); } });
|
214 | (function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵngcc0.ɵɵsetNgModuleScope(CollapseModule, { declarations: [CollapseDirective], exports: [CollapseDirective] }); })();
|
215 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(CollapseModule, [{
|
216 | type: NgModule,
|
217 | args: [{
|
218 | declarations: [CollapseDirective],
|
219 | exports: [CollapseDirective]
|
220 | }]
|
221 | }], null, null); })();
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | export { CollapseDirective, CollapseModule };
|
228 |
|
229 |
|
\ | No newline at end of file |