forked from MapComplete/MapComplete
Add wikidata-images to etymology theme, various fixes for custom image carousels and gracious handling of wikidata/wikimedia
This commit is contained in:
parent
54abe7d057
commit
ff11f96e91
10 changed files with 155 additions and 99 deletions
|
@ -64,13 +64,16 @@ export default class SpecialVisualizations {
|
|||
funcName: "image_carousel",
|
||||
docs: "Creates an image carousel for the given sources. An attempt will be made to guess what source is used. Supported: Wikidata identifiers, Wikipedia pages, Wikimedia categories, IMGUR (with attribution, direct links)",
|
||||
args: [{
|
||||
name: "image key/prefix",
|
||||
name: "image key/prefix (multiple values allowed if comma-seperated)",
|
||||
defaultValue: "image",
|
||||
doc: "The keys given to the images, e.g. if <span class='literal-code'>image</span> is given, the first picture URL will be added as <span class='literal-code'>image</span>, the second as <span class='literal-code'>image:0</span>, the third as <span class='literal-code'>image:1</span>, etc... "
|
||||
}],
|
||||
constr: (state: State, tags, args) => {
|
||||
const imagePrefix = args[0];
|
||||
return new ImageCarousel(AllImageProviders.LoadImagesFor(tags, imagePrefix), tags);
|
||||
let imagePrefixes = undefined;
|
||||
if(args.length > 0){
|
||||
imagePrefixes = args;
|
||||
}
|
||||
return new ImageCarousel(AllImageProviders.LoadImagesFor(tags, imagePrefixes), tags);
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -11,6 +11,7 @@ import Svg from "../Svg";
|
|||
import Wikidata, {WikidataResponse} from "../Logic/Web/Wikidata";
|
||||
import Locale from "./i18n/Locale";
|
||||
import Toggle from "./Input/Toggle";
|
||||
import Link from "./Base/Link";
|
||||
|
||||
export default class WikipediaBox extends Toggle {
|
||||
|
||||
|
@ -22,49 +23,78 @@ export default class WikipediaBox extends Toggle {
|
|||
}
|
||||
|
||||
|
||||
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) {
|
||||
return new Loading(wp.loading.Clone())
|
||||
}
|
||||
if (maybewikidata["error"] !== undefined) {
|
||||
return wp.failed.Clone().SetClass("alert p-4")
|
||||
}
|
||||
const wikidata = <WikidataResponse>maybewikidata["success"]
|
||||
console.log("Got wikidata response", wikidata)
|
||||
if (wikidata.wikisites.size === 0) {
|
||||
return wp.noWikipediaPage.Clone()
|
||||
}
|
||||
const wikiLink: UIEventSource<[string, string] | "loading" | "failed" | "no page"> =
|
||||
wikidataId
|
||||
.bind(id => {
|
||||
if (id === undefined) {
|
||||
return undefined
|
||||
}
|
||||
return Wikidata.LoadWikidataEntry(id);
|
||||
})
|
||||
.map(maybewikidata => {
|
||||
if (maybewikidata === undefined) {
|
||||
return "loading"
|
||||
}
|
||||
if (maybewikidata["error"] !== undefined) {
|
||||
return "failed"
|
||||
|
||||
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 WikipediaBox.createContents(pagetitle, language)
|
||||
}, [Locale.language])
|
||||
}
|
||||
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(
|
||||
wikibox
|
||||
wikiLink.map(status => {
|
||||
if (status === "loading") {
|
||||
return new Loading(wp.loading.Clone()).SetClass("pl-6 pt-2")
|
||||
}
|
||||
|
||||
if (status === "failed") {
|
||||
return wp.failed.Clone().SetClass("alert p-4")
|
||||
}
|
||||
if (status == "no page") {
|
||||
return wp.noWikipediaPage.Clone()
|
||||
}
|
||||
|
||||
const [pagetitle, language] = status
|
||||
return WikipediaBox.createContents(pagetitle, language)
|
||||
|
||||
})
|
||||
).SetClass("overflow-auto normal-background rounded-lg")
|
||||
|
||||
|
||||
const linkElement = new VariableUiElement(wikiLink.map(state => {
|
||||
if (typeof state !== "string") {
|
||||
const [pagetitle, language] = state
|
||||
const url= `https://${language}.wikipedia.org/wiki/${pagetitle}`
|
||||
return new Link(Svg.pop_out_ui().SetStyle("width: 1.2rem").SetClass("block "), url, true)
|
||||
}
|
||||
return undefined}))
|
||||
.SetClass("flex items-center")
|
||||
|
||||
const mainContent = new Combine([
|
||||
new Combine([Svg.wikipedia_ui().SetStyle("width: 1.5rem").SetClass("mr-3"),
|
||||
new Title(Translations.t.general.wikipedia.wikipediaboxTitle.Clone(), 2)]).SetClass("flex"),
|
||||
new Combine([
|
||||
new Combine([
|
||||
Svg.wikipedia_ui().SetStyle("width: 1.5rem").SetClass("mr-3"),
|
||||
new Title(Translations.t.general.wikipedia.wikipediaboxTitle.Clone(), 2),
|
||||
]).SetClass("flex"),
|
||||
|
||||
linkElement
|
||||
]).SetClass("flex justify-between"),
|
||||
contents]).SetClass("block rounded-xl subtle-background m-1 p-2 flex flex-col")
|
||||
.SetStyle("max-height: inherit")
|
||||
super(
|
||||
|
@ -102,8 +132,8 @@ export default class WikipediaBox extends Toggle {
|
|||
return undefined
|
||||
})
|
||||
|
||||
return new Combine([new VariableUiElement(contents).SetClass("block pl-6 pt-2")])
|
||||
.SetClass("block")
|
||||
return new Combine([new VariableUiElement(contents)
|
||||
.SetClass("block pl-6 pt-2")])
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue