5
5
HostBinding ,
6
6
HostListener ,
7
7
ChangeDetectionStrategy ,
8
+ ElementRef ,
9
+ Renderer ,
8
10
} from 'angular2/core' ;
9
11
import { TimerWrapper } from 'angular2/src/facade/async' ;
10
12
@@ -18,7 +20,6 @@ import {TimerWrapper} from 'angular2/src/facade/async';
18
20
inputs : [ 'color' ] ,
19
21
host : {
20
22
'[class.md-button-focus]' : 'isKeyboardFocused' ,
21
- '[class]' : 'setClassList()' ,
22
23
'(mousedown)' : 'setMousedown()' ,
23
24
'(focus)' : 'setKeyboardFocus()' ,
24
25
'(blur)' : 'removeKeyboardFocus()'
@@ -29,15 +30,23 @@ import {TimerWrapper} from 'angular2/src/facade/async';
29
30
changeDetection : ChangeDetectionStrategy . OnPush ,
30
31
} )
31
32
export class MdButton {
32
- color : string ;
33
+ private _color : string ;
33
34
34
35
/** Whether the button has focus from the keyboard (not the mouse). Used for class binding. */
35
36
isKeyboardFocused : boolean = false ;
36
37
37
38
/** Whether a mousedown has occurred on this element in the last 100ms. */
38
39
isMouseDown : boolean = false ;
39
40
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
+ }
41
50
42
51
setMousedown ( ) {
43
52
// We only *show* the focus style when focus has come to the button via the keyboard.
@@ -48,6 +57,18 @@ export class MdButton {
48
57
TimerWrapper . setTimeout ( ( ) => { this . isMouseDown = false ; } , 100 ) ;
49
58
}
50
59
60
+ _updateColor ( newColor : string ) {
61
+ if ( this . _color != null && this . _color != '' ) {
62
+ this . renderer . setElementClass ( this . elementRef . nativeElement , `md-${ this . _color } ` , false ) ;
63
+ }
64
+
65
+ if ( newColor != null && newColor != '' ) {
66
+ this . renderer . setElementClass ( this . elementRef . nativeElement , `md-${ newColor } ` , true ) ;
67
+ }
68
+
69
+ this . _color = newColor ;
70
+ }
71
+
51
72
setKeyboardFocus ( $event : any ) {
52
73
this . isKeyboardFocused = ! this . isMouseDown ;
53
74
}
@@ -62,7 +83,6 @@ export class MdButton {
62
83
inputs : [ 'color' ] ,
63
84
host : {
64
85
'[class.md-button-focus]' : 'isKeyboardFocused' ,
65
- '[class]' : 'setClassList()' ,
66
86
'(mousedown)' : 'setMousedown()' ,
67
87
'(focus)' : 'setKeyboardFocus()' ,
68
88
'(blur)' : 'removeKeyboardFocus()'
@@ -74,6 +94,10 @@ export class MdButton {
74
94
export class MdAnchor extends MdButton {
75
95
_disabled : boolean = null ;
76
96
97
+ constructor ( elementRef : ElementRef , renderer : Renderer ) {
98
+ super ( elementRef , renderer ) ;
99
+ }
100
+
77
101
@HostBinding ( 'tabIndex' )
78
102
get tabIndex ( ) : number {
79
103
return this . disabled ? - 1 : 0 ;
0 commit comments