bia-framework
Version:
Bia Framework to work with angular 2
101 lines (79 loc) • 2.53 kB
text/typescript
/**
* Created by jd on 08/07/2016.
*/
import {AfterContentInit, Injectable, EventEmitter} from "@angular/core";
import {Validators} from "@angular/forms";
import {InternalControl} from "./InternalControl";
import {CoreForm} from "./core-form/core-form.component";
/**
* Created by jd on 29/04/2016.
*/
export class CoreFormControlBase implements AfterContentInit
{
//states
isProcessing:boolean = false;
name:string;
bind:Object;
bindChange:EventEmitter<Object>;
controlValidators:Array<any>;
validationList:Array<ValidationMessage>;
control:InternalControl;
ngAfterContentInit():any
{
if (this.formHost != null)
{
this.control = new InternalControl(this.bind, Validators.compose(this.controlValidators));
this.control.valueChanges.subscribe(a =>
{
this.controlValueChange(a);
this.bindChange.emit(a);
});
this.controlValueChange(this.bind);
this.formHost.addFormControl(this.name, this.control);
}
return undefined;
}
public setupBind = ( _name:string,
_bind:Object,
_bindChange:EventEmitter<Object> ):void =>
{
this.name = _name;
this.bind = _bind;
this.bindChange = _bindChange;
};
constructor( private formHost?:CoreForm )
{
this.controlValidators = [];
this.validationList = [];
}
public OnFocus = ():void =>
{
this.control.hasFocus = true;
};
public OnBlur = ():void =>
{
this.control.hasFocus = false;
this.control.markAsTouched();
};
public OnChange = ( a ):void =>
{
this.control.updateValue(a);
};
public isInForm = ():boolean =>
{
return this.formHost != null;
};
public addFormValidator = ( validationDesc:ValidationMessage, validator:any ) =>
{
this.controlValidators.push(validator);
this.validationList.push(validationDesc);
};
public controlValueChange = ( v:any ):void =>
{
if (v != null && v != "")
this.control.hasValue = true;
else
this.control.hasValue = false;
};
}