| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import BaseUIElement from "../BaseUIElement" | 
					
						
							|  |  |  | import { Store, UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							| 
									
										
										
										
											2020-11-05 12:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  | export default class Link extends BaseUIElement { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _href: string | Store<string> | 
					
						
							|  |  |  |     private readonly _embeddedShow: BaseUIElement | 
					
						
							|  |  |  |     private readonly _newTab: boolean | 
					
						
							| 
									
										
										
										
											2020-11-05 12:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         embeddedShow: BaseUIElement | string, | 
					
						
							|  |  |  |         href: string | Store<string>, | 
					
						
							|  |  |  |         newTab: boolean = false | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         super() | 
					
						
							|  |  |  |         this._embeddedShow = Translations.W(embeddedShow) | 
					
						
							|  |  |  |         this._href = href | 
					
						
							|  |  |  |         this._newTab = newTab | 
					
						
							| 
									
										
										
										
											2022-01-18 18:52:42 +01:00
										 |  |  |         if (this._embeddedShow === undefined) { | 
					
						
							| 
									
										
										
										
											2022-01-14 19:34:00 +01:00
										 |  |  |             throw "Error: got a link where embeddedShow is undefined" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-01 12:51:55 +02:00
										 |  |  |         this.onClick(() => {}) | 
					
						
							| 
									
										
										
										
											2021-06-16 17:09:32 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |     public static OsmWiki(key: string, value?: string, hideKey = false) { | 
					
						
							|  |  |  |         if (value !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             let k = "" | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |             if (!hideKey) { | 
					
						
							|  |  |  |                 k = key + "=" | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return new Link( | 
					
						
							|  |  |  |                 k + value, | 
					
						
							|  |  |  |                 `https://wiki.openstreetmap.org/wiki/Tag:${key}%3D${value}`, | 
					
						
							|  |  |  |                 true | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-16 03:57:13 +02:00
										 |  |  |         return new Link(key, "https://wiki.openstreetmap.org/wiki/Key:" + key, true) | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     AsMarkdown(): string { | 
					
						
							|  |  |  |         // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return `[${this._embeddedShow.AsMarkdown()}](${this._href.data ?? this._href})` | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 17:09:32 +02:00
										 |  |  |     protected InnerConstructElement(): HTMLElement { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const embeddedShow = this._embeddedShow?.ConstructElement() | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (embeddedShow === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-06-16 17:09:32 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |         const el = document.createElement("a") | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (typeof this._href === "string") { | 
					
						
							| 
									
										
										
										
											2021-06-16 17:09:32 +02:00
										 |  |  |             el.href = this._href | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             this._href.addCallbackAndRun((href) => { | 
					
						
							|  |  |  |                 el.href = href | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-16 17:09:32 +02:00
										 |  |  |         if (this._newTab) { | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |             el.target = "_blank" | 
					
						
							| 
									
										
										
										
											2020-11-05 12:28:02 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-16 17:09:32 +02:00
										 |  |  |         el.appendChild(embeddedShow) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return el | 
					
						
							| 
									
										
										
										
											2020-11-05 12:28:02 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |