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