Fix 'addExtraTags' in mappings

This commit is contained in:
Pieter Vander Vennet 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 * Allows fixed-tag inputs, shown either as radiobuttons or as checkboxes
*/ */
mappings?: { mappings?: {
/** /**
* If this condition is met, then the text under `then` will be shown. * 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. * 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 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 {And} from "../../Logic/Tags/And";
import ValidatedTextField from "../../UI/Input/ValidatedTextField"; import ValidatedTextField from "../../UI/Input/ValidatedTextField";
import {Utils} from "../../Utils"; import {Utils} from "../../Utils";
import {Tag} from "../../Logic/Tags/Tag";
/*** /***
* The parsed version of TagRenderingConfigJSON * The parsed version of TagRenderingConfigJSON
@ -36,6 +37,7 @@ export default class TagRenderingConfig {
readonly ifnot?: TagsFilter, readonly ifnot?: TagsFilter,
readonly then: Translation readonly then: Translation
readonly hideInAnswer: boolean | TagsFilter readonly hideInAnswer: boolean | TagsFilter
readonly addExtraTags: Tag[]
}[] }[]
readonly roaming: boolean; readonly roaming: boolean;
@ -119,21 +121,24 @@ export default class TagRenderingConfig {
this.mappings = json.mappings.map((mapping, i) => { this.mappings = json.mappings.map((mapping, i) => {
const ctx = `${context}.mapping[${i}]`
if (mapping.then === undefined) { 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) { 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) { 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) { 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; let hideInAnswer: boolean | TagsFilter = false;
if (typeof mapping.hideInAnswer === "boolean") { if (typeof mapping.hideInAnswer === "boolean") {
@ -141,12 +146,12 @@ export default class TagRenderingConfig {
} else if (mapping.hideInAnswer !== undefined) { } else if (mapping.hideInAnswer !== undefined) {
hideInAnswer = TagUtils.Tag(mapping.hideInAnswer, `${context}.mapping[${i}].hideInAnswer`); hideInAnswer = TagUtils.Tag(mapping.hideInAnswer, `${context}.mapping[${i}].hideInAnswer`);
} }
const mappingContext = `${context}.mapping[${i}]`
const mp = { const mp = {
if: TagUtils.Tag(mapping.if, `${mappingContext}.if`), if: TagUtils.Tag(mapping.if, `${ctx}.if`),
ifnot: (mapping.ifnot !== undefined ? TagUtils.Tag(mapping.ifnot, `${mappingContext}.ifnot`) : undefined), ifnot: (mapping.ifnot !== undefined ? TagUtils.Tag(mapping.ifnot, `${ctx}.ifnot`) : undefined),
then: Translations.T(mapping.then, `{mappingContext}.then`), 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 (this.question) {
if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) { if (hideInAnswer !== true && mp.if !== undefined && !mp.if.isUsableAsAnswer()) {

View file

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

File diff suppressed because it is too large Load diff

View file

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