| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { UIEventSource } from "../UIEventSource" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 14:14:56 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Wrapper around the hash to create an UIEventSource from it | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  | export default class Hash { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static hash: UIEventSource<string> = Hash.Get() | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |      * Gets the current string, including the pound sign if there is any | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |      * @constructor | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static Current(): string { | 
					
						
							|  |  |  |         if (Hash.hash.data === undefined || Hash.hash.data === "") { | 
					
						
							|  |  |  |             return "" | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return "#" + Hash.hash.data | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static Get(): UIEventSource<string> { | 
					
						
							|  |  |  |         if (Utils.runningFromConsole) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return new UIEventSource<string>(undefined) | 
					
						
							| 
									
										
										
										
											2021-01-06 02:21:50 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const hash = new UIEventSource<string>(window.location.hash.substr(1)) | 
					
						
							|  |  |  |         hash.addCallback((h) => { | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |             if (h === "undefined") { | 
					
						
							|  |  |  |                 console.warn("Got a literal 'undefined' as hash, ignoring") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 h = undefined | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (h === undefined || h === "") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 window.location.hash = "" | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2021-01-06 02:09:04 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 14:14:56 +01:00
										 |  |  |             history.pushState({}, "") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             window.location.hash = "#" + h | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |         window.onhashchange = () => { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             let newValue = window.location.hash.substr(1) | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |             if (newValue === "") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 newValue = undefined | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             hash.setData(newValue) | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         window.addEventListener("popstate", (_) => { | 
					
						
							|  |  |  |             let newValue = window.location.hash.substr(1) | 
					
						
							| 
									
										
										
										
											2021-03-12 14:52:34 +01:00
										 |  |  |             if (newValue === "") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 newValue = undefined | 
					
						
							| 
									
										
										
										
											2021-03-12 14:52:34 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             hash.setData(newValue) | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-01-08 18:02:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return hash | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |