2015-02-11 11:10:31 -08: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 07:27:09 -08:00
|
|
|
|
2015-02-24 11:59:10 -08:00
|
|
|
//interface IControl {
|
|
|
|
// get value():any;
|
|
|
|
// validator:Function;
|
|
|
|
// get status():string;
|
2015-02-25 12:54:27 -08:00
|
|
|
// get valid():boolean;
|
2015-02-24 11:59:10 -08:00
|
|
|
// get errors():Map;
|
|
|
|
// get active():boolean {}
|
|
|
|
// updateValue(value:any){}
|
|
|
|
// setParent(parent){}
|
|
|
|
//}
|
|
|
|
|
2015-02-03 07:27:09 -08:00
|
|
|
export class Control {
|
2015-02-24 11:59:10 -08:00
|
|
|
_value:any;
|
|
|
|
_status:string;
|
|
|
|
_errors;
|
2015-02-25 12:54:27 -08:00
|
|
|
_dirty:boolean;
|
2015-02-11 11:10:31 -08:00
|
|
|
_parent:ControlGroup;
|
2015-02-24 11:59:10 -08:00
|
|
|
validator:Function;
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-02-11 11:10:31 -08:00
|
|
|
constructor(value:any, validator:Function = nullValidator) {
|
2015-02-24 11:59:10 -08:00
|
|
|
this._value = value;
|
2015-02-11 11:10:31 -08:00
|
|
|
this.validator = validator;
|
2015-02-25 12:54:27 -08:00
|
|
|
this._dirty = true;
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateValue(value:any) {
|
2015-02-24 11:59:10 -08:00
|
|
|
this._value = value;
|
2015-02-25 12:54:27 -08:00
|
|
|
this._dirty = true;
|
2015-02-11 11:10:31 -08:00
|
|
|
this._updateParent();
|
|
|
|
}
|
|
|
|
|
2015-02-24 11:59:10 -08:00
|
|
|
get active():boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
|
2015-02-11 11:10:31 -08:00
|
|
|
get valid() {
|
2015-02-24 11:59:10 -08:00
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._status === VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
get errors() {
|
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
setParent(parent){
|
|
|
|
this._parent = parent;
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|
|
|
|
|
2015-02-24 11:59:10 -08:00
|
|
|
_updateIfNeeded() {
|
2015-02-25 12:54:27 -08:00
|
|
|
if (this._dirty) {
|
|
|
|
this._dirty = false;
|
2015-02-24 11:59:10 -08:00
|
|
|
this._errors = this.validator(this);
|
|
|
|
this._status = isPresent(this._errors) ? INVALID : VALID;
|
|
|
|
}
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateParent() {
|
|
|
|
if (isPresent(this._parent)){
|
|
|
|
this._parent._controlChanged();
|
|
|
|
}
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ControlGroup {
|
2015-02-24 11:59:10 -08:00
|
|
|
_value:any;
|
|
|
|
_status:string;
|
|
|
|
_errors;
|
2015-02-25 12:54:27 -08:00
|
|
|
_dirty:boolean;
|
2015-02-11 11:10:31 -08:00
|
|
|
validator:Function;
|
2015-02-24 11:59:10 -08:00
|
|
|
controls;
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-02-11 11:10:31 -08:00
|
|
|
constructor(controls, validator:Function = controlGroupValidator) {
|
2015-02-03 07:27:09 -08:00
|
|
|
this.controls = controls;
|
2015-02-11 11:10:31 -08:00
|
|
|
this.validator = validator;
|
2015-02-25 12:54:27 -08:00
|
|
|
this._dirty = true;
|
2015-02-11 11:10:31 -08:00
|
|
|
this._setParentForControls();
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
2015-02-24 11:59:10 -08:00
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._status;
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
2015-02-11 11:10:31 -08:00
|
|
|
|
|
|
|
get valid() {
|
2015-02-24 11:59:10 -08:00
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._status === VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
get errors() {
|
|
|
|
this._updateIfNeeded();
|
|
|
|
return this._errors;
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_setParentForControls() {
|
|
|
|
StringMapWrapper.forEach(this.controls, (control, name) => {
|
2015-02-24 11:59:10 -08:00
|
|
|
control.setParent(this);
|
2015-02-11 11:10:31 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-24 11:59:10 -08:00
|
|
|
_updateIfNeeded() {
|
2015-02-25 12:54:27 -08:00
|
|
|
if (this._dirty) {
|
|
|
|
this._dirty = false;
|
2015-02-24 11:59:10 -08: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) => {
|
|
|
|
if (control.active) {
|
|
|
|
newValue[name] = control.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newValue;
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_controlChanged() {
|
2015-02-25 12:54:27 -08:00
|
|
|
this._dirty = true;
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
2015-02-24 11:59:10 -08:00
|
|
|
|
|
|
|
export class OptionalControl {
|
|
|
|
_control:Control;
|
|
|
|
_cond:boolean;
|
|
|
|
|
|
|
|
constructor(control:Control, cond:boolean) {
|
|
|
|
super();
|
|
|
|
this._control = control;
|
|
|
|
this._cond = cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
get active():boolean {
|
|
|
|
return this._cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._control.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._control.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
get errors() {
|
|
|
|
return this._control.errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
set validator(v) {
|
|
|
|
this._control.validator = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
get validator() {
|
|
|
|
return this._control.validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
set cond(value:boolean){
|
|
|
|
this._cond = value;
|
|
|
|
this._control._updateParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
get cond():boolean{
|
|
|
|
return this._cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateValue(value:any){
|
|
|
|
this._control.updateValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
setParent(parent){
|
|
|
|
this._control.setParent(parent);
|
|
|
|
}
|
|
|
|
}
|