| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | import BaseUIElement from "../../UI/BaseUIElement"; | 
					
						
							|  |  |  | import {LicenseInfo} from "./LicenseInfo"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface ProvidedImage { | 
					
						
							|  |  |  |     url: string, key: string, provider: ImageProvider | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default abstract class ImageProvider { | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |     public abstract readonly defaultKeyPrefixes : string[] = ["mapillary", "image"] | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |      | 
					
						
							|  |  |  |     private _cache = new Map<string, UIEventSource<LicenseInfo>>() | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     GetAttributionFor(url: string): UIEventSource<LicenseInfo> { | 
					
						
							|  |  |  |         const cached = this._cache.get(url); | 
					
						
							|  |  |  |         if (cached !== undefined) { | 
					
						
							|  |  |  |             return cached; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const src =UIEventSource.FromPromise(this.DownloadAttribution(url)) | 
					
						
							|  |  |  |         this._cache.set(url, src) | 
					
						
							|  |  |  |         return src; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public abstract SourceIcon(backlinkSource?: string): BaseUIElement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected abstract DownloadAttribution(url: string): Promise<LicenseInfo>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Given a properies object, maps it onto _all_ the available pictures for this imageProvider | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public GetRelevantUrls(allTags: UIEventSource<any>, options?: { | 
					
						
							|  |  |  |         prefixes?: string[] | 
					
						
							|  |  |  |     }):UIEventSource<ProvidedImage[]> { | 
					
						
							|  |  |  |         const prefixes = options?.prefixes ?? this.defaultKeyPrefixes | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |         if(prefixes === undefined){ | 
					
						
							|  |  |  |             throw "The image provider"+this.constructor.name+" doesn't define `defaultKeyPrefixes`" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |         const relevantUrls = new UIEventSource<{ url: string; key: string; provider: ImageProvider }[]>([]) | 
					
						
							|  |  |  |         const seenValues = new Set<string>() | 
					
						
							|  |  |  |         allTags.addCallbackAndRunD(tags => { | 
					
						
							|  |  |  |             for (const key in tags) { | 
					
						
							|  |  |  |                 if(!prefixes.some(prefix => key.startsWith(prefix))){ | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const value = tags[key] | 
					
						
							|  |  |  |                 if(seenValues.has(value)){ | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 seenValues.add(value) | 
					
						
							|  |  |  |                 this.ExtractUrls(key, value).then(promises => { | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |                     for (const promise of promises ?? []) { | 
					
						
							|  |  |  |                         if(promise === undefined){ | 
					
						
							|  |  |  |                             continue | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |                         promise.then(providedImage => { | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |                             if(providedImage === undefined){ | 
					
						
							|  |  |  |                                 return | 
					
						
							|  |  |  |                             } | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |                             relevantUrls.data.push(providedImage) | 
					
						
							|  |  |  |                             relevantUrls.ping() | 
					
						
							|  |  |  |                         }) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         return relevantUrls | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public abstract ExtractUrls(key: string, value: string) : Promise<Promise<ProvidedImage>[]>; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | } |