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-06 02:30:23 +02:00
|
|
|
import Link from "./Base/Link";
|
2021-10-07 22:06:47 +02:00
|
|
|
import {TabbedComponent} from "./Base/TabbedComponent";
|
2021-10-02 17:57:54 +02:00
|
|
|
|
2021-10-07 22:06:47 +02:00
|
|
|
export default class WikipediaBox extends Combine {
|
2021-10-02 22:31:16 +02:00
|
|
|
|
|
|
|
|
2021-10-07 22:06:47 +02:00
|
|
|
constructor(wikidataIds: string[]) {
|
|
|
|
|
|
|
|
const mainContents = []
|
|
|
|
|
|
|
|
const pages = wikidataIds.map(wdId => WikipediaBox.createLinkedContent(wdId))
|
|
|
|
if (wikidataIds.length == 1) {
|
|
|
|
const page = pages[0]
|
|
|
|
mainContents.push(
|
|
|
|
new Combine([
|
|
|
|
new Combine([Svg.wikipedia_ui()
|
|
|
|
.SetStyle("width: 1.5rem").SetClass("inline-block mr-3"), page.titleElement])
|
|
|
|
.SetClass("flex"),
|
|
|
|
page.linkElement
|
|
|
|
]).SetClass("flex justify-between align-middle"),
|
|
|
|
)
|
|
|
|
mainContents.push(page.contents)
|
|
|
|
} else if (wikidataIds.length > 1) {
|
|
|
|
|
|
|
|
const tabbed = new TabbedComponent(
|
|
|
|
pages.map(page => {
|
|
|
|
const contents = page.contents.SetClass("block").SetStyle("max-height: inherit; height: inherit; padding-bottom: 3.3rem")
|
|
|
|
return {
|
|
|
|
header: page.titleElement.SetClass("pl-2 pr-2"),
|
|
|
|
content: new Combine([
|
|
|
|
page.linkElement
|
|
|
|
.SetStyle("top: 2rem; right: 2.5rem;")
|
|
|
|
.SetClass("absolute subtle-background rounded-full p-3 opacity-50 hover:opacity-100 transition-opacity"),
|
|
|
|
contents
|
|
|
|
]).SetStyle("max-height: inherit; height: inherit").SetClass("relative")
|
|
|
|
}
|
|
|
|
|
|
|
|
}),
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
leftOfHeader: Svg.wikipedia_ui().SetStyle("width: 1.5rem; align-self: center;").SetClass("mr-4"),
|
|
|
|
styleHeader: header => header.SetClass("subtle-background").SetStyle("height: 3.3rem")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tabbed.SetStyle("height: inherit; max-height: inherit; overflow: hidden")
|
|
|
|
mainContents.push(tabbed)
|
|
|
|
|
2021-10-02 22:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 01:38:57 +02:00
|
|
|
|
2021-10-07 22:06:47 +02:00
|
|
|
super(mainContents)
|
|
|
|
|
|
|
|
|
|
|
|
this.SetClass("block rounded-xl subtle-background m-1 p-2 flex flex-col")
|
|
|
|
.SetStyle("max-height: inherit")
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createLinkedContent(wikidataId: string): {
|
|
|
|
titleElement: BaseUIElement,
|
|
|
|
contents: BaseUIElement,
|
|
|
|
linkElement: BaseUIElement
|
|
|
|
} {
|
|
|
|
|
|
|
|
const wp = Translations.t.general.wikipedia;
|
|
|
|
|
2021-10-06 02:30:23 +02:00
|
|
|
const wikiLink: UIEventSource<[string, string] | "loading" | "failed" | "no page"> =
|
2021-10-07 22:06:47 +02:00
|
|
|
Wikidata.LoadWikidataEntry(wikidataId)
|
2021-10-06 02:30:23 +02:00
|
|
|
.map(maybewikidata => {
|
|
|
|
if (maybewikidata === undefined) {
|
|
|
|
return "loading"
|
|
|
|
}
|
|
|
|
if (maybewikidata["error"] !== undefined) {
|
|
|
|
return "failed"
|
|
|
|
|
|
|
|
}
|
|
|
|
const wikidata = <WikidataResponse>maybewikidata["success"]
|
|
|
|
if (wikidata.wikisites.size === 0) {
|
|
|
|
return "no page"
|
|
|
|
}
|
|
|
|
|
|
|
|
const preferredLanguage = [Locale.language.data, "en", Array.from(wikidata.wikisites.keys())[0]]
|
|
|
|
let language
|
|
|
|
let pagetitle;
|
|
|
|
let i = 0
|
|
|
|
do {
|
|
|
|
language = preferredLanguage[i]
|
|
|
|
pagetitle = wikidata.wikisites.get(language)
|
|
|
|
i++;
|
|
|
|
} while (pagetitle === undefined)
|
|
|
|
return [pagetitle, language]
|
|
|
|
}, [Locale.language])
|
|
|
|
|
|
|
|
|
|
|
|
const contents = new VariableUiElement(
|
|
|
|
wikiLink.map(status => {
|
|
|
|
if (status === "loading") {
|
|
|
|
return new Loading(wp.loading.Clone()).SetClass("pl-6 pt-2")
|
2021-10-02 22:31:16 +02:00
|
|
|
}
|
2021-10-06 02:30:23 +02:00
|
|
|
|
|
|
|
if (status === "failed") {
|
2021-10-02 22:31:16 +02:00
|
|
|
return wp.failed.Clone().SetClass("alert p-4")
|
|
|
|
}
|
2021-10-06 02:30:23 +02:00
|
|
|
if (status == "no page") {
|
2021-10-03 01:38:57 +02:00
|
|
|
return wp.noWikipediaPage.Clone()
|
|
|
|
}
|
2021-10-02 22:31:16 +02:00
|
|
|
|
2021-10-06 02:30:23 +02:00
|
|
|
const [pagetitle, language] = status
|
2021-10-03 01:38:57 +02:00
|
|
|
return WikipediaBox.createContents(pagetitle, language)
|
2021-10-07 22:06:47 +02:00
|
|
|
|
2021-10-06 02:30:23 +02:00
|
|
|
})
|
2021-10-02 22:31:16 +02:00
|
|
|
).SetClass("overflow-auto normal-background rounded-lg")
|
|
|
|
|
|
|
|
|
2021-10-07 22:06:47 +02:00
|
|
|
const titleElement = new VariableUiElement(wikiLink.map(state => {
|
2021-10-06 02:30:23 +02:00
|
|
|
if (typeof state !== "string") {
|
|
|
|
const [pagetitle, language] = state
|
2021-10-07 22:06:47 +02:00
|
|
|
return new Title(pagetitle, 3)
|
|
|
|
}
|
|
|
|
//return new Title(Translations.t.general.wikipedia.wikipediaboxTitle.Clone(), 2)
|
|
|
|
return new Title(wikidataId,3)
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
const linkElement = new VariableUiElement(wikiLink.map(state => {
|
|
|
|
if (typeof state !== "string") {
|
|
|
|
const [pagetitle, language] = state
|
|
|
|
const url = `https://${language}.wikipedia.org/wiki/${pagetitle}`
|
2021-10-06 02:30:23 +02:00
|
|
|
return new Link(Svg.pop_out_ui().SetStyle("width: 1.2rem").SetClass("block "), url, true)
|
|
|
|
}
|
2021-10-07 22:06:47 +02:00
|
|
|
return undefined
|
|
|
|
}))
|
|
|
|
.SetClass("flex items-center")
|
|
|
|
|
|
|
|
return {
|
|
|
|
contents: contents,
|
|
|
|
linkElement: linkElement,
|
|
|
|
titleElement: titleElement
|
|
|
|
}
|
2021-10-02 22:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 22:06:47 +02:00
|
|
|
|
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
|
|
|
|
2021-10-06 02:30:23 +02:00
|
|
|
return new Combine([new VariableUiElement(contents)
|
|
|
|
.SetClass("block pl-6 pt-2")])
|
2021-10-02 17:57:54 +02:00
|
|
|
}
|
2021-10-02 22:31:16 +02:00
|
|
|
|
2021-10-02 17:57:54 +02:00
|
|
|
}
|