Refactor createChildMetadataGroups to respect children_objects order.

This commit is contained in:
mateuswetah 2020-05-08 09:59:25 -03:00
parent ac9e82002f
commit b0b8620fad
1 changed files with 33 additions and 39 deletions

View File

@ -163,11 +163,12 @@
if (childItemMetadata && childItemMetadata.length) { if (childItemMetadata && childItemMetadata.length) {
// Loads the existing values for (let childMetadatum of this.itemMetadatum.metadatum.metadata_type_options.children_objects) {
for (let childIndex = 0; childIndex < childItemMetadata.length; childIndex++) {
const childItemMetadatum = childItemMetadata[childIndex];
const childMetadatum = this.itemMetadatum.metadatum.metadata_type_options.children_objects.find((aMetadatum) => aMetadatum.id == childItemMetadatum.metadatum_id);
const childItemMetadatumIndex = childItemMetadata.findIndex((aChildItemMetadatum) => childMetadatum.id == aChildItemMetadatum.metadatum_id);
// Loads the existing values
if (childItemMetadatumIndex >= 0) {
const childItemMetadatum = childItemMetadata[childItemMetadatumIndex];
existingChildItemMetadata.push({ existingChildItemMetadata.push({
item: this.itemMetadatum.item, item: this.itemMetadatum.item,
metadatum: childMetadatum, metadatum: childMetadatum,
@ -175,21 +176,14 @@
value: childItemMetadatum.value, value: childItemMetadatum.value,
value_as_html: childItemMetadatum.value_as_html, value_as_html: childItemMetadatum.value_as_html,
value_as_string: childItemMetadatum.value_as_string, value_as_string: childItemMetadatum.value_as_string,
collapse: this.childItemMetadataGroups[groupIndex] && this.childItemMetadataGroups[groupIndex][childIndex] ? this.childItemMetadataGroups[groupIndex][childIndex].collapse : (this.collapseAllChildren ? this.collapseAllChildren : false) collapse: this.childItemMetadataGroups[groupIndex] && this.childItemMetadataGroups[groupIndex][childItemMetadatumIndex] ? this.childItemMetadataGroups[groupIndex][childItemMetadatumIndex].collapse : (this.collapseAllChildren ? this.collapseAllChildren : false)
}) })
} // Creates inputs for non existing ones
} else {
// If some have empty childs, we need to create their input const existingParentMetaIdIndex = childItemMetadata.findIndex((aChildItemMetadatum) => aChildItemMetadatum.parent_meta_id > 0);
if (childItemMetadata.length < this.itemMetadatum.metadatum.metadata_type_options.children_objects.length) {
const existingParentMetaIdIndex = childItemMetadata.findIndex((anItemMetadatum) => anItemMetadatum.parent_meta_id > 0);
for (let child of this.itemMetadatum.metadatum.metadata_type_options.children_objects) {
const existingValueIndex = childItemMetadata.findIndex((anItemMetadatum) => anItemMetadatum.metadatum_id == child.id);
if (existingValueIndex < 0) {
existingChildItemMetadata.push({ existingChildItemMetadata.push({
item: this.itemMetadatum.item, item: this.itemMetadatum.item,
metadatum: child, metadatum: childMetadatum,
parent_meta_id: existingParentMetaIdIndex >= 0 ? childItemMetadata[existingParentMetaIdIndex].parent_meta_id : 0, parent_meta_id: existingParentMetaIdIndex >= 0 ? childItemMetadata[existingParentMetaIdIndex].parent_meta_id : 0,
value: '', value: '',
value_as_html: '', value_as_html: '',
@ -198,14 +192,14 @@
}); });
} }
} }
}
} else { } else {
// A new input for each type of child metadatum // A new input for each type of child metadatum
for (let child of this.itemMetadatum.metadatum.metadata_type_options.children_objects) { for (let childMetadatum of this.itemMetadatum.metadatum.metadata_type_options.children_objects) {
let childObject = { let childObject = {
item: this.itemMetadatum.item, item: this.itemMetadatum.item,
metadatum: child, metadatum: childMetadatum,
parent_meta_id: '0', parent_meta_id: '0',
value: '', value: '',
value_as_html: '', value_as_html: '',
@ -250,10 +244,10 @@
this.itemMetadatum.metadatum.metadata_type_options && this.itemMetadatum.metadatum.metadata_type_options &&
this.itemMetadatum.metadatum.metadata_type_options.children_objects.length > 0 this.itemMetadatum.metadatum.metadata_type_options.children_objects.length > 0
) { ) {
for (let child of this.itemMetadatum.metadatum.metadata_type_options.children_objects) { for (let childMetadatum of this.itemMetadatum.metadatum.metadata_type_options.children_objects) {
let childObject = { let childObject = {
item: this.itemMetadatum.item, item: this.itemMetadatum.item,
metadatum: child, metadatum: childMetadatum,
parent_meta_id: parentMetaId, parent_meta_id: parentMetaId,
value: '', value: '',
value_as_html: '', value_as_html: '',