2020-10-27 01:01:34 +01:00
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import Combine from "../Base/Combine";
|
|
|
|
import TagRenderingConfig from "../../Customizations/JSON/TagRenderingConfig";
|
|
|
|
import {InputElement} from "../Input/InputElement";
|
|
|
|
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 State from "../../State";
|
|
|
|
import {Changes} from "../../Logic/Osm/Changes";
|
|
|
|
import {VariableUiElement} from "../Base/VariableUIElement";
|
|
|
|
import Translations from "../i18n/Translations";
|
|
|
|
import {FixedUiElement} from "../Base/FixedUiElement";
|
2020-11-06 01:58:26 +01:00
|
|
|
import {Translation} 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-06-22 03:16:45 +02:00
|
|
|
import {Unit} from "../../Customizations/JSON/Denomination";
|
2021-07-11 15:44:17 +02:00
|
|
|
import InputElementWrapper from "../Input/InputElementWrapper";
|
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-06-28 00:45:49 +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,
|
2021-07-01 02:26:45 +02:00
|
|
|
options?: {
|
|
|
|
units?: Unit[],
|
|
|
|
afterSave?: () => void,
|
|
|
|
cancelButton?: BaseUIElement,
|
|
|
|
saveButtonConstr?: (src: UIEventSource<TagsFilter>) => BaseUIElement,
|
|
|
|
bottomText?: (src: UIEventSource<TagsFilter>) => BaseUIElement
|
|
|
|
}
|
2021-03-12 13:48:49 +01:00
|
|
|
) {
|
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];
|
2021-06-28 00:45:49 +02:00
|
|
|
const question = new SubstitutedTranslation(configuration.question, tags)
|
|
|
|
.SetClass("question-text");
|
2020-10-27 01:01:34 +01:00
|
|
|
|
|
|
|
|
2021-07-01 02:26:45 +02:00
|
|
|
const inputElement: InputElement<TagsFilter> = TagRenderingQuestion.GenerateInputElement(configuration, applicableUnit, tags)
|
2020-10-27 01:01:34 +01: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) {
|
|
|
|
(State.state?.changes ?? new Changes())
|
|
|
|
.addTag(tags.data.id, selection, tags);
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:26:45 +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,
|
|
|
|
State.state?.osmConnection)
|
|
|
|
.onClick(save)
|
|
|
|
}
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-07-01 02:26:45 +02:00
|
|
|
const saveButton = options.saveButtonConstr(inputElement.GetValue())
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-07-01 02:26:45 +02:00
|
|
|
let bottomTags: BaseUIElement;
|
|
|
|
if (options.bottomText !== undefined) {
|
|
|
|
bottomTags = options.bottomText(inputElement.GetValue())
|
|
|
|
} else {
|
|
|
|
bottomTags = new VariableUiElement(
|
|
|
|
inputElement.GetValue().map(
|
|
|
|
(tagsFilter: TagsFilter) => {
|
|
|
|
const csCount = State.state?.osmConnection?.userDetails?.data?.csCount ?? 1000;
|
|
|
|
if (csCount < Constants.userJourney.tagsVisibleAt) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (tagsFilter === undefined) {
|
|
|
|
return Translations.t.general.noTagsSelected.Clone().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);
|
2020-10-27 01:01:34 +01:00
|
|
|
}
|
2021-07-01 02:26:45 +02:00
|
|
|
)
|
|
|
|
).SetClass("block break-all")
|
|
|
|
}
|
|
|
|
super([
|
2021-06-28 00:45:49 +02:00
|
|
|
question,
|
2021-07-01 02:26:45 +02:00
|
|
|
inputElement,
|
|
|
|
options.cancelButton,
|
2021-06-28 00:45:49 +02:00
|
|
|
saveButton,
|
2021-07-01 02:26:45 +02:00
|
|
|
bottomTags]
|
2021-03-31 15:50:29 +02:00
|
|
|
)
|
2021-07-01 02:26:45 +02:00
|
|
|
this.SetClass("question")
|
2021-06-28 00:45:49 +02:00
|
|
|
|
2021-03-31 15:50:29 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 02:26:45 +02:00
|
|
|
private static GenerateInputElement(configuration: TagRenderingConfig, applicableUnit: Unit, tagsSource: UIEventSource<any>): InputElement<TagsFilter> {
|
2021-06-18 01:50:03 +02:00
|
|
|
let inputEls: InputElement<TagsFilter>[];
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-06-28 00:45:49 +02:00
|
|
|
const mappings = (configuration.mappings ?? [])
|
2021-06-22 03:16:45 +02:00
|
|
|
.filter(mapping => {
|
|
|
|
if (mapping.hideInAnswer === true) {
|
2021-06-18 01:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-28 00:45:49 +02:00
|
|
|
return !(typeof (mapping.hideInAnswer) !== "boolean" && mapping.hideInAnswer.matchesProperties(tagsSource.data));
|
2021-07-01 02:26:45 +02:00
|
|
|
|
2021-06-18 01:50:03 +02:00
|
|
|
})
|
2021-06-22 03:16:45 +02:00
|
|
|
|
|
|
|
|
2021-07-05 00:45:41 +02:00
|
|
|
function allIfNotsExcept(excludeIndex: number) : TagsFilter[]{
|
|
|
|
if(configuration.mappings === undefined){
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
if(configuration.multiAnswer){
|
|
|
|
// The multianswer will do the ifnot configuration themself
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
return Utils.NoNull(configuration.mappings?.map((m,i) => excludeIndex === i ? undefined: m.ifnot))
|
|
|
|
}
|
2021-07-11 15:44:17 +02:00
|
|
|
const ff = TagRenderingQuestion.GenerateFreeform(configuration, applicableUnit, tagsSource);
|
2021-06-18 01:50:03 +02:00
|
|
|
const hasImages = mappings.filter(mapping => mapping.then.ExtractImages().length > 0).length > 0
|
|
|
|
|
2021-06-28 00:45:49 +02:00
|
|
|
if (mappings.length < 8 || configuration.multiAnswer || hasImages) {
|
2021-07-05 00:45:41 +02:00
|
|
|
inputEls = (mappings ?? []).map((mapping,i) => TagRenderingQuestion.GenerateMappingElement(tagsSource, mapping, allIfNotsExcept(i)));
|
2021-06-18 01:50:03 +02:00
|
|
|
inputEls = Utils.NoNull(inputEls);
|
|
|
|
} else {
|
|
|
|
const dropdown: InputElement<TagsFilter> = new DropDown("",
|
2021-07-05 00:45:41 +02:00
|
|
|
mappings.map((mapping, i) => {
|
2021-06-18 01:50:03 +02:00
|
|
|
return {
|
2021-07-05 00:45:41 +02:00
|
|
|
value: new And([mapping.if, ...allIfNotsExcept(i)]),
|
2021-06-18 01:50:03 +02:00
|
|
|
shown: Translations.WT(mapping.then).Clone()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
if (ff == undefined) {
|
|
|
|
return dropdown;
|
|
|
|
} else {
|
|
|
|
inputEls = [dropdown]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inputEls.length == 0) {
|
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) {
|
|
|
|
return TagRenderingQuestion.GenerateMultiAnswer(configuration, inputEls, ff, configuration.mappings.map(mp => mp.ifnot))
|
2020-10-27 01:01:34 +01:00
|
|
|
} else {
|
2021-06-18 01:50:03 +02:00
|
|
|
return new RadioButton(inputEls, false)
|
2020-10-27 01:01:34 +01: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);
|
|
|
|
const inputEl = new InputElementMap<number[], TagsFilter>(
|
|
|
|
checkBoxes,
|
|
|
|
(t0, t1) => {
|
|
|
|
return t0?.isEquivalent(t1) ?? false
|
|
|
|
},
|
|
|
|
(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-06-16 21:23:03 +02:00
|
|
|
const actualTags = TagUtils.FlattenMultiAnswer(tags);
|
|
|
|
console.log("Converted ", indices.join(","), "into", actualTags.asHumanString(false, false, {}), "with elems", elements)
|
|
|
|
return actualTags;
|
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-06-28 00:45:49 +02:00
|
|
|
private static GenerateMappingElement(
|
|
|
|
tagsSource: UIEventSource<any>,
|
|
|
|
mapping: {
|
2021-07-01 02:26:45 +02:00
|
|
|
if: TagsFilter,
|
|
|
|
then: Translation,
|
|
|
|
hideInAnswer: boolean | TagsFilter
|
|
|
|
}, ifNot?: TagsFilter[]): InputElement<TagsFilter> {
|
2021-06-22 03:16:45 +02:00
|
|
|
|
2021-06-18 01:50:03 +02:00
|
|
|
let tagging = mapping.if;
|
|
|
|
if (ifNot.length > 0) {
|
|
|
|
tagging = new And([tagging, ...ifNot])
|
2020-10-27 01:01:34 +01:00
|
|
|
}
|
2021-06-18 01:50:03 +02:00
|
|
|
|
2020-10-27 01:01:34 +01:00
|
|
|
return new FixedInputElement(
|
2021-06-28 00:45:49 +02:00
|
|
|
new SubstitutedTranslation(mapping.then, tagsSource),
|
2021-06-18 01:50:03 +02:00
|
|
|
tagging,
|
2020-10-27 01:01:34 +01:00
|
|
|
(t0, t1) => t1.isEquivalent(t0));
|
|
|
|
}
|
|
|
|
|
2021-07-11 15:44:17 +02:00
|
|
|
private static GenerateFreeform(configuration: TagRenderingConfig, applicableUnit: Unit, tags: UIEventSource<any>): 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-07-20 01:33:58 +02:00
|
|
|
const feature = State.state.allElements.ContainingFeatures.get(tagsData.id)
|
2021-07-11 15:44:17 +02:00
|
|
|
const input: InputElement<string> = ValidatedTextField.InputForType(configuration.freeform.type, {
|
2020-10-27 01:01:34 +01:00
|
|
|
isValid: (str) => (str.length <= 255),
|
2021-06-28 00:45:49 +02:00
|
|
|
country: () => tagsData._country,
|
|
|
|
location: [tagsData._lat, tagsData._lon],
|
2021-06-25 20:38:12 +02:00
|
|
|
mapBackgroundLayer: State.state.backgroundLayer,
|
2021-07-20 01:33:58 +02:00
|
|
|
unit: applicableUnit,
|
|
|
|
args: configuration.freeform.helperArgs,
|
|
|
|
feature: feature
|
2020-10-27 01:01:34 +01:00
|
|
|
});
|
|
|
|
|
2021-07-11 15:44:17 +02:00
|
|
|
input.GetValue().setData(tagsData[freeform.key] ?? freeform.default);
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-07-11 15:44:17 +02:00
|
|
|
let inputTagsFilter : InputElement<TagsFilter> = new InputElementMap(
|
2021-06-22 03:16:45 +02:00
|
|
|
input, (a, b) => a === b || (a?.isEquivalent(b) ?? false),
|
2020-10-27 01:01:34 +01:00
|
|
|
pickString, toString
|
|
|
|
);
|
2021-07-11 15:44:17 +02:00
|
|
|
|
|
|
|
if(freeform.inline){
|
|
|
|
|
|
|
|
inputTagsFilter.SetClass("w-16-imp")
|
|
|
|
inputTagsFilter = new InputElementWrapper(inputTagsFilter, configuration.render, freeform.key, tags)
|
|
|
|
inputTagsFilter.SetClass("block")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputTagsFilter;
|
2020-10-27 01:01:34 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|