@@ -7,22 +7,26 @@ import {
7
7
NgModule ,
8
8
ModuleWithProviders ,
9
9
Renderer ,
10
- ViewEncapsulation
10
+ ViewEncapsulation ,
11
+ Inject ,
12
+ Optional ,
11
13
} from '@angular/core' ;
12
14
import { CommonModule } from '@angular/common' ;
13
15
import { ENTER , SPACE } from '../keyboard/keycodes' ;
14
16
import { coerceBooleanProperty } from '../coercion/boolean-property' ;
15
17
import { MdRippleModule } from '../ripple/index' ;
18
+ import { MdSelectionModule } from '../selection/index' ;
19
+ import { MATERIAL_COMPATIBILITY_MODE } from '../../core/compatibility/compatibility' ;
16
20
17
21
/**
18
22
* Option IDs need to be unique across components, so this counter exists outside of
19
23
* the component definition.
20
24
*/
21
25
let _uniqueIdCounter = 0 ;
22
26
23
- /** Event object emitted by MdOption when selected. */
24
- export class MdOptionSelectEvent {
25
- constructor ( public source : MdOption , public isUserInput = false ) { }
27
+ /** Event object emitted by MdOption when selected or deselected . */
28
+ export class MdOptionSelectionChange {
29
+ constructor ( public source : MdOption , public isUserInput = false ) { }
26
30
}
27
31
28
32
@@ -36,6 +40,7 @@ export class MdOptionSelectEvent {
36
40
'role' : 'option' ,
37
41
'[attr.tabindex]' : '_getTabIndex()' ,
38
42
'[class.mat-selected]' : 'selected' ,
43
+ '[class.mat-option-multiple]' : 'multiple' ,
39
44
'[class.mat-active]' : 'active' ,
40
45
'[id]' : 'id' ,
41
46
'[attr.aria-selected]' : 'selected.toString()' ,
@@ -57,9 +62,15 @@ export class MdOption {
57
62
58
63
private _id : string = `md-option-${ _uniqueIdCounter ++ } ` ;
59
64
65
+ /** Whether the wrapping component is in multiple selection mode. */
66
+ multiple : boolean = false ;
67
+
60
68
/** The unique ID of the option. */
61
69
get id ( ) { return this . _id ; }
62
70
71
+ /** Whether or not the option is currently selected. */
72
+ get selected ( ) : boolean { return this . _selected ; }
73
+
63
74
/** The form value of the option. */
64
75
@Input ( ) value : any ;
65
76
@@ -68,15 +79,13 @@ export class MdOption {
68
79
get disabled ( ) { return this . _disabled ; }
69
80
set disabled ( value : any ) { this . _disabled = coerceBooleanProperty ( value ) ; }
70
81
71
- /** Event emitted when the option is selected. */
72
- @Output ( ) onSelect = new EventEmitter < MdOptionSelectEvent > ( ) ;
82
+ /** Event emitted when the option is selected or deselected . */
83
+ @Output ( ) onSelectionChange = new EventEmitter < MdOptionSelectionChange > ( ) ;
73
84
74
- constructor ( private _element : ElementRef , private _renderer : Renderer ) { }
75
-
76
- /** Whether or not the option is currently selected. */
77
- get selected ( ) : boolean {
78
- return this . _selected ;
79
- }
85
+ constructor (
86
+ private _element : ElementRef ,
87
+ private _renderer : Renderer ,
88
+ @Optional ( ) @Inject ( MATERIAL_COMPATIBILITY_MODE ) public _isCompatibilityMode : boolean ) { }
80
89
81
90
/**
82
91
* Whether or not the option is currently active and ready to be selected.
@@ -100,12 +109,13 @@ export class MdOption {
100
109
/** Selects the option. */
101
110
select ( ) : void {
102
111
this . _selected = true ;
103
- this . onSelect . emit ( new MdOptionSelectEvent ( this , false ) ) ;
112
+ this . _emitSelectionChangeEvent ( ) ;
104
113
}
105
114
106
115
/** Deselects the option. */
107
116
deselect ( ) : void {
108
117
this . _selected = false ;
118
+ this . _emitSelectionChangeEvent ( ) ;
109
119
}
110
120
111
121
/** Sets focus onto this option. */
@@ -118,7 +128,7 @@ export class MdOption {
118
128
* active. This is used by the ActiveDescendantKeyManager so key
119
129
* events will display the proper options as active on arrow key events.
120
130
*/
121
- setActiveStyles ( ) {
131
+ setActiveStyles ( ) : void {
122
132
Promise . resolve ( null ) . then ( ( ) => this . _active = true ) ;
123
133
}
124
134
@@ -127,7 +137,7 @@ export class MdOption {
127
137
* active. This is used by the ActiveDescendantKeyManager so key
128
138
* events will display the proper options as active on arrow key events.
129
139
*/
130
- setInactiveStyles ( ) {
140
+ setInactiveStyles ( ) : void {
131
141
Promise . resolve ( null ) . then ( ( ) => this . _active = false ) ;
132
142
}
133
143
@@ -142,26 +152,32 @@ export class MdOption {
142
152
* Selects the option while indicating the selection came from the user. Used to
143
153
* determine if the select's view -> model callback should be invoked.
144
154
*/
145
- _selectViaInteraction ( ) {
155
+ _selectViaInteraction ( ) : void {
146
156
if ( ! this . disabled ) {
147
- this . _selected = true ;
148
- this . onSelect . emit ( new MdOptionSelectEvent ( this , true ) ) ;
157
+ this . _selected = this . multiple ? ! this . _selected : true ;
158
+ this . _emitSelectionChangeEvent ( true ) ;
149
159
}
150
160
}
151
161
152
162
/** Returns the correct tabindex for the option depending on disabled state. */
153
- _getTabIndex ( ) {
163
+ _getTabIndex ( ) : string {
154
164
return this . disabled ? '-1' : '0' ;
155
165
}
156
166
167
+ /** Fetches the host DOM element. */
157
168
_getHostElement ( ) : HTMLElement {
158
169
return this . _element . nativeElement ;
159
170
}
160
171
172
+ /** Emits the selection change event. */
173
+ private _emitSelectionChangeEvent ( isUserInput = false ) : void {
174
+ this . onSelectionChange . emit ( new MdOptionSelectionChange ( this , isUserInput ) ) ;
175
+ } ;
176
+
161
177
}
162
178
163
179
@NgModule ( {
164
- imports : [ MdRippleModule , CommonModule ] ,
180
+ imports : [ MdRippleModule , CommonModule , MdSelectionModule ] ,
165
181
exports : [ MdOption ] ,
166
182
declarations : [ MdOption ]
167
183
} )
0 commit comments