Skip to content

Commit

Permalink
Populate available select field options with matching dce/ldda entries
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Sep 8, 2023
1 parent 250fb8c commit b998f75
Showing 1 changed file with 24 additions and 55 deletions.
79 changes: 24 additions & 55 deletions client/src/components/Form/Elements/FormData/FormData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,49 +151,28 @@ const formattedOptions = computed(() => {
value: null,
});
}
// Add other sources
const otherSources = [SOURCE.COLLECTION_ELEMENT, SOURCE.LIBRARY_DATASET];
for (const otherSource of otherSources) {
const otherOptions = props.options[otherSource];
if (Array.isArray(otherOptions)) {
for (const otherOption of otherOptions) {
if (getSourceType(otherOption) === currentSource.value) {
const otherLabel = `${otherOption.name} (${otherSource})`;
result.unshift({
label: otherLabel,
value: otherOption,
});
}
}
}
}
return result;
} else {
return [];
}
});
/**
* Dataset collection elements should be treated as plain values.
*/
const isDCE = computed(() => {
if (props.value && props.value.values.length > 0) {
return props.value.values.findIndex((v) => v.src !== SOURCE.COLLECTION_ELEMENT) === -1;
} else {
return false;
}
});
const isLDDA = computed(() => {
if (props.value && props.value.values.length > 0) {
return props.value.values.findIndex((v) => v.src !== SOURCE.LIBRARY_DATASET) === -1;
} else {
return false;
}
});
/**
* Matches an array of values to available options
*/
const matchedValues = computed(() => {
const values: Array<DataOption> = [];
if (props.value && props.value.values.length > 0) {
props.value.values.forEach((entry) => {
if ("src" in entry && entry.src) {
const options = props.options[entry.src] || [];
const option = options.find((v) => v.id === entry.id && v.src === entry.src);
if (option) {
values.push({ ...option, name: option.name || entry.id });
}
}
});
}
return values;
});
/**
* Provides placeholder label for select field
*/
Expand Down Expand Up @@ -241,15 +220,11 @@ function createValue(val: Array<DataOption> | DataOption | null) {
if (variantIndex >= 0) {
const variantDetails = variant.value[variantIndex];
if (variantDetails) {
if ((isLDDA.value || isDCE.value) && variantDetails.batch) {
batch = variantDetails.batch;
} else {
// Switch to another field type if source differs from current field
if (currentVariant.value && currentVariant.value.src !== sourceType) {
currentField.value = variantIndex;
}
batch = (currentVariant.value && currentVariant.value.batch) || BATCH.DISABLED;
// Switch to another field type if source differs from current field
if (currentVariant.value && currentVariant.value.src !== sourceType) {
currentField.value = variantIndex;
}
batch = (currentVariant.value && currentVariant.value.batch) || BATCH.DISABLED;
}
}
Expand Down Expand Up @@ -280,9 +255,9 @@ function getSourceLabel(src: string | null) {
* Determine source type of a given value
*/
function getSourceType(val: DataOption) {
if (isDCE.value) {
if (val.src === SOURCE.COLLECTION_ELEMENT) {
return val.is_dataset ? SOURCE.DATASET : SOURCE.COLLECTION;
} else if (isLDDA.value) {
} else if (val.src === SOURCE.LIBRARY_DATASET) {
return SOURCE.DATASET;
} else {
return val.src;
Expand Down Expand Up @@ -430,11 +405,7 @@ onMounted(() => {
eventBus.$on("waiting", (value: boolean) => {
waiting.value = value;
});
if (props.value && props.value.values) {
$emit("input", createValue(matchedValues.value));
} else {
$emit("input", createValue(currentValue.value));
}
$emit("input", createValue(currentValue.value));
});
/**
Expand All @@ -443,9 +414,7 @@ onMounted(() => {
watch(
() => [props.options, currentLinked.value, currentVariant.value],
() => {
if (!isLDDA.value && !isDCE.value) {
$emit("input", createValue(currentValue.value));
}
$emit("input", createValue(currentValue.value));
}
);
</script>
Expand Down

0 comments on commit b998f75

Please sign in to comment.