| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import { FlowStep } from "./FlowStep" | 
					
						
							|  |  |  | import { Store } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import ValidatedTextField from "../Input/ValidatedTextField" | 
					
						
							|  |  |  | import { LocalStorageSource } from "../../Logic/Web/LocalStorageSource" | 
					
						
							|  |  |  | import Title from "../Base/Title" | 
					
						
							|  |  |  | import { VariableUiElement } from "../Base/VariableUIElement" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import { SubtleButton } from "../Base/SubtleButton" | 
					
						
							|  |  |  | import Svg from "../../Svg" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | export class AskMetadata | 
					
						
							|  |  |  |     extends Combine | 
					
						
							|  |  |  |     implements | 
					
						
							|  |  |  |         FlowStep<{ | 
					
						
							|  |  |  |             features: any[] | 
					
						
							|  |  |  |             wikilink: string | 
					
						
							|  |  |  |             intro: string | 
					
						
							|  |  |  |             source: string | 
					
						
							|  |  |  |             theme: string | 
					
						
							|  |  |  |         }> | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |     public readonly Value: Store<{ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         features: any[] | 
					
						
							|  |  |  |         wikilink: string | 
					
						
							|  |  |  |         intro: string | 
					
						
							|  |  |  |         source: string | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         theme: string | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     }> | 
					
						
							|  |  |  |     public readonly IsValid: Store<boolean> | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor(params: { features: any[]; theme: string }) { | 
					
						
							| 
									
										
										
										
											2022-04-14 03:01:54 +02:00
										 |  |  |         const t = Translations.t.importHelper.askMetadata | 
					
						
							| 
									
										
										
										
											2022-02-12 02:53:41 +01:00
										 |  |  |         const introduction = ValidatedTextField.ForType("text").ConstructInputElement({ | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             value: LocalStorageSource.Get("import-helper-introduction-text"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             inputStyle: "width: 100%", | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |         const wikilink = ValidatedTextField.ForType("url").ConstructInputElement({ | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             value: LocalStorageSource.Get("import-helper-wikilink-text"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             inputStyle: "width: 100%", | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 02:53:41 +01:00
										 |  |  |         const source = ValidatedTextField.ForType("string").ConstructInputElement({ | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             value: LocalStorageSource.Get("import-helper-source-text"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             inputStyle: "width: 100%", | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super([ | 
					
						
							| 
									
										
										
										
											2022-04-14 03:01:54 +02:00
										 |  |  |             new Title(t.title), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             t.intro.Subs({ count: params.features.length }), | 
					
						
							|  |  |  |             t.giveDescription, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             introduction.SetClass("w-full border border-black"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             t.giveSource, | 
					
						
							|  |  |  |             source.SetClass("w-full border border-black"), | 
					
						
							|  |  |  |             t.giveWikilink, | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             wikilink.SetClass("w-full border border-black"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new VariableUiElement( | 
					
						
							|  |  |  |                 wikilink.GetValue().map((wikilink) => { | 
					
						
							|  |  |  |                     try { | 
					
						
							|  |  |  |                         const url = new URL(wikilink) | 
					
						
							|  |  |  |                         if (url.hostname.toLowerCase() !== "wiki.openstreetmap.org") { | 
					
						
							|  |  |  |                             return t.shouldBeOsmWikilink.SetClass("alert") | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         if (url.pathname.toLowerCase() === "/wiki/main_page") { | 
					
						
							|  |  |  |                             return t.shouldNotBeHomepage.SetClass("alert") | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } catch (e) { | 
					
						
							|  |  |  |                         return t.shouldBeUrl.SetClass("alert") | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 }) | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2022-04-14 03:01:54 +02:00
										 |  |  |             t.orDownload, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new SubtleButton(Svg.download_svg(), t.downloadGeojson).OnClickWithLoading( | 
					
						
							|  |  |  |                 "Preparing your download", | 
					
						
							|  |  |  |                 async () => { | 
					
						
							| 
									
										
										
										
											2022-04-14 03:01:54 +02:00
										 |  |  |                     const geojson = { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         type: "FeatureCollection", | 
					
						
							|  |  |  |                         features: params.features, | 
					
						
							| 
									
										
										
										
											2022-04-14 03:01:54 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     Utils.offerContentsAsDownloadableFile( | 
					
						
							|  |  |  |                         JSON.stringify(geojson), | 
					
						
							|  |  |  |                         "prepared_import_" + params.theme + ".geojson", | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             mimetype: "application/vnd.geo+json", | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         this.SetClass("flex flex-col") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.Value = introduction.GetValue().map( | 
					
						
							|  |  |  |             (intro) => { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                     features: params.features, | 
					
						
							|  |  |  |                     wikilink: wikilink.GetValue().data, | 
					
						
							|  |  |  |                     intro, | 
					
						
							|  |  |  |                     source: source.GetValue().data, | 
					
						
							|  |  |  |                     theme: params.theme, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             [wikilink.GetValue(), source.GetValue()] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.IsValid = this.Value.map((obj) => { | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |             if (obj === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return false | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if ([obj.features, obj.intro, obj.wikilink, obj.source].some((v) => v === undefined)) { | 
					
						
							|  |  |  |                 return false | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |                 const url = new URL(obj.wikilink) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 if (url.hostname.toLowerCase() !== "wiki.openstreetmap.org") { | 
					
						
							|  |  |  |                     return false | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |                 return false | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return true | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |