Fix 'addExtraTags' in mappings

This commit is contained in:
pietervdvn 2021-10-26 22:53:27 +02:00
parent bd3395af22
commit 78dbe0baa2
5 changed files with 2219 additions and 2339 deletions

View file

@ -83,6 +83,7 @@ export interface TagRenderingConfigJson {
* Allows fixed-tag inputs, shown either as radiobuttons or as checkboxes
*/
mappings?: {
/**
* If this condition is met, then the text under `then` will be shown.
* If no value matches, and the user selects this mapping as an option, then these tags will be uploaded to OSM.
@ -168,6 +169,13 @@ export interface TagRenderingConfigJson {
*/
ifnot?: AndOrTagConfigJson | string
/**
* If chosen as answer, these tags will be applied as well onto the object.
* Not compatible with multiAnswer
*/
addExtraTags: string[]
}[]
/**

View file

@ -6,6 +6,7 @@ import {TagUtils} from "../../Logic/Tags/TagUtils";
import {And} from "../../Logic/Tags/And";
import ValidatedTextField from "../../UI/Input/ValidatedTextField";
import {Utils} from "../../Utils";
import {Tag} from "../../Logic/Tags/Tag";
/***
* The parsed version of TagRenderingConfigJSON
@ -36,6 +37,7 @@ export default class TagRenderingConfig {
readonly ifnot?: TagsFilter,
readonly then: Translation
readonly hideInAnswer: boolean | TagsFilter
readonly addExtraTags: Tag[]
}[]
readonly roaming: boolean;
@ -119,21 +121,24 @@ export default class TagRenderingConfig {
this.mappings = json.mappings.map((mapping, i) => {
const ctx = `${context}.mapping[${i}]`
if (mapping.then === undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: if without body`
throw `${ctx}: Invalid mapping: if without body`
}
if (mapping.ifnot !== undefined && !this.multiAnswer) {
throw `${context}.mapping[${i}]: Invalid mapping: ifnot defined, but the tagrendering is not a multianswer`
throw `${ctx}: Invalid mapping: ifnot defined, but the tagrendering is not a multianswer`
}
if (mapping.if === undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: "if" is not defined, but the tagrendering is not a multianswer`
throw `${ctx}: Invalid mapping: "if" is not defined, but the tagrendering is not a multianswer`
}
if (typeof mapping.if !== "string" && mapping.if["length"] !== undefined) {
throw `${context}.mapping[${i}]: Invalid mapping: "if" is defined as an array. Use {"and": <your conditions>} or {"or": <your conditions>} instead`
throw `${ctx}: Invalid mapping: "if" is defined as an array. Use {"and": <your conditions>} or {"or": <your conditions>} instead`
}
if(mapping.addExtraTags !== undefined && this.multiAnswer){
throw `${ctx}: Invalid mapping: got a multi-Answer with addExtraTags; this is not allowed`
}
let hideInAnswer: boolean | TagsFilter = false;
if (typeof mapping.hideInAnswer === "boolean") {
@ -141,12 +146,12 @@ export default class TagRenderingConfig {
} else if (mapping.hideInAnswer !== undefined) {
hideInAnswer = TagUtils.Tag(mapping.hideInAnswer, `${context}.mapping[${i}].hideInAnswer`);
}
const mappingContext = `${context}.mapping[${i}]`
const mp = {
if: TagUtils.Tag(mapping.if, `${mappingContext}.if`),
ifnot: (mapping.ifnot !== undefined ? TagUtils.Tag(mapping.ifnot, `${mappingContext}.ifnot`) : undefined),
if: TagUtils.Tag(mapping.if, `${ctx}.if`),
ifnot: (mapping.ifnot !== undefined ? TagUtils.Tag(mapping.ifnot, `${ctx}.ifnot`) : undefined),
then: Translations.T(mapping.then, `{mappingContext}.then`),
hideInAnswer: hideInAnswer
hideInAnswer: hideInAnswer,
addExtraTags: (mapping.addExtraTags??[]).map((str, j) => TagUtils.SimpleTag(str, `${ctx}.addExtraTags[${j}]`))
};
if (this.question) {
if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) {

View file

@ -49,7 +49,7 @@ export default class TagRenderingQuestion extends Combine {
const applicableMappingsSrc =
UIEventSource.ListStabilized(tags.map(tags => {
const applicableMappings: { if: TagsFilter, then: any, ifnot?: TagsFilter }[] = []
const applicableMappings: { if: TagsFilter, then: any, ifnot?: TagsFilter, addExtraTags: Tag[] }[] = []
for (const mapping of configuration.mappings ?? []) {
if (mapping.hideInAnswer === true) {
continue
@ -110,7 +110,7 @@ export default class TagRenderingQuestion extends Combine {
options.saveButtonConstr(inputElement.GetValue()),
new Toggle(Translations.t.general.testing.SetClass("alert"), undefined, State.state.featureSwitchIsTesting)
])
let bottomTags: BaseUIElement;
if (options.bottomText !== undefined) {
bottomTags = options.bottomText(inputElement.GetValue())
@ -147,7 +147,7 @@ export default class TagRenderingQuestion extends Combine {
private static GenerateInputElement(configuration: TagRenderingConfig,
applicableMappings: { if: TagsFilter, then: any, ifnot?: TagsFilter }[],
applicableMappings: { if: TagsFilter, then: any, ifnot?: TagsFilter, addExtraTags: Tag[] }[],
applicableUnit: Unit,
tagsSource: UIEventSource<any>)
: InputElement<TagsFilter> {
@ -341,12 +341,16 @@ export default class TagRenderingQuestion extends Combine {
mapping: {
if: TagsFilter,
then: Translation,
addExtraTags: Tag[]
}, ifNot?: TagsFilter[]): InputElement<TagsFilter> {
let tagging: TagsFilter = mapping.if;
if (ifNot !== undefined) {
tagging = new And([mapping.if, ...ifNot])
}
if (mapping.addExtraTags) {
tagging = new And([tagging, ...mapping.addExtraTags])
}
return new FixedInputElement(
new SubstitutedTranslation(mapping.then, tagsSource),

File diff suppressed because it is too large Load diff

View file

@ -83,7 +83,6 @@
}
]
},
"description": {},
"tagRenderings": [
{
"question": {