11import { isPresent } from 'angular2/src/facade/lang' ;
22import { Observable , ObservableWrapper } from 'angular2/src/facade/async' ;
3- import { StringMap , StringMapWrapper } from 'angular2/src/facade/collection' ;
3+ import { StringMap , StringMapWrapper , ListWrapper , List } from 'angular2/src/facade/collection' ;
44import { Validators } from './validators' ;
55
66export const VALID = "VALID" ;
@@ -23,9 +23,12 @@ export class AbstractControl {
2323 _status:string ;
2424 _errors ;
2525 _pristine:boolean ;
26- _parent:ControlGroup ;
26+ _parent:any ; /* ControlGroup | ControlArray */
2727 validator:Function ;
2828
29+ valueChanges:Observable ;
30+ _valueChangesController ;
31+
2932 constructor ( validator :Function ) {
3033 this . validator = validator ;
3134 this . _pristine = true ;
@@ -67,9 +70,6 @@ export class AbstractControl {
6770}
6871
6972export class Control extends AbstractControl {
70- valueChanges :Observable ;
71- _valueChangesController ;
72-
7373 constructor ( value :any , validator :Function = Validators . nullValidator ) {
7474 super ( validator ) ;
7575 this . _setValueErrorsStatus ( value ) ;
@@ -98,9 +98,6 @@ export class ControlGroup extends AbstractControl {
9898 controls ;
9999 optionals ;
100100
101- valueChanges :Observable ;
102- _valueChangesController ;
103-
104101 constructor ( controls , optionals = null , validator :Function = Validators . group ) {
105102 super ( validator ) ;
106103 this . controls = controls ;
@@ -170,4 +167,65 @@ export class ControlGroup extends AbstractControl {
170167 var isOptional = StringMapWrapper . contains ( this . optionals , controlName ) ;
171168 return ! isOptional || StringMapWrapper . get ( this . optionals , controlName ) ;
172169 }
170+ }
171+
172+ export class ControlArray extends AbstractControl {
173+ controls :List ;
174+
175+ constructor ( controls :List , validator :Function = Validators . array ) {
176+ super ( validator ) ;
177+ this . controls = controls ;
178+
179+ this . _valueChangesController = ObservableWrapper . createController ( ) ;
180+ this . valueChanges = ObservableWrapper . createObservable ( this . _valueChangesController ) ;
181+
182+ this . _setParentForControls ( ) ;
183+ this . _setValueErrorsStatus ( ) ;
184+ }
185+
186+ at ( index :number ) {
187+ return this . controls [ index ] ;
188+ }
189+
190+ push ( control ) {
191+ ListWrapper . push ( this . controls , control ) ;
192+ control . setParent ( this ) ;
193+ this . _updateValue ( ) ;
194+ }
195+
196+ insert ( index :number , control ) {
197+ ListWrapper . insert ( this . controls , index , control ) ;
198+ control . setParent ( this ) ;
199+ this . _updateValue ( ) ;
200+ }
201+
202+ removeAt ( index :number ) {
203+ ListWrapper . removeAt ( this . controls , index ) ;
204+ this . _updateValue ( ) ;
205+ }
206+
207+ get length ( ) {
208+ return this . controls . length ;
209+ }
210+
211+ _updateValue ( ) {
212+ this . _setValueErrorsStatus ( ) ;
213+ this . _pristine = false ;
214+
215+ ObservableWrapper . callNext ( this . _valueChangesController , this . _value ) ;
216+
217+ this . _updateParent ( ) ;
218+ }
219+
220+ _setParentForControls ( ) {
221+ ListWrapper . forEach ( this . controls , ( control ) => {
222+ control . setParent ( this ) ;
223+ } ) ;
224+ }
225+
226+ _setValueErrorsStatus ( ) {
227+ this . _value = ListWrapper . map ( this . controls , ( c ) => c . value ) ;
228+ this . _errors = this . validator ( this ) ;
229+ this . _status = isPresent ( this . _errors ) ? INVALID : VALID ;
230+ }
173231}
0 commit comments