@@ -15,11 +15,11 @@ import {
1515} from '@angular/core' ;
1616import { NG_VALUE_ACCESSOR , ControlValueAccessor } from '@angular/forms' ;
1717import { coerceBooleanProperty } from '../core/coercion/boolean-property' ;
18- import { Subscription } from 'rxjs/Subscription' ;
1918import {
2019 MdRipple ,
2120 RippleRef ,
2221 FocusOriginMonitor ,
22+ FocusOrigin ,
2323} from '../core' ;
2424
2525
@@ -183,10 +183,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
183183 private _controlValueAccessorChangeFn : ( value : any ) => void = ( value ) => { } ;
184184
185185 /** 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 ;
190187
191188 constructor ( private _renderer : Renderer ,
192189 private _elementRef : ElementRef ,
@@ -196,13 +193,9 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
196193 }
197194
198195 ngAfterViewInit ( ) {
199- this . _focusedSubscription = this . _focusOriginMonitor
196+ this . _focusOriginMonitor
200197 . 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 ) ) ;
206199 }
207200
208201 ngOnDestroy ( ) {
@@ -343,10 +336,14 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
343336 this . change . emit ( event ) ;
344337 }
345338
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+ }
350347 }
351348
352349 /** Toggles the `checked` state of the checkbox. */
@@ -371,7 +368,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
371368 // Preventing bubbling for the second event will solve that issue.
372369 event . stopPropagation ( ) ;
373370
374- this . _removeFocusedRipple ( ) ;
371+ this . _removeFocusRipple ( ) ;
375372
376373 if ( ! this . disabled ) {
377374 this . toggle ( ) ;
@@ -387,7 +384,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
387384
388385 /** Focuses the checkbox. */
389386 focus ( ) : void {
390- this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , this . _renderer , 'program ' ) ;
387+ this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , this . _renderer , 'keyboard ' ) ;
391388 }
392389
393390 _onInteractionEvent ( event : Event ) {
@@ -429,11 +426,11 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
429426 return `mat-checkbox-anim-${ animSuffix } ` ;
430427 }
431428
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 ;
437434 }
438435 }
439436}
0 commit comments