| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | import {Translation} from "../../UI/i18n/Translation"; | 
					
						
							|  |  |  | import {TagsFilter} from "../../Logic/Tags/TagsFilter"; | 
					
						
							|  |  |  | import FilterConfigJson from "./Json/FilterConfigJson"; | 
					
						
							|  |  |  | import Translations from "../../UI/i18n/Translations"; | 
					
						
							|  |  |  | import {TagUtils} from "../../Logic/Tags/TagUtils"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class FilterConfig { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:35:32 +02:00
										 |  |  |     public readonly id: string | 
					
						
							|  |  |  |     public readonly options: { | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         question: Translation; | 
					
						
							|  |  |  |         osmTags: TagsFilter; | 
					
						
							|  |  |  |     }[]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(json: FilterConfigJson, context: string) { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (json.options === undefined) { | 
					
						
							| 
									
										
										
										
											2021-09-06 22:19:57 +02:00
										 |  |  |             throw `A filter without options was given at ${context}` | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-27 18:35:32 +02:00
										 |  |  |         if (json.id === undefined) { | 
					
						
							|  |  |  |             throw `A filter without id was found at ${context}` | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if(json.id.match(/^[a-zA-Z0-9_-]*$/) === null){ | 
					
						
							|  |  |  |             throw `A filter with invalid id was found at ${context}. Ids should only contain letters, numbers or - _` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-06 22:19:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         if (json.options.map === undefined) { | 
					
						
							| 
									
										
										
										
											2021-09-06 22:19:57 +02:00
										 |  |  |             throw `A filter was given where the options aren't a list at ${context}` | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-27 18:35:32 +02:00
										 |  |  |         this.id = json.id; | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         this.options = json.options.map((option, i) => { | 
					
						
							|  |  |  |             const question = Translations.T( | 
					
						
							|  |  |  |                 option.question, | 
					
						
							|  |  |  |                 context + ".options-[" + i + "].question" | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |             const osmTags = TagUtils.Tag( | 
					
						
							|  |  |  |                 option.osmTags ?? {and: []}, | 
					
						
							|  |  |  |                 `${context}.options-[${i}].osmTags` | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             if (question === undefined) { | 
					
						
							| 
									
										
										
										
											2021-09-07 00:23:00 +02:00
										 |  |  |                 throw `Invalid filter: no question given at ${context}[${i}]` | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return {question: question, osmTags: osmTags}; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |