2015-02-11 19:10:31 +00:00
|
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
|
|
|
|
import {StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
|
import {nullValidator, controlGroupValidator} from './validators';
|
|
|
|
|
|
|
|
|
|
export const VALID = "VALID";
|
|
|
|
|
export const INVALID = "INVALID";
|
2015-02-03 15:27:09 +00:00
|
|
|
|
2015-02-24 19:59:10 +00:00
|
|
|
//interface IControl {
|
|
|
|
|
// get value():any;
|
|
|
|
|
// validator:Function;
|
|
|
|
|
// get status():string;
|
2015-02-25 20:54:27 +00:00
|
|
|
// get valid():boolean;
|
2015-02-24 19:59:10 +00:00
|
|
|
// get errors():Map;
|
|
|
|
|
// updateValue(value:any){}
|
|
|
|
|
// setParent(parent){}
|
|
|
|
|
//}
|
|
|
|
|
|
2015-02-25 23:10:27 +00:00
|
|
|
export class AbstractControl {
|
2015-02-24 19:59:10 +00:00
|
|
|
_value:any;
|
|
|
|
|
_status:string;
|
|
|
|
|
_errors;
|
2015-02-25 20:54:27 +00:00
|
|
|
_dirty:boolean;
|
2015-02-11 19:10:31 +00:00
|
|
|
_parent:ControlGroup;
|
2015-02-24 19:59:10 +00:00
|
|
|
validator:Function;
|
2015-02-03 15:27:09 +00:00
|
|
|
|
2015-02-25 23:10:27 +00:00
|
|
|
constructor(validator:Function = nullValidator) {
|
2015-02-11 19:10:31 +00:00
|
|
|
this.validator = validator;
|
2015-02-25 20:54:27 +00:00
|
|
|
this._dirty = true;
|
2015-02-11 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-24 19:59:10 +00:00
|
|
|
get value() {
|
2015-02-25 23:10:27 +00:00
|
|
|
this._updateIfNeeded();
|
2015-02-24 19:59:10 +00:00
|
|
|
return this._value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
|
this._updateIfNeeded();
|
|
|
|
|
return this._status;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 19:10:31 +00:00
|
|
|
get valid() {
|
2015-02-24 19:59:10 +00:00
|
|
|
this._updateIfNeeded();
|
|
|
|
|
return this._status === VALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get errors() {
|
|
|
|
|
this._updateIfNeeded();
|
|
|
|
|
return this._errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setParent(parent){
|
|
|
|
|
this._parent = parent;
|
2015-02-11 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-24 19:59:10 +00:00
|
|
|
_updateIfNeeded() {
|
2015-02-11 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_updateParent() {
|
|
|
|
|
if (isPresent(this._parent)){
|
|
|
|
|
this._parent._controlChanged();
|
|
|
|
|
}
|
2015-02-03 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-25 23:10:27 +00:00
|
|
|
export class Control extends AbstractControl {
|
|
|
|
|
constructor(value:any, validator:Function = nullValidator) {
|
|
|
|
|
super(validator);
|
|
|
|
|
this._value = value;
|
2015-02-03 15:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-25 23:10:27 +00:00
|
|
|
updateValue(value:any) {
|
|
|
|
|
this._value = value;
|
|
|
|
|
this._dirty = true;
|
|
|
|
|
this._updateParent();
|
2015-02-24 19:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-25 23:10:27 +00:00
|
|
|
_updateIfNeeded() {
|
|
|
|
|
if (this._dirty) {
|
|
|
|
|
this._dirty = false;
|
|
|
|
|
this._errors = this.validator(this);
|
|
|
|
|
this._status = isPresent(this._errors) ? INVALID : VALID;
|
|
|
|
|
}
|
2015-02-03 15:27:09 +00:00
|
|
|
}
|
2015-02-25 23:10:27 +00:00
|
|
|
}
|
2015-02-11 19:10:31 +00:00
|
|
|
|
2015-02-25 23:10:27 +00:00
|
|
|
export class ControlGroup extends AbstractControl {
|
|
|
|
|
controls;
|
2015-03-11 01:12:30 +00:00
|
|
|
optionals;
|
2015-02-24 19:59:10 +00:00
|
|
|
|
2015-03-11 01:12:30 +00:00
|
|
|
constructor(controls, optionals = null, validator:Function = controlGroupValidator) {
|
2015-02-25 23:10:27 +00:00
|
|
|
super(validator);
|
|
|
|
|
this.controls = controls;
|
2015-03-11 01:12:30 +00:00
|
|
|
this.optionals = isPresent(optionals) ? optionals : {};
|
2015-02-25 23:10:27 +00:00
|
|
|
this._setParentForControls();
|
2015-02-11 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-11 01:12:30 +00:00
|
|
|
include(controlName:string) {
|
|
|
|
|
this._dirty = true;
|
|
|
|
|
StringMapWrapper.set(this.optionals, controlName, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exclude(controlName:string) {
|
|
|
|
|
this._dirty = true;
|
|
|
|
|
StringMapWrapper.set(this.optionals, controlName, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contains(controlName:string) {
|
|
|
|
|
var c = StringMapWrapper.contains(this.controls, controlName);
|
|
|
|
|
return c && this._included(controlName);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 19:10:31 +00:00
|
|
|
_setParentForControls() {
|
|
|
|
|
StringMapWrapper.forEach(this.controls, (control, name) => {
|
2015-02-24 19:59:10 +00:00
|
|
|
control.setParent(this);
|
2015-02-11 19:10:31 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 19:59:10 +00:00
|
|
|
_updateIfNeeded() {
|
2015-02-25 20:54:27 +00:00
|
|
|
if (this._dirty) {
|
|
|
|
|
this._dirty = false;
|
2015-02-24 19:59:10 +00:00
|
|
|
this._value = this._reduceValue();
|
|
|
|
|
this._errors = this.validator(this);
|
|
|
|
|
this._status = isPresent(this._errors) ? INVALID : VALID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_reduceValue() {
|
|
|
|
|
var newValue = {};
|
|
|
|
|
StringMapWrapper.forEach(this.controls, (control, name) => {
|
2015-03-11 01:12:30 +00:00
|
|
|
if (this._included(name)) {
|
2015-02-24 19:59:10 +00:00
|
|
|
newValue[name] = control.value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return newValue;
|
2015-02-11 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_controlChanged() {
|
2015-02-25 20:54:27 +00:00
|
|
|
this._dirty = true;
|
2015-02-25 23:10:27 +00:00
|
|
|
this._updateParent();
|
2015-02-11 19:10:31 +00:00
|
|
|
}
|
2015-02-24 19:59:10 +00:00
|
|
|
|
2015-03-11 01:12:30 +00:00
|
|
|
_included(controlName:string):boolean {
|
|
|
|
|
var isOptional = StringMapWrapper.contains(this.optionals, controlName);
|
|
|
|
|
return !isOptional || StringMapWrapper.get(this.optionals, controlName);
|
2015-02-24 19:59:10 +00:00
|
|
|
}
|
|
|
|
|
}
|