| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Svg from "../../Svg" | 
					
						
							|  |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							| 
									
										
										
										
											2023-05-22 01:37:02 +02:00
										 |  |  | import LayoutConfig, {LayoutInformation} from "../../Models/ThemeConfig/LayoutConfig" | 
					
						
							| 
									
										
										
										
											2023-06-06 00:03:14 +02:00
										 |  |  | import {ImmutableStore, Store} from "../../Logic/UIEventSource" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import UserRelatedState from "../../Logic/State/UserRelatedState" | 
					
						
							| 
									
										
										
										
											2023-05-22 01:37:02 +02:00
										 |  |  | import {Utils} from "../../Utils" | 
					
						
							| 
									
										
										
										
											2023-02-08 01:14:21 +01:00
										 |  |  | import themeOverview from "../../assets/generated/theme_overview.json" | 
					
						
							| 
									
										
										
										
											2023-05-22 01:37:02 +02:00
										 |  |  | import {TextField} from "../Input/TextField" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Locale from "../i18n/Locale" | 
					
						
							| 
									
										
										
										
											2023-02-03 22:28:11 +01:00
										 |  |  | import SvelteUIElement from "../Base/SvelteUIElement" | 
					
						
							|  |  |  | import ThemesList from "./ThemesList.svelte" | 
					
						
							|  |  |  | import HiddenThemeList from "./HiddenThemeList.svelte" | 
					
						
							|  |  |  | import UnofficialThemeList from "./UnofficialThemeList.svelte" | 
					
						
							| 
									
										
										
										
											2020-07-29 15:48:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-30 02:10:57 +02:00
										 |  |  | export default class MoreScreen extends Combine { | 
					
						
							| 
									
										
										
										
											2023-02-11 15:04:20 +01:00
										 |  |  |     private static readonly officialThemes: LayoutInformation[] = themeOverview | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor( | 
					
						
							|  |  |  |         state: UserRelatedState & { | 
					
						
							|  |  |  |             layoutToUse?: LayoutConfig | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onMainScreen: boolean = false | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         const tr = Translations.t.general.morescreen | 
					
						
							| 
									
										
										
										
											2021-05-30 00:17:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-24 01:32:19 +02:00
										 |  |  |         const search = new TextField({ | 
					
						
							| 
									
										
										
										
											2022-04-28 02:04:25 +02:00
										 |  |  |             placeholder: tr.searchForATheme, | 
					
						
							| 
									
										
										
										
											2022-04-24 01:32:19 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         search.enterPressed.addCallbackD((searchTerm) => { | 
					
						
							| 
									
										
										
										
											2022-04-30 14:00:03 +02:00
										 |  |  |             searchTerm = searchTerm.toLowerCase() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (searchTerm === "personal") { | 
					
						
							|  |  |  |                 window.location.href = MoreScreen.createUrlFor( | 
					
						
							| 
									
										
										
										
											2023-05-07 23:54:31 +02:00
										 |  |  |                     { id: "personal" }, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     false, | 
					
						
							|  |  |  |                     state | 
					
						
							|  |  |  |                 ).data | 
					
						
							| 
									
										
										
										
											2022-04-30 14:00:03 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (searchTerm === "bugs" || searchTerm === "issues") { | 
					
						
							| 
									
										
										
										
											2022-04-30 14:00:03 +02:00
										 |  |  |                 window.location.href = "https://github.com/pietervdvn/MapComplete/issues" | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (searchTerm === "source") { | 
					
						
							| 
									
										
										
										
											2022-04-30 14:00:03 +02:00
										 |  |  |                 window.location.href = "https://github.com/pietervdvn/MapComplete" | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (searchTerm === "docs") { | 
					
						
							| 
									
										
										
										
											2022-04-30 14:00:03 +02:00
										 |  |  |                 window.location.href = "https://github.com/pietervdvn/MapComplete/tree/develop/Docs" | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             if (searchTerm === "osmcha" || searchTerm === "stats") { | 
					
						
							| 
									
										
										
										
											2022-04-30 14:00:03 +02:00
										 |  |  |                 window.location.href = Utils.OsmChaLinkFor(7) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-04-30 02:10:57 +02:00
										 |  |  |             // Enter pressed -> search the first _official_ matchin theme and open it
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const publicTheme = MoreScreen.officialThemes.find( | 
					
						
							|  |  |  |                 (th) => | 
					
						
							|  |  |  |                     th.hideFromOverview == false && | 
					
						
							|  |  |  |                     th.id !== "personal" && | 
					
						
							| 
									
										
										
										
											2023-04-23 13:22:57 +02:00
										 |  |  |                     MoreScreen.MatchesLayout(th, searchTerm) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-04-30 02:10:57 +02:00
										 |  |  |             if (publicTheme !== undefined) { | 
					
						
							|  |  |  |                 window.location.href = MoreScreen.createUrlFor(publicTheme, false, state).data | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const hiddenTheme = MoreScreen.officialThemes.find( | 
					
						
							| 
									
										
										
										
											2023-04-23 13:22:57 +02:00
										 |  |  |                 (th) => th.id !== "personal" && MoreScreen.MatchesLayout(th, searchTerm) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-04-30 02:10:57 +02:00
										 |  |  |             if (hiddenTheme !== undefined) { | 
					
						
							|  |  |  |                 window.location.href = MoreScreen.createUrlFor(hiddenTheme, false, state).data | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-04-28 02:04:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (onMainScreen) { | 
					
						
							|  |  |  |             search.focus() | 
					
						
							| 
									
										
										
										
											2022-12-24 02:01:58 +01:00
										 |  |  |             document.addEventListener("keydown", function (event) { | 
					
						
							|  |  |  |                 if (event.ctrlKey && event.code === "KeyF") { | 
					
						
							|  |  |  |                     search.focus() | 
					
						
							|  |  |  |                     event.preventDefault() | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2022-04-28 02:04:25 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-24 01:32:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const searchBar = new Combine([ | 
					
						
							|  |  |  |             Svg.search_svg().SetClass("w-8"), | 
					
						
							|  |  |  |             search.SetClass("mr-4 w-full"), | 
					
						
							|  |  |  |         ]).SetClass("flex rounded-full border-2 border-black items-center my-2 w-1/2") | 
					
						
							| 
									
										
										
										
											2022-04-24 01:32:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-28 02:04:25 +02:00
										 |  |  |         super([ | 
					
						
							|  |  |  |             new Combine([searchBar]).SetClass("flex justify-center"), | 
					
						
							| 
									
										
										
										
											2023-02-03 22:28:11 +01:00
										 |  |  |             new SvelteUIElement(ThemesList, { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 state, | 
					
						
							| 
									
										
										
										
											2023-02-03 22:28:11 +01:00
										 |  |  |                 onMainScreen, | 
					
						
							|  |  |  |                 search: search.GetValue(), | 
					
						
							|  |  |  |                 themes: MoreScreen.officialThemes, | 
					
						
							|  |  |  |             }), | 
					
						
							|  |  |  |             new SvelteUIElement(HiddenThemeList, { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 state, | 
					
						
							| 
									
										
										
										
											2023-02-03 22:28:11 +01:00
										 |  |  |                 onMainScreen, | 
					
						
							|  |  |  |                 search: search.GetValue(), | 
					
						
							|  |  |  |             }), | 
					
						
							|  |  |  |             new SvelteUIElement(UnofficialThemeList, { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 state, | 
					
						
							| 
									
										
										
										
											2023-02-03 22:28:11 +01:00
										 |  |  |                 onMainScreen, | 
					
						
							|  |  |  |                 search: search.GetValue(), | 
					
						
							|  |  |  |             }), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             tr.streetcomplete.Clone().SetClass("block text-base mx-10 my-3 mb-10"), | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-28 02:04:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 23:54:31 +02:00
										 |  |  |     public static MatchesLayout( | 
					
						
							|  |  |  |         layout: { | 
					
						
							|  |  |  |             id: string | 
					
						
							|  |  |  |             title: any | 
					
						
							|  |  |  |             shortDescription: any | 
					
						
							|  |  |  |             keywords?: any[] | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         search: string | 
					
						
							|  |  |  |     ): boolean { | 
					
						
							|  |  |  |         if (search === undefined) { | 
					
						
							| 
									
										
										
										
											2023-04-23 13:22:57 +02:00
										 |  |  |             return true | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         search = search.toLocaleLowerCase() | 
					
						
							|  |  |  |         if (search.length > 3 && layout.id.toLowerCase().indexOf(search) >= 0) { | 
					
						
							|  |  |  |             return true | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-07 23:54:31 +02:00
										 |  |  |         if (layout.id === "personal") { | 
					
						
							| 
									
										
										
										
											2023-04-23 13:22:57 +02:00
										 |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-07 23:54:31 +02:00
										 |  |  |         const entitiesToSearch = [layout.shortDescription, layout.title, ...(layout.keywords ?? [])] | 
					
						
							| 
									
										
										
										
											2023-04-23 13:22:57 +02:00
										 |  |  |         for (const entity of entitiesToSearch) { | 
					
						
							|  |  |  |             if (entity === undefined) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const term = entity["*"] ?? entity[Locale.language.data] | 
					
						
							|  |  |  |             if (term?.toLowerCase()?.indexOf(search) >= 0) { | 
					
						
							|  |  |  |                 return true | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private static createUrlFor( | 
					
						
							|  |  |  |         layout: { id: string; definition?: string }, | 
					
						
							|  |  |  |         isCustom: boolean, | 
					
						
							| 
									
										
										
										
											2023-06-06 00:03:14 +02:00
										 |  |  |         state?: { layoutToUse?: { id } } | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |     ): Store<string> { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (layout === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (layout.id === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             console.error("ID is undefined for layout", layout) | 
					
						
							|  |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (layout.id === state?.layoutToUse?.id) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let path = window.location.pathname | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         // Path starts with a '/' and contains everything, e.g. '/dir/dir/page.html'
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         path = path.substr(0, path.lastIndexOf("/")) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         // Path will now contain '/dir/dir', or empty string in case of nothing
 | 
					
						
							|  |  |  |         if (path === "") { | 
					
						
							|  |  |  |             path = "." | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let linkPrefix = `${path}/${layout.id.toLowerCase()}.html?` | 
					
						
							|  |  |  |         if (location.hostname === "localhost" || location.hostname === "127.0.0.1") { | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  |             linkPrefix = `${path}/theme.html?layout=${layout.id}&` | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  |         if (isCustom) { | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  |             linkPrefix = `${path}/theme.html?userlayout=${layout.id}&` | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 15:41:14 +01:00
										 |  |  |         let hash = "" | 
					
						
							| 
									
										
										
										
											2022-04-13 02:44:06 +02:00
										 |  |  |         if (layout.definition !== undefined) { | 
					
						
							|  |  |  |             hash = "#" + btoa(JSON.stringify(layout.definition)) | 
					
						
							| 
									
										
										
										
											2022-02-14 15:41:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-13 02:44:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-06 00:03:14 +02:00
										 |  |  |         return new ImmutableStore<string>(`${linkPrefix}${hash}`) | 
					
						
							| 
									
										
										
										
											2022-04-30 02:10:57 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |