[CST-3782] Make changes in order to remove template row in form's repeatable fields

This commit is contained in:
Giuseppe Digilio
2021-02-17 16:10:22 +01:00
parent 942dd2e72e
commit 151b02aeec
12 changed files with 154 additions and 179 deletions

View File

@@ -15,6 +15,8 @@ import { setLayout } from './parser.utils';
import { ParserOptions } from './parser-options';
import { RelationshipOptions } from '../models/relationship-options.model';
import { VocabularyOptions } from '../../../../core/submission/vocabularies/models/vocabulary-options.model';
import { ParserType } from './parser-type';
import { isNgbDateStruct } from '../../../date.util';
export const SUBMISSION_ID: InjectionToken<string> = new InjectionToken<string>('submissionId');
export const CONFIG_DATA: InjectionToken<FormFieldModel> = new InjectionToken<FormFieldModel>('configData');
@@ -37,9 +39,8 @@ export abstract class FieldParser {
public parse() {
if (((this.getInitValueCount() > 1 && !this.configData.repeatable) || (this.configData.repeatable))
&& (this.configData.input.type !== 'list')
&& (this.configData.input.type !== 'tag')
&& (this.configData.input.type !== 'group')
&& (this.configData.input.type !== ParserType.List)
&& (this.configData.input.type !== ParserType.Tag)
) {
let arrayCounter = 0;
let fieldArrayCounter = 0;
@@ -66,22 +67,16 @@ export abstract class FieldParser {
model = this.modelFactory();
arrayCounter++;
} else {
const fieldArrayOfValueLength = this.getInitValueCount(arrayCounter - 1);
const fieldArrayOfValueLenght = this.getInitValueCount(arrayCounter - 1);
let fieldValue = null;
if (fieldArrayOfValueLength > 0) {
if (fieldArrayCounter === 0) {
fieldValue = '';
} else {
fieldValue = this.getInitFieldValue(arrayCounter - 1, fieldArrayCounter - 1);
}
fieldArrayCounter++;
if (fieldArrayCounter === fieldArrayOfValueLength + 1) {
if (fieldArrayOfValueLenght > 0) {
fieldValue = this.getInitFieldValue(arrayCounter - 1, fieldArrayCounter++);
if (fieldArrayCounter === fieldArrayOfValueLenght) {
fieldArrayCounter = 0;
arrayCounter++;
}
}
model = this.modelFactory(fieldValue, false);
model.id = `${model.id}_${fieldArrayCounter}`;
}
setLayout(model, 'element', 'host', 'col');
if (model.hasLanguages || isNotEmpty(model.relationship)) {
@@ -130,7 +125,9 @@ export abstract class FieldParser {
return;
}
if (typeof fieldValue === 'object') {
if (isNgbDateStruct(fieldValue)) {
modelConfig.value = fieldValue;
} else if (typeof fieldValue === 'object') {
modelConfig.metadataValue = fieldValue;
modelConfig.language = fieldValue.language;
modelConfig.place = fieldValue.place;
@@ -210,10 +207,9 @@ export abstract class FieldParser {
}
protected getInitArrayIndex() {
let fieldCount = 0;
const fieldIds: any = this.getAllFieldIds();
if (isNotEmpty(this.initFormValues) && isNotNull(fieldIds) && fieldIds.length === 1 && this.initFormValues.hasOwnProperty(fieldIds)) {
fieldCount = this.initFormValues[fieldIds].filter((value) => hasValue(value) && hasValue(value.value)).length;
return this.initFormValues[fieldIds].length;
} else if (isNotEmpty(this.initFormValues) && isNotNull(fieldIds) && fieldIds.length > 1) {
let counter = 0;
fieldIds.forEach((id) => {
@@ -221,9 +217,10 @@ export abstract class FieldParser {
counter = counter + this.initFormValues[id].length;
}
});
fieldCount = counter;
return (counter === 0) ? 1 : counter;
} else {
return 1;
}
return (fieldCount === 0) ? 1 : fieldCount + 1;
}
protected getFieldId(): string {
@@ -245,7 +242,7 @@ export abstract class FieldParser {
}
}
protected initModel(id?: string, label = true, setErrors = true, hint = true) {
protected initModel(id?: string, label = true, labelEmpty = false, setErrors = true, hint = true) {
const controlModel = Object.create(null);
@@ -316,7 +313,7 @@ export abstract class FieldParser {
protected setLabel(controlModel, label = true, labelEmpty = false) {
if (label) {
controlModel.label = this.configData.label;
controlModel.label = (labelEmpty) ? '&nbsp;' : this.configData.label;
}
}