@@ -3,10 +3,13 @@ import {MdDrag} from '../../core/services/drag/drag';
33import { ControlValueAccessor } from "angular2/common" ;
44import { NgControl } from "angular2/common" ;
55import { Optional } from "angular2/core" ;
6+ import { Renderer } from "angular2/core" ;
67
78@Component ( {
89 selector : 'md-switch' ,
10+ inputs : [ 'disabled' ] ,
911 host : {
12+ '[attr.aria-disabled]' : 'disabled' ,
1013 '(click)' : 'onClick()'
1114 } ,
1215 templateUrl : './components/switch/switch.html' ,
@@ -27,10 +30,12 @@ export class MdSwitch implements ControlValueAccessor {
2730 onChange = ( _ :any ) => { } ;
2831 onTouched = ( ) => { } ;
2932
30- // Model Values
31- value : boolean ;
33+ // storage values
34+ checked_ : any ;
35+ disabled_ : boolean ;
3236
33- constructor ( private _elementRef : ElementRef , @Optional ( ) ngControl : NgControl ) {
37+ constructor ( private _elementRef : ElementRef , private _renderer : Renderer , @Optional ( ) ngControl : NgControl ) {
38+ this . componentElement = _elementRef . nativeElement ;
3439 this . elementRef = _elementRef ;
3540
3641 if ( ngControl ) {
@@ -39,7 +44,6 @@ export class MdSwitch implements ControlValueAccessor {
3944 }
4045
4146 ngOnInit ( ) {
42- this . componentElement = this . elementRef . nativeElement ;
4347 this . switchContainer = < HTMLElement > this . componentElement . querySelector ( '.md-container' ) ;
4448 this . thumbContainer = < HTMLElement > this . componentElement . querySelector ( '.md-thumb-container' ) ;
4549
@@ -48,10 +52,13 @@ export class MdSwitch implements ControlValueAccessor {
4852 this . switchContainer . addEventListener ( '$md.dragstart' , ( ev : CustomEvent ) => this . onDragStart ( ev ) ) ;
4953 this . switchContainer . addEventListener ( '$md.drag' , ( ev : CustomEvent ) => this . onDrag ( ev ) ) ;
5054 this . switchContainer . addEventListener ( '$md.dragend' , ( ev : CustomEvent ) => this . onDragEnd ( ev ) ) ;
55+
5156 }
5257
5358
5459 onDragStart ( event : CustomEvent ) {
60+ if ( this . disabled ) return ;
61+
5562 this . componentElement . classList . add ( 'md-dragging' ) ;
5663
5764 this . dragData = {
@@ -62,23 +69,27 @@ export class MdSwitch implements ControlValueAccessor {
6269 }
6370
6471 onDrag ( event : CustomEvent ) {
72+ if ( this . disabled ) return ;
73+
6574 let percent = event . detail . pointer . distanceX / this . dragData . width ;
6675
67- let translate = this . value ? 1 + percent : percent ;
76+ let translate = this . checked ? 1 + percent : percent ;
6877 translate = Math . max ( 0 , Math . min ( 1 , translate ) ) ;
6978
7079 this . thumbContainer . style . transform = 'translate3d(' + ( 100 * translate ) + '%,0,0)' ;
7180 this . dragData . translate = translate ;
7281 }
7382
7483 onDragEnd ( event : CustomEvent ) {
84+ if ( this . disabled ) return ;
85+
7586 this . componentElement . classList . remove ( 'md-dragging' ) ;
7687 this . thumbContainer . style . transform = null ;
7788
7889
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 ;
8091 if ( isChanged || ! this . dragData . translate ) {
81- this . changeValue ( ! this . value ) ;
92+ this . checked = ! this . checked ;
8293 }
8394
8495 this . dragData = null ;
@@ -89,19 +100,14 @@ export class MdSwitch implements ControlValueAccessor {
89100 }
90101
91102 onClick ( ) {
92- if ( ! this . dragClick ) this . changeValue ( ! this . value ) ;
103+ if ( ! this . dragClick && ! this . disabled ) {
104+ this . checked = ! this . checked ;
105+ }
93106 }
94107
95- changeValue ( newValue : boolean ) {
96- this . onChange ( newValue ) ;
97- this . writeValue ( newValue ) ;
98- }
99108
100109 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 ;
105111 }
106112
107113 registerOnChange ( fn : any ) : void {
@@ -111,4 +117,31 @@ export class MdSwitch implements ControlValueAccessor {
111117 registerOnTouched ( fn : any ) : void {
112118 this . onTouched = fn ;
113119 }
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+
114147}
0 commit comments