| 
									
										
										
										
											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 Locale from "../i18n/Locale"; | 
					
						
							|  |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | import {Tag, TagUtils} from "../../Logic/Tags"; | 
					
						
							|  |  |  | import {UIElement} from "../UIElement"; | 
					
						
							|  |  |  | import Svg from "../../Svg"; | 
					
						
							|  |  |  | import {SubtleButton} from "../Base/SubtleButton"; | 
					
						
							|  |  |  | import State from "../../State"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | import {FixedUiElement} from "../Base/FixedUiElement"; | 
					
						
							|  |  |  | 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-01-04 04:06:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class SimpleAddUI extends UIElement { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:28:21 +02:00
										 |  |  |     private readonly _addButtons: UIElement[]; | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |     private _loginButton : UIElement; | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |     private _confirmPreset: UIEventSource<{ | 
					
						
							|  |  |  |         description: string | UIElement, | 
					
						
							|  |  |  |         name: string | UIElement, | 
					
						
							| 
									
										
										
										
											2020-11-27 13:39:00 +01:00
										 |  |  |         icon: UIElement, | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |         tags: Tag[], | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |         layerToAddTo: { | 
					
						
							| 
									
										
										
										
											2021-01-04 04:36:21 +01:00
										 |  |  |             layerDef: LayerConfig, | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |             isDisplayed: UIEventSource<boolean> } | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |     }> | 
					
						
							|  |  |  |         = new UIEventSource(undefined); | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |     private confirmButton: UIElement = undefined; | 
					
						
							| 
									
										
										
										
											2020-12-09 16:45:37 +01:00
										 |  |  |     private _confirmDescription: UIElement = undefined; | 
					
						
							| 
									
										
										
										
											2020-09-17 23:53:57 +02:00
										 |  |  |     private openLayerControl: UIElement; | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |     private cancelButton: UIElement; | 
					
						
							| 
									
										
										
										
											2020-11-06 04:02:53 +01:00
										 |  |  |     private goToInboxButton: UIElement = new SubtleButton(Svg.envelope_ui(),  | 
					
						
							| 
									
										
										
										
											2020-07-29 21:32:51 +02:00
										 |  |  |         Translations.t.general.goToInbox, {url:"https://www.openstreetmap.org/messages/inbox", newTab: false}); | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         super(State.state.locationControl); | 
					
						
							| 
									
										
										
										
											2020-07-29 21:50:19 +02:00
										 |  |  |         this.ListenTo(Locale.language); | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         this.ListenTo(State.state.osmConnection.userDetails); | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |         this.ListenTo(State.state.layerUpdater.runningQuery); | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |         this.ListenTo(this._confirmPreset); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:43:30 +02:00
										 |  |  |         this.ListenTo(State.state.locationControl); | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |          | 
					
						
							|  |  |  |         this._loginButton = Translations.t.general.add.pleaseLogin.Clone().onClick(() => State.state.osmConnection.AttemptLogin()); | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         this._addButtons = []; | 
					
						
							| 
									
										
										
										
											2020-09-14 20:16:03 +02:00
										 |  |  |         this.SetStyle("font-size:large"); | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |         for (const layer of State.state.filteredLayers.data) { | 
					
						
							| 
									
										
										
										
											2020-10-30 00:56:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-17 21:10:35 +02:00
										 |  |  |             this.ListenTo(layer.isDisplayed); | 
					
						
							| 
									
										
										
										
											2020-10-30 00:56:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const presets = layer.layerDef.presets; | 
					
						
							|  |  |  |             for (const preset of presets) { | 
					
						
							| 
									
										
										
										
											2020-11-27 13:39:00 +01:00
										 |  |  |                 const tags = TagUtils.KVtoProperties(preset.tags ?? []); | 
					
						
							| 
									
										
										
										
											2020-12-06 00:20:27 +01:00
										 |  |  |                 let icon: UIElement = new FixedUiElement(layer.layerDef.GenerateLeafletStyle(new UIEventSource<any>(tags), false).icon.html.Render()).SetClass("simple-add-ui-icon"); | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                 const csCount = State.state.osmConnection.userDetails.data.csCount; | 
					
						
							|  |  |  |                 let tagInfo = ""; | 
					
						
							| 
									
										
										
										
											2021-01-02 19:09:49 +01:00
										 |  |  |                 if (csCount > Constants.userJourney.tagsVisibleAt) { | 
					
						
							| 
									
										
										
										
											2020-08-30 01:13:18 +02:00
										 |  |  |                     tagInfo = preset.tags.map(t => t.asHumanString(false, true)).join("&"); | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                     tagInfo = `<br/><span class='subtle'>${tagInfo}</span>` | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const button: UIElement = | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                     new SubtleButton( | 
					
						
							| 
									
										
										
										
											2020-08-22 02:12:46 +02:00
										 |  |  |                         icon, | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                         new Combine([ | 
					
						
							|  |  |  |                             "<b>", | 
					
						
							|  |  |  |                             preset.title, | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                             "</b>", | 
					
						
							| 
									
										
										
										
											2020-12-09 16:45:37 +01:00
										 |  |  |                             preset.description !== undefined ? new Combine(["<br/>", preset.description.FirstSentence()]) : "", | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                             tagInfo | 
					
						
							|  |  |  |                         ]) | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                     ).onClick( | 
					
						
							|  |  |  |                         () => { | 
					
						
							| 
									
										
										
										
											2020-08-22 02:12:46 +02:00
										 |  |  |                             self.confirmButton = new SubtleButton(icon, | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                                 new Combine([ | 
					
						
							| 
									
										
										
										
											2020-08-07 00:45:33 +02:00
										 |  |  |                                     "<b>", | 
					
						
							| 
									
										
										
										
											2020-08-06 23:49:35 +02:00
										 |  |  |                                     Translations.t.general.add.confirmButton.Subs({category: preset.title}), | 
					
						
							| 
									
										
										
										
											2020-12-09 16:45:37 +01:00
										 |  |  |                                     "</b>"])); | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |                             self.confirmButton.onClick(self.CreatePoint(preset.tags)); | 
					
						
							| 
									
										
										
										
											2020-12-09 16:45:37 +01:00
										 |  |  |                             self._confirmDescription = preset.description; | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                             self._confirmPreset.setData({ | 
					
						
							|  |  |  |                                 tags: preset.tags, | 
					
						
							|  |  |  |                                 layerToAddTo: layer, | 
					
						
							|  |  |  |                                 name: preset.title, | 
					
						
							|  |  |  |                                 description: preset.description, | 
					
						
							| 
									
										
										
										
											2020-08-22 02:12:46 +02:00
										 |  |  |                                 icon: icon | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                             }); | 
					
						
							| 
									
										
										
										
											2020-09-03 03:16:43 +02:00
										 |  |  |                             self.Update(); | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                 this._addButtons.push(button); | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-17 23:53:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 04:02:53 +01:00
										 |  |  |         this.cancelButton = new SubtleButton(Svg.close_ui(), | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |             Translations.t.general.cancel | 
					
						
							|  |  |  |         ).onClick(() => { | 
					
						
							|  |  |  |             self._confirmPreset.setData(undefined); | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2020-09-17 23:53:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 04:02:53 +01:00
										 |  |  |         this.openLayerControl = new SubtleButton(Svg.layers_ui(), | 
					
						
							| 
									
										
										
										
											2020-09-17 23:53:57 +02:00
										 |  |  |             Translations.t.general.add.openLayerControl | 
					
						
							|  |  |  |         ).onClick(() => { | 
					
						
							|  |  |  |             State.state.layerControlIsOpened.setData(true); | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |     private CreatePoint(tags: Tag[]) { | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         return () => { | 
					
						
							| 
									
										
										
										
											2021-01-02 21:03:40 +01:00
										 |  |  |             const loc = State.state.LastClickLocation.data; | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |             let feature = State.state.changes.createElement(tags, loc.lat, loc.lon); | 
					
						
							| 
									
										
										
										
											2020-11-15 01:16:35 +01:00
										 |  |  |             State.state.selectedElement.setData(feature); | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 18:24:00 +02:00
										 |  |  |     InnerRender(): string { | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         const userDetails = State.state.osmConnection.userDetails; | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this._confirmPreset.data !== undefined) { | 
					
						
							| 
									
										
										
										
											2020-09-17 21:10:35 +02:00
										 |  |  |              | 
					
						
							|  |  |  |             if(!this._confirmPreset.data.layerToAddTo.isDisplayed.data){ | 
					
						
							|  |  |  |                 return new Combine([ | 
					
						
							| 
									
										
										
										
											2021-01-04 04:36:21 +01:00
										 |  |  |                     Translations.t.general.add.layerNotEnabled.Subs({layer: this._confirmPreset.data.layerToAddTo.layerDef.name}) | 
					
						
							| 
									
										
										
										
											2020-09-17 21:10:35 +02:00
										 |  |  |                         .SetClass("alert"), | 
					
						
							| 
									
										
										
										
											2020-09-18 00:31:54 +02:00
										 |  |  |                     this.openLayerControl, | 
					
						
							| 
									
										
										
										
											2020-09-17 21:10:35 +02:00
										 |  |  |                      | 
					
						
							|  |  |  |                     this.cancelButton | 
					
						
							|  |  |  |                 ]).Render(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |             let tagInfo = ""; | 
					
						
							|  |  |  |             const csCount = State.state.osmConnection.userDetails.data.csCount; | 
					
						
							| 
									
										
										
										
											2021-01-02 19:09:49 +01:00
										 |  |  |             if (csCount > Constants.userJourney.tagsVisibleAt) { | 
					
						
							|  |  |  |                 tagInfo = this._confirmPreset.data .tags.map(t => t.asHumanString(csCount > Constants.userJourney.tagsVisibleAndWikiLinked, true)).join("&"); | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                 tagInfo = `<br/>More information about the preset: ${tagInfo}` | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-09-14 20:16:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |             return new Combine([ | 
					
						
							|  |  |  |                 Translations.t.general.add.confirmIntro.Subs({title: this._confirmPreset.data.name}), | 
					
						
							| 
									
										
										
										
											2020-09-14 20:16:03 +02:00
										 |  |  |                 userDetails.data.dryRun ? "<span class='alert'>TESTING - changes won't be saved</span>" : "", | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |                 this.confirmButton, | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                 this.cancelButton, | 
					
						
							| 
									
										
										
										
											2020-12-09 16:45:37 +01:00
										 |  |  |                 this._confirmDescription, | 
					
						
							| 
									
										
										
										
											2020-08-22 18:57:27 +02:00
										 |  |  |                 tagInfo | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             ]).Render(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let header: UIElement = Translations.t.general.add.header; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-17 13:13:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (userDetails === undefined) { | 
					
						
							| 
									
										
										
										
											2020-07-30 00:59:08 +02:00
										 |  |  |             return header.Render(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-17 13:13:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         if (!userDetails.data.loggedIn) { | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |             return new Combine([header, this._loginButton]).Render() | 
					
						
							| 
									
										
										
										
											2020-07-15 13:15:36 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 19:09:49 +01:00
										 |  |  |         if (userDetails.data.unreadMessages > 0 && userDetails.data.csCount < Constants.userJourney.addNewPointWithUnreadMessagesUnlock) { | 
					
						
							| 
									
										
										
										
											2020-11-06 01:58:26 +01:00
										 |  |  |             return new Combine([header, | 
					
						
							|  |  |  |                 Translations.t.general.readYourMessages.Clone().SetClass("alert"), | 
					
						
							| 
									
										
										
										
											2020-07-29 19:02:36 +02:00
										 |  |  |                 this.goToInboxButton | 
					
						
							|  |  |  |             ]).Render(); | 
					
						
							| 
									
										
										
										
											2020-09-17 13:13:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         if (userDetails.data.dryRun) { | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |             header = new Combine([header, | 
					
						
							|  |  |  |                 "<span class='alert'>", | 
					
						
							|  |  |  |                 "Test mode - changes won't be saved", | 
					
						
							|  |  |  |                 "</span>" | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 19:09:49 +01:00
										 |  |  |         if (userDetails.data.csCount < Constants.userJourney.addNewPointsUnlock) { | 
					
						
							| 
									
										
										
										
											2020-07-29 18:35:46 +02:00
										 |  |  |             return new Combine([header, "<span class='alert'>", | 
					
						
							|  |  |  |                 Translations.t.general.fewChangesBefore, | 
					
						
							|  |  |  |                 "</span>"]).Render(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 19:09:49 +01:00
										 |  |  |         if (State.state.locationControl.data.zoom < Constants.userJourney.minZoomLevelToAddNewPoints) { | 
					
						
							| 
									
										
										
										
											2020-12-09 16:45:37 +01:00
										 |  |  |             return new Combine([header, Translations.t.general.add.zoomInFurther.SetClass("alert")]).Render() | 
					
						
							| 
									
										
										
										
											2020-06-29 03:40:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-29 16:21:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |         if (State.state.layerUpdater.runningQuery.data) { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:52:21 +02:00
										 |  |  |             return new Combine([header, Translations.t.general.add.stillLoading]).Render() | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 11:37:34 +02:00
										 |  |  |         return header.Render() + new Combine(this._addButtons).SetClass("add-popup-all-buttons").Render(); | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |