| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  | import {UIEventSource} from "../Logic/UIEventSource"; | 
					
						
							|  |  |  | import {VariableUiElement} from "./Base/VariableUIElement"; | 
					
						
							|  |  |  | import Wikipedia from "../Logic/Web/Wikipedia"; | 
					
						
							|  |  |  | import Loading from "./Base/Loading"; | 
					
						
							|  |  |  | import {FixedUiElement} from "./Base/FixedUiElement"; | 
					
						
							|  |  |  | import Combine from "./Base/Combine"; | 
					
						
							|  |  |  | import BaseUIElement from "./BaseUIElement"; | 
					
						
							|  |  |  | import Title from "./Base/Title"; | 
					
						
							|  |  |  | import Translations from "./i18n/Translations"; | 
					
						
							|  |  |  | import Svg from "../Svg"; | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  | import Wikidata, {WikidataResponse} from "../Logic/Web/Wikidata"; | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  | import Locale from "./i18n/Locale"; | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  | import Toggle from "./Input/Toggle"; | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  | export default class WikipediaBox extends Toggle { | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(wikidataId: string | UIEventSource<string>) { | 
					
						
							|  |  |  |         const wp = Translations.t.general.wikipedia; | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |         if (typeof wikidataId === "string") { | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |             wikidataId = new UIEventSource(wikidataId) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const wikibox = wikidataId | 
					
						
							|  |  |  |             .bind(id => { | 
					
						
							|  |  |  |                 console.log("Wikidata is", id) | 
					
						
							|  |  |  |                 if(id === undefined){ | 
					
						
							|  |  |  |                     return undefined | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 console.log("Initing load WIkidataentry with id", id) | 
					
						
							|  |  |  |                 return Wikidata.LoadWikidataEntry(id); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             .map(maybewikidata => { | 
					
						
							|  |  |  |                 if (maybewikidata === undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |                     return new Loading(wp.loading.Clone()) | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |                 if (maybewikidata["error"] !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |                     return wp.failed.Clone().SetClass("alert p-4") | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |                 const wikidata = <WikidataResponse>maybewikidata["success"] | 
					
						
							|  |  |  |                 console.log("Got wikidata response", wikidata) | 
					
						
							|  |  |  |                 if (wikidata.wikisites.size === 0) { | 
					
						
							|  |  |  |                     return wp.noWikipediaPage.Clone() | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |                 const preferredLanguage = [Locale.language.data, "en", Array.from(wikidata.wikisites.keys())[0]] | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |                 let language | 
					
						
							|  |  |  |                 let pagetitle; | 
					
						
							|  |  |  |                 let i = 0 | 
					
						
							|  |  |  |                 do { | 
					
						
							|  |  |  |                     language = preferredLanguage[i] | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |                     pagetitle = wikidata.wikisites.get(language) | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |                     i++; | 
					
						
							|  |  |  |                 } while (pagetitle === undefined) | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |                 return WikipediaBox.createContents(pagetitle, language) | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |             }, [Locale.language]) | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const contents = new VariableUiElement( | 
					
						
							|  |  |  |             wikibox | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |         ).SetClass("overflow-auto normal-background rounded-lg") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |         const mainContent = new Combine([ | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |             new Combine([Svg.wikipedia_ui().SetStyle("width: 1.5rem").SetClass("mr-3"), | 
					
						
							|  |  |  |                 new Title(Translations.t.general.wikipedia.wikipediaboxTitle.Clone(), 2)]).SetClass("flex"), | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |             contents]).SetClass("block rounded-xl subtle-background m-1 p-2 flex flex-col") | 
					
						
							| 
									
										
										
										
											2021-10-03 01:57:36 +02:00
										 |  |  |             .SetStyle("max-height: inherit") | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             mainContent, | 
					
						
							|  |  |  |             undefined, | 
					
						
							|  |  |  |             wikidataId.map(id => id !== undefined) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the actual content in a scrollable way | 
					
						
							|  |  |  |      * @param pagename | 
					
						
							|  |  |  |      * @param language | 
					
						
							|  |  |  |      * @private | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private static createContents(pagename: string, language: string): BaseUIElement { | 
					
						
							|  |  |  |         const htmlContent = Wikipedia.GetArticle({ | 
					
						
							|  |  |  |             pageName: pagename, | 
					
						
							|  |  |  |             language: language | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         const wp = Translations.t.general.wikipedia | 
					
						
							|  |  |  |         const contents: UIEventSource<string | BaseUIElement> = htmlContent.map(htmlContent => { | 
					
						
							|  |  |  |             if (htmlContent === undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  |                 // Still loading
 | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |                 return new Loading(wp.loading.Clone()) | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |             if (htmlContent["success"] !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  |                 return new FixedUiElement(htmlContent["success"]).SetClass("wikipedia-article") | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |             if (htmlContent["error"]) { | 
					
						
							| 
									
										
										
										
											2021-10-03 01:38:57 +02:00
										 |  |  |                 console.warn("Loading wikipage failed due to", htmlContent["error"]) | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  |                 return wp.failed.Clone().SetClass("alert p-4") | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  |             return undefined | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return new Combine([new VariableUiElement(contents).SetClass("block pl-6 pt-2")]) | 
					
						
							|  |  |  |             .SetClass("block") | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-02 22:31:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 17:57:54 +02:00
										 |  |  | } |