Fix: fix icons in mappings which use a builtin svg

This commit is contained in:
Pieter Vander Vennet 2023-05-09 00:05:38 +02:00
parent beb86919a8
commit ab28fbe35c
2 changed files with 33 additions and 31 deletions

View file

@ -1,17 +1,16 @@
import PointRenderingConfigJson from "./Json/PointRenderingConfigJson" import PointRenderingConfigJson from "./Json/PointRenderingConfigJson"
import TagRenderingConfig from "./TagRenderingConfig" import TagRenderingConfig from "./TagRenderingConfig"
import { TagsFilter } from "../../Logic/Tags/TagsFilter" import {TagsFilter} from "../../Logic/Tags/TagsFilter"
import { TagUtils } from "../../Logic/Tags/TagUtils" import {TagUtils} from "../../Logic/Tags/TagUtils"
import { Utils } from "../../Utils" import {Utils} from "../../Utils"
import Svg from "../../Svg" import Svg from "../../Svg"
import WithContextLoader from "./WithContextLoader" import WithContextLoader from "./WithContextLoader"
import { Store } from "../../Logic/UIEventSource" import {Store} from "../../Logic/UIEventSource"
import BaseUIElement from "../../UI/BaseUIElement" import BaseUIElement from "../../UI/BaseUIElement"
import { FixedUiElement } from "../../UI/Base/FixedUiElement" import {FixedUiElement} from "../../UI/Base/FixedUiElement"
import Img from "../../UI/Base/Img" import Img from "../../UI/Base/Img"
import Combine from "../../UI/Base/Combine" import Combine from "../../UI/Base/Combine"
import { VariableUiElement } from "../../UI/Base/VariableUIElement" import {VariableUiElement} from "../../UI/Base/VariableUIElement"
import Constants from "../Constants";
export default class PointRenderingConfig extends WithContextLoader { export default class PointRenderingConfig extends WithContextLoader {
static readonly allowed_location_codes: ReadonlySet<string> = new Set<string>([ static readonly allowed_location_codes: ReadonlySet<string> = new Set<string>([
@ -84,7 +83,7 @@ export default class PointRenderingConfig extends WithContextLoader {
} }
}) })
const iconPath = this.icon?.GetRenderValue({ id: "node/-1" })?.txt const iconPath = this.icon?.GetRenderValue({id: "node/-1"})?.txt
if (iconPath !== undefined && iconPath.startsWith(Utils.assets_path)) { if (iconPath !== undefined && iconPath.startsWith(Utils.assets_path)) {
const iconKey = iconPath.substr(Utils.assets_path.length) const iconKey = iconPath.substr(Utils.assets_path.length)
if (Svg.All[iconKey] === undefined) { if (Svg.All[iconKey] === undefined) {
@ -110,7 +109,7 @@ export default class PointRenderingConfig extends WithContextLoader {
return undefined return undefined
} }
const match = htmlSpec.match(/([a-zA-Z0-9_]*):([^;]*)/) const match = htmlSpec.match(/([a-zA-Z0-9_]*):([^;]*)/)
if (match !== null && Constants.defaultPinIcons.indexOf(match[1] ) >= 0) { if (match !== null && Svg.All[match[1] + ".svg"] !== undefined) {
const svg = Svg.All[match[1] + ".svg"] as string const svg = Svg.All[match[1] + ".svg"] as string
const targetColor = match[2] const targetColor = match[2]
const img = new Img( const img = new Img(
@ -169,7 +168,7 @@ export default class PointRenderingConfig extends WithContextLoader {
noFullWidth?: boolean noFullWidth?: boolean
} }
): BaseUIElement { ): BaseUIElement {
tags = tags ?? { id: "node/-1" } tags = tags ?? {id: "node/-1"}
let defaultPin: BaseUIElement = undefined let defaultPin: BaseUIElement = undefined
if (this.label === undefined) { if (this.label === undefined) {
defaultPin = Svg.teardrop_with_hole_green_svg() defaultPin = Svg.teardrop_with_hole_green_svg()

View file

@ -15,7 +15,6 @@ import {FixedUiElement} from "../../UI/Base/FixedUiElement"
import {Paragraph} from "../../UI/Base/Paragraph" import {Paragraph} from "../../UI/Base/Paragraph"
import Svg from "../../Svg" import Svg from "../../Svg"
import Validators, {ValidatorType} from "../../UI/InputElement/Validators"; import Validators, {ValidatorType} from "../../UI/InputElement/Validators";
import Constants from "../Constants";
export interface Mapping { export interface Mapping {
readonly if: UploadableTag readonly if: UploadableTag
@ -69,6 +68,7 @@ export default class TagRenderingConfig {
public readonly mappings?: Mapping[] public readonly mappings?: Mapping[]
public readonly labels: string[] public readonly labels: string[]
public readonly classes: string[] public readonly classes: string[]
constructor(json: string | QuestionableTagRenderingConfigJson, context?: string) { constructor(json: string | QuestionableTagRenderingConfigJson, context?: string) {
if (json === undefined) { if (json === undefined) {
throw "Initing a TagRenderingConfig with undefined in " + context throw "Initing a TagRenderingConfig with undefined in " + context
@ -118,9 +118,9 @@ export default class TagRenderingConfig {
this.question = Translations.T(json.question, translationKey + ".question") this.question = Translations.T(json.question, translationKey + ".question")
this.questionhint = Translations.T(json.questionHint, translationKey + ".questionHint") this.questionhint = Translations.T(json.questionHint, translationKey + ".questionHint")
this.description = Translations.T(json.description, translationKey + ".description") this.description = Translations.T(json.description, translationKey + ".description")
this.condition = TagUtils.Tag(json.condition ?? { and: [] }, `${context}.condition`) this.condition = TagUtils.Tag(json.condition ?? {and: []}, `${context}.condition`)
this.metacondition = TagUtils.Tag( this.metacondition = TagUtils.Tag(
json.metacondition ?? { and: [] }, json.metacondition ?? {and: []},
`${context}.metacondition` `${context}.metacondition`
) )
if (json.freeform) { if (json.freeform) {
@ -379,9 +379,7 @@ export default class TagRenderingConfig {
if (stripped.endsWith(".svg")) { if (stripped.endsWith(".svg")) {
stripped = stripped.substring(0, stripped.length - 4) stripped = stripped.substring(0, stripped.length - 4)
} }
if ( if (Svg.All[stripped + ".svg"] !== undefined) {
Constants.defaultPinIcons.indexOf(stripped)
) {
icon = "./assets/svg/" + mapping.icon icon = "./assets/svg/" + mapping.icon
if (!icon.endsWith(".svg")) { if (!icon.endsWith(".svg")) {
icon += ".svg" icon += ".svg"
@ -544,7 +542,7 @@ export default class TagRenderingConfig {
this.freeform?.key === undefined || this.freeform?.key === undefined ||
tags[this.freeform.key] !== undefined tags[this.freeform.key] !== undefined
) { ) {
return { then: this.render } return {then: this.render}
} }
return undefined return undefined
@ -634,9 +632,9 @@ export default class TagRenderingConfig {
currentProperties: Record<string, string> currentProperties: Record<string, string>
): UploadableTag { ): UploadableTag {
freeformValue = freeformValue?.trim() freeformValue = freeformValue?.trim()
const validator = Validators.get(<ValidatorType> this.freeform?.type) const validator = Validators.get(<ValidatorType>this.freeform?.type)
if(validator && freeformValue){ if (validator && freeformValue) {
freeformValue = validator.reformat(freeformValue,() => currentProperties["_country"]) freeformValue = validator.reformat(freeformValue, () => currentProperties["_country"])
} }
if (freeformValue === "") { if (freeformValue === "") {
freeformValue = undefined freeformValue = undefined
@ -687,12 +685,12 @@ export default class TagRenderingConfig {
} else { } else {
// Is at least one mapping shown in the answer? // Is at least one mapping shown in the answer?
const someMappingIsShown = this.mappings.some(m => { const someMappingIsShown = this.mappings.some(m => {
if(typeof m.hideInAnswer === "boolean"){ if (typeof m.hideInAnswer === "boolean") {
return !m.hideInAnswer return !m.hideInAnswer
} }
const isHidden = m.hideInAnswer.matchesProperties(currentProperties) const isHidden = m.hideInAnswer.matchesProperties(currentProperties)
return !isHidden return !isHidden
} ) })
// If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key // 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) const useFreeform = freeformValue !== undefined && (singleSelectedMapping === this.mappings.length || !someMappingIsShown)
if (useFreeform) { if (useFreeform) {
@ -700,13 +698,18 @@ export default class TagRenderingConfig {
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.warn("TagRenderingConfig.ConstructSpecification has a weird fallback for", {freeformValue, singleSelectedMapping, multiSelectedMapping, currentProperties}) console.warn("TagRenderingConfig.ConstructSpecification has a weird fallback for", {
freeformValue,
singleSelectedMapping,
multiSelectedMapping,
currentProperties
})
return undefined return undefined
} }
} }
@ -751,7 +754,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 return msgs
@ -781,12 +784,12 @@ export default class TagRenderingConfig {
this.description, this.description,
this.question !== undefined this.question !== undefined
? new Combine([ ? new Combine([
"The question is ", "The question is ",
new FixedUiElement(this.question.txt).SetClass("font-bold bold"), new FixedUiElement(this.question.txt).SetClass("font-bold bold"),
]) ])
: new FixedUiElement( : new FixedUiElement(
"This tagrendering has no question and is thus read-only" "This tagrendering has no question and is thus read-only"
).SetClass("italic"), ).SetClass("italic"),
new Combine(withRender), new Combine(withRender),
mappings, mappings,
condition, condition,