mapcomplete/Logic/ImageSearcher.ts

174 lines
5.1 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
import {UIEventSource} from "../UI/UIEventSource";
import {ImagesInCategory, Wikidata, Wikimedia} from "./Wikimedia";
import {WikimediaImage} from "../UI/Image/WikimediaImage";
import {SimpleImageElement} from "../UI/Image/SimpleImageElement";
import {UIElement} from "../UI/UIElement";
2020-07-07 15:08:52 +02:00
import {Changes} from "./Changes";
2020-06-24 00:35:19 +02:00
/**
* Class which search for all the possible locations for images and which builds a list of UI-elements for it.
* Note that this list is embedded into an UIEVentSource, ready to put it into a carousel
*/
export class ImageSearcher extends UIEventSource<string[]> {
private readonly _tags: UIEventSource<any>;
private readonly _wdItem = new UIEventSource<string>("");
private readonly _commons = new UIEventSource<string>("");
private _activated: boolean = false;
2020-07-07 15:08:52 +02:00
private _changes: Changes;
constructor(tags: UIEventSource<any>,
changes: Changes) {
2020-06-24 00:35:19 +02:00
super([]);
this._tags = tags;
2020-07-07 15:08:52 +02:00
this._changes = changes;
2020-06-24 00:35:19 +02:00
const self = this;
this._wdItem.addCallback(() => {
2020-07-07 15:08:52 +02:00
// Load the wikidata item, then detect usage on 'commons'
let wikidataId = self._wdItem.data;
// @ts-ignore
if (wikidataId.startsWith("Q")) {
2020-06-24 00:35:19 +02:00
wikidataId = wikidataId.substr(1);
}
Wikimedia.GetWikiData(parseInt(wikidataId), (wd: Wikidata) => {
self.AddImage(wd.image);
Wikimedia.GetCategoryFiles(wd.commonsWiki, (images: ImagesInCategory) => {
for (const image of images.images) {
2020-07-07 15:08:52 +02:00
// @ts-ignore
if (image.startsWith("File:")) {
self.AddImage(image);
}
2020-06-24 00:35:19 +02:00
}
})
})
}
);
this._commons.addCallback(() => {
const commons: string = self._commons.data;
// @ts-ignore
2020-06-24 00:35:19 +02:00
if (commons.startsWith("Category:")) {
Wikimedia.GetCategoryFiles(commons, (images: ImagesInCategory) => {
for (const image of images.images) {
// @ts-ignore
if (image.startsWith("File:")) {
self.AddImage(image);
}
2020-06-24 00:35:19 +02:00
}
})
} else { // @ts-ignore
if (commons.startsWith("File:")) {
self.AddImage(commons);
}
2020-06-24 00:35:19 +02:00
}
});
}
private AddImage(url: string) {
2020-07-07 15:08:52 +02:00
if (url === undefined || url === null) {
2020-06-24 00:35:19 +02:00
return;
}
2020-07-07 15:08:52 +02:00
for (const el of this.data) {
if (el === url) {
return;
}
}
this.data.push(url);
this.ping();
}
private ImageKey(url: string): string {
const tgs = this._tags.data;
for (const key in tgs) {
if (tgs[key] === url) {
return key;
}
}
return undefined;
}
public IsDeletable(url: string): boolean {
return this.ImageKey(url) !== undefined;
}
public Delete(url: string): void {
const key = this.ImageKey(url);
if(key === undefined){
return;
2020-06-24 00:35:19 +02:00
}
2020-07-07 15:08:52 +02:00
console.log("Deleting image...");
// this._changes.addChange(this._tags.data.id, key, "");
2020-06-24 00:35:19 +02:00
}
public Activate() {
2020-07-07 15:08:52 +02:00
if (this._activated) {
2020-06-24 00:35:19 +02:00
return;
}
this._activated = true;
this.LoadImages();
const self = this;
this._tags.addCallback(() => self.LoadImages());
}
private LoadImages(): void {
if (!this._activated) {
2020-06-24 00:35:19 +02:00
return;
}
const imageTag = this._tags.data.image;
if (imageTag !== undefined) {
const bareImages = imageTag.split(";");
for (const bareImage of bareImages) {
this.AddImage(bareImage);
}
}
const image0 = this._tags.data["image:0"];
if (image0 !== undefined) {
this.AddImage(image0);
}
let imageIndex = 1;
let imagei = this._tags.data["image:" + imageIndex];
while (imagei !== undefined) {
this.AddImage(imagei);
imageIndex++;
imagei = this._tags.data["image:" + imageIndex];
}
2020-06-24 00:35:19 +02:00
const wdItem = this._tags.data.wikidata;
if (wdItem !== undefined) {
this._wdItem.setData(wdItem);
}
const commons = this._tags.data.wikimedia_commons;
if (commons !== undefined) {
this._commons.setData(commons);
}
}
/***
* Creates either a 'simpleimage' or a 'wikimediaimage' based on the string
* @param url
* @constructor
*/
static CreateImageElement(url: string): UIElement {
const urlSource = new UIEventSource<string>(url);
// @ts-ignore
if (url.startsWith("File:")) {
return new WikimediaImage(urlSource.data);
2020-06-24 00:35:19 +02:00
} else {
return new SimpleImageElement(urlSource);
}
}
}