@@ -15,11 +15,11 @@ import {
15
15
} from '@angular/core' ;
16
16
import { NG_VALUE_ACCESSOR , ControlValueAccessor } from '@angular/forms' ;
17
17
import { coerceBooleanProperty } from '../core/coercion/boolean-property' ;
18
- import { Subscription } from 'rxjs/Subscription' ;
19
18
import {
20
19
MdRipple ,
21
20
RippleRef ,
22
21
FocusOriginMonitor ,
22
+ FocusOrigin ,
23
23
} from '../core' ;
24
24
25
25
@@ -183,10 +183,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
183
183
private _controlValueAccessorChangeFn : ( value : any ) => void = ( value ) => { } ;
184
184
185
185
/** Reference to the focused state ripple. */
186
- private _focusedRipple : RippleRef ;
187
-
188
- /** Reference to the focus origin monitor subscription. */
189
- private _focusedSubscription : Subscription ;
186
+ private _focusRipple : RippleRef ;
190
187
191
188
constructor ( private _renderer : Renderer ,
192
189
private _elementRef : ElementRef ,
@@ -196,13 +193,9 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
196
193
}
197
194
198
195
ngAfterViewInit ( ) {
199
- this . _focusedSubscription = this . _focusOriginMonitor
196
+ this . _focusOriginMonitor
200
197
. monitor ( this . _inputElement . nativeElement , this . _renderer , false )
201
- . subscribe ( focusOrigin => {
202
- if ( ! this . _focusedRipple && ( focusOrigin === 'keyboard' || focusOrigin === 'program' ) ) {
203
- this . _focusedRipple = this . _ripple . launch ( 0 , 0 , { persistent : true , centered : true } ) ;
204
- }
205
- } ) ;
198
+ . subscribe ( focusOrigin => this . _onInputFocusChange ( focusOrigin ) ) ;
206
199
}
207
200
208
201
ngOnDestroy ( ) {
@@ -343,10 +336,14 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
343
336
this . change . emit ( event ) ;
344
337
}
345
338
346
- /** Informs the component when we lose focus in order to style accordingly */
347
- _onInputBlur ( ) {
348
- this . _removeFocusedRipple ( ) ;
349
- this . onTouched ( ) ;
339
+ /** Function is called whenever the focus changes for the input element. */
340
+ private _onInputFocusChange ( focusOrigin : FocusOrigin ) {
341
+ if ( ! this . _focusRipple && focusOrigin === 'keyboard' ) {
342
+ this . _focusRipple = this . _ripple . launch ( 0 , 0 , { persistent : true , centered : true } ) ;
343
+ } else if ( ! focusOrigin ) {
344
+ this . _removeFocusRipple ( ) ;
345
+ this . onTouched ( ) ;
346
+ }
350
347
}
351
348
352
349
/** Toggles the `checked` state of the checkbox. */
@@ -371,7 +368,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
371
368
// Preventing bubbling for the second event will solve that issue.
372
369
event . stopPropagation ( ) ;
373
370
374
- this . _removeFocusedRipple ( ) ;
371
+ this . _removeFocusRipple ( ) ;
375
372
376
373
if ( ! this . disabled ) {
377
374
this . toggle ( ) ;
@@ -387,7 +384,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
387
384
388
385
/** Focuses the checkbox. */
389
386
focus ( ) : void {
390
- this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , this . _renderer , 'program ' ) ;
387
+ this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , this . _renderer , 'keyboard ' ) ;
391
388
}
392
389
393
390
_onInteractionEvent ( event : Event ) {
@@ -429,11 +426,11 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
429
426
return `mat-checkbox-anim-${ animSuffix } ` ;
430
427
}
431
428
432
- /** Fades out the focused state ripple. */
433
- private _removeFocusedRipple ( ) : void {
434
- if ( this . _focusedRipple ) {
435
- this . _focusedRipple . fadeOut ( ) ;
436
- this . _focusedRipple = null ;
429
+ /** Fades out the focus state ripple. */
430
+ private _removeFocusRipple ( ) : void {
431
+ if ( this . _focusRipple ) {
432
+ this . _focusRipple . fadeOut ( ) ;
433
+ this . _focusRipple = null ;
437
434
}
438
435
}
439
436
}
0 commit comments