1- import { Template , Component , Decorator , NgElement , Ancestor , onChange } from 'angular2/angular2' ;
1+ import { Template , Component , Decorator , Ancestor , onChange , PropertySetter } from 'angular2/angular2' ;
22import { Optional } from 'angular2/di' ;
33import { DOM } from 'angular2/src/dom/dom_adapter' ;
44import { isBlank , isPresent , isString , CONST } from 'angular2/src/facade/lang' ;
55import { StringMapWrapper , ListWrapper } from 'angular2/src/facade/collection' ;
66import { ControlGroup , Control } from './model' ;
77import { Validators } from './validators' ;
88
9- @CONST ( )
109export class ControlValueAccessor {
11- readValue ( el ) { }
12- writeValue ( el , value ) : void { }
10+ writeValue ( value ) : void { }
11+ set onChange ( fn ) { }
1312}
1413
15- @CONST ( )
16- class DefaultControlValueAccessor extends ControlValueAccessor {
17- constructor ( ) {
18- super ( ) ;
14+ @Decorator ( {
15+ selector : '[control]' ,
16+ events : {
17+ 'change' : 'onChange($event.target.value)'
1918 }
19+ } )
20+ export class DefaultControlDecorator extends ControlValueAccessor {
21+ _setValueProperty :Function ;
22+ onChange :Function ;
2023
21- readValue ( el ) {
22- return DOM . getValue ( el ) ;
24+ constructor ( @PropertySetter ( 'value' ) setValueProperty :Function ) {
25+ this . _setValueProperty = setValueProperty ;
26+ this . onChange = ( _ ) => { } ;
2327 }
2428
25- writeValue ( el , value ) : void {
26- DOM . setValue ( el , value ) ;
29+ writeValue ( value ) {
30+ this . _setValueProperty ( value ) ;
2731 }
2832}
2933
30- @CONST ( )
31- class CheckboxControlValueAccessor extends ControlValueAccessor {
32- constructor ( ) {
33- super ( ) ;
34- }
35-
36- readValue ( el ) :boolean {
37- return DOM . getChecked ( el ) ;
34+ @Decorator ( {
35+ selector : 'input[type=checkbox]' , //should be input[type=checkbox][control]
36+ events : {
37+ 'change' : 'onChange($event.target.checked)'
3838 }
39+ } )
40+ export class CheckboxControlDecorator extends ControlValueAccessor {
41+ _setCheckedProperty :Function ;
42+ onChange :Function ;
3943
40- writeValue ( el , value :boolean ) :void {
41- DOM . setChecked ( el , value ) ;
44+ constructor ( cd :ControlDirective , @PropertySetter ( 'checked' ) setCheckedProperty :Function ) {
45+ this . _setCheckedProperty = setCheckedProperty ;
46+ this . onChange = ( _ ) => { } ;
47+ //TODO: vsavkin ControlDirective should inject CheckboxControlDirective
48+ cd . valueAccessor = this ;
4249 }
43- }
4450
45- var controlValueAccessors = {
46- "checkbox" : new CheckboxControlValueAccessor ( ) ,
47- "text" : new DefaultControlValueAccessor ( )
48- } ;
49-
50- function controlValueAccessorFor ( controlType :string ) :ControlValueAccessor {
51- var accessor = StringMapWrapper . get ( controlValueAccessors , controlType ) ;
52- if ( isPresent ( accessor ) ) {
53- return accessor ;
54- } else {
55- return StringMapWrapper . get ( controlValueAccessors , "text" ) ;
51+ writeValue ( value ) {
52+ this . _setCheckedProperty ( value ) ;
5653 }
5754}
5855
5956@Decorator ( {
6057 lifecycle : [ onChange ] ,
6158 selector : '[control]' ,
6259 bind : {
63- 'controlName' : 'control' ,
64- 'type' : 'type'
60+ 'controlName' : 'control'
6561 }
6662} )
6763export class ControlDirective {
6864 _groupDirective :ControlGroupDirective ;
69- _el :NgElement ;
7065
7166 controlName :string ;
72- type :string ;
7367 valueAccessor :ControlValueAccessor ;
7468
7569 validator :Function ;
7670
77- constructor ( @Ancestor ( ) groupDirective :ControlGroupDirective , el : NgElement ) {
71+ constructor ( @Ancestor ( ) groupDirective :ControlGroupDirective , valueAccessor : DefaultControlDecorator ) {
7872 this . _groupDirective = groupDirective ;
79- this . _el = el ;
8073 this . controlName = null ;
81- this . type = null ;
74+ this . valueAccessor = valueAccessor ;
8275 this . validator = Validators . nullValidator ;
8376 }
8477
@@ -94,20 +87,20 @@ export class ControlDirective {
9487 var c = this . _control ( ) ;
9588 c . validator = Validators . compose ( [ c . validator , this . validator ] ) ;
9689
97- if ( isBlank ( this . valueAccessor ) ) {
98- this . valueAccessor = controlValueAccessorFor ( this . type ) ;
99- }
100-
10190 this . _updateDomValue ( ) ;
102- DOM . on ( this . _el . domElement , "change" , ( _ ) => this . _updateControlValue ( ) ) ;
91+ this . _setUpUpdateControlValue ( ) ;
10392 }
10493
10594 _updateDomValue ( ) {
106- this . valueAccessor . writeValue ( this . _el . domElement , this . _control ( ) . value ) ;
95+ this . valueAccessor . writeValue ( this . _control ( ) . value ) ;
96+ }
97+
98+ _setUpUpdateControlValue ( ) {
99+ this . valueAccessor . onChange = ( newValue ) => this . _control ( ) . updateValue ( newValue ) ;
107100 }
108101
109- _updateControlValue ( ) {
110- this . _control ( ) . updateValue ( this . valueAccessor . readValue ( this . _el . domElement ) ) ;
102+ _updateControlValue ( newValue ) {
103+ this . _control ( ) . updateValue ( newValue ) ;
111104 }
112105
113106 _control ( ) {
@@ -165,5 +158,5 @@ export class ControlGroupDirective {
165158}
166159
167160export var FormDirectives = [
168- ControlGroupDirective , ControlDirective
161+ ControlGroupDirective , ControlDirective , CheckboxControlDecorator , DefaultControlDecorator
169162] ;
0 commit comments