Reformat all files with prettier
This commit is contained in:
parent
e22d189376
commit
b541d3eab4
382 changed files with 50893 additions and 35566 deletions
|
@ -1,125 +1,149 @@
|
|||
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";
|
||||
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"
|
||||
|
||||
export default class WikidataPreviewBox extends VariableUiElement {
|
||||
|
||||
private static isHuman = [
|
||||
{p: 31/*is a*/, q: 5 /* human */},
|
||||
]
|
||||
private static isHuman = [{ p: 31 /*is a*/, q: 5 /* human */ }]
|
||||
// @ts-ignore
|
||||
private static extraProperties: {
|
||||
requires?: { p: number, q?: number }[],
|
||||
property: string,
|
||||
display: TypedTranslation<{value}> | Map<string, string | (() => BaseUIElement) /*If translation: Subs({value: * }) */>,
|
||||
requires?: { p: number; q?: number }[]
|
||||
property: string
|
||||
display:
|
||||
| TypedTranslation<{ value }>
|
||||
| Map<string, string | (() => BaseUIElement) /*If translation: Subs({value: * }) */>
|
||||
textMode?: Map<string, string>
|
||||
}[] = [
|
||||
{
|
||||
requires: WikidataPreviewBox.isHuman,
|
||||
property: "P21",
|
||||
display: new Map([
|
||||
['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")]
|
||||
["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")],
|
||||
]),
|
||||
textMode: new Map([
|
||||
['Q6581097', "♂️"],
|
||||
['Q6581072', "♀️"],
|
||||
['Q1097630', "⚥️"],
|
||||
['Q1052281', "🏳️⚧️"/*'transwomen'*/],
|
||||
['Q2449503', "🏳️⚧️"/*'transmen'*/],
|
||||
['Q48270', "🏳️🌈 ⚧"]
|
||||
])
|
||||
["Q6581097", "♂️"],
|
||||
["Q6581072", "♀️"],
|
||||
["Q1097630", "⚥️"],
|
||||
["Q1052281", "🏳️⚧️" /*'transwomen'*/],
|
||||
["Q2449503", "🏳️⚧️" /*'transmen'*/],
|
||||
["Q48270", "🏳️🌈 ⚧"],
|
||||
]),
|
||||
},
|
||||
{
|
||||
property: "P569",
|
||||
requires: WikidataPreviewBox.isHuman,
|
||||
display: Translations.t.general.wikipedia.previewbox.born
|
||||
display: Translations.t.general.wikipedia.previewbox.born,
|
||||
},
|
||||
{
|
||||
property: "P570",
|
||||
requires: WikidataPreviewBox.isHuman,
|
||||
display:Translations.t.general.wikipedia.previewbox.died
|
||||
}
|
||||
display: Translations.t.general.wikipedia.previewbox.died,
|
||||
},
|
||||
]
|
||||
|
||||
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;
|
||||
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
|
||||
}
|
||||
inited = true
|
||||
return Wikidata.LoadWikidataEntry(id)
|
||||
})
|
||||
|
||||
super(
|
||||
wikidata.map((maybeWikidata) => {
|
||||
if (maybeWikidata === null || !inited) {
|
||||
return options?.whileLoading
|
||||
}
|
||||
inited = true;
|
||||
return Wikidata.LoadWikidataEntry(id)
|
||||
|
||||
if (maybeWikidata === undefined) {
|
||||
return new Loading(Translations.t.general.loading)
|
||||
}
|
||||
|
||||
if (maybeWikidata["error"] !== undefined) {
|
||||
return new FixedUiElement(maybeWikidata["error"]).SetClass("alert")
|
||||
}
|
||||
const wikidata = <WikidataResponse>maybeWikidata["success"]
|
||||
return WikidataPreviewBox.WikidataResponsePreview(wikidata, options)
|
||||
})
|
||||
|
||||
super(wikidata.map(maybeWikidata => {
|
||||
if (maybeWikidata === null || !inited) {
|
||||
return options?.whileLoading;
|
||||
}
|
||||
|
||||
if (maybeWikidata === undefined) {
|
||||
return new Loading(Translations.t.general.loading)
|
||||
}
|
||||
|
||||
if (maybeWikidata["error"] !== undefined) {
|
||||
return new FixedUiElement(maybeWikidata["error"]).SetClass("alert")
|
||||
}
|
||||
const wikidata = <WikidataResponse>maybeWikidata["success"]
|
||||
return WikidataPreviewBox.WikidataResponsePreview(wikidata, options)
|
||||
}))
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
public static WikidataResponsePreview(wikidata: WikidataResponse, options?: {
|
||||
noImages?: boolean,
|
||||
imageStyle?: string,
|
||||
extraItems?: (BaseUIElement | string)[]}): BaseUIElement {
|
||||
public static WikidataResponsePreview(
|
||||
wikidata: WikidataResponse,
|
||||
options?: {
|
||||
noImages?: boolean
|
||||
imageStyle?: string
|
||||
extraItems?: (BaseUIElement | string)[]
|
||||
}
|
||||
): BaseUIElement {
|
||||
let link = new Link(
|
||||
new Combine([
|
||||
wikidata.id,
|
||||
options?.noImages ? wikidata.id : Svg.wikidata_svg().SetStyle("width: 2.5rem").SetClass("block")
|
||||
options?.noImages
|
||||
? wikidata.id
|
||||
: Svg.wikidata_svg().SetStyle("width: 2.5rem").SetClass("block"),
|
||||
]).SetClass("flex"),
|
||||
Wikidata.IdToArticle(wikidata.id), true)?.SetClass("must-link")
|
||||
Wikidata.IdToArticle(wikidata.id),
|
||||
true
|
||||
)?.SetClass("must-link")
|
||||
|
||||
let info = new Combine([
|
||||
new Combine(
|
||||
[Translation.fromMap(wikidata.labels)?.SetClass("font-bold"),
|
||||
link]).SetClass("flex justify-between"),
|
||||
new Combine([
|
||||
Translation.fromMap(wikidata.labels)?.SetClass("font-bold"),
|
||||
link,
|
||||
]).SetClass("flex justify-between"),
|
||||
Translation.fromMap(wikidata.descriptions),
|
||||
WikidataPreviewBox.QuickFacts(wikidata, options),
|
||||
...(options?.extraItems ?? [])
|
||||
...(options?.extraItems ?? []),
|
||||
]).SetClass("flex flex-col link-underline")
|
||||
|
||||
|
||||
let imageUrl = undefined
|
||||
if (wikidata.claims.get("P18")?.size > 0) {
|
||||
imageUrl = Array.from(wikidata.claims.get("P18"))[0]
|
||||
}
|
||||
if (imageUrl && !options?.noImages) {
|
||||
imageUrl = WikimediaImageProvider.singleton.PrepUrl(imageUrl).url
|
||||
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")
|
||||
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")
|
||||
}
|
||||
|
||||
info.SetClass("p-2 w-full")
|
||||
|
@ -127,18 +151,20 @@ export default class WikidataPreviewBox extends VariableUiElement {
|
|||
return info
|
||||
}
|
||||
|
||||
public static QuickFacts(wikidata: WikidataResponse, options?: {noImages?: boolean}): BaseUIElement {
|
||||
|
||||
public static QuickFacts(
|
||||
wikidata: WikidataResponse,
|
||||
options?: { noImages?: boolean }
|
||||
): BaseUIElement {
|
||||
const els: BaseUIElement[] = []
|
||||
for (const extraProperty of WikidataPreviewBox.extraProperties) {
|
||||
let hasAllRequirements = true
|
||||
for (const requirement of extraProperty.requires) {
|
||||
if (!wikidata.claims?.has("P" + requirement.p)) {
|
||||
hasAllRequirements = false;
|
||||
hasAllRequirements = false
|
||||
break
|
||||
}
|
||||
if (!wikidata.claims?.get("P" + requirement.p).has("Q" + requirement.q)) {
|
||||
hasAllRequirements = false;
|
||||
hasAllRequirements = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -147,31 +173,32 @@ export default class WikidataPreviewBox extends VariableUiElement {
|
|||
}
|
||||
|
||||
const key = extraProperty.property
|
||||
const display = (options?.noImages ? extraProperty.textMode: extraProperty.display) ?? extraProperty.display
|
||||
const display =
|
||||
(options?.noImages ? extraProperty.textMode : extraProperty.display) ??
|
||||
extraProperty.display
|
||||
if (wikidata.claims?.get(key) === undefined) {
|
||||
continue
|
||||
}
|
||||
const value: string[] = Array.from(wikidata.claims.get(key))
|
||||
|
||||
if (display instanceof Translation) {
|
||||
els.push(display.Subs({value: value.join(", ")}).SetClass("m-2"))
|
||||
els.push(display.Subs({ value: value.join(", ") }).SetClass("m-2"))
|
||||
continue
|
||||
}
|
||||
const constructors = Utils.NoNull(value.map(property => display.get(property)))
|
||||
const elems = constructors.map(v => {
|
||||
const constructors = Utils.NoNull(value.map((property) => display.get(property)))
|
||||
const elems = constructors.map((v) => {
|
||||
if (typeof v === "string") {
|
||||
return new FixedUiElement(v)
|
||||
} else {
|
||||
return v();
|
||||
return v()
|
||||
}
|
||||
})
|
||||
els.push(new Combine(elems).SetClass("flex m-2"))
|
||||
}
|
||||
if (els.length === 0) {
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
|
||||
return new Combine(els).SetClass("flex")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,142 +1,150 @@
|
|||
import Combine from "../Base/Combine";
|
||||
import {InputElement} from "../Input/InputElement";
|
||||
import {TextField} from "../Input/TextField";
|
||||
import Translations from "../i18n/Translations";
|
||||
import {ImmutableStore, Store, Stores, UIEventSource} from "../../Logic/UIEventSource";
|
||||
import Wikidata, {WikidataResponse} from "../../Logic/Web/Wikidata";
|
||||
import Locale from "../i18n/Locale";
|
||||
import {VariableUiElement} from "../Base/VariableUIElement";
|
||||
import WikidataPreviewBox from "./WikidataPreviewBox";
|
||||
import Title from "../Base/Title";
|
||||
import WikipediaBox from "./WikipediaBox";
|
||||
import Svg from "../../Svg";
|
||||
import Loading from "../Base/Loading";
|
||||
import Combine from "../Base/Combine"
|
||||
import { InputElement } from "../Input/InputElement"
|
||||
import { TextField } from "../Input/TextField"
|
||||
import Translations from "../i18n/Translations"
|
||||
import { ImmutableStore, Store, Stores, UIEventSource } from "../../Logic/UIEventSource"
|
||||
import Wikidata, { WikidataResponse } from "../../Logic/Web/Wikidata"
|
||||
import Locale from "../i18n/Locale"
|
||||
import { VariableUiElement } from "../Base/VariableUIElement"
|
||||
import WikidataPreviewBox from "./WikidataPreviewBox"
|
||||
import Title from "../Base/Title"
|
||||
import WikipediaBox from "./WikipediaBox"
|
||||
import Svg from "../../Svg"
|
||||
import Loading from "../Base/Loading"
|
||||
|
||||
export default class WikidataSearchBox extends InputElement<string> {
|
||||
|
||||
private static readonly _searchCache = new Map<string, Promise<WikidataResponse[]>>()
|
||||
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
||||
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false)
|
||||
private readonly wikidataId: UIEventSource<string>
|
||||
private readonly searchText: UIEventSource<string>
|
||||
private readonly instanceOf?: number[];
|
||||
private readonly notInstanceOf?: number[];
|
||||
private readonly instanceOf?: number[]
|
||||
private readonly notInstanceOf?: number[]
|
||||
|
||||
constructor(options?: {
|
||||
searchText?: UIEventSource<string>,
|
||||
value?: UIEventSource<string>,
|
||||
notInstanceOf?: number[],
|
||||
searchText?: UIEventSource<string>
|
||||
value?: UIEventSource<string>
|
||||
notInstanceOf?: number[]
|
||||
instanceOf?: number[]
|
||||
}) {
|
||||
super();
|
||||
super()
|
||||
this.searchText = options?.searchText
|
||||
this.wikidataId = options?.value ?? new UIEventSource<string>(undefined);
|
||||
this.wikidataId = options?.value ?? new UIEventSource<string>(undefined)
|
||||
this.instanceOf = options?.instanceOf
|
||||
this.notInstanceOf = options?.notInstanceOf
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<string> {
|
||||
return this.wikidataId;
|
||||
return this.wikidataId
|
||||
}
|
||||
|
||||
IsValid(t: string): boolean {
|
||||
return t.startsWith("Q") && !isNaN(Number(t.substring(1)));
|
||||
return t.startsWith("Q") && !isNaN(Number(t.substring(1)))
|
||||
}
|
||||
|
||||
protected InnerConstructElement(): HTMLElement {
|
||||
|
||||
const searchField = new TextField({
|
||||
placeholder: Translations.t.general.wikipedia.searchWikidata,
|
||||
value: this.searchText,
|
||||
inputStyle: "width: calc(100% - 0.5rem); border: 1px solid black"
|
||||
|
||||
inputStyle: "width: calc(100% - 0.5rem); border: 1px solid black",
|
||||
})
|
||||
const selectedWikidataId = this.wikidataId
|
||||
|
||||
const tooShort = new ImmutableStore<{success: WikidataResponse[]}>({success: undefined})
|
||||
const searchResult: Store<{success?: WikidataResponse[], error?: any}> = searchField.GetValue().bind(
|
||||
searchText => {
|
||||
const tooShort = new ImmutableStore<{ success: WikidataResponse[] }>({ success: undefined })
|
||||
const searchResult: Store<{ success?: WikidataResponse[]; error?: any }> = searchField
|
||||
.GetValue()
|
||||
.bind((searchText) => {
|
||||
if (searchText.length < 3) {
|
||||
return tooShort;
|
||||
return tooShort
|
||||
}
|
||||
const lang = Locale.language.data
|
||||
const key = lang + ":" + searchText
|
||||
let promise = WikidataSearchBox._searchCache.get(key)
|
||||
if (promise === undefined) {
|
||||
promise = Wikidata.searchAndFetch(searchText, {
|
||||
lang,
|
||||
maxCount: 5,
|
||||
notInstanceOf: this.notInstanceOf,
|
||||
instanceOf: this.instanceOf
|
||||
}
|
||||
)
|
||||
lang,
|
||||
maxCount: 5,
|
||||
notInstanceOf: this.notInstanceOf,
|
||||
instanceOf: this.instanceOf,
|
||||
})
|
||||
WikidataSearchBox._searchCache.set(key, promise)
|
||||
}
|
||||
return Stores.FromPromiseWithErr(promise)
|
||||
}
|
||||
)
|
||||
|
||||
})
|
||||
|
||||
const previews = new VariableUiElement(searchResult.map(searchResultsOrFail => {
|
||||
|
||||
if (searchField.GetValue().data.length === 0) {
|
||||
return Translations.t.general.wikipedia.doSearch
|
||||
}
|
||||
|
||||
if (searchField.GetValue().data.length < 3) {
|
||||
return Translations.t.general.wikipedia.searchToShort
|
||||
}
|
||||
|
||||
if( searchResultsOrFail === undefined) {
|
||||
return new Loading(Translations.t.general.loading)
|
||||
}
|
||||
|
||||
if (searchResultsOrFail.error !== undefined) {
|
||||
return new Combine([Translations.t.general.wikipedia.failed.Clone().SetClass("alert"), searchResultsOrFail.error])
|
||||
}
|
||||
|
||||
|
||||
const searchResults = searchResultsOrFail.success;
|
||||
if (searchResults.length === 0) {
|
||||
return Translations.t.general.wikipedia.noResults.Subs({search: searchField.GetValue().data ?? ""})
|
||||
}
|
||||
|
||||
|
||||
return new Combine(searchResults.map(wikidataresponse => {
|
||||
const el = WikidataPreviewBox.WikidataResponsePreview(wikidataresponse).SetClass("rounded-xl p-1 sm:p-2 md:p-3 m-px border-2 sm:border-4 transition-colors")
|
||||
el.onClick(() => {
|
||||
selectedWikidataId.setData(wikidataresponse.id)
|
||||
})
|
||||
selectedWikidataId.addCallbackAndRunD(selected => {
|
||||
if (selected === wikidataresponse.id) {
|
||||
el.SetClass("subtle-background border-attention")
|
||||
} else {
|
||||
el.RemoveClass("subtle-background")
|
||||
el.RemoveClass("border-attention")
|
||||
const previews = new VariableUiElement(
|
||||
searchResult.map(
|
||||
(searchResultsOrFail) => {
|
||||
if (searchField.GetValue().data.length === 0) {
|
||||
return Translations.t.general.wikipedia.doSearch
|
||||
}
|
||||
})
|
||||
return el;
|
||||
|
||||
})).SetClass("flex flex-col")
|
||||
if (searchField.GetValue().data.length < 3) {
|
||||
return Translations.t.general.wikipedia.searchToShort
|
||||
}
|
||||
|
||||
}, [searchField.GetValue()]))
|
||||
if (searchResultsOrFail === undefined) {
|
||||
return new Loading(Translations.t.general.loading)
|
||||
}
|
||||
|
||||
if (searchResultsOrFail.error !== undefined) {
|
||||
return new Combine([
|
||||
Translations.t.general.wikipedia.failed.Clone().SetClass("alert"),
|
||||
searchResultsOrFail.error,
|
||||
])
|
||||
}
|
||||
|
||||
const searchResults = searchResultsOrFail.success
|
||||
if (searchResults.length === 0) {
|
||||
return Translations.t.general.wikipedia.noResults.Subs({
|
||||
search: searchField.GetValue().data ?? "",
|
||||
})
|
||||
}
|
||||
|
||||
return new Combine(
|
||||
searchResults.map((wikidataresponse) => {
|
||||
const el = WikidataPreviewBox.WikidataResponsePreview(
|
||||
wikidataresponse
|
||||
).SetClass(
|
||||
"rounded-xl p-1 sm:p-2 md:p-3 m-px border-2 sm:border-4 transition-colors"
|
||||
)
|
||||
el.onClick(() => {
|
||||
selectedWikidataId.setData(wikidataresponse.id)
|
||||
})
|
||||
selectedWikidataId.addCallbackAndRunD((selected) => {
|
||||
if (selected === wikidataresponse.id) {
|
||||
el.SetClass("subtle-background border-attention")
|
||||
} else {
|
||||
el.RemoveClass("subtle-background")
|
||||
el.RemoveClass("border-attention")
|
||||
}
|
||||
})
|
||||
return el
|
||||
})
|
||||
).SetClass("flex flex-col")
|
||||
},
|
||||
[searchField.GetValue()]
|
||||
)
|
||||
)
|
||||
|
||||
const full = new Combine([
|
||||
new Title(Translations.t.general.wikipedia.searchWikidata, 3).SetClass("m-2"),
|
||||
new Combine([
|
||||
Svg.search_ui().SetStyle("width: 1.5rem"),
|
||||
searchField.SetClass("m-2 w-full")]).SetClass("flex"),
|
||||
previews
|
||||
searchField.SetClass("m-2 w-full"),
|
||||
]).SetClass("flex"),
|
||||
previews,
|
||||
]).SetClass("flex flex-col border-2 border-black rounded-xl m-2 p-2")
|
||||
|
||||
return new Combine([
|
||||
new VariableUiElement(selectedWikidataId.map(wid => {
|
||||
if (wid === undefined) {
|
||||
return undefined
|
||||
}
|
||||
return new WikipediaBox(wid.split(";"));
|
||||
})).SetStyle("max-height:12.5rem"),
|
||||
full
|
||||
]).ConstructElement();
|
||||
new VariableUiElement(
|
||||
selectedWikidataId.map((wid) => {
|
||||
if (wid === undefined) {
|
||||
return undefined
|
||||
}
|
||||
return new WikipediaBox(wid.split(";"))
|
||||
})
|
||||
).SetStyle("max-height:12.5rem"),
|
||||
full,
|
||||
]).ConstructElement()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,83 +1,99 @@
|
|||
import BaseUIElement from "../BaseUIElement";
|
||||
import Locale from "../i18n/Locale";
|
||||
import {VariableUiElement} from "../Base/VariableUIElement";
|
||||
import {Translation} from "../i18n/Translation";
|
||||
import Svg from "../../Svg";
|
||||
import Combine from "../Base/Combine";
|
||||
import Title from "../Base/Title";
|
||||
import Wikipedia from "../../Logic/Web/Wikipedia";
|
||||
import Wikidata, {WikidataResponse} from "../../Logic/Web/Wikidata";
|
||||
import {TabbedComponent} from "../Base/TabbedComponent";
|
||||
import {Store, UIEventSource} from "../../Logic/UIEventSource";
|
||||
import Loading from "../Base/Loading";
|
||||
import {FixedUiElement} from "../Base/FixedUiElement";
|
||||
import Translations from "../i18n/Translations";
|
||||
import Link from "../Base/Link";
|
||||
import WikidataPreviewBox from "./WikidataPreviewBox";
|
||||
import {Paragraph} from "../Base/Paragraph";
|
||||
import BaseUIElement from "../BaseUIElement"
|
||||
import Locale from "../i18n/Locale"
|
||||
import { VariableUiElement } from "../Base/VariableUIElement"
|
||||
import { Translation } from "../i18n/Translation"
|
||||
import Svg from "../../Svg"
|
||||
import Combine from "../Base/Combine"
|
||||
import Title from "../Base/Title"
|
||||
import Wikipedia from "../../Logic/Web/Wikipedia"
|
||||
import Wikidata, { WikidataResponse } from "../../Logic/Web/Wikidata"
|
||||
import { TabbedComponent } from "../Base/TabbedComponent"
|
||||
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
||||
import Loading from "../Base/Loading"
|
||||
import { FixedUiElement } from "../Base/FixedUiElement"
|
||||
import Translations from "../i18n/Translations"
|
||||
import Link from "../Base/Link"
|
||||
import WikidataPreviewBox from "./WikidataPreviewBox"
|
||||
import { Paragraph } from "../Base/Paragraph"
|
||||
|
||||
export interface WikipediaBoxOptions {
|
||||
addHeader: boolean,
|
||||
firstParagraphOnly: boolean,
|
||||
addHeader: boolean
|
||||
firstParagraphOnly: boolean
|
||||
noImages: boolean
|
||||
currentState?: UIEventSource<"loading" | "loaded" | "error">
|
||||
}
|
||||
|
||||
export default class WikipediaBox extends Combine {
|
||||
|
||||
constructor(wikidataIds: string[], options?: WikipediaBoxOptions) {
|
||||
const mainContents = []
|
||||
options = options??{addHeader: false, firstParagraphOnly: true, noImages: false};
|
||||
const pages = wikidataIds.map(entry => WikipediaBox.createLinkedContent(entry.trim(), options))
|
||||
options = options ?? { addHeader: false, firstParagraphOnly: true, noImages: false }
|
||||
const pages = wikidataIds.map((entry) =>
|
||||
WikipediaBox.createLinkedContent(entry.trim(), options)
|
||||
)
|
||||
if (wikidataIds.length == 1) {
|
||||
const page = pages[0]
|
||||
mainContents.push(
|
||||
new Combine([
|
||||
new Combine([
|
||||
options.noImages ? undefined : Svg.wikipedia_ui().SetStyle("width: 1.5rem").SetClass("inline-block mr-3"),
|
||||
page.titleElement]).SetClass("flex"),
|
||||
page.linkElement
|
||||
]).SetClass("flex justify-between align-middle"),
|
||||
options.noImages
|
||||
? undefined
|
||||
: 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.SetClass("overflow-auto normal-background rounded-lg"))
|
||||
} else if (wikidataIds.length > 1) {
|
||||
|
||||
const tabbed = new TabbedComponent(
|
||||
pages.map(page => {
|
||||
const contents = page.contents.SetClass("overflow-auto normal-background rounded-lg block").SetStyle("max-height: inherit; height: inherit; padding-bottom: 3.3rem")
|
||||
pages.map((page) => {
|
||||
const contents = page.contents
|
||||
.SetClass("overflow-auto normal-background rounded-lg 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")
|
||||
.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: options.noImages ? undefined : Svg.wikipedia_svg().SetStyle("width: 1.5rem; align-self: center;").SetClass("mr-4"),
|
||||
styleHeader: header => header.SetClass("subtle-background").SetStyle("height: 3.3rem")
|
||||
leftOfHeader: options.noImages
|
||||
? undefined
|
||||
: Svg.wikipedia_svg()
|
||||
.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)
|
||||
|
||||
}
|
||||
|
||||
|
||||
super(mainContents)
|
||||
|
||||
|
||||
this.SetClass("block rounded-xl subtle-background m-1 p-2 flex flex-col")
|
||||
.SetStyle("max-height: inherit")
|
||||
this.SetClass("block rounded-xl subtle-background m-1 p-2 flex flex-col").SetStyle(
|
||||
"max-height: inherit"
|
||||
)
|
||||
}
|
||||
|
||||
private static createLinkedContent(entry: string, options: WikipediaBoxOptions): {
|
||||
titleElement: BaseUIElement,
|
||||
contents: BaseUIElement,
|
||||
private static createLinkedContent(
|
||||
entry: string,
|
||||
options: WikipediaBoxOptions
|
||||
): {
|
||||
titleElement: BaseUIElement
|
||||
contents: BaseUIElement
|
||||
linkElement: BaseUIElement
|
||||
} {
|
||||
if (entry.match("[qQ][0-9]+")) {
|
||||
|
@ -90,77 +106,94 @@ export default class WikipediaBox extends Combine {
|
|||
/**
|
||||
* Given a '<language>:<article-name>'-string, constructs the wikipedia article
|
||||
*/
|
||||
private static createWikipediabox(wikipediaArticle: string, options: WikipediaBoxOptions): {
|
||||
titleElement: BaseUIElement,
|
||||
contents: BaseUIElement,
|
||||
private static createWikipediabox(
|
||||
wikipediaArticle: string,
|
||||
options: WikipediaBoxOptions
|
||||
): {
|
||||
titleElement: BaseUIElement
|
||||
contents: BaseUIElement
|
||||
linkElement: BaseUIElement
|
||||
} {
|
||||
const wp = Translations.t.general.wikipedia;
|
||||
const wp = Translations.t.general.wikipedia
|
||||
|
||||
const article = Wikipedia.extractLanguageAndName(wikipediaArticle)
|
||||
if (article === undefined) {
|
||||
return {
|
||||
titleElement: undefined,
|
||||
contents: wp.noWikipediaPage,
|
||||
linkElement: undefined
|
||||
linkElement: undefined,
|
||||
}
|
||||
}
|
||||
const wikipedia = new Wikipedia({language: article.language})
|
||||
const wikipedia = new Wikipedia({ language: article.language })
|
||||
const url = wikipedia.getPageUrl(article.pageName)
|
||||
const linkElement = new Link(Svg.pop_out_svg().SetStyle("width: 1.2rem").SetClass("block "), url, true) .SetClass("flex items-center enable-links")
|
||||
const linkElement = new Link(
|
||||
Svg.pop_out_svg().SetStyle("width: 1.2rem").SetClass("block "),
|
||||
url,
|
||||
true
|
||||
).SetClass("flex items-center enable-links")
|
||||
|
||||
return {
|
||||
titleElement: new Title(article.pageName, 3),
|
||||
contents: WikipediaBox.createContents(article.pageName, wikipedia, options),
|
||||
linkElement
|
||||
linkElement,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a `Q1234`, constructs a wikipedia box (if a wikipedia page is available) or wikidata box as fallback.
|
||||
*
|
||||
*
|
||||
*/
|
||||
private static createWikidatabox(wikidataId: string, options: WikipediaBoxOptions): {
|
||||
titleElement: BaseUIElement,
|
||||
contents: BaseUIElement,
|
||||
private static createWikidatabox(
|
||||
wikidataId: string,
|
||||
options: WikipediaBoxOptions
|
||||
): {
|
||||
titleElement: BaseUIElement
|
||||
contents: BaseUIElement
|
||||
linkElement: BaseUIElement
|
||||
} {
|
||||
const wp = Translations.t.general.wikipedia
|
||||
|
||||
const wp = Translations.t.general.wikipedia;
|
||||
|
||||
const wikiLink: Store<[string, string, WikidataResponse] | "loading" | "failed" | ["no page", WikidataResponse]> =
|
||||
Wikidata.LoadWikidataEntry(wikidataId)
|
||||
.map(maybewikidata => {
|
||||
if (maybewikidata === undefined) {
|
||||
return "loading"
|
||||
}
|
||||
if (maybewikidata["error"] !== undefined) {
|
||||
return "failed"
|
||||
|
||||
}
|
||||
const wikidata = <WikidataResponse>maybewikidata["success"]
|
||||
if (wikidata === undefined) {
|
||||
return "failed"
|
||||
}
|
||||
if (wikidata.wikisites.size === 0) {
|
||||
return ["no page", wikidata]
|
||||
}
|
||||
|
||||
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, wikidata]
|
||||
}, [Locale.language])
|
||||
const wikiLink: Store<
|
||||
| [string, string, WikidataResponse]
|
||||
| "loading"
|
||||
| "failed"
|
||||
| ["no page", WikidataResponse]
|
||||
> = Wikidata.LoadWikidataEntry(wikidataId).map(
|
||||
(maybewikidata) => {
|
||||
if (maybewikidata === undefined) {
|
||||
return "loading"
|
||||
}
|
||||
if (maybewikidata["error"] !== undefined) {
|
||||
return "failed"
|
||||
}
|
||||
const wikidata = <WikidataResponse>maybewikidata["success"]
|
||||
if (wikidata === undefined) {
|
||||
return "failed"
|
||||
}
|
||||
if (wikidata.wikisites.size === 0) {
|
||||
return ["no page", wikidata]
|
||||
}
|
||||
|
||||
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, wikidata]
|
||||
},
|
||||
[Locale.language]
|
||||
)
|
||||
|
||||
const contents = new VariableUiElement(
|
||||
wikiLink.map(status => {
|
||||
wikiLink.map((status) => {
|
||||
if (status === "loading") {
|
||||
return new Loading(wp.loading.Clone()).SetClass("pl-6 pt-2")
|
||||
}
|
||||
|
@ -173,106 +206,116 @@ export default class WikipediaBox extends Combine {
|
|||
options.currentState?.setData("loaded")
|
||||
return new Combine([
|
||||
WikidataPreviewBox.WikidataResponsePreview(wd),
|
||||
wp.noWikipediaPage.Clone().SetClass("subtle")]).SetClass("flex flex-col p-4")
|
||||
wp.noWikipediaPage.Clone().SetClass("subtle"),
|
||||
]).SetClass("flex flex-col p-4")
|
||||
}
|
||||
|
||||
const [pagetitle, language, wd] = <[string, string, WikidataResponse]>status
|
||||
const wikipedia = new Wikipedia({language})
|
||||
const quickFacts = WikidataPreviewBox.QuickFacts(wd);
|
||||
return WikipediaBox.createContents(pagetitle, wikipedia, {topBar: quickFacts, ...options})
|
||||
|
||||
const wikipedia = new Wikipedia({ language })
|
||||
const quickFacts = WikidataPreviewBox.QuickFacts(wd)
|
||||
return WikipediaBox.createContents(pagetitle, wikipedia, {
|
||||
topBar: quickFacts,
|
||||
...options,
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
const titleElement = new VariableUiElement(wikiLink.map(state => {
|
||||
if (typeof state !== "string") {
|
||||
const [pagetitle, _] = state
|
||||
if (pagetitle === "no page") {
|
||||
const wd = <WikidataResponse>state[1]
|
||||
return new Title(Translation.fromMap(wd.labels), 3)
|
||||
const titleElement = new VariableUiElement(
|
||||
wikiLink.map((state) => {
|
||||
if (typeof state !== "string") {
|
||||
const [pagetitle, _] = state
|
||||
if (pagetitle === "no page") {
|
||||
const wd = <WikidataResponse>state[1]
|
||||
return new Title(Translation.fromMap(wd.labels), 3)
|
||||
}
|
||||
return new Title(pagetitle, 3)
|
||||
}
|
||||
return new Title(pagetitle, 3)
|
||||
}
|
||||
return new Link(new Title(wikidataId, 3), "https://www.wikidata.org/wiki/" + wikidataId, true)
|
||||
return new Link(
|
||||
new Title(wikidataId, 3),
|
||||
"https://www.wikidata.org/wiki/" + wikidataId,
|
||||
true
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
}))
|
||||
const linkElement = new VariableUiElement(
|
||||
wikiLink.map((state) => {
|
||||
if (typeof state !== "string") {
|
||||
const [pagetitle, language] = state
|
||||
const popout = options.noImages
|
||||
? "Source"
|
||||
: Svg.pop_out_svg().SetStyle("width: 1.2rem").SetClass("block")
|
||||
if (pagetitle === "no page") {
|
||||
const wd = <WikidataResponse>state[1]
|
||||
return new Link(popout, "https://www.wikidata.org/wiki/" + wd.id, true)
|
||||
}
|
||||
|
||||
const linkElement = new VariableUiElement(wikiLink.map(state => {
|
||||
if (typeof state !== "string") {
|
||||
const [pagetitle, language] = state
|
||||
const popout = options.noImages ? "Source" : Svg.pop_out_svg().SetStyle("width: 1.2rem").SetClass("block")
|
||||
if (pagetitle === "no page") {
|
||||
const wd = <WikidataResponse>state[1]
|
||||
return new Link(popout,
|
||||
"https://www.wikidata.org/wiki/" + wd.id
|
||||
, true)
|
||||
const url = `https://${language}.wikipedia.org/wiki/${pagetitle}`
|
||||
return new Link(popout, url, true)
|
||||
}
|
||||
|
||||
const url = `https://${language}.wikipedia.org/wiki/${pagetitle}`
|
||||
return new Link(popout, url, true)
|
||||
}
|
||||
return undefined
|
||||
}))
|
||||
.SetClass("flex items-center enable-links")
|
||||
return undefined
|
||||
})
|
||||
).SetClass("flex items-center enable-links")
|
||||
|
||||
return {
|
||||
contents: contents,
|
||||
linkElement: linkElement,
|
||||
titleElement: titleElement
|
||||
titleElement: titleElement,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the actual content in a scrollable way for the given wikipedia page
|
||||
*/
|
||||
private static createContents(pagename: string, wikipedia: Wikipedia, options:{
|
||||
topBar?: BaseUIElement} & WikipediaBoxOptions): BaseUIElement {
|
||||
private static createContents(
|
||||
pagename: string,
|
||||
wikipedia: Wikipedia,
|
||||
options: {
|
||||
topBar?: BaseUIElement
|
||||
} & WikipediaBoxOptions
|
||||
): BaseUIElement {
|
||||
const htmlContent = wikipedia.GetArticle(pagename, options)
|
||||
const wp = Translations.t.general.wikipedia
|
||||
const contents: VariableUiElement =new VariableUiElement(
|
||||
htmlContent.map(htmlContent => {
|
||||
if (htmlContent === undefined) {
|
||||
// Still loading
|
||||
return new Loading(wp.loading.Clone())
|
||||
}
|
||||
if (htmlContent["success"] !== undefined) {
|
||||
let content: BaseUIElement = new FixedUiElement(htmlContent["success"]);
|
||||
if (options?.addHeader) {
|
||||
content = new Combine(
|
||||
[
|
||||
new Paragraph(
|
||||
new Link(wp.fromWikipedia, wikipedia.getPageUrl(pagename), true),
|
||||
),
|
||||
new Paragraph(
|
||||
content
|
||||
)
|
||||
]
|
||||
)
|
||||
const contents: VariableUiElement = new VariableUiElement(
|
||||
htmlContent.map((htmlContent) => {
|
||||
if (htmlContent === undefined) {
|
||||
// Still loading
|
||||
return new Loading(wp.loading.Clone())
|
||||
}
|
||||
if (htmlContent["success"] !== undefined) {
|
||||
let content: BaseUIElement = new FixedUiElement(htmlContent["success"])
|
||||
if (options?.addHeader) {
|
||||
content = new Combine([
|
||||
new Paragraph(
|
||||
new Link(wp.fromWikipedia, wikipedia.getPageUrl(pagename), true)
|
||||
),
|
||||
new Paragraph(content),
|
||||
])
|
||||
}
|
||||
return content.SetClass("wikipedia-article")
|
||||
}
|
||||
if (htmlContent["error"]) {
|
||||
console.warn("Loading wikipage failed due to", htmlContent["error"])
|
||||
return wp.failed.Clone().SetClass("alert p-4")
|
||||
}
|
||||
return content.SetClass("wikipedia-article")
|
||||
}
|
||||
if (htmlContent["error"]) {
|
||||
console.warn("Loading wikipage failed due to", htmlContent["error"])
|
||||
return wp.failed.Clone().SetClass("alert p-4")
|
||||
}
|
||||
|
||||
return undefined
|
||||
}))
|
||||
return undefined
|
||||
})
|
||||
)
|
||||
|
||||
htmlContent.addCallbackAndRunD(c => {
|
||||
if(c["success"] !== undefined){
|
||||
htmlContent.addCallbackAndRunD((c) => {
|
||||
if (c["success"] !== undefined) {
|
||||
options.currentState?.setData("loaded")
|
||||
}else if (c["error"] !== undefined){
|
||||
} else if (c["error"] !== undefined) {
|
||||
options.currentState?.setData("error")
|
||||
}else {
|
||||
} else {
|
||||
options.currentState?.setData("loading")
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return new Combine([
|
||||
options?.topBar?.SetClass("border-2 border-grey rounded-lg m-1 mb-0"),
|
||||
contents .SetClass("block pl-6 pt-2")])
|
||||
contents.SetClass("block pl-6 pt-2"),
|
||||
])
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue