@@ -3,10 +3,13 @@ import {MdDrag} from '../../core/services/drag/drag';
3
3
import { ControlValueAccessor } from "angular2/common" ;
4
4
import { NgControl } from "angular2/common" ;
5
5
import { Optional } from "angular2/core" ;
6
+ import { Renderer } from "angular2/core" ;
6
7
7
8
@Component ( {
8
9
selector : 'md-switch' ,
10
+ inputs : [ 'disabled' ] ,
9
11
host : {
12
+ '[attr.aria-disabled]' : 'disabled' ,
10
13
'(click)' : 'onClick()'
11
14
} ,
12
15
templateUrl : './components/switch/switch.html' ,
@@ -27,10 +30,12 @@ export class MdSwitch implements ControlValueAccessor {
27
30
onChange = ( _ :any ) => { } ;
28
31
onTouched = ( ) => { } ;
29
32
30
- // Model Values
31
- value : boolean ;
33
+ // storage values
34
+ checked_ : any ;
35
+ disabled_ : boolean ;
32
36
33
- constructor ( private _elementRef : ElementRef , @Optional ( ) ngControl : NgControl ) {
37
+ constructor ( private _elementRef : ElementRef , private _renderer : Renderer , @Optional ( ) ngControl : NgControl ) {
38
+ this . componentElement = _elementRef . nativeElement ;
34
39
this . elementRef = _elementRef ;
35
40
36
41
if ( ngControl ) {
@@ -39,7 +44,6 @@ export class MdSwitch implements ControlValueAccessor {
39
44
}
40
45
41
46
ngOnInit ( ) {
42
- this . componentElement = this . elementRef . nativeElement ;
43
47
this . switchContainer = < HTMLElement > this . componentElement . querySelector ( '.md-container' ) ;
44
48
this . thumbContainer = < HTMLElement > this . componentElement . querySelector ( '.md-thumb-container' ) ;
45
49
@@ -48,10 +52,13 @@ export class MdSwitch implements ControlValueAccessor {
48
52
this . switchContainer . addEventListener ( '$md.dragstart' , ( ev : CustomEvent ) => this . onDragStart ( ev ) ) ;
49
53
this . switchContainer . addEventListener ( '$md.drag' , ( ev : CustomEvent ) => this . onDrag ( ev ) ) ;
50
54
this . switchContainer . addEventListener ( '$md.dragend' , ( ev : CustomEvent ) => this . onDragEnd ( ev ) ) ;
55
+
51
56
}
52
57
53
58
54
59
onDragStart ( event : CustomEvent ) {
60
+ if ( this . disabled ) return ;
61
+
55
62
this . componentElement . classList . add ( 'md-dragging' ) ;
56
63
57
64
this . dragData = {
@@ -62,23 +69,27 @@ export class MdSwitch implements ControlValueAccessor {
62
69
}
63
70
64
71
onDrag ( event : CustomEvent ) {
72
+ if ( this . disabled ) return ;
73
+
65
74
let percent = event . detail . pointer . distanceX / this . dragData . width ;
66
75
67
- let translate = this . value ? 1 + percent : percent ;
76
+ let translate = this . checked ? 1 + percent : percent ;
68
77
translate = Math . max ( 0 , Math . min ( 1 , translate ) ) ;
69
78
70
79
this . thumbContainer . style . transform = 'translate3d(' + ( 100 * translate ) + '%,0,0)' ;
71
80
this . dragData . translate = translate ;
72
81
}
73
82
74
83
onDragEnd ( event : CustomEvent ) {
84
+ if ( this . disabled ) return ;
85
+
75
86
this . componentElement . classList . remove ( 'md-dragging' ) ;
76
87
this . thumbContainer . style . transform = null ;
77
88
78
89
79
- var isChanged = this . value ? this . dragData . translate < 0.5 : this . dragData . translate > 0.5 ;
90
+ var isChanged = this . checked ? this . dragData . translate < 0.5 : this . dragData . translate > 0.5 ;
80
91
if ( isChanged || ! this . dragData . translate ) {
81
- this . changeValue ( ! this . value ) ;
92
+ this . checked = ! this . checked ;
82
93
}
83
94
84
95
this . dragData = null ;
@@ -89,19 +100,14 @@ export class MdSwitch implements ControlValueAccessor {
89
100
}
90
101
91
102
onClick ( ) {
92
- if ( ! this . dragClick ) this . changeValue ( ! this . value ) ;
103
+ if ( ! this . dragClick && ! this . disabled ) {
104
+ this . checked = ! this . checked ;
105
+ }
93
106
}
94
107
95
- changeValue ( newValue : boolean ) {
96
- this . onChange ( newValue ) ;
97
- this . writeValue ( newValue ) ;
98
- }
99
108
100
109
writeValue ( value : any ) : void {
101
- this . value = ! ! value ;
102
-
103
- // Apply Checked Class
104
- this . componentElement . classList . toggle ( 'md-checked' , this . value ) ;
110
+ this . checked = value ;
105
111
}
106
112
107
113
registerOnChange ( fn : any ) : void {
@@ -111,4 +117,31 @@ export class MdSwitch implements ControlValueAccessor {
111
117
registerOnTouched ( fn : any ) : void {
112
118
this . onTouched = fn ;
113
119
}
120
+
121
+ get disabled ( ) : string | boolean {
122
+ return this . disabled_ ;
123
+ }
124
+
125
+ set disabled ( value : string | boolean ) {
126
+ if ( typeof value == 'string' ) {
127
+ this . disabled_ = ( value === 'true' || value === '' ) ;
128
+ } else {
129
+ this . disabled_ = < boolean > value ;
130
+ }
131
+
132
+ this . _renderer . setElementAttribute ( this . _elementRef , 'disabled' , this . disabled_ ? 'true' : undefined ) ;
133
+ }
134
+
135
+ get checked ( ) {
136
+ return ! ! this . checked_ ;
137
+ }
138
+
139
+ set checked ( value ) {
140
+ this . checked_ = ! ! value ;
141
+ this . onChange ( this . checked_ ) ;
142
+
143
+ this . _renderer . setElementAttribute ( this . _elementRef , 'aria-checked' , this . checked_ ) ;
144
+ this . componentElement . classList . toggle ( 'md-checked' , this . checked ) ;
145
+ }
146
+
114
147
}
0 commit comments