| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | 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 {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-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"; | 
					
						
							| 
									
										
										
										
											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-01-19 20:34:04 +01:00
										 |  |  |                 state, | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2021-10-02 15:16:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 21:44:43 +02:00
										 |  |  |         const applicableMappingsSrc = | 
					
						
							| 
									
										
										
										
											2021-10-02 15:16:41 +02:00
										 |  |  |             UIEventSource.ListStabilized(tags.map(tags => { | 
					
						
							| 
									
										
										
										
											2021-10-26 22:53:27 +02:00
										 |  |  |                 const applicableMappings: { if: TagsFilter, then: any, 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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 21:44:43 +02:00
										 |  |  |         const inputElement: InputElement<TagsFilter> = | 
					
						
							| 
									
										
										
										
											2021-10-25 21:50:38 +02:00
										 |  |  |             new VariableInputElement(applicableMappingsSrc.map(applicableMappings => | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |                 TagRenderingQuestion.GenerateInputElement(state, configuration, applicableMappings, applicableUnit, tags) | 
					
						
							| 
									
										
										
										
											2021-10-03 21:44:43 +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-01 04:14:54 +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) => { | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |                         const csCount = state?.osmConnection?.userDetails?.data?.csCount ?? 1000; | 
					
						
							| 
									
										
										
										
											2021-07-01 02:26:45 +02:00
										 |  |  |                         if (csCount < Constants.userJourney.tagsVisibleAt) { | 
					
						
							|  |  |  |                             return ""; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         if (tagsFilter === undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-25 21:50:38 +02:00
										 |  |  |                             return Translations.t.general.noTagsSelected.SetClass("subtle"); | 
					
						
							| 
									
										
										
										
											2021-07-01 02:26:45 +02:00
										 |  |  |                         } | 
					
						
							|  |  |  |                         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") | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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-04 00:45:22 +01:00
										 |  |  |             new Combine([options.cancelButton, | 
					
						
							|  |  |  |             saveButton]).SetClass("flex w-full justify-end flex-wrap-reverse"), | 
					
						
							| 
									
										
										
										
											2022-02-01 04:14:54 +01:00
										 |  |  |             bottomTags, | 
					
						
							|  |  |  |             new Toggle(Translations.t.general.testing.SetClass("alert"), undefined, state.featureSwitchIsTesting) | 
					
						
							|  |  |  |             ]) | 
					
						
							| 
									
										
										
										
											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( | 
					
						
							|  |  |  |         state, | 
					
						
							|  |  |  |         configuration: TagRenderingConfig, | 
					
						
							|  |  |  |         applicableMappings: { if: TagsFilter, then: any, ifnot?: TagsFilter, addExtraTags: Tag[] }[], | 
					
						
							|  |  |  |         applicableUnit: Unit, | 
					
						
							|  |  |  |         tagsSource: UIEventSource<any>) | 
					
						
							| 
									
										
										
										
											2021-10-03 21:44:43 +02:00
										 |  |  |         : InputElement<TagsFilter> { | 
					
						
							| 
									
										
										
										
											2021-10-03 20:50:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // FreeForm input will be undefined if not present; will already contain a special input element if applicable
 | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |         const ff = TagRenderingQuestion.GenerateFreeform(state, configuration, applicableUnit, tagsSource); | 
					
						
							| 
									
										
										
										
											2021-10-03 20:50:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const hasImages = applicableMappings.filter(mapping => mapping.then.ExtractImages().length > 0).length > 0 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:50:03 +02:00
										 |  |  |         let inputEls: InputElement<TagsFilter>[]; | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 03:16:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 15:16:41 +02:00
										 |  |  |         const ifNotsPresent = applicableMappings.some(mapping => mapping.ifnot !== undefined) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 15:16:41 +02:00
										 |  |  |         if (applicableMappings.length < 8 || configuration.multiAnswer || hasImages || 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)]), | 
					
						
							| 
									
										
										
										
											2021-10-25 21:50:38 +02:00
										 |  |  |                         shown: Translations.WT(mapping.then) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							|  |  |  |                 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-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[], | 
					
						
							|  |  |  |             img?: string | 
					
						
							| 
									
										
										
										
											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-01-29 02:45:59 +01:00
										 |  |  |       | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  |         return new FixedInputElement( | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  |             TagRenderingQuestion.GenerateMappingContent(mapping, tagsSource, state)  , | 
					
						
							| 
									
										
										
										
											2021-06-18 01:50:03 +02:00
										 |  |  |             tagging, | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  |             (t0, t1) => t1.isEquivalent(t0)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-29 02:45:59 +01:00
										 |  |  |      | 
					
						
							|  |  |  |     private static GenerateMappingContent(  mapping: { | 
					
						
							|  |  |  |         then: Translation, | 
					
						
							|  |  |  |         icon?: string | 
					
						
							|  |  |  |     }, tagsSource: UIEventSource<any>, state: FeaturePipelineState): BaseUIElement{ | 
					
						
							|  |  |  |         const text =  new SubstitutedTranslation(mapping.then, tagsSource, state) | 
					
						
							|  |  |  |         if (mapping.icon === undefined) { | 
					
						
							|  |  |  |             return text; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return new Combine([new Img(mapping.icon).SetClass("w-6 max-h-6 pr-2"), text]).SetClass("flex") | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |     private static GenerateFreeform(state, 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; | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |         const feature = 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], | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |             mapBackgroundLayer: state.backgroundLayer, | 
					
						
							| 
									
										
										
										
											2021-07-20 01:33:58 +02:00
										 |  |  |             unit: applicableUnit, | 
					
						
							|  |  |  |             args: configuration.freeform.helperArgs, | 
					
						
							| 
									
										
										
										
											2022-02-07 01:57:15 +01:00
										 |  |  |             feature: feature, | 
					
						
							|  |  |  |             placeholder: configuration.freeform.placeholder | 
					
						
							| 
									
										
										
										
											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-09-09 00:05:51 +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-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (freeform.inline) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 15:44:17 +02:00
										 |  |  |             inputTagsFilter.SetClass("w-16-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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |