forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectives.js
More file actions
162 lines (135 loc) · 4.04 KB
/
Copy pathdirectives.js
File metadata and controls
162 lines (135 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {Template, Component, Decorator, Ancestor, onChange, PropertySetter} from 'angular2/angular2';
import {Optional} from 'angular2/di';
import {isBlank, isPresent, isString, CONST} from 'angular2/src/facade/lang';
import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {ControlGroup, Control} from './model';
import {Validators} from './validators';
//export interface ControlValueAccessor {
// writeValue(value):void{}
// set onChange(fn){}
//}
@Decorator({
selector: '[control]',
events: {
'change' : 'onChange($event.target.value)',
'input' : 'onChange($event.target.value)'
}
})
export class DefaultValueAccessor {
_setValueProperty:Function;
onChange:Function;
constructor(@PropertySetter('value') setValueProperty:Function) {
this._setValueProperty = setValueProperty;
this.onChange = (_) => {};
}
writeValue(value) {
this._setValueProperty(value);
}
}
@Decorator({
selector: 'input[type=checkbox][control]',
events: {
'change' : 'onChange($event.target.checked)'
}
})
export class CheckboxControlValueAccessor {
_setCheckedProperty:Function;
onChange:Function;
constructor(cd:ControlDirective, @PropertySetter('checked') setCheckedProperty:Function) {
this._setCheckedProperty = setCheckedProperty;
this.onChange = (_) => {};
cd.valueAccessor = this; //ControlDirective should inject CheckboxControlDirective
}
writeValue(value) {
this._setCheckedProperty(value);
}
}
@Decorator({
lifecycle: [onChange],
selector: '[control]',
bind: {
'controlOrName' : 'control'
}
})
export class ControlDirective {
_groupDirective:ControlGroupDirective;
controlOrName:any;
valueAccessor:any; //ControlValueAccessor
validator:Function;
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
this._groupDirective = groupDirective;
this.controlOrName = null;
this.valueAccessor = valueAccessor;
this.validator = Validators.nullValidator;
}
// TODO: vsavkin this should be moved into the constructor once static bindings
// are implemented
onChange(_) {
this._initialize();
}
_initialize() {
if(isPresent(this._groupDirective)) {
this._groupDirective.addDirective(this);
}
var c = this._control();
c.validator = Validators.compose([c.validator, this.validator]);
this._updateDomValue();
this._setUpUpdateControlValue();
}
_updateDomValue() {
this.valueAccessor.writeValue(this._control().value);
}
_setUpUpdateControlValue() {
this.valueAccessor.onChange = (newValue) => this._control().updateValue(newValue);
}
_control() {
if (isString(this.controlOrName)) {
return this._groupDirective.findControl(this.controlOrName);
} else {
return this.controlOrName;
}
}
}
@Decorator({
selector: '[control-group]',
bind: {
'controlGroup' : 'control-group'
}
})
export class ControlGroupDirective {
_groupDirective:ControlGroupDirective;
_controlGroupName:string;
_controlGroup:ControlGroup;
_directives:List<ControlDirective>;
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective) {
this._groupDirective = groupDirective;
this._directives = ListWrapper.create();
}
set controlGroup(controlGroup) {
if (isString(controlGroup)) {
this._controlGroupName = controlGroup;
} else {
this._controlGroup = controlGroup;
}
this._updateDomValue();
}
_updateDomValue() {
ListWrapper.forEach(this._directives, (cd) => cd._updateDomValue());
}
addDirective(c:ControlDirective) {
ListWrapper.push(this._directives, c);
}
findControl(name:string):any {
return this._getControlGroup().controls[name];
}
_getControlGroup():ControlGroup {
if (isPresent(this._controlGroupName)) {
return this._groupDirective.findControl(this._controlGroupName)
} else {
return this._controlGroup;
}
}
}
export var FormDirectives = [
ControlGroupDirective, ControlDirective, CheckboxControlValueAccessor, DefaultValueAccessor
];