| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Asks to add a feature at the last clicked location, at least if zoom is sufficient | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-01-04 04:06:21 +01:00
										 |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | import Svg from "../../Svg"; | 
					
						
							|  |  |  | import {SubtleButton} from "../Base/SubtleButton"; | 
					
						
							|  |  |  | import State from "../../State"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | import Translations from "../i18n/Translations"; | 
					
						
							|  |  |  | import Constants from "../../Models/Constants"; | 
					
						
							| 
									
										
										
										
											2021-01-04 04:36:21 +01:00
										 |  |  | import LayerConfig from "../../Customizations/JSON/LayerConfig"; | 
					
						
							| 
									
										
										
										
											2021-03-29 02:04:42 +02:00
										 |  |  | import {Tag} from "../../Logic/Tags/Tag"; | 
					
						
							|  |  |  | import {TagUtils} from "../../Logic/Tags/TagUtils"; | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | import BaseUIElement from "../BaseUIElement"; | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  | import {VariableUiElement} from "../Base/VariableUIElement"; | 
					
						
							|  |  |  | import Toggle from "../Input/Toggle"; | 
					
						
							|  |  |  | import UserDetails from "../../Logic/Osm/OsmConnection"; | 
					
						
							|  |  |  | import {Translation} from "../i18n/Translation"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | * The SimpleAddUI is a single panel, which can have multiple states: | 
					
						
							|  |  |  | * - A list of presets which can be added by the user | 
					
						
							|  |  |  | * - A 'confirm-selection' button (or alternatively: please enable the layer) | 
					
						
							|  |  |  | * - A 'something is wrong - please soom in further' | 
					
						
							|  |  |  | * - A 'read your unread messages before adding a point' | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-01-04 04:06:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  | interface PresetInfo { | 
					
						
							|  |  |  |     description: string | Translation, | 
					
						
							|  |  |  |     name: string | BaseUIElement, | 
					
						
							|  |  |  |     icon: BaseUIElement, | 
					
						
							|  |  |  |     tags: Tag[], | 
					
						
							|  |  |  |     layerToAddTo: { | 
					
						
							|  |  |  |         layerDef: LayerConfig, | 
					
						
							|  |  |  |         isDisplayed: UIEventSource<boolean> | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  | export default class SimpleAddUI extends Toggle { | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-08 22:32:15 +02:00
										 |  |  |     constructor(isShown: UIEventSource<boolean>) { | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 15:34:32 +02:00
										 |  |  |         const loginButton = new SubtleButton(Svg.osm_logo_ui(), Translations.t.general.add.pleaseLogin.Clone()) | 
					
						
							|  |  |  |             .onClick(() => State.state.osmConnection.AttemptLogin()); | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         const readYourMessages = new Combine([ | 
					
						
							|  |  |  |             Translations.t.general.readYourMessages.Clone().SetClass("alert"), | 
					
						
							|  |  |  |             new SubtleButton(Svg.envelope_ui(), | 
					
						
							|  |  |  |                 Translations.t.general.goToInbox, {url: "https://www.openstreetmap.org/messages/inbox", newTab: false}) | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2021-01-22 00:40:15 +01:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         const selectedPreset = new UIEventSource<PresetInfo>(undefined); | 
					
						
							|  |  |  |         isShown.addCallback(_ => selectedPreset.setData(undefined)) // Clear preset selection when the UI is closed/opened
 | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         function createNewPoint(tags: any[]){ | 
					
						
							|  |  |  |            const loc = State.state.LastClickLocation.data; | 
					
						
							|  |  |  |             let feature = State.state.changes.createElement(tags, loc.lat, loc.lon); | 
					
						
							|  |  |  |             State.state.selectedElement.setData(feature); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         const presetsOverview = SimpleAddUI.CreateAllPresetsPanel(selectedPreset) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const addUi = new VariableUiElement( | 
					
						
							|  |  |  |             selectedPreset.map(preset => { | 
					
						
							|  |  |  |                     if (preset === undefined) { | 
					
						
							|  |  |  |                         return presetsOverview | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     return SimpleAddUI.CreateConfirmButton(preset, | 
					
						
							|  |  |  |                         tags => { | 
					
						
							|  |  |  |                             createNewPoint(tags) | 
					
						
							|  |  |  |                             selectedPreset.setData(undefined) | 
					
						
							|  |  |  |                         }, () => { | 
					
						
							|  |  |  |                             selectedPreset.setData(undefined) | 
					
						
							|  |  |  |                         }) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             )) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super( | 
					
						
							|  |  |  |             new Toggle( | 
					
						
							|  |  |  |                 new Toggle( | 
					
						
							|  |  |  |                     new Toggle( | 
					
						
							|  |  |  |                         Translations.t.general.add.stillLoading.Clone().SetClass("alert"), | 
					
						
							|  |  |  |                         addUi, | 
					
						
							|  |  |  |                         State.state.layerUpdater.runningQuery | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     Translations.t.general.add.zoomInFurther.Clone().SetClass("alert")                    , | 
					
						
							|  |  |  |                     State.state.locationControl.map(loc => loc.zoom >= Constants.userJourney.minZoomLevelToAddNewPoints) | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 readYourMessages, | 
					
						
							|  |  |  |                 State.state.osmConnection.userDetails.map((userdetails: UserDetails) => | 
					
						
							|  |  |  |                     userdetails.csCount >= Constants.userJourney.addNewPointWithUnreadMessagesUnlock || | 
					
						
							|  |  |  |                     userdetails.unreadMessages == 0) | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             loginButton, | 
					
						
							|  |  |  |             State.state.osmConnection.isLoggedIn | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         this.SetStyle("font-size:large"); | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |     private static CreateConfirmButton(preset: PresetInfo, | 
					
						
							|  |  |  |                                        confirm: (tags: any[]) => void,  | 
					
						
							|  |  |  |                                        cancel: () => void): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         const confirmButton = new SubtleButton(preset.icon, | 
					
						
							|  |  |  |             new Combine([ | 
					
						
							|  |  |  |                 Translations.t.general.add.addNew.Subs({category: preset.name}), | 
					
						
							|  |  |  |                 Translations.t.general.add.warnVisibleForEveryone.Clone().SetClass("alert") | 
					
						
							|  |  |  |             ]).SetClass("flex flex-col") | 
					
						
							|  |  |  |         ).SetClass("font-bold break-words") | 
					
						
							|  |  |  |             .onClick(() => confirm(preset.tags)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const openLayerControl =   | 
					
						
							|  |  |  |             new SubtleButton( | 
					
						
							|  |  |  |                 Svg.layers_ui(), | 
					
						
							|  |  |  |                 new Combine([ | 
					
						
							|  |  |  |                     Translations.t.general.add.layerNotEnabled | 
					
						
							|  |  |  |                         .Subs({layer: preset.layerToAddTo.layerDef.name}) | 
					
						
							|  |  |  |                         .SetClass("alert"), | 
					
						
							|  |  |  |                     Translations.t.general.add.openLayerControl | 
					
						
							|  |  |  |                 ]) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             | 
					
						
							|  |  |  |             .onClick(() => State.state.layerControlIsOpened.setData(true)) | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         const openLayerOrConfirm = new Toggle( | 
					
						
							|  |  |  |             confirmButton, | 
					
						
							|  |  |  |             openLayerControl, | 
					
						
							|  |  |  |             preset.layerToAddTo.isDisplayed | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         const tagInfo = SimpleAddUI.CreateTagInfoFor(preset); | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         const cancelButton = new SubtleButton(Svg.close_ui(), | 
					
						
							|  |  |  |             Translations.t.general.cancel | 
					
						
							|  |  |  |         ).onClick(cancel        ) | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         return new Combine([ | 
					
						
							|  |  |  |             Translations.t.general.add.confirmIntro.Subs({title: preset.name}), | 
					
						
							|  |  |  |             State.state.osmConnection.userDetails.data.dryRun ?  | 
					
						
							|  |  |  |                 Translations.t.general.testing.Clone().SetClass("alert") : undefined           ,  | 
					
						
							|  |  |  |             openLayerOrConfirm, | 
					
						
							|  |  |  |             cancelButton, | 
					
						
							|  |  |  |             preset.description, | 
					
						
							|  |  |  |             tagInfo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ]).SetClass("flex flex-col") | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |     private static CreateTagInfoFor(preset: PresetInfo, optionallyLinkToWiki = true) { | 
					
						
							|  |  |  |         const csCount = State.state.osmConnection.userDetails.data.csCount; | 
					
						
							|  |  |  |         return new Toggle( | 
					
						
							| 
									
										
										
										
											2021-06-21 03:12:43 +02:00
										 |  |  |             Translations.t.general.add.presetInfo.Subs({ | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |                 tags: preset.tags.map(t => t.asHumanString(optionallyLinkToWiki && csCount > Constants.userJourney.tagsVisibleAndWikiLinked, true)).join("&"), | 
					
						
							| 
									
										
										
										
											2021-06-19 18:28:30 +02:00
										 |  |  |             }).SetStyle("word-break: break-all"), | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             undefined, | 
					
						
							|  |  |  |             State.state.osmConnection.userDetails.map(userdetails => userdetails.csCount >= Constants.userJourney.tagsVisibleAt) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |     private static CreateAllPresetsPanel(selectedPreset: UIEventSource<PresetInfo>): BaseUIElement { | 
					
						
							|  |  |  |         const presetButtons = SimpleAddUI.CreatePresetButtons(selectedPreset) | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  |         let intro: BaseUIElement = Translations.t.general.add.intro; | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  |         let testMode: BaseUIElement = undefined; | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |         if (State.state.osmConnection?.userDetails?.data?.dryRun) { | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  |             testMode = Translations.t.general.testing.Clone().SetClass("alert") | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         return new Combine([intro, testMode, presetButtons]).SetClass("flex flex-col") | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |     private static CreatePresetSelectButton(preset: PresetInfo){ | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         const tagInfo =SimpleAddUI.CreateTagInfoFor(preset, false); | 
					
						
							|  |  |  |         return new SubtleButton( | 
					
						
							|  |  |  |             preset.icon, | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |             new Combine([ | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |                 Translations.t.general.add.addNew.Subs({ | 
					
						
							|  |  |  |                     category: preset.name | 
					
						
							|  |  |  |                 }).SetClass("font-bold"), | 
					
						
							|  |  |  |                 Translations.WT(preset.description)?.FirstSentence(), | 
					
						
							|  |  |  |                 tagInfo?.SetClass("subtle") | 
					
						
							|  |  |  |             ]).SetClass("flex flex-col") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |   | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | * Generates the list with all the buttons.*/ | 
					
						
							|  |  |  |     private static CreatePresetButtons(selectedPreset: UIEventSource<PresetInfo>): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |         const allButtons = []; | 
					
						
							|  |  |  |         for (const layer of State.state.filteredLayers.data) { | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |              | 
					
						
							|  |  |  |             if(layer.isDisplayed.data === false && State.state.featureSwitchLayers){ | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2020-10-30 00:56:46 +01:00
										 |  |  |             const presets = layer.layerDef.presets; | 
					
						
							|  |  |  |             for (const preset of presets) { | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |                 const tags = TagUtils.KVtoProperties(preset.tags ?? []); | 
					
						
							|  |  |  |                 let icon: BaseUIElement = layer.layerDef.GenerateLeafletStyle(new UIEventSource<any>(tags), false).icon.html | 
					
						
							|  |  |  |                     .SetClass("w-12 h-12 block relative"); | 
					
						
							|  |  |  |                 const presetInfo: PresetInfo = { | 
					
						
							|  |  |  |                     tags: preset.tags, | 
					
						
							|  |  |  |                     layerToAddTo: layer, | 
					
						
							|  |  |  |                     name: preset.title, | 
					
						
							|  |  |  |                     description: preset.description, | 
					
						
							|  |  |  |                     icon: icon | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 const button = SimpleAddUI.CreatePresetSelectButton(presetInfo); | 
					
						
							|  |  |  |                 button.onClick(() => { | 
					
						
							|  |  |  |                     selectedPreset.setData(presetInfo) | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-01-08 16:49:42 +01:00
										 |  |  |                 allButtons.push(button); | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-14 02:39:23 +02:00
										 |  |  |         return new Combine(allButtons).SetClass("flex flex-col"); | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |