| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Wraps the query parameters into UIEventSources | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { UIEventSource } from "../UIEventSource" | 
					
						
							|  |  |  | import Hash from "./Hash" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class QueryParameters { | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |     static defaults: Record<string, string> = {} | 
					
						
							| 
									
										
										
										
											2022-06-14 18:44:24 +02:00
										 |  |  |     static documentation: Map<string, string> = new Map<string, string>() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private static order: string[] = ["layout", "test", "z", "lat", "lon"] | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |     protected static readonly _wasInitialized: Set<string> = new Set() | 
					
						
							|  |  |  |     protected static readonly knownSources: Record<string, UIEventSource<string>> = {} | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private static initialized = false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static GetQueryParameter( | 
					
						
							|  |  |  |         key: string, | 
					
						
							|  |  |  |         deflt: string, | 
					
						
							|  |  |  |         documentation?: string | 
					
						
							|  |  |  |     ): UIEventSource<string> { | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         if (!this.initialized) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             this.init() | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         QueryParameters.documentation.set(key, documentation) | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         if (deflt !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             QueryParameters.defaults[key] = deflt | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (QueryParameters.knownSources[key] !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return QueryParameters.knownSources[key] | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         QueryParameters.addOrder(key) | 
					
						
							|  |  |  |         const source = new UIEventSource<string>(deflt, "&" + key) | 
					
						
							|  |  |  |         QueryParameters.knownSources[key] = source | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         source.addCallback(() => QueryParameters.Serialize()) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return source | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static GetBooleanQueryParameter( | 
					
						
							|  |  |  |         key: string, | 
					
						
							|  |  |  |         deflt: boolean, | 
					
						
							|  |  |  |         documentation?: string | 
					
						
							|  |  |  |     ): UIEventSource<boolean> { | 
					
						
							|  |  |  |         return QueryParameters.GetQueryParameter(key, "" + deflt, documentation).sync( | 
					
						
							|  |  |  |             (str) => str === "true", | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             (b) => "" + b | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-10-23 02:46:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     public static wasInitialized(key: string): boolean { | 
					
						
							|  |  |  |         return QueryParameters._wasInitialized.has(key) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 23:58:11 +01:00
										 |  |  |     private static addOrder(key) { | 
					
						
							|  |  |  |         if (this.order.indexOf(key) < 0) { | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |             this.order.push(key) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static init() { | 
					
						
							| 
									
										
										
										
											2020-11-13 23:58:11 +01:00
										 |  |  |         if (this.initialized) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2020-07-25 18:00:08 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.initialized = true | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (Utils.runningFromConsole) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  |         if (window?.location?.search) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const params = window.location.search.substr(1).split("&") | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |             for (const param of params) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const kv = param.split("=") | 
					
						
							|  |  |  |                 const key = decodeURIComponent(kv[0]) | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |                 QueryParameters.addOrder(key) | 
					
						
							| 
									
										
										
										
											2021-08-26 12:15:26 +02:00
										 |  |  |                 QueryParameters._wasInitialized.add(key) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const v = decodeURIComponent(kv[1]) | 
					
						
							|  |  |  |                 const source = new UIEventSource<string>(v) | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |                 source.addCallback(() => QueryParameters.Serialize()) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 QueryParameters.knownSources[key] = source | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static Serialize() { | 
					
						
							|  |  |  |         const parts = [] | 
					
						
							|  |  |  |         for (const key of QueryParameters.order) { | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |             if (QueryParameters.knownSources[key]?.data === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-06-15 00:28:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:30:35 +01:00
										 |  |  |             if (QueryParameters.knownSources[key].data === "undefined") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2020-12-04 21:30:35 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |             if (QueryParameters.knownSources[key].data === QueryParameters.defaults[key]) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2020-07-29 15:05:19 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-11-02 20:15:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             parts.push( | 
					
						
							|  |  |  |                 encodeURIComponent(key) + | 
					
						
							|  |  |  |                     "=" + | 
					
						
							|  |  |  |                     encodeURIComponent(QueryParameters.knownSources[key].data) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2020-07-22 23:47:04 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         if (!Utils.runningFromConsole) { | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |             // Don't pollute the history every time a parameter changes
 | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |                 history.replaceState(null, "", "?" + parts.join("&") + Hash.Current()) | 
					
						
							| 
									
										
										
										
											2022-10-27 01:50:41 +02:00
										 |  |  |             } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |                 console.error(e) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-13 23:58:11 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-17 03:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static ClearAll() { | 
					
						
							|  |  |  |         for (const name in QueryParameters.knownSources) { | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |             QueryParameters.knownSources[name].setData(undefined) | 
					
						
							| 
									
										
										
										
											2022-09-17 03:24:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         QueryParameters._wasInitialized.clear() | 
					
						
							| 
									
										
										
										
											2022-09-18 12:45:02 +02:00
										 |  |  |         QueryParameters.order = [] | 
					
						
							| 
									
										
										
										
											2022-09-17 03:24:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |