Fix: fix partially #2343

This commit is contained in:
Pieter Vander Vennet 2025-03-09 13:11:57 +01:00
parent 7e854bb164
commit c68bce2421
2 changed files with 56 additions and 24 deletions

View file

@ -223,7 +223,7 @@ export default class TagRenderingConfig {
inline: json.freeform.inline ?? false, inline: json.freeform.inline ?? false,
default: json.freeform.default, default: json.freeform.default,
postfixDistinguished: json.freeform.postfixDistinguished?.trim(), postfixDistinguished: json.freeform.postfixDistinguished?.trim(),
args: json.freeform.helperArgs, args: json.freeform.helperArgs
} }
if (json.freeform["extraTags"] !== undefined) { if (json.freeform["extraTags"] !== undefined) {
throw `Freeform.extraTags is defined. This should probably be 'freeform.addExtraTag' (at ${context})` throw `Freeform.extraTags is defined. This should probably be 'freeform.addExtraTag' (at ${context})`
@ -447,7 +447,7 @@ export default class TagRenderingConfig {
iconClass, iconClass,
addExtraTags, addExtraTags,
searchTerms: mapping.searchTerms, searchTerms: mapping.searchTerms,
priorityIf: prioritySearch, priorityIf: prioritySearch
} }
if (isQuestionable) { if (isQuestionable) {
if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) { if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) {
@ -554,7 +554,7 @@ export default class TagRenderingConfig {
then: new TypedTranslation<object>( then: new TypedTranslation<object>(
this.render.replace("{" + this.freeform.key + "}", leftover).translations, this.render.replace("{" + this.freeform.key + "}", leftover).translations,
this.render.context this.render.context
), )
}) })
} }
} }
@ -607,7 +607,7 @@ export default class TagRenderingConfig {
return { return {
then: this.render.PartialSubs({ [this.freeform.key]: v.trim() }), then: this.render.PartialSubs({ [this.freeform.key]: v.trim() }),
icon: this.renderIcon, icon: this.renderIcon,
iconClass: this.renderIconClass, iconClass: this.renderIconClass
} }
} }
} }
@ -662,7 +662,7 @@ export default class TagRenderingConfig {
key: commonKey, key: commonKey,
values: Utils.NoNull( values: Utils.NoNull(
values.map((arr) => arr.filter((item) => item.k === commonKey)[0]?.v) values.map((arr) => arr.filter((item) => item.k === commonKey)[0]?.v)
), )
} }
} }
@ -677,7 +677,7 @@ export default class TagRenderingConfig {
return { return {
key, key,
type: this.freeform.type, type: this.freeform.type,
values, values
} }
} catch (e) { } catch (e) {
console.error("Could not create FreeformValues for tagrendering", this.id) console.error("Could not create FreeformValues for tagrendering", this.id)
@ -746,14 +746,18 @@ export default class TagRenderingConfig {
if (freeformValue === "") { if (freeformValue === "") {
freeformValue = undefined freeformValue = undefined
} }
if (this.freeform?.postfixDistinguished && freeformValue !== undefined) { if (this.freeform?.postfixDistinguished) {
const allValues = currentProperties[this.freeform.key].split(";").map((s) => s.trim()) const allValues = currentProperties[this.freeform.key].split(";").map((s) => s.trim())
const perPostfix: Record<string, string> = {} const perPostfix: Record<string, string> = {}
for (const value of allValues) { for (const value of allValues) {
const [v, postfix] = value.split("/") const [v, postfix] = value.split("/")
perPostfix[postfix.trim()] = v.trim() perPostfix[postfix.trim()] = v.trim()
} }
perPostfix[this.freeform.postfixDistinguished] = freeformValue if (freeformValue === "" || freeformValue === undefined) {
delete perPostfix[this.freeform.postfixDistinguished]
} else {
perPostfix[this.freeform.postfixDistinguished] = freeformValue
}
const keys = Object.keys(perPostfix) const keys = Object.keys(perPostfix)
keys.sort() keys.sort()
freeformValue = keys.map((k) => perPostfix[k] + "/" + k).join("; ") freeformValue = keys.map((k) => perPostfix[k] + "/" + k).join("; ")
@ -783,7 +787,7 @@ export default class TagRenderingConfig {
// Either no mappings, or this is a radio-button selected freeform value // Either no mappings, or this is a radio-button selected freeform value
const tag = new And([ const tag = new And([
new Tag(this.freeform.key, freeformValue), new Tag(this.freeform.key, freeformValue),
...(this.freeform.addExtraTags ?? []), ...(this.freeform.addExtraTags ?? [])
]) ])
const newProperties = tag.applyOn(currentProperties) const newProperties = tag.applyOn(currentProperties)
if (this.invalidValues?.matchesProperties(newProperties)) { if (this.invalidValues?.matchesProperties(newProperties)) {
@ -807,7 +811,7 @@ export default class TagRenderingConfig {
selectedMappings.push( selectedMappings.push(
new And([ new And([
new Tag(this.freeform.key, freeformValue), new Tag(this.freeform.key, freeformValue),
...(this.freeform.addExtraTags ?? []), ...(this.freeform.addExtraTags ?? [])
]) ])
) )
} }
@ -845,12 +849,12 @@ export default class TagRenderingConfig {
if (useFreeform) { if (useFreeform) {
return new And([ return new And([
new Tag(this.freeform.key, freeformValue), new Tag(this.freeform.key, freeformValue),
...(this.freeform.addExtraTags ?? []), ...(this.freeform.addExtraTags ?? [])
]) ])
} else if (singleSelectedMapping !== undefined) { } else if (singleSelectedMapping !== undefined) {
return new And([ return new And([
this.mappings[singleSelectedMapping].if, this.mappings[singleSelectedMapping].if,
...(this.mappings[singleSelectedMapping].addExtraTags ?? []), ...(this.mappings[singleSelectedMapping].addExtraTags ?? [])
]) ])
} else { } else {
console.error("TagRenderingConfig.ConstructSpecification has a weird fallback for", { console.error("TagRenderingConfig.ConstructSpecification has a weird fallback for", {
@ -858,7 +862,7 @@ export default class TagRenderingConfig {
singleSelectedMapping, singleSelectedMapping,
multiSelectedMapping, multiSelectedMapping,
currentProperties, currentProperties,
useFreeform, useFreeform
}) })
return undefined return undefined
@ -888,11 +892,11 @@ export default class TagRenderingConfig {
} }
const msgs: string[] = [ const msgs: string[] = [
icon + icon +
" " + " " +
"*" + "*" +
m.then.textFor(lang) + m.then.textFor(lang) +
"* is shown if with " + "* is shown if with " +
m.if.asHumanString(true, false, {}), m.if.asHumanString(true, false, {})
] ]
if (m.hideInAnswer === true) { if (m.hideInAnswer === true) {
@ -901,7 +905,7 @@ export default class TagRenderingConfig {
if (m.ifnot !== undefined) { if (m.ifnot !== undefined) {
msgs.push( msgs.push(
"Unselecting this answer will add " + "Unselecting this answer will add " +
m.ifnot.asHumanString(true, false, {}) m.ifnot.asHumanString(true, false, {})
) )
} }
return msgs.join(". ") return msgs.join(". ")
@ -925,7 +929,7 @@ export default class TagRenderingConfig {
if (this.labels?.length > 0) { if (this.labels?.length > 0) {
labels = [ labels = [
"This tagrendering has labels ", "This tagrendering has labels ",
...this.labels.map((label) => "`" + label + "`"), ...this.labels.map((label) => "`" + label + "`")
].join("\n") ].join("\n")
} }
@ -938,7 +942,7 @@ export default class TagRenderingConfig {
freeform, freeform,
mappings, mappings,
condition, condition,
labels, labels
].join("\n") ].join("\n")
} }
@ -964,6 +968,32 @@ export default class TagRenderingConfig {
return Utils.NoNull(tags) return Utils.NoNull(tags)
} }
/**
* Returns the freeform value that should be initially shown in the question
* @param properties
*/
public initialFreeformValue(properties: Record<string, string>): string {
const value = properties[this.freeform.key]
if (!value) {
return ""
}
const distinguish = this.freeform.postfixDistinguished
if (!distinguish) {
return value
}
const parts = value.split(";")
for (const part of parts) {
if (part.indexOf("/") < 0) {
continue
}
const [v, denom] = part.split("/").map(s => s.trim())
if (denom === distinguish) {
return v
}
}
return ""
}
/** /**
* The keys that should be erased if one has to revert to 'unknown'. * The keys that should be erased if one has to revert to 'unknown'.
* Might give undefined if setting to unknown is not possible * Might give undefined if setting to unknown is not possible
@ -1050,7 +1080,7 @@ export class TagRenderingConfigUtils {
clone.mappings?.map((m) => { clone.mappings?.map((m) => {
const mapping = { const mapping = {
...m, ...m,
priorityIf: m.priorityIf ?? TagUtils.Tag("id~*"), priorityIf: m.priorityIf ?? TagUtils.Tag("id~*")
} }
if (m.if.usedKeys().indexOf("nobrand") < 0) { if (m.if.usedKeys().indexOf("nobrand") < 0) {
// Erase 'nobrand=yes', unless this option explicitly sets it // Erase 'nobrand=yes', unless this option explicitly sets it

View file

@ -143,7 +143,7 @@
if (confg.freeform?.key) { if (confg.freeform?.key) {
if (!confg.multiAnswer) { if (!confg.multiAnswer) {
// Somehow, setting multi-answer freeform values is broken if this is not set // Somehow, setting multi-answer freeform values is broken if this is not set
freeformInput.set(tgs[confg.freeform.key]) freeformInput.set(confg.initialFreeformValue(tgs))
} }
} else { } else {
freeformInput.set(undefined) freeformInput.set(undefined)
@ -208,6 +208,7 @@
!$freeformInput && !$freeformInput &&
!$freeformInputUnvalidated && !$freeformInputUnvalidated &&
!checkedMappings?.some((m) => m) && !checkedMappings?.some((m) => m) &&
!config.freeform.postfixDistinguished &&
$tags[config.freeform.key] // We need to have a current value in order to delete it $tags[config.freeform.key] // We need to have a current value in order to delete it
) { ) {
selectedTags = new Tag(config.freeform.key, "") selectedTags = new Tag(config.freeform.key, "")
@ -613,7 +614,8 @@
<!-- TagRenderingQuestion-buttons --> <!-- TagRenderingQuestion-buttons -->
<slot name="cancel" /> <slot name="cancel" />
<slot name="save-button" {selectedTags}> <slot name="save-button" {selectedTags}>
{#if config.freeform?.key && !checkedMappings?.some((m) => m) && !$freeformInput && !$freeformInputUnvalidated && $tags[config.freeform.key]} {#if config.freeform?.key && !checkedMappings?.some((m) => m) && !$freeformInput && !$freeformInputUnvalidated && $tags[config.freeform.key]
&& (!config.freeform.postfixDistinguished)}
<button <button
class="primary flex" class="primary flex"
on:click|stopPropagation|preventDefault={() => onSave()} on:click|stopPropagation|preventDefault={() => onSave()}