2022-06-05 02:24:14 +02:00
|
|
|
import {Store, Stores, UIEventSource} from "../../Logic/UIEventSource";
|
2020-10-27 01:01:34 +01:00
|
|
|
import Combine from "../Base/Combine";
|
2022-06-05 02:24:14 +02:00
|
|
|
import {InputElement, ReadonlyInputElement} from "../Input/InputElement";
|
2020-10-27 01:01:34 +01:00
|
|
|
import ValidatedTextField from "../Input/ValidatedTextField";
|
|
|
|
import {FixedInputElement} from "../Input/FixedInputElement";
|
|
|
|
import {RadioButton} from "../Input/RadioButton";
|
|
|
|
import {Utils} from "../../Utils";
|
|
|
|
import CheckBoxes from "../Input/Checkboxes";
|
|
|
|
import InputElementMap from "../Input/InputElementMap";
|
|
|
|
import {SaveButton} from "./SaveButton";
|
|
|
|
import {VariableUiElement} from "../Base/VariableUIElement";
|
|
|
|
import Translations from "../i18n/Translations";
|
|
|
|
import {FixedUiElement} from "../Base/FixedUiElement";
|
2022-04-22 16:24:21 +02:00
|
|
|
import {Translation, TypedTranslation} from "../i18n/Translation";
|
2021-01-02 19:09:49 +01:00
|
|
|
import Constants from "../../Models/Constants";
|
2021-02-05 16:32:37 +01:00
|
|
|
import {SubstitutedTranslation} from "../SubstitutedTranslation";
|
2021-03-29 00:41:53 +02:00
|
|
|
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
|
|
|
|
import {Tag} from "../../Logic/Tags/Tag";
|
|
|
|
import {And} from "../../Logic/Tags/And";
|
|
|
|
import {TagUtils} from "../../Logic/Tags/TagUtils";
|
2021-06-12 02:58:32 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement";
|
2021-06-18 01:50:03 +02:00
|
|
|
import {DropDown} from "../Input/DropDown";
|
2021-07-11 15:44:17 +02:00
|
|
|
import InputElementWrapper from "../Input/InputElementWrapper";
|
2021-07-15 20:47:28 +02:00
|
|
|
import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction";
|
2021-08-07 23:11:34 +02:00
|
|
|
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig";
|
|
|
|
import {Unit} from "../../Models/Unit";
|
2021-10-03 21:44:43 +02:00
|
|
|
import VariableInputElement from "../Input/VariableInputElement";
|
2021-10-25 21:50:38 +02:00
|
|
|
import Toggle from "../Input/Toggle";
|
2022-01-29 02:45:59 +01:00
|
|
|
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-05-01 04:17:40 +02:00
|
|
|
import {OsmConnection} from "../../Logic/Osm/OsmConnection";
|
2022-05-01 23:29:40 +02:00
|
|
|
import {GeoOperations} from "../../Logic/GeoOperations";
|
2022-07-10 03:58:07 +02:00
|
|
|
import {SearchablePillsSelector} from "../Input/SearchableMappingsSelector";
|
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 {
|
2021-07-01 02:26:45 +02:00
|
|
|
|
2020-10-27 01:01:34 +01:00
|
|
|
constructor(tags: UIEventSource<any>,
|
|
|
|
configuration: TagRenderingConfig,
|
2022-06-06 19:37:22 +02:00
|
|
|
state?: FeaturePipelineState,
|
2021-07-01 02:26:45 +02:00
|
|
|
options?: {
|
|
|
|
units?: Unit[],
|
|
|
|
afterSave?: () => void,
|
|
|
|
cancelButton?: BaseUIElement,
|
2022-06-05 02:24:14 +02:00
|
|
|
saveButtonConstr?: (src: Store<TagsFilter>) => BaseUIElement,
|
|
|
|
bottomText?: (src: Store<TagsFilter>) => BaseUIElement
|
2021-07-01 02:26:45 +02:00
|
|
|
}
|
2021-03-12 13:48:49 +01:00
|
|
|
) {
|
2021-10-02 15:16:41 +02:00
|
|
|
|
2021-10-03 21:44:43 +02:00
|
|
|
const applicableMappingsSrc =
|
2022-06-05 02:24:14 +02:00
|
|
|
Stores.ListStabilized(tags.map(tags => {
|
2022-04-22 16:24:21 +02:00
|
|
|
const applicableMappings: { if: TagsFilter, icon?: string, then: TypedTranslation<object>, ifnot?: TagsFilter, addExtraTags: Tag[] }[] = []
|
2021-10-02 22:27:44 +02:00
|
|
|
for (const mapping of configuration.mappings ?? []) {
|
2021-10-02 15:16:41 +02:00
|
|
|
if (mapping.hideInAnswer === true) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (mapping.hideInAnswer === false || mapping.hideInAnswer === undefined) {
|
2021-10-03 21:44:43 +02:00
|
|
|
applicableMappings.push(mapping)
|
2021-10-02 15:16:41 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-10-03 21:44:43 +02:00
|
|
|
const condition = <TagsFilter>mapping.hideInAnswer;
|
2021-10-02 15:16:41 +02:00
|
|
|
const isShown = !condition.matchesProperties(tags)
|
2021-10-03 21:44:43 +02:00
|
|
|
if (isShown) {
|
2021-10-02 15:16:41 +02:00
|
|
|
applicableMappings.push(mapping)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return applicableMappings
|
|
|
|
}));
|
|
|
|
|
2020-10-27 01:01:34 +01:00
|
|
|
if (configuration === undefined) {
|
|
|
|
throw "A question is needed for a question visualization"
|
|
|
|
}
|
2021-07-01 02:26:45 +02:00
|
|
|
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);
|
2020-10-27 01:01:34 +01:00
|
|
|
|
|
|
|
|
2022-02-12 02:53:41 +01:00
|
|
|
const feedback = new UIEventSource<Translation>(undefined)
|
2022-06-05 02:24:14 +02:00
|
|
|
const inputElement: ReadonlyInputElement<TagsFilter> =
|
2022-07-10 18:08:06 +02:00
|
|
|
new VariableInputElement(applicableMappingsSrc.map(applicableMappings => {
|
|
|
|
return TagRenderingQuestion.GenerateInputElement(state, configuration, applicableMappings, applicableUnit, tags, feedback)
|
2022-06-06 19:37:22 +02:00
|
|
|
}
|
2021-10-03 21:44:43 +02:00
|
|
|
))
|
2022-07-10 18:08:06 +02:00
|
|
|
|
2021-10-03 02:50:11 +02:00
|
|
|
const save = () => {
|
2021-06-28 00:45:49 +02:00
|
|
|
const selection = inputElement.GetValue().data;
|
2020-10-27 01:01:34 +01:00
|
|
|
if (selection) {
|
2022-01-19 20:34:04 +01:00
|
|
|
(state?.changes)
|
2021-07-15 20:47:28 +02:00
|
|
|
.applyAction(new ChangeTagAction(
|
2021-10-04 03:12:42 +02:00
|
|
|
tags.data.id, selection, tags.data, {
|
2022-01-19 20:34:04 +01:00
|
|
|
theme: state?.layoutToUse?.id ?? "unkown",
|
2021-10-04 03:12:42 +02:00
|
|
|
changeType: "answer",
|
|
|
|
}
|
2021-10-03 02:50:11 +02:00
|
|
|
)).then(_ => {
|
2021-10-03 21:44:43 +02:00
|
|
|
console.log("Tagchanges applied")
|
2021-10-03 02:50:11 +02:00
|
|
|
})
|
|
|
|
if (options.afterSave) {
|
|
|
|
options.afterSave();
|
|
|
|
}
|
2020-10-27 01:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:26:45 +02:00
|
|
|
if (options.saveButtonConstr === undefined) {
|
|
|
|
options.saveButtonConstr = v => new SaveButton(v,
|
2022-01-19 20:34:04 +01:00
|
|
|
state?.osmConnection)
|
2021-07-01 02:26:45 +02:00
|
|
|
.onClick(save)
|
|
|
|
}
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-10-25 21:50:38 +02:00
|
|
|
const saveButton = new Combine([
|
|
|
|
options.saveButtonConstr(inputElement.GetValue()),
|
|
|
|
])
|
2022-02-12 02:53:41 +01:00
|
|
|
|
2021-07-01 02:26:45 +02:00
|
|
|
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)
|
2021-07-01 02:26:45 +02:00
|
|
|
}
|
2021-10-03 21:44:43 +02:00
|
|
|
super([
|
2021-06-28 00:45:49 +02:00
|
|
|
question,
|
2021-07-01 02:26:45 +02:00
|
|
|
inputElement,
|
2022-02-12 02:53:41 +01:00
|
|
|
new Combine([
|
2022-02-14 21:46:35 +01:00
|
|
|
new VariableUiElement(feedback.map(t => t?.SetStyle("padding-left: 0.75rem; padding-right: 0.75rem")?.SetClass("alert flex") ?? bottomTags)),
|
2022-02-12 02:53:41 +01:00
|
|
|
new Combine([
|
|
|
|
new Combine([options.cancelButton]),
|
|
|
|
saveButton]).SetClass("flex justify-end flex-wrap-reverse")
|
|
|
|
|
2022-02-14 22:05:04 +01:00
|
|
|
]).SetClass("flex mt-2 justify-between"),
|
2022-06-06 19:37:22 +02:00
|
|
|
new Toggle(Translations.t.general.testing.SetClass("alert"), undefined, state?.featureSwitchIsTesting)
|
2022-02-12 02:53:41 +01:00
|
|
|
])
|
2021-10-25 21:50:38 +02:00
|
|
|
|
|
|
|
|
2021-10-09 22:40:52 +02:00
|
|
|
this.SetClass("question disable-links")
|
2021-03-31 15:50:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 21:44:43 +02:00
|
|
|
|
2022-01-19 20:34:04 +01:00
|
|
|
private static GenerateInputElement(
|
2022-06-06 19:37:22 +02:00
|
|
|
state: FeaturePipelineState,
|
2022-01-19 20:34:04 +01:00
|
|
|
configuration: TagRenderingConfig,
|
2022-07-10 03:58:07 +02:00
|
|
|
applicableMappings: { if: TagsFilter, then: TypedTranslation<object>, icon?: string, ifnot?: TagsFilter, addExtraTags: Tag[], searchTerms?: Record<string, string[]> }[],
|
2022-01-19 20:34:04 +01:00
|
|
|
applicableUnit: Unit,
|
2022-02-12 02:53:41 +01:00
|
|
|
tagsSource: UIEventSource<any>,
|
|
|
|
feedback: UIEventSource<Translation>
|
2022-06-06 19:37:22 +02:00
|
|
|
): ReadonlyInputElement<TagsFilter> {
|
2021-10-03 20:50:18 +02:00
|
|
|
|
2022-07-10 18:08:06 +02:00
|
|
|
|
2022-04-19 23:43:28 +02:00
|
|
|
const hasImages = applicableMappings.findIndex(mapping => mapping.icon !== undefined) >= 0
|
2021-06-18 01:50:03 +02:00
|
|
|
let inputEls: InputElement<TagsFilter>[];
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-10-02 15:16:41 +02:00
|
|
|
const ifNotsPresent = applicableMappings.some(mapping => mapping.ifnot !== undefined)
|
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);
|
2021-06-22 03:16:45 +02:00
|
|
|
|
2021-10-03 02:50:11 +02:00
|
|
|
function allIfNotsExcept(excludeIndex: number): TagsFilter[] {
|
2021-10-02 15:16:41 +02:00
|
|
|
if (configuration.mappings === undefined || configuration.mappings.length === 0) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
if (!ifNotsPresent) {
|
2021-10-03 20:50:18 +02:00
|
|
|
return []
|
2021-07-05 00:45:41 +02:00
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
if (configuration.multiAnswer) {
|
2021-07-05 00:45:41 +02:00
|
|
|
// The multianswer will do the ifnot configuration themself
|
2021-10-03 20:50:18 +02:00
|
|
|
return []
|
2021-07-05 00:45:41 +02:00
|
|
|
}
|
2021-10-03 21:44:43 +02:00
|
|
|
|
2021-10-03 02:50:11 +02:00
|
|
|
const negativeMappings = []
|
2021-10-02 15:16:41 +02:00
|
|
|
|
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-02 15:16:41 +02:00
|
|
|
}
|
2021-10-03 02:50:11 +02:00
|
|
|
negativeMappings.push(mapping.ifnot)
|
|
|
|
}
|
|
|
|
return Utils.NoNull(negativeMappings)
|
2021-10-02 15:16:41 +02:00
|
|
|
|
2021-07-05 00:45:41 +02:00
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-10-03 21:44:43 +02:00
|
|
|
|
2022-07-02 01:59:26 +02:00
|
|
|
if (applicableMappings.length < 8 || configuration.multiAnswer || (hasImages && applicableMappings.length < 16) || ifNotsPresent) {
|
2022-01-19 20:34:04 +01:00
|
|
|
inputEls = (applicableMappings ?? []).map((mapping, i) => TagRenderingQuestion.GenerateMappingElement(state, tagsSource, mapping, allIfNotsExcept(i)));
|
2021-06-18 01:50:03 +02:00
|
|
|
inputEls = Utils.NoNull(inputEls);
|
|
|
|
} else {
|
|
|
|
const dropdown: InputElement<TagsFilter> = new DropDown("",
|
2021-10-02 15:16:41 +02:00
|
|
|
applicableMappings.map((mapping, i) => {
|
2021-06-18 01:50:03 +02:00
|
|
|
return {
|
2021-10-03 02:50:11 +02:00
|
|
|
value: new And([mapping.if, ...allIfNotsExcept(i)]),
|
2022-04-22 16:24:21 +02:00
|
|
|
shown: mapping.then.Subs(tagsSource.data)
|
2021-06-18 01:50:03 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
if (ff == undefined) {
|
|
|
|
return dropdown;
|
|
|
|
} else {
|
|
|
|
inputEls = [dropdown]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inputEls.length == 0) {
|
2021-10-25 21:50:38 +02:00
|
|
|
if (ff === undefined) {
|
2021-10-03 21:44:43 +02:00
|
|
|
throw "Error: could not generate a question: freeform and all mappings are undefined"
|
|
|
|
}
|
2020-10-27 01:01:34 +01:00
|
|
|
return ff;
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:50:29 +02:00
|
|
|
if (ff) {
|
2021-06-18 01:50:03 +02:00
|
|
|
inputEls.push(ff);
|
2021-02-20 01:45:51 +01:00
|
|
|
}
|
|
|
|
|
2021-06-28 00:45:49 +02:00
|
|
|
if (configuration.multiAnswer) {
|
2021-10-03 20:50:18 +02:00
|
|
|
return TagRenderingQuestion.GenerateMultiAnswer(configuration, inputEls, ff, applicableMappings.map(mp => mp.ifnot))
|
2020-10-27 01:01:34 +01:00
|
|
|
} else {
|
2021-07-27 17:00:05 +02:00
|
|
|
return new RadioButton(inputEls, {selectFirstAsDefault: false})
|
2020-10-27 01:01:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-10 03:58:07 +02:00
|
|
|
private static GenerateSearchableSelector(
|
|
|
|
state: FeaturePipelineState,
|
|
|
|
configuration: TagRenderingConfig,
|
2022-07-10 18:08:06 +02:00
|
|
|
applicableMappings: { if: TagsFilter; ifnot?: TagsFilter, then: TypedTranslation<object>; icon?: string; iconClass?: string, addExtraTags: Tag[], searchTerms?: Record<string, string[]> }[], tagsSource: UIEventSource<any>): InputElement<TagsFilter> {
|
|
|
|
const values: { show: BaseUIElement, value: number, mainTerm: Record<string, string>, searchTerms?: Record<string, string[]> }[] = []
|
|
|
|
for (let i = 0; i < applicableMappings.length; i++) {
|
|
|
|
const mapping = applicableMappings[i];
|
2022-07-10 03:58:07 +02:00
|
|
|
const tr = mapping.then.Subs(tagsSource.data)
|
2022-07-10 18:08:06 +02:00
|
|
|
const patchedMapping = <{ iconClass: "small-height", then: TypedTranslation<object> }>{
|
|
|
|
...mapping,
|
|
|
|
iconClass: `small-height`,
|
|
|
|
icon: mapping.icon ?? "./assets/svg/none.svg"
|
|
|
|
}
|
|
|
|
const fancy = TagRenderingQuestion.GenerateMappingContent(patchedMapping, tagsSource, state).SetClass("normal-background")
|
|
|
|
values.push({
|
2022-07-10 03:58:07 +02:00
|
|
|
show: fancy,
|
2022-07-10 18:08:06 +02:00
|
|
|
value: i,
|
2022-07-10 03:58:07 +02:00
|
|
|
mainTerm: tr.translations,
|
|
|
|
searchTerms: mapping.searchTerms
|
|
|
|
})
|
|
|
|
}
|
2022-07-10 18:08:06 +02:00
|
|
|
|
2022-07-10 03:58:07 +02:00
|
|
|
const searchValue: UIEventSource<string> = new UIEventSource<string>(undefined)
|
|
|
|
const ff = configuration.freeform
|
2022-07-10 18:08:06 +02:00
|
|
|
let onEmpty: BaseUIElement = undefined
|
|
|
|
if (ff !== undefined) {
|
|
|
|
onEmpty = new VariableUiElement(searchValue.map(search => configuration.render.Subs({[ff.key]: search})))
|
2022-07-10 03:58:07 +02:00
|
|
|
}
|
2022-07-10 18:08:06 +02:00
|
|
|
|
|
|
|
const classes = "h-64 overflow-scroll"
|
|
|
|
const presetSearch = new SearchablePillsSelector<number>(values, {
|
2022-07-10 03:58:07 +02:00
|
|
|
selectIfSingle: true,
|
|
|
|
mode: configuration.multiAnswer ? "select-many" : "select-one",
|
|
|
|
searchValue,
|
|
|
|
onNoMatches: onEmpty?.SetClass(classes).SetClass("flex justify-center items-center"),
|
2022-07-10 18:08:06 +02:00
|
|
|
searchAreaClass: classes
|
2022-07-10 03:58:07 +02:00
|
|
|
})
|
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) {
|
2022-07-10 03:58:07 +02:00
|
|
|
const t = new Tag(ff.key, searchValue.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
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
console.log("Got tags", tfs)
|
|
|
|
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
|
|
|
}
|
2021-10-02 15:16:41 +02:00
|
|
|
|
2021-06-28 00:45:49 +02:00
|
|
|
private static GenerateMultiAnswer(
|
|
|
|
configuration: TagRenderingConfig,
|
|
|
|
elements: InputElement<TagsFilter>[], freeformField: InputElement<TagsFilter>, ifNotSelected: TagsFilter[]): InputElement<TagsFilter> {
|
2020-10-27 01:01:34 +01:00
|
|
|
const checkBoxes = new CheckBoxes(elements);
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2020-10-27 01:01:34 +01:00
|
|
|
const inputEl = new InputElementMap<number[], TagsFilter>(
|
|
|
|
checkBoxes,
|
|
|
|
(t0, t1) => {
|
2022-04-14 00:53:38 +02:00
|
|
|
return t0?.shadows(t1) ?? false
|
2020-10-27 01:01:34 +01:00
|
|
|
},
|
|
|
|
(indices) => {
|
|
|
|
if (indices.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const tags: TagsFilter[] = indices.map(i => elements[i].GetValue().data);
|
2021-02-20 16:48:42 +01:00
|
|
|
const oppositeTags: TagsFilter[] = [];
|
|
|
|
for (let i = 0; i < ifNotSelected.length; i++) {
|
2021-03-31 15:50:29 +02:00
|
|
|
if (indices.indexOf(i) >= 0) {
|
2021-02-20 16:48:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const notSelected = ifNotSelected[i];
|
2021-03-31 15:50:29 +02:00
|
|
|
if (notSelected === undefined) {
|
2021-02-20 16:48:42 +01:00
|
|
|
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: TagsFilter) => {
|
2020-10-27 14:13:37 +01:00
|
|
|
// {key --> values[]}
|
|
|
|
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[] = []
|
2021-06-28 00:45:49 +02:00
|
|
|
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) {
|
2021-06-28 00:45:49 +02:00
|
|
|
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) {
|
2021-06-28 00:45:49 +02:00
|
|
|
freeformField.GetValue().setData(new Tag(configuration.freeform.key, freeformExtras.join(";")));
|
2020-10-28 11:19:47 +01:00
|
|
|
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 => {
|
2021-02-21 00:15:03 +01:00
|
|
|
// 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;
|
2021-02-21 00:15:03 +01:00
|
|
|
// The actual index of the freeform-element
|
2020-10-27 01:01:34 +01:00
|
|
|
const index = es.data.indexOf(i);
|
|
|
|
if (value === undefined) {
|
2021-02-21 00:15:03 +01:00
|
|
|
// 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) {
|
2021-02-21 00:15:03 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-10-02 15:16:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]
|
|
|
|
*/
|
2021-06-28 00:45:49 +02:00
|
|
|
private static GenerateMappingElement(
|
2022-01-19 20:34:04 +01:00
|
|
|
state,
|
2021-06-28 00:45:49 +02:00
|
|
|
tagsSource: UIEventSource<any>,
|
|
|
|
mapping: {
|
2021-07-01 02:26:45 +02:00
|
|
|
if: TagsFilter,
|
|
|
|
then: Translation,
|
2022-01-29 02:45:59 +01:00
|
|
|
addExtraTags: Tag[],
|
2022-02-17 23:54:14 +01:00
|
|
|
icon?: string,
|
2022-07-10 18:08:06 +02:00
|
|
|
iconClass?: "small" | "medium" | "large" | "small-height"
|
2021-10-03 02:50:11 +02:00
|
|
|
}, ifNot?: TagsFilter[]): InputElement<TagsFilter> {
|
2021-06-22 03:16:45 +02:00
|
|
|
|
2021-10-03 02:50:11 +02:00
|
|
|
let tagging: TagsFilter = mapping.if;
|
2021-10-02 15:16:41 +02:00
|
|
|
if (ifNot !== undefined) {
|
2021-10-03 21:44:43 +02:00
|
|
|
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])
|
|
|
|
}
|
2021-06-18 01:50:03 +02:00
|
|
|
|
2022-02-12 02:53:41 +01:00
|
|
|
|
2020-10-27 01:01:34 +01:00
|
|
|
return new FixedInputElement(
|
2022-02-12 02:53:41 +01:00
|
|
|
TagRenderingQuestion.GenerateMappingContent(mapping, tagsSource, state),
|
2021-06-18 01:50:03 +02:00
|
|
|
tagging,
|
2022-04-14 00:53:38 +02:00
|
|
|
(t0, t1) => t1.shadows(t0));
|
2020-10-27 01:01:34 +01:00
|
|
|
}
|
2022-02-12 02:53:41 +01:00
|
|
|
|
|
|
|
private static GenerateMappingContent(mapping: {
|
2022-01-29 02:45:59 +01:00
|
|
|
then: Translation,
|
2022-02-17 23:54:14 +01:00
|
|
|
icon?: string,
|
2022-07-10 18:08:06 +02:00
|
|
|
iconClass?: "small" | "medium" | "large" | "small-height" | "medium-height" | "large-height"
|
2022-02-12 02:53:41 +01:00
|
|
|
}, tagsSource: UIEventSource<any>, state: FeaturePipelineState): BaseUIElement {
|
|
|
|
const text = new SubstitutedTranslation(mapping.then, tagsSource, state)
|
2022-01-29 02:45:59 +01:00
|
|
|
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")
|
2022-01-29 02:45:59 +01:00
|
|
|
}
|
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>)
|
2022-02-12 02:53:41 +01:00
|
|
|
: InputElement<TagsFilter> {
|
2021-06-28 00:45:49 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-02-12 02:53:41 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
return new And([
|
|
|
|
tag,
|
|
|
|
...freeform.addExtraTags
|
|
|
|
]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-11 15:44:17 +02:00
|
|
|
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({
|
2021-06-28 00:45:49 +02:00
|
|
|
country: () => tagsData._country,
|
2022-05-01 23:29:40 +02:00
|
|
|
location: [center[1], center[0]],
|
2022-06-06 19:37:22 +02:00
|
|
|
mapBackgroundLayer: state?.backgroundLayer,
|
2021-07-20 01:33:58 +02:00
|
|
|
unit: applicableUnit,
|
|
|
|
args: configuration.freeform.helperArgs,
|
2022-02-12 02:53:41 +01:00
|
|
|
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-02-12 02:53:41 +01:00
|
|
|
feedback.setData(Translations.t.validation.tooLong.Subs({count: v.length}))
|
|
|
|
}
|
|
|
|
})
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
let inputTagsFilter: InputElement<TagsFilter> = new InputElementMap(
|
2022-04-14 00:53:38 +02:00
|
|
|
input, (a, b) => a === b || (a?.shadows(b) ?? false),
|
2020-10-27 01:01:34 +01:00
|
|
|
pickString, toString
|
|
|
|
);
|
2021-09-09 00:05:51 +02:00
|
|
|
|
|
|
|
if (freeform.inline) {
|
2022-03-14 20:45:17 +01:00
|
|
|
inputTagsFilter.SetClass("w-48-imp")
|
2022-01-19 20:34:04 +01:00
|
|
|
inputTagsFilter = new InputElementWrapper(inputTagsFilter, configuration.render, freeform.key, tags, state)
|
2021-07-11 15:44:17 +02:00
|
|
|
inputTagsFilter.SetClass("block")
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-07-11 15:44:17 +02:00
|
|
|
return inputTagsFilter;
|
2020-10-27 01:01:34 +01:00
|
|
|
|
|
|
|
}
|
2022-07-10 18:08:06 +02:00
|
|
|
|
2022-06-05 02:24:14 +02:00
|
|
|
public static CreateTagExplanation(selectedValue: Store<TagsFilter>,
|
|
|
|
tags: Store<object>,
|
2022-07-10 18:08:06 +02:00
|
|
|
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")
|
|
|
|
}
|
2020-10-27 01:01:34 +01:00
|
|
|
|
|
|
|
}
|