-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
expansion-panel-header.ts
158 lines (144 loc) · 4.25 KB
/
expansion-panel-header.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
Component,
Directive,
Host,
ViewEncapsulation,
ChangeDetectionStrategy,
ChangeDetectorRef,
OnDestroy,
} from '@angular/core';
import {
trigger,
state,
style,
transition,
animate,
} from '@angular/animations';
import {SPACE, ENTER} from '../core/keyboard/keycodes';
import {MdExpansionPanel, EXPANSION_PANEL_ANIMATION_TIMING} from './expansion-panel';
import {filter} from '../core/rxjs/index';
import {merge} from 'rxjs/observable/merge';
import {Subscription} from 'rxjs/Subscription';
/**
* <md-expansion-panel-header> component.
*
* This component corresponds to the header element of an <md-expansion-panel>.
*
* Please refer to README.md for examples on how to use it.
*/
@Component({
moduleId: module.id,
selector: 'md-expansion-panel-header, mat-expansion-panel-header',
styleUrls: ['./expansion-panel-header.css'],
templateUrl: './expansion-panel-header.html',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'class': 'mat-expansion-panel-header',
'role': 'button',
'tabindex': '0',
'[attr.aria-controls]': '_getPanelId()',
'[attr.aria-expanded]': '_isExpanded()',
'[class.mat-expanded]': '_isExpanded()',
'(click)': '_toggle()',
'(keyup)': '_keyup($event)',
'[@expansionHeight]': '_getExpandedState()',
},
animations: [
trigger('indicatorRotate', [
state('collapsed', style({transform: 'rotate(0deg)'})),
state('expanded', style({transform: 'rotate(180deg)'})),
transition('expanded <=> collapsed', animate(EXPANSION_PANEL_ANIMATION_TIMING)),
]),
trigger('expansionHeight', [
state('collapsed', style({height: '48px', 'line-height': '48px'})),
state('expanded', style({height: '64px', 'line-height': '64px'})),
transition('expanded <=> collapsed', animate(EXPANSION_PANEL_ANIMATION_TIMING)),
]),
],
})
export class MdExpansionPanelHeader implements OnDestroy {
private _parentChangeSubscription: Subscription | null = null;
constructor(
@Host() public panel: MdExpansionPanel,
private _changeDetectorRef: ChangeDetectorRef) {
// Since the toggle state depends on an @Input on the panel, we
// need to subscribe and trigger change detection manually.
this._parentChangeSubscription = merge(
panel.opened,
panel.closed,
filter.call(panel._inputChanges, changes => !!changes.hideToggle)
)
.subscribe(() => this._changeDetectorRef.markForCheck());
}
/** Toggles the expanded state of the panel. */
_toggle(): void {
this.panel.toggle();
}
/** Gets whether the panel is expanded. */
_isExpanded(): boolean {
return this.panel.expanded;
}
/** Gets the expanded state string of the panel. */
_getExpandedState(): string {
return this.panel._getExpandedState();
}
/** Gets the panel id. */
_getPanelId(): string {
return this.panel.id;
}
/** Gets whether the expand indicator is hidden. */
_getHideToggle(): boolean {
return this.panel.hideToggle;
}
/** Handle keyup event calling to toggle() if appropriate. */
_keyup(event: KeyboardEvent) {
switch (event.keyCode) {
// Toggle for space and enter keys.
case SPACE:
case ENTER:
event.preventDefault();
this._toggle();
break;
default:
return;
}
}
ngOnDestroy() {
if (this._parentChangeSubscription) {
this._parentChangeSubscription.unsubscribe();
this._parentChangeSubscription = null;
}
}
}
/**
* <md-panel-description> directive.
*
* This direction is to be used inside of the MdExpansionPanelHeader component.
*/
@Directive({
selector: 'md-panel-description, mat-panel-description',
host : {
class: 'mat-expansion-panel-header-description'
}
})
export class MdExpansionPanelDescription {}
/**
* <md-panel-title> directive.
*
* This direction is to be used inside of the MdExpansionPanelHeader component.
*/
@Directive({
selector: 'md-panel-title, mat-panel-title',
host : {
class: 'mat-expansion-panel-header-title'
}
})
export class MdExpansionPanelTitle {}