mapcomplete/UI/Popup/TagRenderingQuestion.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

766 lines
28 KiB
TypeScript
Raw Normal View History

2022-09-03 18:00:54 +02:00
import { Store, Stores, UIEventSource } from "../../Logic/UIEventSource"
2020-10-27 01:01:34 +01:00
import Combine from "../Base/Combine"
2022-09-03 18:00:54 +02:00
import { InputElement, ReadonlyInputElement } from "../Input/InputElement"
import { FixedInputElement } from "../Input/FixedInputElement"
import { RadioButton } from "../Input/RadioButton"
import { Utils } from "../../Utils"
2020-10-27 01:01:34 +01:00
import CheckBoxes from "../Input/Checkboxes"
import InputElementMap from "../Input/InputElementMap"
2022-09-03 18:00:54 +02:00
import { SaveButton } from "./SaveButton"
import { VariableUiElement } from "../Base/VariableUIElement"
2020-10-27 01:01:34 +01:00
import Translations from "../i18n/Translations"
2022-09-03 18:00:54 +02:00
import { FixedUiElement } from "../Base/FixedUiElement"
import { Translation } from "../i18n/Translation"
2021-01-02 19:09:49 +01:00
import Constants from "../../Models/Constants"
2022-09-03 18:00:54 +02:00
import { SubstitutedTranslation } from "../SubstitutedTranslation"
import { TagsFilter } from "../../Logic/Tags/TagsFilter"
import { Tag } from "../../Logic/Tags/Tag"
import { And } from "../../Logic/Tags/And"
import { TagUtils, UploadableTag } from "../../Logic/Tags/TagUtils"
2021-06-12 02:58:32 +02:00
import BaseUIElement from "../BaseUIElement"
2022-09-03 18:00:54 +02:00
import { DropDown } from "../Input/DropDown"
import InputElementWrapper from "../Input/InputElementWrapper"
import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction"
2022-09-03 18:00:54 +02:00
import TagRenderingConfig, { Mapping } from "../../Models/ThemeConfig/TagRenderingConfig"
import { Unit } from "../../Models/Unit"
import VariableInputElement from "../Input/VariableInputElement"
import Toggle from "../Input/Toggle"
import Img from "../Base/Img"
import FeaturePipelineState from "../../Logic/State/FeaturePipelineState"
2022-02-02 02:08:45 +01:00
import Title from "../Base/Title"
2022-09-03 18:00:54 +02:00
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
import { GeoOperations } from "../../Logic/GeoOperations"
import { SearchablePillsSelector } from "../Input/SearchableMappingsSelector"
import { OsmTags } from "../../Models/OsmFeature"
2020-10-27 01:01:34 +01:00
/**
* Shows the question element.
* Note that the value _migh_ already be known, e.g. when selected or when changing the value
*/
2021-10-03 02:50:11 +02:00
export default class TagRenderingQuestion extends Combine {
constructor(
tags: UIEventSource<Record<string, string> & { id: string }>,
2022-09-03 18:00:54 +02:00
configuration: TagRenderingConfig,
state?: FeaturePipelineState,
options?: {
units?: Unit[]
afterSave?: () => void
cancelButton?: BaseUIElement
saveButtonConstr?: (src: Store<TagsFilter>) => BaseUIElement
bottomText?: (src: Store<UploadableTag>) => BaseUIElement
2022-09-03 18:00:54 +02:00
}
2021-03-12 13:48:49 +01:00
) {
const applicableMappingsSrc = Stores.ListStabilized(
tags.map((tags) => {
const applicableMappings: Mapping[] = []
2021-10-02 22:27:44 +02:00
for (const mapping of configuration.mappings ?? []) {
if (mapping.hideInAnswer === true) {
continue
}
if (mapping.hideInAnswer === false || mapping.hideInAnswer === undefined) {
applicableMappings.push(mapping)
continue
}
const condition = <TagsFilter>mapping.hideInAnswer
const isShown = !condition.matchesProperties(tags)
if (isShown) {
applicableMappings.push(mapping)
}
}
return applicableMappings
})
2022-09-08 21:40:48 +02:00
)
2020-10-27 01:01:34 +01:00
if (configuration === undefined) {
throw "A question is needed for a question visualization"
}
options = options ?? {}
const applicableUnit = (options.units ?? []).filter((unit) =>
unit.isApplicableToKey(configuration.freeform?.key)
)[0]
2022-02-02 02:08:45 +01:00
const question = new Title(
new SubstitutedTranslation(configuration.question, tags, state).SetClass(
"question-text"
),
3
2022-09-08 21:40:48 +02:00
)
let questionHint = undefined
if (configuration.questionhint !== undefined) {
2023-03-08 02:07:22 +01:00
questionHint = new SubstitutedTranslation(
configuration.questionhint,
tags,
state
).SetClass("font-bold subtle")
}
2020-10-27 01:01:34 +01:00
const feedback = new UIEventSource<Translation>(undefined)
const inputElement: ReadonlyInputElement<UploadableTag> = new VariableInputElement(
2022-07-10 18:08:06 +02:00
applicableMappingsSrc.map((applicableMappings) => {
2022-09-03 18:00:54 +02:00
return TagRenderingQuestion.GenerateInputElement(
state,
configuration,
applicableMappings,
applicableUnit,
tags,
feedback
)
})
)
2022-07-10 18:08:06 +02:00
if (options.saveButtonConstr === undefined) {
const save = () => {
const selection = TagUtils.FlattenMultiAnswer(
TagUtils.FlattenAnd(inputElement.GetValue().data, tags.data)
)
if (selection) {
;(state?.changes)
.applyAction(
new ChangeTagAction(tags.data.id, selection, tags.data, {
theme: state?.layoutToUse?.id ?? "unkown",
changeType: "answer",
})
)
.then((_) => {
console.log("Tagchanges applied")
2022-09-03 18:00:54 +02:00
})
if (options.afterSave) {
options.afterSave()
}
2021-10-03 02:50:11 +02:00
}
2020-10-27 01:01:34 +01:00
}
options.saveButtonConstr = (v) => new SaveButton(v, state?.osmConnection).onClick(save)
}
2020-10-27 01:01:34 +01:00
const saveButton = new Combine([options.saveButtonConstr(inputElement.GetValue())])
let bottomTags: BaseUIElement
if (options.bottomText !== undefined) {
bottomTags = options.bottomText(inputElement.GetValue())
} else {
2022-05-01 04:17:40 +02:00
bottomTags = TagRenderingQuestion.CreateTagExplanation(
inputElement.GetValue(),
tags,
state
)
}
super([
question,
questionHint,
inputElement,
new VariableUiElement(
feedback.map(
(t) =>
t
?.SetStyle("padding-left: 0.75rem; padding-right: 0.75rem")
?.SetClass("alert flex") ?? bottomTags
)
),
new Combine([options.cancelButton, saveButton]).SetClass(
"flex justify-end flex-wrap-reverse"
),
2022-06-06 19:37:22 +02:00
new Toggle(
Translations.t.general.testing.SetClass("block alert"),
2022-06-06 19:37:22 +02:00
undefined,
state?.featureSwitchIsTesting
2022-09-08 21:40:48 +02:00
),
])
this.SetClass("question disable-links")
}
private static GenerateInputElement(
2022-06-06 19:37:22 +02:00
state: FeaturePipelineState,
configuration: TagRenderingConfig,
applicableMappings: Mapping[],
applicableUnit: Unit,
tagsSource: UIEventSource<any>,
feedback: UIEventSource<Translation>
): ReadonlyInputElement<UploadableTag> {
2022-04-19 23:43:28 +02:00
const hasImages = applicableMappings.findIndex((mapping) => mapping.icon !== undefined) >= 0
let inputEls: InputElement<UploadableTag>[]
2022-09-08 21:40:48 +02:00
const ifNotsPresent = applicableMappings.some((mapping) => mapping.ifnot !== undefined)
2022-09-08 21:40:48 +02:00
2022-07-10 18:08:06 +02:00
if (
applicableMappings.length > 8 &&
(configuration.freeform?.type === undefined ||
configuration.freeform?.type === "string") &&
(!configuration.multiAnswer || configuration.freeform === undefined)
) {
2022-07-10 03:58:07 +02:00
return TagRenderingQuestion.GenerateSearchableSelector(
state,
configuration,
applicableMappings,
tagsSource
)
}
// FreeForm input will be undefined if not present; will already contain a special input element if applicable
const ff = TagRenderingQuestion.GenerateFreeform(
state,
configuration,
applicableUnit,
tagsSource,
feedback
2022-09-08 21:40:48 +02:00
)
function allIfNotsExcept(excludeIndex: number): UploadableTag[] {
if (configuration.mappings === undefined || configuration.mappings.length === 0) {
return undefined
}
if (!ifNotsPresent) {
return []
}
if (configuration.multiAnswer) {
// The multianswer will do the ifnot configuration themself
return []
}
2021-10-03 02:50:11 +02:00
const negativeMappings = []
2021-10-03 02:50:11 +02:00
for (let i = 0; i < applicableMappings.length; i++) {
const mapping = applicableMappings[i]
if (i === excludeIndex || mapping.ifnot === undefined) {
continue
}
2021-10-03 02:50:11 +02:00
negativeMappings.push(mapping.ifnot)
}
return Utils.NoNull(negativeMappings)
}
if (
applicableMappings.length < 8 ||
configuration.multiAnswer ||
(hasImages && applicableMappings.length < 16) ||
ifNotsPresent
) {
inputEls = (applicableMappings ?? []).map((mapping, i) =>
TagRenderingQuestion.GenerateMappingElement(
state,
tagsSource,
mapping,
allIfNotsExcept(i)
2022-09-08 21:40:48 +02:00
)
)
inputEls = Utils.NoNull(inputEls)
} else {
const dropdown: InputElement<UploadableTag> = new DropDown(
"",
applicableMappings.map((mapping, i) => {
return {
2021-10-03 02:50:11 +02:00
value: new And([mapping.if, ...allIfNotsExcept(i)]),
shown: mapping.then.Subs(tagsSource.data),
}
})
)
if (ff == undefined) {
return dropdown
} else {
inputEls = [dropdown]
}
}
if (inputEls.length == 0) {
if (ff === undefined) {
throw "Error: could not generate a question: freeform and all mappings are undefined"
}
2020-10-27 01:01:34 +01:00
return ff
}
if (ff) {
inputEls.push(ff)
}
if (configuration.multiAnswer) {
return TagRenderingQuestion.GenerateMultiAnswer(
configuration,
inputEls,
ff,
applicableMappings.map((mp) => mp.ifnot)
2022-09-08 21:40:48 +02:00
)
2020-10-27 01:01:34 +01:00
} else {
2022-09-03 18:00:54 +02:00
return new RadioButton(inputEls, { selectFirstAsDefault: false })
2020-10-27 01:01:34 +01:00
}
}
private static MappingToPillValue(
applicableMappings: Mapping[],
tagsSource: UIEventSource<OsmTags>,
state: FeaturePipelineState
): {
show: BaseUIElement
value: number
mainTerm: Record<string, string>
searchTerms?: Record<string, string[]>
2022-11-02 14:44:06 +01:00
original: Mapping
hasPriority?: Store<boolean>
}[] {
const values: {
show: BaseUIElement
value: number
mainTerm: Record<string, string>
searchTerms?: Record<string, string[]>
2022-11-02 14:44:06 +01:00
original: Mapping
hasPriority?: Store<boolean>
}[] = []
const addIcons = applicableMappings.some((m) => m.icon !== undefined)
for (let i = 0; i < applicableMappings.length; i++) {
const mapping = applicableMappings[i]
const tr = mapping.then.Subs(tagsSource.data)
const patchedMapping = <Mapping>{
...mapping,
2022-08-27 10:41:17 +02:00
iconClass: mapping.iconClass ?? `small-height`,
2022-10-27 01:50:41 +02:00
icon: mapping.icon ?? (addIcons ? "./assets/svg/none.svg" : undefined),
}
const fancy = TagRenderingQuestion.GenerateMappingContent(
patchedMapping,
tagsSource,
state
).SetClass("normal-background")
values.push({
show: fancy,
value: i,
mainTerm: tr.translations,
searchTerms: mapping.searchTerms,
original: mapping,
2022-11-02 14:44:06 +01:00
hasPriority: tagsSource.map((tags) => mapping.priorityIf?.matchesProperties(tags)),
})
}
return values
}
/**
*
* // Should return the search as freeform value
* const source = new UIEventSource({id: "1234"})
* const tr = new TagRenderingConfig({
* id:"test",
* render:"The value is {key}",
* freeform: {
* key:"key"
* },
* mappings: [
* {
* if:"x=y",
* then:"z",
* searchTerms: {
* "en" : ["z"]
* }
* }
* ]
* }, "test");
* const selector = TagRenderingQuestion.GenerateSearchableSelector(
* undefined,
* tr,
* tr.mappings,
* source,
* {
* search: new UIEventSource<string>("value")
* }
* );
* selector.GetValue().data // => new And([new Tag("key","value")])
*
* // Should return the search as freeform value, even if a previous search matched
* const source = new UIEventSource({id: "1234"})
* const search = new UIEventSource<string>("")
* const tr = new TagRenderingConfig({
* id:"test",
* render:"The value is {key}",
* freeform: {
* key:"key"
* },
* mappings: [
* {
* if:"x=y",
* then:"z",
* searchTerms: {
* "en" : ["z"]
* }
* }
* ]
* }, "test");
* const selector = TagRenderingQuestion.GenerateSearchableSelector(
* undefined,
* tr,
* tr.mappings,
* source,
* {
* search
* }
* );
* search.setData("z")
* search.setData("zx")
* selector.GetValue().data // => new And([new Tag("key","zx")])
*/
2022-07-10 03:58:07 +02:00
private static GenerateSearchableSelector(
state: FeaturePipelineState,
configuration: TagRenderingConfig,
applicableMappings: Mapping[],
tagsSource: UIEventSource<OsmTags>,
options?: {
search: UIEventSource<string>
}
): InputElement<UploadableTag> {
const values = TagRenderingQuestion.MappingToPillValue(
applicableMappings,
tagsSource,
2022-11-02 14:44:06 +01:00
state
)
2022-09-08 21:40:48 +02:00
const searchValue: UIEventSource<string> =
options?.search ?? new UIEventSource<string>(undefined)
2022-07-10 03:58:07 +02:00
const ff = configuration.freeform
2022-07-10 18:08:06 +02:00
let onEmpty: BaseUIElement = undefined
if (ff !== undefined) {
2022-09-03 18:00:54 +02:00
onEmpty = new VariableUiElement(
searchValue.map((search) => configuration.render.Subs({ [ff.key]: search }))
2022-09-08 21:40:48 +02:00
)
2022-07-10 03:58:07 +02:00
}
const mode = configuration.multiAnswer ? "select-many" : "select-one"
2022-07-10 18:08:06 +02:00
const classes = "h-64 overflow-scroll"
const presetSearch = new SearchablePillsSelector<number>(values, {
mode,
2022-07-10 03:58:07 +02:00
searchValue,
onNoMatches: onEmpty?.SetClass(classes).SetClass("flex justify-center items-center"),
2022-11-02 14:44:06 +01:00
searchAreaClass: classes,
2022-07-10 03:58:07 +02:00
})
const fallbackTag = searchValue.map((s) => {
if (s === undefined || ff?.key === undefined) {
return undefined
}
return new Tag(ff.key, s)
})
2022-07-10 18:08:06 +02:00
return new InputElementMap<number[], And>(
presetSearch,
(x0, x1) => {
if (x0 == x1) {
return true
2022-07-10 03:58:07 +02:00
}
2022-07-10 18:08:06 +02:00
if (x0 === undefined || x1 === undefined) {
return false
}
if (x0.and.length !== x1.and.length) {
return false
}
for (let i = 0; i < x0.and.length; i++) {
if (x1.and[i] != x0.and[i]) {
return false
}
}
return true
},
(selected) => {
if (
ff !== undefined &&
searchValue.data?.length > 0 &&
!presetSearch.someMatchFound.data
) {
const t = fallbackTag.data
2022-07-10 18:08:06 +02:00
if (ff.addExtraTags) {
return new And([t, ...ff.addExtraTags])
2022-07-10 03:58:07 +02:00
}
2022-07-10 18:08:06 +02:00
return new And([t])
2022-07-10 03:58:07 +02:00
}
2022-07-10 18:08:06 +02:00
if (selected === undefined || selected.length == 0) {
return undefined
}
const tfs = Utils.NoNull(
applicableMappings.map((mapping, i) => {
if (selected.indexOf(i) >= 0) {
return mapping.if
} else {
return mapping.ifnot
}
})
2022-09-08 21:40:48 +02:00
)
2022-07-10 18:08:06 +02:00
return new And(tfs)
2022-07-10 03:58:07 +02:00
},
2022-07-10 18:08:06 +02:00
(tf) => {
if (tf === undefined) {
return []
2022-07-10 03:58:07 +02:00
}
2022-07-10 18:08:06 +02:00
const selected: number[] = []
for (let i = 0; i < applicableMappings.length; i++) {
const mapping = applicableMappings[i]
if (tf.and.some((t) => mapping.if == t)) {
selected.push(i)
}
}
return selected
2022-07-10 03:58:07 +02:00
},
[searchValue, presetSearch.someMatchFound]
2022-07-10 18:08:06 +02:00
)
2022-07-10 03:58:07 +02:00
}
private static GenerateMultiAnswer(
configuration: TagRenderingConfig,
elements: InputElement<UploadableTag>[],
freeformField: InputElement<UploadableTag>,
ifNotSelected: UploadableTag[]
): InputElement<UploadableTag> {
2020-10-27 01:01:34 +01:00
const checkBoxes = new CheckBoxes(elements)
const inputEl = new InputElementMap<number[], UploadableTag>(
2020-10-27 01:01:34 +01:00
checkBoxes,
(t0, t1) => {
return t0?.shadows(t1) ?? false
2020-10-27 01:01:34 +01:00
},
(indices) => {
if (indices.length === 0) {
return undefined
}
const tags: UploadableTag[] = indices.map((i) => elements[i].GetValue().data)
const oppositeTags: UploadableTag[] = []
for (let i = 0; i < ifNotSelected.length; i++) {
if (indices.indexOf(i) >= 0) {
continue
}
const notSelected = ifNotSelected[i]
if (notSelected === undefined) {
continue
}
oppositeTags.push(notSelected)
}
tags.push(TagUtils.FlattenMultiAnswer(oppositeTags))
2021-07-26 16:25:57 +02:00
return TagUtils.FlattenMultiAnswer(tags)
2020-10-27 01:01:34 +01:00
},
(tags: UploadableTag) => {
2020-10-27 14:13:37 +01:00
// {key --> values[]}
2022-09-03 18:00:54 +02:00
2020-10-27 14:13:37 +01:00
const presentTags = TagUtils.SplitKeys([tags])
2020-10-27 01:01:34 +01:00
const indices: number[] = []
2020-10-27 14:13:37 +01:00
// We also collect the values that have to be added to the freeform field
let freeformExtras: string[] = []
if (configuration.freeform?.key) {
freeformExtras = [...(presentTags[configuration.freeform.key] ?? [])]
2020-10-27 14:13:37 +01:00
}
2020-10-27 01:01:34 +01:00
2020-10-27 14:13:37 +01:00
for (let j = 0; j < elements.length; j++) {
const inputElement = elements[j]
if (inputElement === freeformField) {
continue
}
const val = inputElement.GetValue()
const neededTags = TagUtils.SplitKeys([val.data])
// if every 'neededKeys'-value is present in presentKeys, we have a match and enable the index
if (TagUtils.AllKeysAreContained(presentTags, neededTags)) {
indices.push(j)
if (freeformExtras.length > 0) {
const freeformsToRemove: string[] =
neededTags[configuration.freeform.key] ?? []
2020-10-27 14:13:37 +01:00
for (const toRm of freeformsToRemove) {
const i = freeformExtras.indexOf(toRm)
if (i >= 0) {
freeformExtras.splice(i, 1)
}
}
2020-10-27 01:01:34 +01:00
}
}
2020-10-27 14:13:37 +01:00
}
if (freeformField) {
if (freeformExtras.length > 0) {
freeformField
.GetValue()
.setData(new Tag(configuration.freeform.key, freeformExtras.join(";")))
indices.push(elements.indexOf(freeformField))
2020-10-27 14:13:37 +01:00
} else {
freeformField.GetValue().setData(undefined)
}
2020-10-27 01:01:34 +01:00
}
2020-10-27 14:13:37 +01:00
2020-10-27 01:01:34 +01:00
return indices
},
elements.map((el) => el.GetValue())
)
freeformField?.GetValue()?.addCallbackAndRun((value) => {
// The list of indices of the selected elements
2020-10-27 01:01:34 +01:00
const es = checkBoxes.GetValue()
const i = elements.length - 1
// The actual index of the freeform-element
2020-10-27 01:01:34 +01:00
const index = es.data.indexOf(i)
if (value === undefined) {
// No data is set in the freeform text field; so we delete the checkmark if it is selected
2020-10-27 01:01:34 +01:00
if (index >= 0) {
es.data.splice(index, 1)
es.ping()
}
} else if (index < 0) {
// There is data defined in the checkmark, but the checkmark isn't checked, so we check it
// This is of course because the data changed
2020-10-27 01:01:34 +01:00
es.data.push(i)
es.ping()
}
})
return inputEl
}
/**
* Generates a (Fixed) input element for this mapping.
* Note that the mapping might hide itself if the condition is not met anymore.
*
* Returns: [the element itself, the value to select if not selected. The contents of this UIEventSource might swap to undefined if the conditions to show the answer are unmet]
*/
private static GenerateMappingElement(
state,
tagsSource: UIEventSource<any>,
mapping: Mapping,
ifNot?: UploadableTag[]
): InputElement<UploadableTag> {
let tagging: UploadableTag = mapping.if
if (ifNot !== undefined) {
tagging = new And([mapping.if, ...ifNot])
2020-10-27 01:01:34 +01:00
}
2021-10-26 22:53:27 +02:00
if (mapping.addExtraTags) {
tagging = new And([tagging, ...mapping.addExtraTags])
}
2020-10-27 01:01:34 +01:00
return new FixedInputElement(
TagRenderingQuestion.GenerateMappingContent(mapping, tagsSource, state),
tagging,
(t0, t1) => t1.shadows(t0)
2022-09-08 21:40:48 +02:00
)
2020-10-27 01:01:34 +01:00
}
private static GenerateMappingContent(
mapping: Mapping,
tagsSource: UIEventSource<any>,
state: FeaturePipelineState
): BaseUIElement {
const text = new SubstitutedTranslation(mapping.then, tagsSource, state)
if (mapping.icon === undefined) {
return text
}
2022-07-10 18:08:06 +02:00
return new Combine([
new Img(mapping.icon).SetClass("mr-1 mapping-icon-" + (mapping.iconClass ?? "small")),
text,
]).SetClass("flex items-center")
}
2020-10-27 01:01:34 +01:00
2022-06-06 19:37:22 +02:00
private static GenerateFreeform(
state: FeaturePipelineState,
configuration: TagRenderingConfig,
applicableUnit: Unit,
tags: UIEventSource<any>,
feedback: UIEventSource<Translation>
): InputElement<UploadableTag> {
const freeform = configuration.freeform
2020-10-27 01:01:34 +01:00
if (freeform === undefined) {
return undefined
}
const pickString = (string: any) => {
if (string === "" || string === undefined) {
return undefined
}
if (string.length >= 255) {
return undefined
}
2020-10-27 01:01:34 +01:00
const tag = new Tag(freeform.key, string)
if (freeform.addExtraTags === undefined) {
return tag
}
2022-09-03 18:00:54 +02:00
return new And([tag, ...freeform.addExtraTags])
2020-10-27 01:01:34 +01:00
}
const toString = (tag) => {
if (tag instanceof And) {
for (const subtag of tag.and) {
if (subtag instanceof Tag && subtag.key === freeform.key) {
return subtag.value
}
}
return undefined
} else if (tag instanceof Tag) {
return tag.value
}
return undefined
}
const tagsData = tags.data
2022-06-06 19:37:22 +02:00
const feature = state?.allElements?.ContainingFeatures?.get(tagsData.id)
2022-07-10 18:08:06 +02:00
const center = feature != undefined ? GeoOperations.centerpointCoordinates(feature) : [0, 0]
2022-06-19 19:10:56 +02:00
const input: InputElement<string> = ValidatedTextField.ForType(
configuration.freeform.type
)?.ConstructInputElement({
country: () => tagsData._country,
location: [center[1], center[0]],
2022-06-06 19:37:22 +02:00
mapBackgroundLayer: state?.backgroundLayer,
unit: applicableUnit,
args: configuration.freeform.helperArgs,
feature,
placeholder: configuration.freeform.placeholder,
feedback,
2020-10-27 01:01:34 +01:00
})
2022-07-10 18:08:06 +02:00
2022-06-06 19:37:22 +02:00
// Init with correct value
2022-06-19 19:10:56 +02:00
input?.GetValue().setData(tagsData[freeform.key] ?? freeform.default)
2022-07-10 18:08:06 +02:00
2022-06-06 19:37:22 +02:00
// Add a length check
2022-07-10 18:08:06 +02:00
input?.GetValue().addCallbackD((v: string | undefined) => {
if (v?.length >= 255) {
2022-09-03 18:00:54 +02:00
feedback.setData(Translations.t.validation.tooLong.Subs({ count: v.length }))
}
})
2020-10-27 01:01:34 +01:00
let inputTagsFilter: InputElement<UploadableTag> = new InputElementMap(
input,
(a, b) => a === b || (a?.shadows(b) ?? false),
2020-10-27 01:01:34 +01:00
pickString,
toString
)
if (freeform.inline) {
inputTagsFilter.SetClass("w-48-imp")
inputTagsFilter = new InputElementWrapper(
inputTagsFilter,
configuration.render,
freeform.key,
tags,
state
)
inputTagsFilter.SetClass("block")
}
return inputTagsFilter
2020-10-27 01:01:34 +01:00
}
2022-07-10 18:08:06 +02:00
public static CreateTagExplanation(
selectedValue: Store<TagsFilter>,
2022-09-03 18:00:54 +02:00
tags: Store<object>,
state?: { osmConnection?: OsmConnection }
) {
2022-05-01 04:17:40 +02:00
return new VariableUiElement(
selectedValue.map(
(tagsFilter: TagsFilter) => {
const csCount =
state?.osmConnection?.userDetails?.data?.csCount ??
Constants.userJourney.tagsVisibleAndWikiLinked + 1
if (csCount < Constants.userJourney.tagsVisibleAt) {
return ""
}
if (tagsFilter === undefined) {
return Translations.t.general.noTagsSelected.SetClass("subtle")
}
if (csCount < Constants.userJourney.tagsVisibleAndWikiLinked) {
const tagsStr = tagsFilter.asHumanString(false, true, tags.data)
return new FixedUiElement(tagsStr).SetClass("subtle")
}
return tagsFilter.asHumanString(true, true, tags.data)
2022-06-06 19:37:22 +02:00
},
[state?.osmConnection?.userDetails]
2022-05-01 04:17:40 +02:00
)
).SetClass("block break-all")
}
2022-09-03 18:00:54 +02:00
}