mapcomplete/UI/Image/ImageCarousel.ts

65 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-10-14 12:15:09 +02:00
import {SlideShow} from "./SlideShow";
import {UIEventSource} from "../../Logic/UIEventSource";
import Combine from "../Base/Combine";
import DeleteImage from "./DeleteImage";
import {AttributedImage} from "./AttributedImage";
2021-06-11 22:51:45 +02:00
import BaseUIElement from "../BaseUIElement";
import Img from "../Base/Img";
2021-06-14 19:21:33 +02:00
import Toggle from "../Input/Toggle";
import {Wikimedia} from "../../Logic/ImageProviders/Wikimedia";
import {Imgur} from "../../Logic/ImageProviders/Imgur";
import {Mapillary} from "../../Logic/ImageProviders/Mapillary";
2021-06-14 19:21:33 +02:00
export class ImageCarousel extends Toggle {
2020-06-24 00:35:19 +02:00
2021-06-14 19:21:33 +02:00
constructor(images: UIEventSource<{ key: string, url: string }[]>, tags: UIEventSource<any>) {
const uiElements = images.map((imageURLS: { key: string, url: string }[]) => {
2021-06-11 22:51:45 +02:00
const uiElements: BaseUIElement[] = [];
2020-06-24 00:35:19 +02:00
for (const url of imageURLS) {
let image = ImageCarousel.CreateImageElement(url.url)
2021-06-14 19:21:33 +02:00
if (url.key !== undefined) {
image = new Combine([
image,
new DeleteImage(url.key, tags).SetClass("delete-image-marker absolute top-0 left-0 pl-3")
]).SetClass("relative");
}
2021-06-14 19:21:33 +02:00
image
.SetClass("w-full block")
.SetStyle("min-width: 50px; background: grey;")
2020-07-07 15:08:52 +02:00
uiElements.push(image);
2020-06-24 00:35:19 +02:00
}
return uiElements;
});
2020-07-07 15:08:52 +02:00
2021-06-14 19:21:33 +02:00
super(
new SlideShow(uiElements).SetClass("w-full"),
undefined,
uiElements.map(els => els.length > 0)
)
this.SetClass("block w-full");
2020-06-24 00:35:19 +02:00
}
/***
* Creates either a 'simpleimage' or a 'wikimediaimage' based on the string
* @param url
* @constructor
*/
2021-06-11 22:51:45 +02:00
private static CreateImageElement(url: string): BaseUIElement {
// @ts-ignore
let attrSource : ImageAttributionSource = undefined;
if (url.startsWith("File:")) {
attrSource = Wikimedia.singleton
} else if (url.toLowerCase().startsWith("https://commons.wikimedia.org/wiki/")) {
attrSource = Wikimedia.singleton;
} else if (url.toLowerCase().startsWith("https://i.imgur.com/")) {
attrSource = Imgur.singleton
} else if (url.toLowerCase().startsWith("https://www.mapillary.com/map/im/")) {
attrSource = Mapillary.singleton
} else {
2021-06-11 22:51:45 +02:00
return new Img(url);
}
return new AttributedImage(url, attrSource)
}
2020-06-24 00:35:19 +02:00
}