| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { VariableUiElement } from "../Base/VariableUIElement" | 
					
						
							|  |  |  | import { Store, UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import Wikidata, { WikidataResponse } from "../../Logic/Web/Wikidata" | 
					
						
							|  |  |  | import { Translation, TypedTranslation } from "../i18n/Translation" | 
					
						
							|  |  |  | import { FixedUiElement } from "../Base/FixedUiElement" | 
					
						
							|  |  |  | import Loading from "../Base/Loading" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import Img from "../Base/Img" | 
					
						
							|  |  |  | import { WikimediaImageProvider } from "../../Logic/ImageProviders/WikimediaImageProvider" | 
					
						
							|  |  |  | import Link from "../Base/Link" | 
					
						
							|  |  |  | import Svg from "../../Svg" | 
					
						
							|  |  |  | import BaseUIElement from "../BaseUIElement" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class WikidataPreviewBox extends VariableUiElement { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private static isHuman = [{ p: 31 /*is a*/, q: 5 /* human */ }] | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     // @ts-ignore
 | 
					
						
							|  |  |  |     private static extraProperties: { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         requires?: { p: number; q?: number }[] | 
					
						
							|  |  |  |         property: string | 
					
						
							|  |  |  |         display: | 
					
						
							|  |  |  |             | TypedTranslation<{ value }> | 
					
						
							|  |  |  |             | Map<string, string | (() => BaseUIElement) /*If translation: Subs({value: * })  */> | 
					
						
							| 
									
										
										
										
											2022-05-27 05:49:21 +02:00
										 |  |  |         textMode?: Map<string, string> | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     }[] = [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             requires: WikidataPreviewBox.isHuman, | 
					
						
							|  |  |  |             property: "P21", | 
					
						
							|  |  |  |             display: new Map([ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 ["Q6581097", () => Svg.gender_male_svg().SetStyle("width: 1rem; height: auto")], | 
					
						
							|  |  |  |                 ["Q6581072", () => Svg.gender_female_svg().SetStyle("width: 1rem; height: auto")], | 
					
						
							|  |  |  |                 ["Q1097630", () => Svg.gender_inter_svg().SetStyle("width: 1rem; height: auto")], | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     "Q1052281", | 
					
						
							|  |  |  |                     () => | 
					
						
							|  |  |  |                         Svg.gender_trans_svg().SetStyle( | 
					
						
							|  |  |  |                             "width: 1rem; height: auto" | 
					
						
							|  |  |  |                         ) /*'transwomen'*/, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     "Q2449503", | 
					
						
							|  |  |  |                     () => | 
					
						
							|  |  |  |                         Svg.gender_trans_svg().SetStyle("width: 1rem; height: auto") /*'transmen'*/, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 ["Q48270", () => Svg.gender_queer_svg().SetStyle("width: 1rem; height: auto")], | 
					
						
							| 
									
										
										
										
											2022-05-27 05:49:21 +02:00
										 |  |  |             ]), | 
					
						
							|  |  |  |             textMode: new Map([ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 ["Q6581097", "♂️"], | 
					
						
							|  |  |  |                 ["Q6581072", "♀️"], | 
					
						
							|  |  |  |                 ["Q1097630", "⚥️"], | 
					
						
							|  |  |  |                 ["Q1052281", "🏳️⚧️" /*'transwomen'*/], | 
					
						
							|  |  |  |                 ["Q2449503", "🏳️⚧️" /*'transmen'*/], | 
					
						
							|  |  |  |                 ["Q48270", "🏳️🌈 ⚧"], | 
					
						
							|  |  |  |             ]), | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             property: "P569", | 
					
						
							|  |  |  |             requires: WikidataPreviewBox.isHuman, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             display: Translations.t.general.wikipedia.previewbox.born, | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             property: "P570", | 
					
						
							|  |  |  |             requires: WikidataPreviewBox.isHuman, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             display: Translations.t.general.wikipedia.previewbox.died, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         wikidataId: Store<string>, | 
					
						
							|  |  |  |         options?: { | 
					
						
							|  |  |  |             noImages?: boolean | 
					
						
							|  |  |  |             imageStyle?: string | 
					
						
							|  |  |  |             whileLoading?: BaseUIElement | string | 
					
						
							|  |  |  |             extraItems?: (BaseUIElement | string)[] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         let inited = false | 
					
						
							|  |  |  |         const wikidata = wikidataId.stabilized(250).bind((id) => { | 
					
						
							|  |  |  |             if (id === undefined || id === "" || id === "Q") { | 
					
						
							|  |  |  |                 return null | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             inited = true | 
					
						
							|  |  |  |             return Wikidata.LoadWikidataEntry(id) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super( | 
					
						
							|  |  |  |             wikidata.map((maybeWikidata) => { | 
					
						
							|  |  |  |                 if (maybeWikidata === null || !inited) { | 
					
						
							|  |  |  |                     return options?.whileLoading | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 if (maybeWikidata === undefined) { | 
					
						
							|  |  |  |                     return new Loading(Translations.t.general.loading) | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 if (maybeWikidata["error"] !== undefined) { | 
					
						
							|  |  |  |                     return new FixedUiElement(maybeWikidata["error"]).SetClass("alert") | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const wikidata = <WikidataResponse>maybeWikidata["success"] | 
					
						
							|  |  |  |                 return WikidataPreviewBox.WikidataResponsePreview(wikidata, options) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static WikidataResponsePreview( | 
					
						
							|  |  |  |         wikidata: WikidataResponse, | 
					
						
							|  |  |  |         options?: { | 
					
						
							|  |  |  |             noImages?: boolean | 
					
						
							|  |  |  |             imageStyle?: string | 
					
						
							|  |  |  |             extraItems?: (BaseUIElement | string)[] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |         let link = new Link( | 
					
						
							|  |  |  |             new Combine([ | 
					
						
							|  |  |  |                 wikidata.id, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 options?.noImages | 
					
						
							|  |  |  |                     ? wikidata.id | 
					
						
							|  |  |  |                     : Svg.wikidata_svg().SetStyle("width: 2.5rem").SetClass("block"), | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |             ]).SetClass("flex"), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             Wikidata.IdToArticle(wikidata.id), | 
					
						
							|  |  |  |             true | 
					
						
							|  |  |  |         )?.SetClass("must-link") | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let info = new Combine([ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new Combine([ | 
					
						
							|  |  |  |                 Translation.fromMap(wikidata.labels)?.SetClass("font-bold"), | 
					
						
							|  |  |  |                 link, | 
					
						
							|  |  |  |             ]).SetClass("flex justify-between"), | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |             Translation.fromMap(wikidata.descriptions), | 
					
						
							| 
									
										
										
										
											2022-08-17 02:42:59 +02:00
										 |  |  |             WikidataPreviewBox.QuickFacts(wikidata, options), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             ...(options?.extraItems ?? []), | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |         ]).SetClass("flex flex-col link-underline") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let imageUrl = undefined | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |         if (wikidata.claims.get("P18")?.size > 0) { | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |             imageUrl = Array.from(wikidata.claims.get("P18"))[0] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-05-27 05:49:21 +02:00
										 |  |  |         if (imageUrl && !options?.noImages) { | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |             imageUrl = WikimediaImageProvider.singleton.PrepUrl(imageUrl).url | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             info = new Combine([ | 
					
						
							|  |  |  |                 new Img(imageUrl) | 
					
						
							|  |  |  |                     .SetStyle(options?.imageStyle ?? "max-width: 5rem; width: unset; height: 4rem") | 
					
						
							|  |  |  |                     .SetClass("rounded-xl mr-2"), | 
					
						
							|  |  |  |                 info.SetClass("w-full"), | 
					
						
							|  |  |  |             ]).SetClass("flex") | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         info.SetClass("p-2 w-full") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return info | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static QuickFacts( | 
					
						
							|  |  |  |         wikidata: WikidataResponse, | 
					
						
							|  |  |  |         options?: { noImages?: boolean } | 
					
						
							|  |  |  |     ): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         const els: BaseUIElement[] = [] | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |         for (const extraProperty of WikidataPreviewBox.extraProperties) { | 
					
						
							| 
									
										
										
										
											2021-10-25 14:02:32 +02:00
										 |  |  |             let hasAllRequirements = true | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |             for (const requirement of extraProperty.requires) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (!wikidata.claims?.has("P" + requirement.p)) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     hasAllRequirements = false | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                     break | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (!wikidata.claims?.get("P" + requirement.p).has("Q" + requirement.q)) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     hasAllRequirements = false | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                     break | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (!hasAllRequirements) { | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |             const key = extraProperty.property | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const display = | 
					
						
							|  |  |  |                 (options?.noImages ? extraProperty.textMode : extraProperty.display) ?? | 
					
						
							|  |  |  |                 extraProperty.display | 
					
						
							| 
									
										
										
										
											2021-12-23 23:44:40 +01:00
										 |  |  |             if (wikidata.claims?.get(key) === undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-23 23:44:40 +01:00
										 |  |  |             const value: string[] = Array.from(wikidata.claims.get(key)) | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (display instanceof Translation) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 els.push(display.Subs({ value: value.join(", ") }).SetClass("m-2")) | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const constructors = Utils.NoNull(value.map((property) => display.get(property))) | 
					
						
							|  |  |  |             const elems = constructors.map((v) => { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (typeof v === "string") { | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                     return new FixedUiElement(v) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     return v() | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             els.push(new Combine(elems).SetClass("flex m-2")) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (els.length === 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 20:40:24 +02:00
										 |  |  |         return new Combine(els).SetClass("flex") | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |