This commit is contained in:
Pieter Vander Vennet 2023-08-08 20:41:18 +02:00
parent 3c08c12974
commit 5d00f3cc6a

View file

@ -58,7 +58,7 @@ export default class TagRenderingConfig {
public readonly freeform?: {
readonly key: string
readonly type: string
readonly type: ValidatorType
readonly placeholder: Translation
readonly addExtraTags: UploadableTag[]
readonly inline: boolean
@ -133,7 +133,17 @@ export default class TagRenderingConfig {
) {
throw `Freeform.addExtraTags should be a list of strings - not a single string (at ${context})`
}
const type = json.freeform.type ?? "string"
if (
json.freeform.type &&
Validators.availableTypes.indexOf(<any>json.freeform.type) < 0
) {
throw `At ${context}: invalid type, perhaps you meant ${Utils.sortedByLevenshteinDistance(
json.freeform.key,
<any>Validators.availableTypes,
(s) => <any>s
)}`
}
const type: ValidatorType = <any>json.freeform.type ?? "string"
let placeholder: Translation = Translations.T(json.freeform.placeholder)
if (placeholder === undefined) {
@ -622,7 +632,7 @@ export default class TagRenderingConfig {
*
* @param singleSelectedMapping (Only used if multiAnswer == false): the single mapping to apply. Use (mappings.length) for the freeform
* @param multiSelectedMapping (Only used if multiAnswer == true): all the mappings that must be applied. Set multiSelectedMapping[mappings.length] to use the freeform as well
* @param currentProperties: The current properties of the object for which the question should be answered
* @param currentProperties The current properties of the object for which the question should be answered
*/
public constructChangeSpecification(
freeformValue: string | undefined,
@ -685,38 +695,42 @@ export default class TagRenderingConfig {
return undefined
}
return and
} else {
// Is at least one mapping shown in the answer?
const someMappingIsShown = this.mappings.some((m) => {
if (typeof m.hideInAnswer === "boolean") {
return !m.hideInAnswer
}
const isHidden = m.hideInAnswer.matchesProperties(currentProperties)
return !isHidden
})
// If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key
const useFreeform =
freeformValue !== undefined &&
(singleSelectedMapping === this.mappings.length || !someMappingIsShown)
if (useFreeform) {
return new And([
new Tag(this.freeform.key, freeformValue),
...(this.freeform.addExtraTags ?? []),
])
} else if (singleSelectedMapping !== undefined) {
return new And([
this.mappings[singleSelectedMapping].if,
...(this.mappings[singleSelectedMapping].addExtraTags ?? []),
])
} else {
console.warn("TagRenderingConfig.ConstructSpecification has a weird fallback for", {
freeformValue,
singleSelectedMapping,
multiSelectedMapping,
currentProperties,
})
return undefined
}
// Is at least one mapping shown in the answer?
const someMappingIsShown = this.mappings.some((m) => {
if (typeof m.hideInAnswer === "boolean") {
return !m.hideInAnswer
}
const isHidden = m.hideInAnswer.matchesProperties(currentProperties)
return !isHidden
})
// If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key
const useFreeform =
freeformValue !== undefined &&
(singleSelectedMapping === this.mappings.length ||
!someMappingIsShown ||
singleSelectedMapping === undefined)
if (useFreeform) {
return new And([
new Tag(this.freeform.key, freeformValue),
...(this.freeform.addExtraTags ?? []),
])
} else if (singleSelectedMapping !== undefined) {
return new And([
this.mappings[singleSelectedMapping].if,
...(this.mappings[singleSelectedMapping].addExtraTags ?? []),
])
} else {
console.error("TagRenderingConfig.ConstructSpecification has a weird fallback for", {
freeformValue,
singleSelectedMapping,
multiSelectedMapping,
currentProperties,
useFreeform,
})
return undefined
}
}