mapcomplete/UI/Image/WikimediaImage.ts

55 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
import {UIElement} from "../UIElement";
import {LicenseInfo, Wikimedia} from "../../Logic/Web/Wikimedia";
import {UIEventSource} from "../../Logic/UIEventSource";
2020-11-06 01:58:26 +01:00
import Svg from "../../Svg";
import Link from "../Base/Link";
import {FixedUiElement} from "../Base/FixedUiElement";
import Combine from "../Base/Combine";
2020-06-24 00:35:19 +02:00
export class WikimediaImage extends UIElement {
static allLicenseInfos: any = {};
2020-09-10 19:33:06 +02:00
private readonly _imageMeta: UIEventSource<LicenseInfo>;
2020-11-06 01:58:26 +01:00
private readonly _imageLocation: string;
constructor(source: string) {
super(undefined)
this._imageLocation = source;
if (WikimediaImage.allLicenseInfos[source] !== undefined) {
this._imageMeta = WikimediaImage.allLicenseInfos[source];
} else {
this._imageMeta = new UIEventSource<LicenseInfo>(new LicenseInfo());
WikimediaImage.allLicenseInfos[source] = this._imageMeta;
2020-07-08 15:09:34 +02:00
const self = this;
Wikimedia.LicenseData(source, (info) => {
self._imageMeta.setData(info);
})
}
this.ListenTo(this._imageMeta);
2020-07-08 15:09:34 +02:00
2020-06-24 00:35:19 +02:00
}
InnerRender(): string {
let url = Wikimedia.ImageNameToUrl(this._imageLocation, 500, 400);
2020-06-24 00:35:19 +02:00
url = url.replace(/'/g, '%27');
2020-11-06 01:58:26 +01:00
const wikimediaLink = new Link(Svg.wikimedia_commons_white_img,
`https://commons.wikimedia.org/wiki/${this._imageLocation}`, true)
.SetStyle("width:2em;height: 2em");
2020-11-06 01:58:26 +01:00
const attribution = new FixedUiElement(this._imageMeta.data.artist ?? "").SetClass("attribution-author");
const license = new FixedUiElement(this._imageMeta.data.licenseShortName ?? "").SetClass("license");
const image = "<img src='" + url + "' " + "alt='" + this._imageMeta.data.description + "' >";
return "<div class='imgWithAttr'>" +
image +
2020-11-06 01:58:26 +01:00
new Combine([wikimediaLink, attribution]).SetClass("attribution").Render() +
2020-06-24 00:35:19 +02:00
"</div>";
}
}