ng-metamagic-extensions
Version:
[](https://badge.fury.io/js/ng-metamagic-extensions) []() [ • 6.07 kB
text/typescript
import {Input, OnInit, forwardRef, Component} from "@angular/core";
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms";
import {FormInputBase} from "../baseclass/form.input.base";
const noop = () => {
};
export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => PasswordInputComponent),
multi: true
};
export const BASE_IMPL_PASSWORD_INPUT : any = {
provide : FormInputBase,
useExisting: forwardRef(() => PasswordInputComponent)
};
export class PasswordInputComponent extends FormInputBase implements OnInit,ControlValueAccessor {
fieldLabel : string;
fieldName : string;
minLength: number;
minErrorMsg : string;
maxLength : number;
maxErrorMsg : string;
allowBlank : string;
errorMsg : string;
placeholder: string;
disabled: boolean;
iconFeedBack : boolean;
fontStyle : string;
fontFamily : string;
fontSize : string;
fieldIcon : string;
hasLabel : boolean;
elementId: string;
spanId : string;
iconName : string;
helpInfoMsg: string;
isValid: boolean;
divCss : string;
iconClassName : string;
fieldglyphIcon : string;
constructor() {
super();
this.elementId = 'input-text-' + new Date().getTime() + Math.random();
this.spanId = 'span-msg-'+ Math.random();
if(this.iconFeedBack)
this.divCss = 'form-group has-feedback';
else
this.divCss = 'form-group has-feedback has-feedback-left';
}
ngOnInit() {
if(this.errorMsg)
this.helpInfoMsg = this.errorMsg +'<br/>';
if(this.minErrorMsg)
this.helpInfoMsg = this.helpInfoMsg + 'Min Length: '+this.minErrorMsg+'<br/>';
if(this.maxErrorMsg)
this.helpInfoMsg = this.helpInfoMsg + 'Max Length: '+this.maxErrorMsg;
if(!this.iconFeedBack)
this.fieldglyphIcon = 'form-control-feedback glyphicon glyphicon-'+this.fieldIcon;
}
//The internal dataviews model
private innerValue: any = '';
//Placeholders for the callbacks which are later provided
//by the Control Value Accessor
private onTouchedCallback: () => void = noop;
private onChangeCallback: (_: any) => void = noop;
//get accessor
get value(): any {
return this.innerValue;
};
//set accessor including call the onchange callback
set value(v: any) {
if (v !== this.innerValue) {
this.innerValue = v;
this.onChangeCallback(v);
}
}
//Set touched on blur
onBlur() {
this.onTouchedCallback();
this.validate();
}
//From ControlValueAccessor interface
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
//From ControlValueAccessor interface
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
//From ControlValueAccessor interface
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
validate(){
this.isValid = this.isValidInput();
}
isValidInput(){
var hasError = false;
if((this.allowBlank && (!this.value || this.value.length==0))){
hasError = true;
}else if(this.minLength > this.value.length){
hasError = true;
}else if(this.maxLength < this.value.length){
hasError = true;
}
if(hasError){
this.setValidClassNames();
}else{
this.setInvalidatedClassNames();
}
return hasError;
}
setValidClassNames(){
this.divCss = 'form-group has-error has-feedback';
this.iconName = 'error';
this.iconClassName = 'glyphicon glyphicon-remove form-control-feedback';
}
setInvalidatedClassNames(){
this.divCss = 'form-group has-success has-feedback';
this.iconName = 'success';
this.iconClassName = 'glyphicon glyphicon-ok form-control-feedback';
}
}