55 HostBinding ,
66 HostListener ,
77 ChangeDetectionStrategy ,
8+ ElementRef ,
9+ Renderer ,
810} from 'angular2/core' ;
911import { TimerWrapper } from 'angular2/src/facade/async' ;
1012
@@ -18,7 +20,6 @@ import {TimerWrapper} from 'angular2/src/facade/async';
1820 inputs : [ 'color' ] ,
1921 host : {
2022 '[class.md-button-focus]' : 'isKeyboardFocused' ,
21- '[class]' : 'setClassList()' ,
2223 '(mousedown)' : 'setMousedown()' ,
2324 '(focus)' : 'setKeyboardFocus()' ,
2425 '(blur)' : 'removeKeyboardFocus()'
@@ -29,15 +30,23 @@ import {TimerWrapper} from 'angular2/src/facade/async';
2930 changeDetection : ChangeDetectionStrategy . OnPush ,
3031} )
3132export class MdButton {
32- color : string ;
33+ private _color : string ;
3334
3435 /** Whether the button has focus from the keyboard (not the mouse). Used for class binding. */
3536 isKeyboardFocused : boolean = false ;
3637
3738 /** Whether a mousedown has occurred on this element in the last 100ms. */
3839 isMouseDown : boolean = false ;
3940
40- setClassList ( ) { return `md-${ this . color } ` ; }
41+ constructor ( private elementRef : ElementRef , private renderer : Renderer ) { }
42+
43+ get color ( ) : string {
44+ return this . _color ;
45+ }
46+
47+ set color ( value : string ) {
48+ this . _updateColor ( value ) ;
49+ }
4150
4251 setMousedown ( ) {
4352 // We only *show* the focus style when focus has come to the button via the keyboard.
@@ -48,6 +57,18 @@ export class MdButton {
4857 TimerWrapper . setTimeout ( ( ) => { this . isMouseDown = false ; } , 100 ) ;
4958 }
5059
60+ _updateColor ( newColor : string ) {
61+ this . _setElementColor ( this . _color , false ) ;
62+ this . _setElementColor ( newColor , true ) ;
63+ this . _color = newColor ;
64+ }
65+
66+ _setElementColor ( color : string , isAdd : boolean ) {
67+ if ( color != null && color != '' ) {
68+ this . renderer . setElementClass ( this . elementRef . nativeElement , `md-${ color } ` , isAdd ) ;
69+ }
70+ }
71+
5172 setKeyboardFocus ( $event : any ) {
5273 this . isKeyboardFocused = ! this . isMouseDown ;
5374 }
@@ -62,7 +83,6 @@ export class MdButton {
6283 inputs : [ 'color' ] ,
6384 host : {
6485 '[class.md-button-focus]' : 'isKeyboardFocused' ,
65- '[class]' : 'setClassList()' ,
6686 '(mousedown)' : 'setMousedown()' ,
6787 '(focus)' : 'setKeyboardFocus()' ,
6888 '(blur)' : 'removeKeyboardFocus()'
@@ -74,6 +94,10 @@ export class MdButton {
7494export class MdAnchor extends MdButton {
7595 _disabled : boolean = null ;
7696
97+ constructor ( elementRef : ElementRef , renderer : Renderer ) {
98+ super ( elementRef , renderer ) ;
99+ }
100+
77101 @HostBinding ( 'tabIndex' )
78102 get tabIndex ( ) : number {
79103 return this . disabled ? - 1 : 0 ;
0 commit comments