| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | import {Mapillary} from "./Mapillary"; | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | import {WikimediaImageProvider} from "./WikimediaImageProvider"; | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | import {Imgur} from "./Imgur"; | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | import GenericImageProvider from "./GenericImageProvider"; | 
					
						
							|  |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | import ImageProvider, {ProvidedImage} from "./ImageProvider"; | 
					
						
							|  |  |  | import {WikidataImageProvider} from "./WikidataImageProvider"; | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A generic 'from the interwebz' image picker, without attribution | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | export default class AllImageProviders { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |     public static ImageAttributionSource: ImageProvider[] = [ | 
					
						
							|  |  |  |         Imgur.singleton, | 
					
						
							|  |  |  |         Mapillary.singleton, | 
					
						
							|  |  |  |         WikidataImageProvider.singleton, | 
					
						
							|  |  |  |         WikimediaImageProvider.singleton, | 
					
						
							| 
									
										
										
										
											2021-10-06 17:48:07 +02:00
										 |  |  |         new GenericImageProvider( | 
					
						
							|  |  |  |             [].concat(...Imgur.defaultValuePrefix, ...WikimediaImageProvider.commonsPrefixes, ...Mapillary.valuePrefixes) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 17:48:07 +02:00
										 |  |  |     ] | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 00:09:40 +02:00
										 |  |  |     public static defaultKeys = [].concat(AllImageProviders.ImageAttributionSource.map(provider => provider.defaultKeyPrefixes)) | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static _cache: Map<string, UIEventSource<ProvidedImage[]>> = new Map<string, UIEventSource<ProvidedImage[]>>() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 00:09:40 +02:00
										 |  |  |     public static LoadImagesFor(tags: UIEventSource<any>, tagKey?: string[]): UIEventSource<ProvidedImage[]> { | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |         if (tags.data.id === undefined) { | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |             return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         const cacheKey = tags.data.id + tagKey | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |         const cached = this._cache.get(cacheKey) | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |         if (cached !== undefined) { | 
					
						
							|  |  |  |             return cached | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 00:26:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |         const source = new UIEventSource([]) | 
					
						
							| 
									
										
										
										
											2021-10-08 04:33:39 +02:00
										 |  |  |         this._cache.set(cacheKey, source) | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |         const allSources = [] | 
					
						
							|  |  |  |         for (const imageProvider of AllImageProviders.ImageAttributionSource) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |             let prefixes = imageProvider.defaultKeyPrefixes | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (tagKey !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-10-20 00:09:40 +02:00
										 |  |  |                 prefixes = tagKey | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 00:26:21 +02:00
										 |  |  |             const singleSource = imageProvider.GetRelevantUrls(tags, { | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |                 prefixes: prefixes | 
					
						
							| 
									
										
										
										
											2021-09-30 00:26:21 +02:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |             allSources.push(singleSource) | 
					
						
							|  |  |  |             singleSource.addCallbackAndRunD(_ => { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 const all: ProvidedImage[] = [].concat(...allSources.map(source => source.data)) | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |                 const uniq = [] | 
					
						
							|  |  |  |                 const seen = new Set<string>() | 
					
						
							|  |  |  |                 for (const img of all) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                     if (seen.has(img.url)) { | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |                         continue | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     seen.add(img.url) | 
					
						
							|  |  |  |                     uniq.push(img) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 source.setData(uniq) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return source; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 14:21:32 +02:00
										 |  |  | } |