Merge branch 'master' of gitlab.eudat.eu:dmp/OpenAIRE-EUDAT-DMP-service-pilot

This commit is contained in:
Nikolaos Laskaris 2017-11-20 11:57:34 +02:00
commit 023756c29d
2 changed files with 63 additions and 35 deletions
dmp-frontend/src/app

View File

@ -12,7 +12,7 @@ import { RuleStyle } from '../../entities/common/rulestyle';
@Component({
selector: 'df-field',
templateUrl: './dynamic-form-field.component.html',
styles:['.checkBoxLabelCustom {font-weight: 700;}']
styles: ['.checkBoxLabelCustom {font-weight: 700;}']
})
export class DynamicFormFieldComponent {
@ -70,14 +70,14 @@ export class DynamicFormFieldComponent {
if (field.visible == true)
return true;
}
FieldValueRuleMethod(field, rule) { //fieldValue rule -- checks the value of target and apply rules, at the same time when the field becomes visible
var targetField = this.dataModel.getFieldByKey(rule._target); //calling the AddvalidationRules we apply the validation rules for the new field
FieldValueRuleMethod(field, rule, targetField) { //fieldValue rule -- checks the value of target and apply rules, at the same time when the field becomes visible /calling the AddvalidationRules we apply the validation rules for the new field
var fieldValue = this.form.get(field.key).value;//to do: change field.value
if (rule._ruleStyle == "range") {
if (parseInt(rule._from) < parseInt(field.value) && parseInt(field.value) < parseInt(rule._to)) {
console.log("visible" + field.value)
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
@ -87,7 +87,7 @@ export class DynamicFormFieldComponent {
let ruleValue = rule.value.__text;
if (field.value.toString() == ruleValue) { //field.value.value.toString() == ruleValue
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
@ -95,26 +95,26 @@ export class DynamicFormFieldComponent {
if (rule._ruleStyle == "checked") { //checkbox field
if (field.value == true) {
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
}
if (rule._ruleStyle == "unchecked") { //checkbox field
if (field.value !=="") {
if (field.value !== "") {
if (field.value == false) {
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
}
}
if (rule._ruleStyle == "existence") {
if (field.visible == "true" || field.visible == true) {
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
@ -122,26 +122,26 @@ export class DynamicFormFieldComponent {
if (rule._ruleStyle == "regex") {
if (new RegExp(rule.__cdata).test(field.value)) {
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
}
if (rule._ruleStyle == "dropdownValue" ) { //dropdown field
if (rule._ruleStyle == "dropdownValue") { //dropdown field
let fieldValue = this.form.get(field.key).value;
let ruleValue;
let ruleValueBoolean = false ;
let ruleValueBoolean = false;
if (rule.value.length)
rule.value.forEach(value => {
if (fieldValue.toString() == value.__text)
ruleValueBoolean = true;
if (fieldValue.toString() == value.__text)
ruleValueBoolean = true;
return;
});
else
ruleValue = rule.value.__text;
if (fieldValue.toString() == ruleValue || ruleValueBoolean.valueOf() == true) { //field.value.value.toString() == ruleValue
targetField.visible = true;
this.AddvalidationRules(rule._target);
this.AddvalidationRules(targetField);
} else {
this.hideField(targetField, rule);
}
@ -151,38 +151,66 @@ export class DynamicFormFieldComponent {
hideField(targetField, rule) {
targetField.visible = false;
targetField.value = ' ';
if (this.form.controls[rule._target].hasError("pattern"))
this.form.controls[rule._target].reset(); //the regex error message didn't remove without field reset
this.form.controls[rule._target].clearValidators(); // when a field is hidden must clear the validators and the errors
this.form.controls[rule._target].updateValueAndValidity();
if (this.form.controls[targetField.key].hasError("pattern"))
this.form.controls[targetField.key].reset(); //the regex error message didn't remove without field reset
this.form.controls[targetField.key].clearValidators(); // when a field is hidden must clear the validators and the errors
this.form.controls[targetField.key].updateValueAndValidity();
}
findTargetField (field, rule){
// var targetField = this.dataModel.getFieldByKey(rule._target);
let targetFields = new Array();
if (this.dataModel.getFieldByKey(rule._target) == undefined) {
this.dataModel.groups.forEach(gr => {
if (gr.key == rule._target) {
gr.groupFields.forEach(field=>{
targetFields.push(field);
});
if(gr.compositeFields)
gr.compositeFields.groupFields.forEach(field=>{
targetFields.push(field);
});
}
})
} else {
targetFields.push(this.dataModel.getFieldByKey(rule._target));
}
return targetFields;
}
toggleVisibility(e, field, ckb) { //ckb the checkbox only send this parameter, it's essential to change the field value
if (ckb){
if(e) this.form.get(field.key).patchValue(e.target.checked)
if (ckb) {
if (e) this.form.get(field.key).patchValue(e.target.checked)
field.value = this.form.get(field.key).value;
}
}
if (field.rules.length != undefined && field.rules.length > 1)
field.rules.forEach(rule => {
if (rule._type == "fieldValue") {
this.FieldValueRuleMethod(field, rule);
let targetFieldsArray = this.findTargetField(field,rule);
targetFieldsArray.forEach(targetField =>{
this.FieldValueRuleMethod(field, rule, targetField);
})
}
});
else if (field.rules._type == "fieldValue") {
this.FieldValueRuleMethod(field, field.rules);
let targetFieldsArray = this.findTargetField(field,field.rules);
targetFieldsArray.forEach(targetField =>{
this.FieldValueRuleMethod(field, field.rules, targetField);
})
}
}
AddvalidationRules(field) {
if (this.dataModel.getFieldByKey(field).attributes.validation != undefined) {
if (this.dataModel.getFieldByKey(field.key).attributes.validation != undefined) {
let arrayVal = new Array();
this.dataModel.getFieldByKey(field).attributes.validation.forEach(rule => {
this.dataModel.getFieldByKey(field.key).attributes.validation.forEach(rule => {
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.existence]) {
this.dataModel.getFieldByKey(field).required = true;
this.dataModel.getFieldByKey(field.key).required = true;
arrayVal.push(Validators.required);
}
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.regex]) {
this.dataModel.getFieldByKey(field).regex = rule.regex;
this.dataModel.getFieldByKey(field.key).regex = rule.regex;
arrayVal.push(Validators.pattern(rule.regex));
}
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.customValidation]) {
@ -190,8 +218,8 @@ export class DynamicFormFieldComponent {
}
});
this.form.controls[field].setValidators(arrayVal); //Multiple Validators, Usage of array because setValidator override any validators that are provided during initialistaion
this.form.controls[field].updateValueAndValidity(); //hide--> visible must update the validators
this.form.controls[field.key].setValidators(arrayVal); //Multiple Validators, Usage of array because setValidator override any validators that are provided during initialistaion
this.form.controls[field.key].updateValueAndValidity(); //hide--> visible must update the validators
}

View File

@ -173,7 +173,7 @@ export class dataModelBuilder {
if (dataValues[fieldGroup._id][field.key] != undefined) // to put values in fields
field.value = dataValues[fieldGroup._id][field.key].value == undefined ? dataValues[fieldGroup._id][field.key] : dataValues[fieldGroup._id][field.key].value;
if(field.order.toString().split(",").length > 1){//--------------Composite Fields Multiplicity --------------------------------------------------
if(field.order.toString().split(".").length > 1){//--------------Composite Fields Multiplicity --------------------------------------------------
compositeFields.groupFields.push(field);
newfldGroup.compositeFields = compositeFields;
}else
@ -246,7 +246,7 @@ export class dataModelBuilder {
newAttribute.multiplicityMin = attr.multiplicity._min;
newAttribute.ordinal = attr._ordinal
if (multiplicity){
fields.find(x => x.key == newAttribute.id).order = fields.find(x => x.key == newAttribute.id).order +','+ newAttribute.ordinal;
fields.find(x => x.key == newAttribute.id).order = fields.find(x => x.key == newAttribute.id).order +'.'+ newAttribute.ordinal;
fields.find(x => x.key == newAttribute.id).multiplicity = true;
}
newAttribute.ordinal = attr._ordinal;
@ -305,7 +305,7 @@ export class dataModelBuilder {
fields.sort((a, b) => a.order - b.order);
return newAttribute;
}
@ -337,7 +337,7 @@ export class dataModelBuilder {
fields.find(x => x.key == newAttribute.id).attributes.sources = newAttribute.sources;
fields.find(x => x.key == newAttribute.id).attributes.validation = newAttribute.validation;
}
});
});
return attribute;
}