| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import { UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import { OsmConnection } from "../../Logic/Osm/OsmConnection" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import Title from "../Base/Title" | 
					
						
							|  |  |  | import ValidatedTextField from "../Input/ValidatedTextField" | 
					
						
							|  |  |  | import { SubtleButton } from "../Base/SubtleButton" | 
					
						
							|  |  |  | import Svg from "../../Svg" | 
					
						
							|  |  |  | import { LocalStorageSource } from "../../Logic/Web/LocalStorageSource" | 
					
						
							|  |  |  | import Toggle from "../Input/Toggle" | 
					
						
							|  |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig" | 
					
						
							|  |  |  | import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline" | 
					
						
							|  |  |  | import FilteredLayer from "../../Models/FilteredLayer" | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class NewNoteUi extends Toggle { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         noteLayer: FilteredLayer, | 
					
						
							|  |  |  |         isShown: UIEventSource<boolean>, | 
					
						
							|  |  |  |         state: { | 
					
						
							|  |  |  |             LastClickLocation: UIEventSource<{ lat: number; lon: number }> | 
					
						
							|  |  |  |             osmConnection: OsmConnection | 
					
						
							|  |  |  |             layoutToUse: LayoutConfig | 
					
						
							|  |  |  |             featurePipeline: FeaturePipeline | 
					
						
							|  |  |  |             selectedElement: UIEventSource<any> | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         const t = Translations.t.notes | 
					
						
							|  |  |  |         const isCreated = new UIEventSource(false) | 
					
						
							|  |  |  |         state.LastClickLocation.addCallbackAndRun((_) => isCreated.setData(false)) // Reset 'isCreated' on every click
 | 
					
						
							| 
									
										
										
										
											2022-02-12 02:53:41 +01:00
										 |  |  |         const text = ValidatedTextField.ForType("text").ConstructInputElement({ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             value: LocalStorageSource.Get("note-text"), | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |         text.SetClass("border rounded-sm border-grey-500") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const postNote = new SubtleButton(Svg.addSmall_svg().SetClass("max-h-7"), t.createNote) | 
					
						
							|  |  |  |         postNote.onClick(async () => { | 
					
						
							|  |  |  |             let txt = text.GetValue().data | 
					
						
							|  |  |  |             if (txt === undefined || txt === "") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             txt += "\n\n #MapComplete #" + state?.layoutToUse?.id | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const loc = state.LastClickLocation.data | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:51 +01:00
										 |  |  |             const id = await state?.osmConnection?.openNote(loc.lat, loc.lon, txt) | 
					
						
							|  |  |  |             const feature = { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 type: "Feature", | 
					
						
							|  |  |  |                 geometry: { | 
					
						
							|  |  |  |                     type: "Point", | 
					
						
							|  |  |  |                     coordinates: [loc.lon, loc.lat], | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:51 +01:00
										 |  |  |                 }, | 
					
						
							|  |  |  |                 properties: { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     id: "" + id.id, | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:51 +01:00
										 |  |  |                     date_created: new Date().toISOString(), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     _first_comment: txt, | 
					
						
							|  |  |  |                     comments: JSON.stringify([ | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             text: txt, | 
					
						
							|  |  |  |                             html: txt, | 
					
						
							|  |  |  |                             user: state.osmConnection?.userDetails?.data?.name, | 
					
						
							|  |  |  |                             uid: state.osmConnection?.userDetails?.data?.uid, | 
					
						
							|  |  |  |                         }, | 
					
						
							|  |  |  |                     ]), | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:51 +01:00
										 |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:51 +01:00
										 |  |  |             state?.featurePipeline?.InjectNewPoint(feature) | 
					
						
							|  |  |  |             state.selectedElement?.setData(feature) | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  |             text.GetValue().setData("") | 
					
						
							|  |  |  |             isCreated.setData(true) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         const createNoteDialog = new Combine([ | 
					
						
							|  |  |  |             new Title(t.createNoteTitle), | 
					
						
							|  |  |  |             t.createNoteIntro, | 
					
						
							|  |  |  |             text, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new Combine([ | 
					
						
							|  |  |  |                 new Toggle( | 
					
						
							|  |  |  |                     undefined, | 
					
						
							|  |  |  |                     t.warnAnonymous.SetClass("alert"), | 
					
						
							|  |  |  |                     state?.osmConnection?.isLoggedIn | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 new Toggle( | 
					
						
							|  |  |  |                     postNote, | 
					
						
							| 
									
										
										
										
											2022-02-04 00:44:51 +01:00
										 |  |  |                     t.textNeeded.SetClass("alert"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     text.GetValue().map((txt) => txt?.length > 3) | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |             ]).SetClass("flex justify-end items-center"), | 
					
						
							|  |  |  |         ]).SetClass("flex flex-col border-2 border-black rounded-xl p-4") | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 02:40:55 +01:00
										 |  |  |         const newNoteUi = new Toggle( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new Toggle(t.isCreated.SetClass("thanks"), createNoteDialog, isCreated), | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  |             undefined, | 
					
						
							|  |  |  |             new UIEventSource<boolean>(true) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-01-14 02:40:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         super( | 
					
						
							|  |  |  |             new Toggle( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 new Combine([ | 
					
						
							|  |  |  |                     t.noteLayerHasFilters.SetClass("alert"), | 
					
						
							|  |  |  |                     new SubtleButton(Svg.filter_svg(), t.disableAllNoteFilters).onClick(() => { | 
					
						
							|  |  |  |                         const filters = noteLayer.appliedFilters.data | 
					
						
							|  |  |  |                         for (const key of Array.from(filters.keys())) { | 
					
						
							|  |  |  |                             filters.set(key, undefined) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         noteLayer.appliedFilters.ping() | 
					
						
							|  |  |  |                         isShown.setData(false) | 
					
						
							|  |  |  |                     }), | 
					
						
							|  |  |  |                 ]).SetClass("flex flex-col"), | 
					
						
							| 
									
										
										
										
											2022-01-14 02:40:55 +01:00
										 |  |  |                 newNoteUi, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 noteLayer.appliedFilters.map((filters) => { | 
					
						
							| 
									
										
										
										
											2022-05-23 01:23:53 +02:00
										 |  |  |                     console.log("Applied filters for notes are: ", filters) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     return Array.from(filters.values()).some((v) => v?.currentFilter !== undefined) | 
					
						
							| 
									
										
										
										
											2022-05-23 01:23:53 +02:00
										 |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2022-01-14 02:40:55 +01:00
										 |  |  |             ), | 
					
						
							|  |  |  |             new Combine([ | 
					
						
							|  |  |  |                 t.noteLayerNotEnabled.SetClass("alert"), | 
					
						
							|  |  |  |                 new SubtleButton(Svg.layers_svg(), t.noteLayerDoEnable).onClick(() => { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     noteLayer.isDisplayed.setData(true) | 
					
						
							|  |  |  |                     isShown.setData(false) | 
					
						
							|  |  |  |                 }), | 
					
						
							| 
									
										
										
										
											2022-01-14 02:40:55 +01:00
										 |  |  |             ]).SetClass("flex flex-col"), | 
					
						
							|  |  |  |             noteLayer.isDisplayed | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-01-14 01:41:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |