| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import { Translation } from "../i18n/Translation" | 
					
						
							|  |  |  | import Svg from "../../Svg" | 
					
						
							|  |  |  | import { TextField } from "../Input/TextField" | 
					
						
							|  |  |  | import { Geocoding } from "../../Logic/Osm/Geocoding" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import Hash from "../../Logic/Web/Hash" | 
					
						
							|  |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import Locale from "../i18n/Locale" | 
					
						
							| 
									
										
										
										
											2021-01-04 04:06:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | export default class SearchAndGo extends Combine { | 
					
						
							| 
									
										
										
										
											2022-12-24 02:01:58 +01:00
										 |  |  |     private readonly _searchField: TextField | 
					
						
							| 
									
										
										
										
											2022-09-17 03:24:01 +02:00
										 |  |  |     constructor(state: { leafletMap: UIEventSource<any>; selectedElement?: UIEventSource<any> }) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const goButton = Svg.search_ui().SetClass("w-8 h-8 full-rounded border-black float-right") | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const placeholder = new UIEventSource<Translation>(Translations.t.general.search.search) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         const searchField = new TextField({ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             placeholder: placeholder.map( | 
					
						
							|  |  |  |                 (tr) => tr.textFor(Locale.language.data), | 
					
						
							|  |  |  |                 [Locale.language] | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             value: new UIEventSource<string>(""), | 
					
						
							|  |  |  |             inputStyle: | 
					
						
							|  |  |  |                 " background: transparent;\n" + | 
					
						
							|  |  |  |                 "    border: none;\n" + | 
					
						
							|  |  |  |                 "    font-size: large;\n" + | 
					
						
							|  |  |  |                 "    width: 100%;\n" + | 
					
						
							|  |  |  |                 "    height: 100%;\n" + | 
					
						
							|  |  |  |                 "    box-sizing: border-box;\n" + | 
					
						
							|  |  |  |                 "    color: var(--foreground-color);", | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         searchField.SetClass("relative float-left mt-0 ml-2") | 
					
						
							|  |  |  |         searchField.SetStyle("width: calc(100% - 3em);height: 100%") | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super([searchField, goButton]) | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.SetClass("block h-8") | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         this.SetStyle( | 
					
						
							|  |  |  |             "background: var(--background-color); color: var(--foreground-color); pointer-evetns:all;" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |         // Triggered by 'enter' or onclick
 | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  |         async function runSearch() { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const searchString = searchField.GetValue().data | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             if (searchString === undefined || searchString === "") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             searchField.GetValue().setData("") | 
					
						
							|  |  |  |             placeholder.setData(Translations.t.general.search.searching) | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const result = await Geocoding.Search(searchString) | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 console.log("Search result", result) | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  |                 if (result.length == 0) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     placeholder.setData(Translations.t.general.search.nothing) | 
					
						
							|  |  |  |                     return | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const poi = result[0] | 
					
						
							|  |  |  |                 const bb = poi.boundingbox | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  |                 const bounds: [[number, number], [number, number]] = [ | 
					
						
							|  |  |  |                     [bb[0], bb[2]], | 
					
						
							|  |  |  |                     [bb[1], bb[3]], | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2022-09-17 03:24:01 +02:00
										 |  |  |                 state.selectedElement?.setData(undefined) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 Hash.hash.setData(poi.osm_type + "/" + poi.osm_id) | 
					
						
							|  |  |  |                 state.leafletMap.data.fitBounds(bounds) | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  |                 placeholder.setData(Translations.t.general.search.search) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 searchField.GetValue().setData("") | 
					
						
							|  |  |  |                 placeholder.setData(Translations.t.general.search.error) | 
					
						
							| 
									
										
										
										
											2022-04-28 00:28:04 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-09-17 14:24:28 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-01 02:12:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         searchField.enterPressed.addCallback(runSearch) | 
					
						
							| 
									
										
										
										
											2022-12-24 02:01:58 +01:00
										 |  |  |         this._searchField = searchField | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         goButton.onClick(runSearch) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-24 02:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     focus() { | 
					
						
							|  |  |  |         this._searchField.focus() | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-19 16:48:10 +02:00
										 |  |  | } |