| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | import {WikimediaImage} from "../UI/Image/WikimediaImage"; | 
					
						
							|  |  |  | import {SimpleImageElement} from "../UI/Image/SimpleImageElement"; | 
					
						
							|  |  |  | import {UIElement} from "../UI/UIElement"; | 
					
						
							| 
									
										
										
										
											2020-07-08 15:09:34 +02:00
										 |  |  | import {ImgurImage} from "../UI/Image/ImgurImage"; | 
					
						
							| 
									
										
										
										
											2020-08-17 17:23:15 +02:00
										 |  |  | import {ImagesInCategory, Wikidata, Wikimedia} from "./Web/Wikimedia"; | 
					
						
							|  |  |  | import {UIEventSource} from "./UIEventSource"; | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | import {MapillaryImage} from "../UI/Image/MapillaryImage"; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-08 13:12:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * There are multiple way to fetch images for an object | 
					
						
							|  |  |  |  * 1) There is an image tag | 
					
						
							|  |  |  |  * 2) There is an image tag, the image tag contains multiple ';'-seperated URLS | 
					
						
							|  |  |  |  * 3) there are multiple image tags, e.g. 'image', 'image:0', 'image:1', and 'image_0', 'image_1' - however, these are pretty rare so we are gonna ignore them | 
					
						
							|  |  |  |  * 4) There is a wikimedia_commons-tag, which either has a 'File': or a 'category:' containing images | 
					
						
							|  |  |  |  * 5) There is a wikidata-tag, and the wikidata item either has an 'image' attribute or has 'a link to a wikimedia commons category' | 
					
						
							|  |  |  |  * 6) There is a wikipedia article, from which we can deduct the wikidata item | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For some images, author and license should be shown | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-13 03:29:44 +02:00
										 |  |  | export class ImageSearcher extends UIEventSource<{key: string, url: string}[]> { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private readonly _tags: UIEventSource<any>; | 
					
						
							|  |  |  |     private readonly _wdItem = new UIEventSource<string>(""); | 
					
						
							|  |  |  |     private readonly _commons = new UIEventSource<string>(""); | 
					
						
							| 
									
										
										
										
											2020-07-08 13:12:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 15:08:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |     constructor(tags: UIEventSource<any>, imagePrefix = "image", loadSpecial = true) { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         super([]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this._tags = tags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // By wrapping this in a UIEventSource, we prevent multiple queries of loadWikiData
 | 
					
						
							|  |  |  |         this._wdItem.addCallback(() => self.loadWikidata()); | 
					
						
							|  |  |  |         this._commons.addCallback(() => self.LoadCommons()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |         this._tags.addCallbackAndRun(() => self.LoadImages(imagePrefix, loadSpecial)); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 03:29:44 +02:00
										 |  |  |     private AddImage(key: string, url: string) { | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |         if (url === undefined || url === null || url === "") { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-07 15:08:52 +02:00
										 |  |  |         for (const el of this.data) { | 
					
						
							| 
									
										
										
										
											2020-09-13 03:29:44 +02:00
										 |  |  |             if (el.url === url) { | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |                 // This url is already seen -> don't add it
 | 
					
						
							| 
									
										
										
										
											2020-07-07 15:08:52 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |         this.data.push({key: key, url: url}); | 
					
						
							| 
									
										
										
										
											2020-07-07 15:08:52 +02:00
										 |  |  |         this.ping(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private loadWikidata() { | 
					
						
							|  |  |  |         // Load the wikidata item, then detect usage on 'commons'
 | 
					
						
							|  |  |  |         let allWikidataId = this._wdItem.data.split(";"); | 
					
						
							|  |  |  |         for (let wikidataId of allWikidataId) { | 
					
						
							|  |  |  |             // @ts-ignore
 | 
					
						
							|  |  |  |             if (wikidataId.startsWith("Q")) { | 
					
						
							|  |  |  |                 wikidataId = wikidataId.substr(1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Wikimedia.GetWikiData(parseInt(wikidataId), (wd: Wikidata) => { | 
					
						
							|  |  |  |                 this.AddImage(undefined, wd.image); | 
					
						
							|  |  |  |                 Wikimedia.GetCategoryFiles(wd.commonsWiki, (images: ImagesInCategory) => { | 
					
						
							|  |  |  |                     for (const image of images.images) { | 
					
						
							|  |  |  |                         // @ts-ignore
 | 
					
						
							|  |  |  |                         if (image.startsWith("File:")) { | 
					
						
							|  |  |  |                             this.AddImage(undefined, image); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private LoadCommons() { | 
					
						
							|  |  |  |         const allCommons: string[] = this._commons.data.split(";"); | 
					
						
							|  |  |  |         for (const commons of allCommons) { | 
					
						
							|  |  |  |             // @ts-ignore
 | 
					
						
							|  |  |  |             if (commons.startsWith("Category:")) { | 
					
						
							|  |  |  |                 Wikimedia.GetCategoryFiles(commons, (images: ImagesInCategory) => { | 
					
						
							|  |  |  |                     for (const image of images.images) { | 
					
						
							|  |  |  |                         // @ts-ignore
 | 
					
						
							|  |  |  |                         if (image.startsWith("File:")) { | 
					
						
							|  |  |  |                             this.AddImage(undefined, image); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             } else { // @ts-ignore
 | 
					
						
							|  |  |  |                 if (commons.startsWith("File:")) { | 
					
						
							|  |  |  |                     this.AddImage(undefined, commons); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |     private LoadImages(imagePrefix: string, loadAdditional: boolean): void { | 
					
						
							|  |  |  |         console.log("Loading images from",this._tags) | 
					
						
							|  |  |  |         const imageTag = this._tags.data[imagePrefix]; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         if (imageTag !== undefined) { | 
					
						
							|  |  |  |             const bareImages = imageTag.split(";"); | 
					
						
							|  |  |  |             for (const bareImage of bareImages) { | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |                 this.AddImage(imagePrefix, bareImage); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-08 11:23:36 +02:00
										 |  |  |         for (const key in this._tags.data) { | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |             if (key.startsWith(imagePrefix+":")) { | 
					
						
							| 
									
										
										
										
											2020-07-08 13:12:23 +02:00
										 |  |  |                 const url = this._tags.data[key] | 
					
						
							| 
									
										
										
										
											2020-09-13 03:29:44 +02:00
										 |  |  |                 this.AddImage(key, url); | 
					
						
							| 
									
										
										
										
											2020-07-08 11:23:36 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |         if (loadAdditional) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (this._tags.data.mapillary) { | 
					
						
							| 
									
										
										
										
											2020-10-17 02:37:53 +02:00
										 |  |  |                 this.AddImage(undefined,"https://www.mapillary.com/map/im/" + this._tags.data.mapillary) | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*** | 
					
						
							|  |  |  |      * Creates either a 'simpleimage' or a 'wikimediaimage' based on the string | 
					
						
							|  |  |  |      * @param url | 
					
						
							|  |  |  |      * @constructor | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     static CreateImageElement(url: string): UIElement { | 
					
						
							|  |  |  |         // @ts-ignore
 | 
					
						
							|  |  |  |         if (url.startsWith("File:")) { | 
					
						
							| 
									
										
										
										
											2020-07-08 15:09:34 +02:00
										 |  |  |             return new WikimediaImage(url); | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |         } else if (url.toLowerCase().startsWith("https://commons.wikimedia.org/wiki/")) { | 
					
						
							| 
									
										
										
										
											2020-07-08 17:09:12 +02:00
										 |  |  |             const commons = url.substr("https://commons.wikimedia.org/wiki/".length); | 
					
						
							|  |  |  |             return new WikimediaImage(commons); | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |         } else if (url.toLowerCase().startsWith("https://i.imgur.com/")) { | 
					
						
							| 
									
										
										
										
											2020-07-08 15:09:34 +02:00
										 |  |  |             return new ImgurImage(url); | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |         } else if (url.toLowerCase().startsWith("https://www.mapillary.com/map/im/")) { | 
					
						
							|  |  |  |             return new MapillaryImage(url); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-07-08 15:09:34 +02:00
										 |  |  |             return new SimpleImageElement(new UIEventSource<string>(url)); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |