ws-form-builder
Version:
Ionic 2 Form Builder
196 lines (149 loc) • 5.82 kB
text/typescript
import { Component, Input, Output, EventEmitter,ElementRef, ViewChild } from '@angular/core';
import { FormBuilderProvider } from '../../providers/form-builder/form-builder';
export class WsPincodeComponent {
question;
answerGiven = new EventEmitter();
pincode1:ElementRef;
pincode2:ElementRef;
pincode3:ElementRef;
pincode4:ElementRef;
pincode5:ElementRef;
pincode6:ElementRef;
fieldValue:string = '';
isFieldValid:boolean = false;
showError:boolean = false;
pincodeLength:number = 4;
pincodeFields:any = [0,1,2,3];
currentStep:number = 0;
fieldAnswers:object = []
fieldClass:string = '';
constructor(
public formBuilder: FormBuilderProvider,
private elementRef : ElementRef
){
this.fieldValue = '';
}
ngOnInit() {
if(typeof this.question.value !== 'undefined'){
this.fieldValue = this.question.value;
}
if(typeof this.question.inputClass !== 'undefined'){
this.fieldClass = this.question.inputClass;
}
if(typeof this.question.codeLength !== 'undefined'){
this.pincodeLength = this.question.codeLength;
let fieldCount = this.pincodeLength;
this.pincodeFields = [];
/*for(let i=0; i < fieldCount; i++){
this.pincodeFields.push(i);
}*/
}
}
updateString(){
let string = '';
let allFieldsSet = true;
string = this.pincode1['value']+''+this.pincode2['value']+''+this.pincode3['value']+''+this.pincode4['value']+''+this.pincode5['value']+''+this.pincode6['value'];
console.log(this.pincode1);
this.fieldValue = string;
this.question.value = this.fieldValue;
if(string.length !== this.pincodeLength){
this.question.valid = false;
}else{
this.question.valid = true;
}
}
setAnswer(event,question){
let isStringValid = this.validateAnswer(question,event.value);
if(!isStringValid){
this.isFieldValid = false;
this.showError = true;
}else{
this.isFieldValid = true;
this.showError = false;
}
if(this.isFieldValid){
question.value = event.value;
this.answerGiven.emit(question);
}
}
testAutoTab(){
this.pincode6['_native'].nativeElement.focus();
}
updateField(event,i){
let autoTab = false;
if(event.value.length === 1){
autoTab = true;
}else if(event.value.length > 1){
event.value = event.value.substring(1,2);
}
let pc = this.pincode1;
if(i === 0){
pc = this.pincode2;
}else if(i === 1){
pc = this.pincode3;
}else if(i === 2){
pc = this.pincode4;
}else if(i === 3){
pc = this.pincode5;
}else if(i === 4){
pc = this.pincode6;
}else if(i === 5){
//end of fields
autoTab = false;
}
this.fieldAnswers[i] = event.value;
if(autoTab){
pc['_native'].nativeElement.focus();
}
this.updateString();
//pincode.setFocus();
}
validateAnswer(question,string){
return this.formBuilder.validateAnswer(question,string);
}
focusDebug(e){
console.log(e);
}
}