| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | import LayerConfig from "./ThemeConfig/LayerConfig" | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  | import { UIEventSource } from "../Logic/UIEventSource" | 
					
						
							| 
									
										
										
										
											2023-04-07 03:54:11 +02:00
										 |  |  | import UserRelatedState from "../Logic/State/UserRelatedState" | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  | import { Utils } from "../Utils" | 
					
						
							|  |  |  | import { LocalStorageSource } from "../Logic/Web/LocalStorageSource" | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:44:01 +02:00
										 |  |  | export type ThemeViewTabStates = (typeof MenuState._themeviewTabs)[number] | 
					
						
							|  |  |  | export type MenuViewTabStates = (typeof MenuState._menuviewTabs)[number] | 
					
						
							| 
									
										
										
										
											2023-05-17 13:21:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Indicates if a menu is open, and if so, which tab is selected; | 
					
						
							|  |  |  |  * Some tabs allow to highlight an element. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Some convenience methods are provided for this as well | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class MenuState { | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |     public static readonly _themeviewTabs = [ | 
					
						
							|  |  |  |         "intro", | 
					
						
							|  |  |  |         "filters", | 
					
						
							|  |  |  |         "download", | 
					
						
							|  |  |  |         "copyright", | 
					
						
							|  |  |  |         "share", | 
					
						
							|  |  |  |     ] as const | 
					
						
							|  |  |  |     public static readonly _menuviewTabs = [ | 
					
						
							|  |  |  |         "about", | 
					
						
							|  |  |  |         "settings", | 
					
						
							|  |  |  |         "community", | 
					
						
							|  |  |  |         "privacy", | 
					
						
							|  |  |  |         "advanced", | 
					
						
							|  |  |  |     ] as const | 
					
						
							| 
									
										
										
										
											2023-04-19 03:20:49 +02:00
										 |  |  |     public readonly themeIsOpened: UIEventSource<boolean> | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |     public readonly themeViewTabIndex: UIEventSource<number> | 
					
						
							| 
									
										
										
										
											2023-05-17 13:21:37 +02:00
										 |  |  |     public readonly themeViewTab: UIEventSource<ThemeViewTabStates> | 
					
						
							| 
									
										
										
										
											2023-04-19 03:20:49 +02:00
										 |  |  |     public readonly menuIsOpened: UIEventSource<boolean> | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |     public readonly menuViewTabIndex: UIEventSource<number> | 
					
						
							| 
									
										
										
										
											2023-05-17 13:21:37 +02:00
										 |  |  |     public readonly menuViewTab: UIEventSource<MenuViewTabStates> | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |     public readonly backgroundLayerSelectionIsOpened: UIEventSource<boolean> = | 
					
						
							|  |  |  |         new UIEventSource<boolean>(false) | 
					
						
							| 
									
										
										
										
											2023-05-18 15:44:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  |     public readonly allToggles: { | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |         toggle: UIEventSource<boolean> | 
					
						
							|  |  |  |         name: string | 
					
						
							|  |  |  |         submenu?: UIEventSource<string> | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  |         showOverOthers?: boolean | 
					
						
							|  |  |  |     }[] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |     public readonly highlightedLayerInFilters: UIEventSource<string> = new UIEventSource<string>( | 
					
						
							|  |  |  |         undefined | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-04-07 02:13:57 +02:00
										 |  |  |     public highlightedUserSetting: UIEventSource<string> = new UIEventSource<string>(undefined) | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-19 03:20:49 +02:00
										 |  |  |     constructor(themeid: string = "") { | 
					
						
							|  |  |  |         if (themeid) { | 
					
						
							|  |  |  |             themeid += "-" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.themeIsOpened = LocalStorageSource.GetParsed(themeid + "thememenuisopened", true) | 
					
						
							|  |  |  |         this.themeViewTabIndex = LocalStorageSource.GetParsed(themeid + "themeviewtabindex", 0) | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |         this.themeViewTab = this.themeViewTabIndex.sync( | 
					
						
							|  |  |  |             (i) => MenuState._themeviewTabs[i], | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             (str) => MenuState._themeviewTabs.indexOf(<any>str) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-19 03:20:49 +02:00
										 |  |  |         this.menuIsOpened = LocalStorageSource.GetParsed(themeid + "menuisopened", false) | 
					
						
							|  |  |  |         this.menuViewTabIndex = LocalStorageSource.GetParsed(themeid + "menuviewtabindex", 0) | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |         this.menuViewTab = this.menuViewTabIndex.sync( | 
					
						
							|  |  |  |             (i) => MenuState._menuviewTabs[i], | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             (str) => MenuState._menuviewTabs.indexOf(<any>str) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-04-14 02:42:57 +02:00
										 |  |  |         this.menuIsOpened.addCallbackAndRun((isOpen) => { | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |             if (!isOpen) { | 
					
						
							| 
									
										
										
										
											2023-04-14 02:42:57 +02:00
										 |  |  |                 this.highlightedUserSetting.setData(undefined) | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         this.themeViewTab.addCallbackAndRun((tab) => { | 
					
						
							|  |  |  |             if (tab !== "filters") { | 
					
						
							|  |  |  |                 this.highlightedLayerInFilters.setData(undefined) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2023-04-24 02:27:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.menuIsOpened.addCallbackAndRunD((opened) => { | 
					
						
							|  |  |  |             if (opened) { | 
					
						
							|  |  |  |                 this.themeIsOpened.setData(false) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         this.themeIsOpened.addCallbackAndRunD((opened) => { | 
					
						
							|  |  |  |             if (opened) { | 
					
						
							|  |  |  |                 this.menuIsOpened.setData(false) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.allToggles = [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 toggle: this.menuIsOpened, | 
					
						
							|  |  |  |                 name: "menu", | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |                 submenu: this.menuViewTab, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  |                 toggle: this.themeIsOpened, | 
					
						
							|  |  |  |                 name: "theme-menu", | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |                 submenu: this.themeViewTab, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  |                 toggle: this.backgroundLayerSelectionIsOpened, | 
					
						
							|  |  |  |                 name: "background", | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |                 showOverOthers: true, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |     public openFilterView(highlightLayer?: LayerConfig | string) { | 
					
						
							|  |  |  |         this.themeIsOpened.setData(true) | 
					
						
							|  |  |  |         this.themeViewTab.setData("filters") | 
					
						
							|  |  |  |         if (highlightLayer) { | 
					
						
							|  |  |  |             if (typeof highlightLayer !== "string") { | 
					
						
							|  |  |  |                 highlightLayer = highlightLayer.id | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             this.highlightedLayerInFilters.setData(highlightLayer) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 02:13:57 +02:00
										 |  |  |     public openUsersettings(highlightTagRendering?: string) { | 
					
						
							|  |  |  |         this.menuIsOpened.setData(true) | 
					
						
							|  |  |  |         this.menuViewTab.setData("settings") | 
					
						
							| 
									
										
										
										
											2023-04-07 03:54:11 +02:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             highlightTagRendering !== undefined && | 
					
						
							|  |  |  |             !UserRelatedState.availableUserSettingsIds.some((tr) => tr === highlightTagRendering) | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             console.error( | 
					
						
							|  |  |  |                 "No tagRendering with id '" + highlightTagRendering + "'; maybe you meant:", | 
					
						
							|  |  |  |                 Utils.sortedByLevenshteinDistance( | 
					
						
							|  |  |  |                     highlightTagRendering, | 
					
						
							|  |  |  |                     UserRelatedState.availableUserSettingsIds, | 
					
						
							|  |  |  |                     (x) => x | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-07 02:13:57 +02:00
										 |  |  |         this.highlightedUserSetting.setData(highlightTagRendering) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Close all floatOvers. | 
					
						
							|  |  |  |      * Returns 'true' if at least one menu was opened | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public closeAll(): boolean { | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |         const toggles = [ | 
					
						
							|  |  |  |             this.menuIsOpened, | 
					
						
							|  |  |  |             this.themeIsOpened, | 
					
						
							|  |  |  |             this.backgroundLayerSelectionIsOpened, | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         const somethingIsOpen = toggles.some((t) => t.data) | 
					
						
							|  |  |  |         toggles.forEach((t) => t.setData(false)) | 
					
						
							| 
									
										
										
										
											2023-06-07 02:42:49 +02:00
										 |  |  |         return somethingIsOpen | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |