mapcomplete/UI/Image/ImgurImage.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-07-08 15:09:34 +02:00
import {UIElement} from "../UIElement";
import {UIEventSource} from "../../Logic/UIEventSource";
import {LicenseInfo} from "../../Logic/Web/Wikimedia";
import {Imgur} from "../../Logic/Web/Imgur";
2020-07-08 15:09:34 +02:00
2020-07-27 01:19:38 +02:00
export class ImgurImage extends UIElement {
2020-07-08 15:09:34 +02:00
/***
* Dictionary from url to alreayd known license info
*/
static allLicenseInfos: any = {};
2020-09-10 19:33:06 +02:00
private readonly _imageMeta: UIEventSource<LicenseInfo>;
private readonly _imageLocation: string;
2020-07-08 15:09:34 +02:00
constructor(source: string) {
super(undefined)
this._imageLocation = source;
if (ImgurImage.allLicenseInfos[source] !== undefined) {
this._imageMeta = ImgurImage.allLicenseInfos[source];
} else {
this._imageMeta = new UIEventSource<LicenseInfo>(null);
ImgurImage.allLicenseInfos[source] = this._imageMeta;
const self = this;
Imgur.getDescriptionOfImage(source, (license) => {
self._imageMeta.setData(license)
})
}
this.ListenTo(this._imageMeta);
}
InnerRender(): string {
2020-07-08 15:09:34 +02:00
const image = "<img src='" + this._imageLocation + "' " + "alt='' >";
if(this._imageMeta.data === null){
return image;
}
const attribution =
"<span class='attribution-author'><b>" + (this._imageMeta.data.artist ?? "") + "</b></span>" + " <span class='license'>" + (this._imageMeta.data.licenseShortName ?? "") + "</span>";
return "<div class='imgWithAttr'>" +
image +
"<div class='attribution'>" +
attribution +
"</div>" +
"</div>";
}
}