From 9ae40d8af2db6dc52e656549f2d02c96d08aaa27 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Mon, 18 Jul 2022 00:10:41 +0200 Subject: [PATCH] Refactoring: split AndOrTagConfigJson into an AndTagConfig and an OrTagConfig --- Logic/Tags/TagUtils.ts | 17 ++++++------- Models/ThemeConfig/FilterConfig.ts | 4 +-- Models/ThemeConfig/Json/FilterConfigJson.ts | 4 +-- .../Json/PointRenderingConfigJson.ts | 4 +-- .../QuestionableTagRenderingConfigJson.ts | 10 ++++---- Models/ThemeConfig/Json/TagConfigJson.ts | 25 ++++++++++++++----- .../Json/TagRenderingConfigJson.ts | 6 ++--- Models/ThemeConfig/LineRenderingConfig.ts | 1 - Models/ThemeConfig/SourceConfig.ts | 1 - Models/ThemeConfig/TagRenderingConfig.ts | 1 - 10 files changed, 41 insertions(+), 32 deletions(-) diff --git a/Logic/Tags/TagUtils.ts b/Logic/Tags/TagUtils.ts index 0440ffd2a..c957d7206 100644 --- a/Logic/Tags/TagUtils.ts +++ b/Logic/Tags/TagUtils.ts @@ -6,12 +6,11 @@ import ComparingTag from "./ComparingTag"; import {RegexTag} from "./RegexTag"; import SubstitutingTag from "./SubstitutingTag"; import {Or} from "./Or"; -import {AndOrTagConfigJson} from "../../Models/ThemeConfig/Json/TagConfigJson"; +import {TagConfigJson} from "../../Models/ThemeConfig/Json/TagConfigJson"; import {isRegExp} from "util"; import * as key_counts from "../../assets/key_totals.json" type Tags = Record -type OsmTags = Tags & {id: string} export class TagUtils { private static keyCounts: { keys: any, tags: any } = key_counts["default"] ?? key_counts @@ -249,7 +248,7 @@ export class TagUtils { * // Must match case insensitive * TagUtils.Tag("name~i~somename").matchesProperties({name: "SoMeName"}) // => true */ - public static Tag(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter { + public static Tag(json: TagConfigJson, context: string = ""): TagsFilter { try { return this.TagUnsafe(json, context); } catch (e) { @@ -308,20 +307,20 @@ export class TagUtils { return {key, value, invert: invert == "!", modifier: (modifier == "i~" ? "i" : "")}; } - private static TagUnsafe(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter { + private static TagUnsafe(json: TagConfigJson, context: string = ""): TagsFilter { if (json === undefined) { throw new Error(`Error while parsing a tag: 'json' is undefined in ${context}. Make sure all the tags are defined and at least one tag is present in a complex expression`) } if (typeof (json) != "string") { - if (json.and !== undefined && json.or !== undefined) { + if (json["and"] !== undefined && json["or"] !== undefined) { throw `Error while parsing a TagConfig: got an object where both 'and' and 'or' are defined` } - if (json.and !== undefined) { - return new And(json.and.map(t => TagUtils.Tag(t, context))); + if (json["and"] !== undefined) { + return new And(json["and"].map(t => TagUtils.Tag(t, context))); } - if (json.or !== undefined) { - return new Or(json.or.map(t => TagUtils.Tag(t, context))); + if (json["or"] !== undefined) { + return new Or(json["or"].map(t => TagUtils.Tag(t, context))); } throw "At " + context + ": unrecognized tag" } diff --git a/Models/ThemeConfig/FilterConfig.ts b/Models/ThemeConfig/FilterConfig.ts index 40bb2a3d3..d649dacea 100644 --- a/Models/ThemeConfig/FilterConfig.ts +++ b/Models/ThemeConfig/FilterConfig.ts @@ -4,7 +4,7 @@ import FilterConfigJson from "./Json/FilterConfigJson"; import Translations from "../../UI/i18n/Translations"; import {TagUtils} from "../../Logic/Tags/TagUtils"; import ValidatedTextField from "../../UI/Input/ValidatedTextField"; -import {AndOrTagConfigJson} from "./Json/TagConfigJson"; +import {TagConfigJson} from "./Json/TagConfigJson"; import {UIEventSource} from "../../Logic/UIEventSource"; import {FilterState} from "../FilteredLayer"; import {QueryParameters} from "../../Logic/Web/QueryParameters"; @@ -16,7 +16,7 @@ export default class FilterConfig { public readonly options: { question: Translation; osmTags: TagsFilter | undefined; - originalTagsSpec: string | AndOrTagConfigJson + originalTagsSpec: TagConfigJson fields: { name: string, type: string }[] }[]; public readonly defaultSelection? : number diff --git a/Models/ThemeConfig/Json/FilterConfigJson.ts b/Models/ThemeConfig/Json/FilterConfigJson.ts index bd03200f3..db0e258a8 100644 --- a/Models/ThemeConfig/Json/FilterConfigJson.ts +++ b/Models/ThemeConfig/Json/FilterConfigJson.ts @@ -1,4 +1,4 @@ -import {AndOrTagConfigJson} from "./TagConfigJson"; +import {TagConfigJson} from "./TagConfigJson"; export default interface FilterConfigJson { /** @@ -13,7 +13,7 @@ export default interface FilterConfigJson { */ options: { question: string | any; - osmTags?: AndOrTagConfigJson | string, + osmTags?: TagConfigJson, default?: boolean, fields?: { /** diff --git a/Models/ThemeConfig/Json/PointRenderingConfigJson.ts b/Models/ThemeConfig/Json/PointRenderingConfigJson.ts index 65655ac32..9f372b880 100644 --- a/Models/ThemeConfig/Json/PointRenderingConfigJson.ts +++ b/Models/ThemeConfig/Json/PointRenderingConfigJson.ts @@ -1,5 +1,5 @@ import {TagRenderingConfigJson} from "./TagRenderingConfigJson"; -import {AndOrTagConfigJson} from "./TagConfigJson"; +import {TagConfigJson} from "./TagConfigJson"; /** * The PointRenderingConfig gives all details onto how to render a single point of a feature. @@ -39,7 +39,7 @@ export default interface PointRenderingConfigJson { * Note: strings are interpreted as icons, so layering and substituting is supported. You can use `circle:white;./my_icon.svg` to add a background circle */ iconBadges?: { - if: string | AndOrTagConfigJson, + if: TagConfigJson, /** * Badge to show * Type: icon diff --git a/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson.ts b/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson.ts index c40a7ab71..963f67e0a 100644 --- a/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson.ts +++ b/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson.ts @@ -1,4 +1,4 @@ -import {AndOrTagConfigJson} from "./TagConfigJson"; +import {TagConfigJson} from "./TagConfigJson"; import {TagRenderingConfigJson} from "./TagRenderingConfigJson"; @@ -7,7 +7,7 @@ export interface MappingConfigJson { /** * @inheritDoc */ - if: AndOrTagConfigJson | string, + if: TagConfigJson, /** * Shown if the 'if is fulfilled * Type: rendered @@ -89,7 +89,7 @@ export interface MappingConfigJson { * hideInAnswer: "_country!=be" * } */ - hideInAnswer?: boolean | string | AndOrTagConfigJson, + hideInAnswer?: boolean | TagConfigJson, /** * Only applicable if 'multiAnswer' is set. * This is for situations such as: @@ -98,7 +98,7 @@ export interface MappingConfigJson { * Note that we can not explicitly render this negative case to the user, we cannot show `does _not_ accept coins`. * If this is important to your usecase, consider using multiple radiobutton-fields without `multiAnswer` */ - ifnot?: AndOrTagConfigJson | string + ifnot?: TagConfigJson /** * If chosen as answer, these tags will be applied as well onto the object. @@ -117,7 +117,7 @@ export interface MappingConfigJson { * If the searchable selector is picked, mappings with this item will have priority and show up even if the others are hidden * Use this sparingly */ - priorityIf?: string | AndOrTagConfigJson + priorityIf?: TagConfigJson } diff --git a/Models/ThemeConfig/Json/TagConfigJson.ts b/Models/ThemeConfig/Json/TagConfigJson.ts index ab3d1a9ce..6118c0c34 100644 --- a/Models/ThemeConfig/Json/TagConfigJson.ts +++ b/Models/ThemeConfig/Json/TagConfigJson.ts @@ -1,9 +1,22 @@ /** - * A small interface to combine tags and tagsfilters. - * + * The main representation of Tags. + * See https://github.com/pietervdvn/MapComplete/blob/develop/Docs/Tags_format.md for more documentation + */ +export type TagConfigJson = string | AndTagConfigJson | OrTagConfigJson + + +/** + * Chain many tags, to match, all of these should be true * See https://github.com/pietervdvn/MapComplete/blob/develop/Docs/Tags_format.md for documentation */ -export interface AndOrTagConfigJson { - and?: (string | AndOrTagConfigJson)[] - or?: (string | AndOrTagConfigJson)[] -} \ No newline at end of file +export type OrTagConfigJson = { + or: TagConfigJson[] +} +/** + * Chain many tags, to match, a single of these should be true + * See https://github.com/pietervdvn/MapComplete/blob/develop/Docs/Tags_format.md for documentation + */ +export type AndTagConfigJson = { + and: TagConfigJson[] +} + diff --git a/Models/ThemeConfig/Json/TagRenderingConfigJson.ts b/Models/ThemeConfig/Json/TagRenderingConfigJson.ts index 1ccf1c2f8..b04ef35a2 100644 --- a/Models/ThemeConfig/Json/TagRenderingConfigJson.ts +++ b/Models/ThemeConfig/Json/TagRenderingConfigJson.ts @@ -1,4 +1,4 @@ -import {AndOrTagConfigJson} from "./TagConfigJson"; +import {TagConfigJson} from "./TagConfigJson"; /** * A TagRenderingConfigJson is a single piece of code which converts one ore more tags into a HTML-snippet. @@ -39,7 +39,7 @@ export interface TagRenderingConfigJson { * * This is useful to ask a follow-up question. E.g. if there is a diaper table, then ask a follow-up question on diaper tables... * */ - condition?: AndOrTagConfigJson | string; + condition?: TagConfigJson; /** * Allow freeform text input from the user @@ -66,7 +66,7 @@ export interface TagRenderingConfigJson { * * This can be an substituting-tag as well, e.g. {'if': 'addr:street:={_calculated_nearby_streetname}', 'then': '{_calculated_nearby_streetname}'} */ - if: AndOrTagConfigJson | string, + if: TagConfigJson, /** * If the condition `if` is met, the text `then` will be rendered. * If not known yet, the user will be presented with `then` as an option diff --git a/Models/ThemeConfig/LineRenderingConfig.ts b/Models/ThemeConfig/LineRenderingConfig.ts index 6b7f8082b..fbe1c6756 100644 --- a/Models/ThemeConfig/LineRenderingConfig.ts +++ b/Models/ThemeConfig/LineRenderingConfig.ts @@ -71,7 +71,6 @@ export default class LineRenderingConfig extends WithContextLoader { } const fillStr = render(this.fill, undefined) - let fill: boolean = undefined; if (fillStr !== undefined && fillStr !== "") { style["fill"] = fillStr === "yes" || fillStr === "true" } diff --git a/Models/ThemeConfig/SourceConfig.ts b/Models/ThemeConfig/SourceConfig.ts index 5c31bdbcf..928284436 100644 --- a/Models/ThemeConfig/SourceConfig.ts +++ b/Models/ThemeConfig/SourceConfig.ts @@ -1,6 +1,5 @@ import {TagsFilter} from "../../Logic/Tags/TagsFilter"; import {RegexTag} from "../../Logic/Tags/RegexTag"; -import {param} from "jquery"; export default class SourceConfig { diff --git a/Models/ThemeConfig/TagRenderingConfig.ts b/Models/ThemeConfig/TagRenderingConfig.ts index bc19e54b9..cdcf916de 100644 --- a/Models/ThemeConfig/TagRenderingConfig.ts +++ b/Models/ThemeConfig/TagRenderingConfig.ts @@ -14,7 +14,6 @@ import List from "../../UI/Base/List"; import {MappingConfigJson, QuestionableTagRenderingConfigJson} from "./Json/QuestionableTagRenderingConfigJson"; import {FixedUiElement} from "../../UI/Base/FixedUiElement"; import {Paragraph} from "../../UI/Base/Paragraph"; -import UserDetails from "../../Logic/Osm/OsmConnection"; export interface Mapping { readonly if: TagsFilter,