@@ -35,13 +35,8 @@ import {
35
35
MdLineSetter ,
36
36
mixinDisabled ,
37
37
mixinDisableRipple ,
38
- RxChain ,
39
38
SPACE ,
40
- startWith ,
41
- switchMap ,
42
39
} from '@angular/material/core' ;
43
- import { merge } from 'rxjs/observable/merge' ;
44
- import { Subscription } from 'rxjs/Subscription' ;
45
40
46
41
47
42
/** @docs -private */
@@ -57,8 +52,6 @@ export interface MdSelectionListOptionEvent {
57
52
option : MdListOption ;
58
53
}
59
54
60
- const FOCUSED_STYLE : string = 'mat-list-item-focus' ;
61
-
62
55
/**
63
56
* Component for list-options of selection-list. Each list-option can automatically
64
57
* generate a checkbox and can put current item into the selectionModel of selection-list
@@ -76,6 +69,7 @@ const FOCUSED_STYLE: string = 'mat-list-item-focus';
76
69
'(click)' : '_handleClick()' ,
77
70
'tabindex' : '-1' ,
78
71
'[class.mat-list-item-disabled]' : 'disabled' ,
72
+ '[class.mat-list-item-focus]' : '_hasFocus' ,
79
73
'[attr.aria-selected]' : 'selected.toString()' ,
80
74
'[attr.aria-disabled]' : 'disabled.toString()' ,
81
75
} ,
@@ -112,18 +106,12 @@ export class MdListOption extends _MdListOptionMixinBase
112
106
get selected ( ) { return this . _selected ; }
113
107
set selected ( value : boolean ) { this . _selected = coerceBooleanProperty ( value ) ; }
114
108
115
- /** Emitted when the option is focused. */
116
- onFocus = new EventEmitter < MdSelectionListOptionEvent > ( ) ;
117
-
118
109
/** Emitted when the option is selected. */
119
110
@Output ( ) selectChange = new EventEmitter < MdSelectionListOptionEvent > ( ) ;
120
111
121
112
/** Emitted when the option is deselected. */
122
113
@Output ( ) deselected = new EventEmitter < MdSelectionListOptionEvent > ( ) ;
123
-
124
- /** Emitted when the option is destroyed. */
125
- @Output ( ) destroyed = new EventEmitter < MdSelectionListOptionEvent > ( ) ;
126
-
114
+
127
115
constructor ( private _renderer : Renderer2 ,
128
116
private _element : ElementRef ,
129
117
private _changeDetector : ChangeDetectorRef ,
@@ -141,7 +129,7 @@ export class MdListOption extends _MdListOptionMixinBase
141
129
}
142
130
143
131
ngOnDestroy ( ) : void {
144
- this . destroyed . emit ( { option : this } ) ;
132
+ this . selectionList . _removeOptionFromList ( this ) ;
145
133
}
146
134
147
135
/** Toggles the selection state of the option. */
@@ -154,7 +142,6 @@ export class MdListOption extends _MdListOptionMixinBase
154
142
/** Allows for programmatic focusing of the option. */
155
143
focus ( ) : void {
156
144
this . _element . nativeElement . focus ( ) ;
157
- this . onFocus . emit ( { option : this } ) ;
158
145
}
159
146
160
147
/** Whether this list item should show a ripple effect when clicked. */
@@ -170,11 +157,11 @@ export class MdListOption extends _MdListOptionMixinBase
170
157
171
158
_handleFocus ( ) {
172
159
this . _hasFocus = true ;
173
- this . _renderer . addClass ( this . _element . nativeElement , FOCUSED_STYLE ) ;
160
+ this . selectionList . _setFocusedOption ( this ) ;
174
161
}
175
162
176
163
_handleBlur ( ) {
177
- this . _renderer . removeClass ( this . _element . nativeElement , FOCUSED_STYLE ) ;
164
+ this . _hasFocus = false ;
178
165
}
179
166
180
167
/** Retrieves the DOM element of the component host. */
@@ -204,17 +191,11 @@ export class MdListOption extends _MdListOptionMixinBase
204
191
changeDetection : ChangeDetectionStrategy . OnPush
205
192
} )
206
193
export class MdSelectionList extends _MdSelectionListMixinBase
207
- implements FocusableOption , CanDisable , CanDisableRipple , AfterContentInit , OnDestroy {
194
+ implements FocusableOption , CanDisable , CanDisableRipple , AfterContentInit {
208
195
209
196
/** Tab index for the selection-list. */
210
197
_tabIndex = 0 ;
211
198
212
- /** Subscription to all list options' onFocus events */
213
- private _optionFocusSubscription = Subscription . EMPTY ;
214
-
215
- /** Subscription to all list options' destroy events */
216
- private _optionDestroyStream = Subscription . EMPTY ;
217
-
218
199
/** The FocusKeyManager which handles focus. */
219
200
_keyManager : FocusKeyManager < MdListOption > ;
220
201
@@ -234,14 +215,6 @@ export class MdSelectionList extends _MdSelectionListMixinBase
234
215
if ( this . disabled ) {
235
216
this . _tabIndex = - 1 ;
236
217
}
237
-
238
- this . _optionFocusSubscription = this . _onFocusSubscription ( ) ;
239
- this . _optionDestroyStream = this . _onDestroySubscription ( ) ;
240
- }
241
-
242
- ngOnDestroy ( ) : void {
243
- this . _optionDestroyStream . unsubscribe ( ) ;
244
- this . _optionFocusSubscription . unsubscribe ( ) ;
245
218
}
246
219
247
220
/** Focus the selection-list. */
@@ -267,36 +240,23 @@ export class MdSelectionList extends _MdSelectionListMixinBase
267
240
} ) ;
268
241
}
269
242
270
- /** Map all the options' destroy event subscriptions and merge them into one stream. */
271
- private _onDestroySubscription ( ) : Subscription {
272
- return RxChain . from ( this . options . changes )
273
- . call ( startWith , this . options )
274
- . call ( switchMap , ( options : MdListOption [ ] ) => {
275
- return merge ( ...options . map ( option => option . destroyed ) ) ;
276
- } ) . subscribe ( ( e : MdSelectionListOptionEvent ) => {
277
- let optionIndex : number = this . options . toArray ( ) . indexOf ( e . option ) ;
278
- if ( e . option . _hasFocus ) {
279
- // Check whether the option is the last item
280
- if ( optionIndex < this . options . length - 1 ) {
281
- this . _keyManager . setActiveItem ( optionIndex ) ;
282
- } else if ( optionIndex - 1 >= 0 ) {
283
- this . _keyManager . setActiveItem ( optionIndex - 1 ) ;
284
- }
285
- }
286
- e . option . destroyed . unsubscribe ( ) ;
287
- } ) ;
243
+ /** Sets the focused option of the selection-list. */
244
+ _setFocusedOption ( option : MdListOption ) {
245
+ this . _keyManager . updateActiveItemIndex ( this . _getIndexFromOption ( option ) ) ;
288
246
}
289
247
290
- /** Map all the options' onFocus event subscriptions and merge them into one stream. */
291
- private _onFocusSubscription ( ) : Subscription {
292
- return RxChain . from ( this . options . changes )
293
- . call ( startWith , this . options )
294
- . call ( switchMap , ( options : MdListOption [ ] ) => {
295
- return merge ( ...options . map ( option => option . onFocus ) ) ;
296
- } ) . subscribe ( ( e : MdSelectionListOptionEvent ) => {
297
- let optionIndex : number = this . options . toArray ( ) . indexOf ( e . option ) ;
298
- this . _keyManager . updateActiveItemIndex ( optionIndex ) ;
299
- } ) ;
248
+ /** Removes an option from the selection list and updates the active item. */
249
+ _removeOptionFromList ( option : MdListOption ) {
250
+ if ( option . _hasFocus ) {
251
+ const optionIndex = this . _getIndexFromOption ( option ) ;
252
+
253
+ // Check whether the option is the last item
254
+ if ( optionIndex - 1 >= 0 ) {
255
+ this . _keyManager . setPreviousItemActive ( ) ;
256
+ } else if ( optionIndex < this . options . length - 1 ) {
257
+ this . _keyManager . setNextItemActive ( ) ;
258
+ }
259
+ }
300
260
}
301
261
302
262
/** Passes relevant key presses to our key manager. */
@@ -334,4 +294,9 @@ export class MdSelectionList extends _MdSelectionListMixinBase
334
294
private _isValidIndex ( index : number ) : boolean {
335
295
return index >= 0 && index < this . options . length ;
336
296
}
297
+
298
+ /** Returns the index of the specified list option. */
299
+ private _getIndexFromOption ( option : MdListOption ) : number {
300
+ return this . options . toArray ( ) . indexOf ( option ) ;
301
+ }
337
302
}
0 commit comments