UNPKG

10.7 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { InjectionToken, Directive, Input, EventEmitter, Optional, Inject, SkipSelf, Output, NgModule } from '@angular/core';
3import * as i1 from '@angular/cdk/collections';
4import { coerceBooleanProperty } from '@angular/cdk/coercion';
5import { Subject, Subscription } from 'rxjs';
6
7/** Used to generate unique ID for each accordion. */
8let nextId$1 = 0;
9/**
10 * Injection token that can be used to reference instances of `CdkAccordion`. It serves
11 * as alternative token to the actual `CdkAccordion` class which could cause unnecessary
12 * retention of the class and its directive metadata.
13 */
14const CDK_ACCORDION = new InjectionToken('CdkAccordion');
15/**
16 * Directive whose purpose is to manage the expanded state of CdkAccordionItem children.
17 */
18class CdkAccordion {
19 constructor() {
20 /** Emits when the state of the accordion changes */
21 this._stateChanges = new Subject();
22 /** Stream that emits true/false when openAll/closeAll is triggered. */
23 this._openCloseAllActions = new Subject();
24 /** A readonly id value to use for unique selection coordination. */
25 this.id = `cdk-accordion-${nextId$1++}`;
26 this._multi = false;
27 }
28 /** Whether the accordion should allow multiple expanded accordion items simultaneously. */
29 get multi() {
30 return this._multi;
31 }
32 set multi(multi) {
33 this._multi = coerceBooleanProperty(multi);
34 }
35 /** Opens all enabled accordion items in an accordion where multi is enabled. */
36 openAll() {
37 if (this._multi) {
38 this._openCloseAllActions.next(true);
39 }
40 }
41 /** Closes all enabled accordion items. */
42 closeAll() {
43 this._openCloseAllActions.next(false);
44 }
45 ngOnChanges(changes) {
46 this._stateChanges.next(changes);
47 }
48 ngOnDestroy() {
49 this._stateChanges.complete();
50 this._openCloseAllActions.complete();
51 }
52 static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordion, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
53 static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: CdkAccordion, selector: "cdk-accordion, [cdkAccordion]", inputs: { multi: "multi" }, providers: [{ provide: CDK_ACCORDION, useExisting: CdkAccordion }], exportAs: ["cdkAccordion"], usesOnChanges: true, ngImport: i0 }); }
54}
55i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordion, decorators: [{
56 type: Directive,
57 args: [{
58 selector: 'cdk-accordion, [cdkAccordion]',
59 exportAs: 'cdkAccordion',
60 providers: [{ provide: CDK_ACCORDION, useExisting: CdkAccordion }],
61 }]
62 }], propDecorators: { multi: [{
63 type: Input
64 }] } });
65
66/** Used to generate unique ID for each accordion item. */
67let nextId = 0;
68/**
69 * An basic directive expected to be extended and decorated as a component. Sets up all
70 * events and attributes needed to be managed by a CdkAccordion parent.
71 */
72class CdkAccordionItem {
73 /** Whether the AccordionItem is expanded. */
74 get expanded() {
75 return this._expanded;
76 }
77 set expanded(expanded) {
78 expanded = coerceBooleanProperty(expanded);
79 // Only emit events and update the internal value if the value changes.
80 if (this._expanded !== expanded) {
81 this._expanded = expanded;
82 this.expandedChange.emit(expanded);
83 if (expanded) {
84 this.opened.emit();
85 /**
86 * In the unique selection dispatcher, the id parameter is the id of the CdkAccordionItem,
87 * the name value is the id of the accordion.
88 */
89 const accordionId = this.accordion ? this.accordion.id : this.id;
90 this._expansionDispatcher.notify(this.id, accordionId);
91 }
92 else {
93 this.closed.emit();
94 }
95 // Ensures that the animation will run when the value is set outside of an `@Input`.
96 // This includes cases like the open, close and toggle methods.
97 this._changeDetectorRef.markForCheck();
98 }
99 }
100 /** Whether the AccordionItem is disabled. */
101 get disabled() {
102 return this._disabled;
103 }
104 set disabled(disabled) {
105 this._disabled = coerceBooleanProperty(disabled);
106 }
107 constructor(accordion, _changeDetectorRef, _expansionDispatcher) {
108 this.accordion = accordion;
109 this._changeDetectorRef = _changeDetectorRef;
110 this._expansionDispatcher = _expansionDispatcher;
111 /** Subscription to openAll/closeAll events. */
112 this._openCloseAllSubscription = Subscription.EMPTY;
113 /** Event emitted every time the AccordionItem is closed. */
114 this.closed = new EventEmitter();
115 /** Event emitted every time the AccordionItem is opened. */
116 this.opened = new EventEmitter();
117 /** Event emitted when the AccordionItem is destroyed. */
118 this.destroyed = new EventEmitter();
119 /**
120 * Emits whenever the expanded state of the accordion changes.
121 * Primarily used to facilitate two-way binding.
122 * @docs-private
123 */
124 this.expandedChange = new EventEmitter();
125 /** The unique AccordionItem id. */
126 this.id = `cdk-accordion-child-${nextId++}`;
127 this._expanded = false;
128 this._disabled = false;
129 /** Unregister function for _expansionDispatcher. */
130 this._removeUniqueSelectionListener = () => { };
131 this._removeUniqueSelectionListener = _expansionDispatcher.listen((id, accordionId) => {
132 if (this.accordion &&
133 !this.accordion.multi &&
134 this.accordion.id === accordionId &&
135 this.id !== id) {
136 this.expanded = false;
137 }
138 });
139 // When an accordion item is hosted in an accordion, subscribe to open/close events.
140 if (this.accordion) {
141 this._openCloseAllSubscription = this._subscribeToOpenCloseAllActions();
142 }
143 }
144 /** Emits an event for the accordion item being destroyed. */
145 ngOnDestroy() {
146 this.opened.complete();
147 this.closed.complete();
148 this.destroyed.emit();
149 this.destroyed.complete();
150 this._removeUniqueSelectionListener();
151 this._openCloseAllSubscription.unsubscribe();
152 }
153 /** Toggles the expanded state of the accordion item. */
154 toggle() {
155 if (!this.disabled) {
156 this.expanded = !this.expanded;
157 }
158 }
159 /** Sets the expanded state of the accordion item to false. */
160 close() {
161 if (!this.disabled) {
162 this.expanded = false;
163 }
164 }
165 /** Sets the expanded state of the accordion item to true. */
166 open() {
167 if (!this.disabled) {
168 this.expanded = true;
169 }
170 }
171 _subscribeToOpenCloseAllActions() {
172 return this.accordion._openCloseAllActions.subscribe(expanded => {
173 // Only change expanded state if item is enabled
174 if (!this.disabled) {
175 this.expanded = expanded;
176 }
177 });
178 }
179 static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordionItem, deps: [{ token: CDK_ACCORDION, optional: true, skipSelf: true }, { token: i0.ChangeDetectorRef }, { token: i1.UniqueSelectionDispatcher }], target: i0.ɵɵFactoryTarget.Directive }); }
180 static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: CdkAccordionItem, selector: "cdk-accordion-item, [cdkAccordionItem]", inputs: { expanded: "expanded", disabled: "disabled" }, outputs: { closed: "closed", opened: "opened", destroyed: "destroyed", expandedChange: "expandedChange" }, providers: [
181 // Provide `CDK_ACCORDION` as undefined to prevent nested accordion items from
182 // registering to the same accordion.
183 { provide: CDK_ACCORDION, useValue: undefined },
184 ], exportAs: ["cdkAccordionItem"], ngImport: i0 }); }
185}
186i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordionItem, decorators: [{
187 type: Directive,
188 args: [{
189 selector: 'cdk-accordion-item, [cdkAccordionItem]',
190 exportAs: 'cdkAccordionItem',
191 providers: [
192 // Provide `CDK_ACCORDION` as undefined to prevent nested accordion items from
193 // registering to the same accordion.
194 { provide: CDK_ACCORDION, useValue: undefined },
195 ],
196 }]
197 }], ctorParameters: function () { return [{ type: CdkAccordion, decorators: [{
198 type: Optional
199 }, {
200 type: Inject,
201 args: [CDK_ACCORDION]
202 }, {
203 type: SkipSelf
204 }] }, { type: i0.ChangeDetectorRef }, { type: i1.UniqueSelectionDispatcher }]; }, propDecorators: { closed: [{
205 type: Output
206 }], opened: [{
207 type: Output
208 }], destroyed: [{
209 type: Output
210 }], expandedChange: [{
211 type: Output
212 }], expanded: [{
213 type: Input
214 }], disabled: [{
215 type: Input
216 }] } });
217
218class CdkAccordionModule {
219 static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordionModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
220 static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordionModule, declarations: [CdkAccordion, CdkAccordionItem], exports: [CdkAccordion, CdkAccordionItem] }); }
221 static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordionModule }); }
222}
223i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAccordionModule, decorators: [{
224 type: NgModule,
225 args: [{
226 exports: [CdkAccordion, CdkAccordionItem],
227 declarations: [CdkAccordion, CdkAccordionItem],
228 }]
229 }] });
230
231/**
232 * Generated bundle index. Do not edit.
233 */
234
235export { CdkAccordion, CdkAccordionItem, CdkAccordionModule };
236//# sourceMappingURL=accordion.mjs.map