| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | import Toggle from "../Input/Toggle"; | 
					
						
							|  |  |  | import MapControlButton from "../MapControlButton"; | 
					
						
							|  |  |  | import GeoLocationHandler from "../../Logic/Actors/GeoLocationHandler"; | 
					
						
							|  |  |  | import Svg from "../../Svg"; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  | import MapState, {GlobalFilter} from "../../Logic/State/MapState"; | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  | import LevelSelector from "../Input/LevelSelector"; | 
					
						
							|  |  |  | import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline"; | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  | import {TagUtils} from "../../Logic/Tags/TagUtils"; | 
					
						
							|  |  |  | import {RegexTag} from "../../Logic/Tags/RegexTag"; | 
					
						
							|  |  |  | import {Or} from "../../Logic/Tags/Or"; | 
					
						
							|  |  |  | import {Tag} from "../../Logic/Tags/Tag"; | 
					
						
							|  |  |  | import {TagsFilter} from "../../Logic/Tags/TagsFilter"; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  | import Translations from "../i18n/Translations"; | 
					
						
							|  |  |  | import {BBox} from "../../Logic/BBox"; | 
					
						
							|  |  |  | import {OsmFeature} from "../../Models/OsmFeature"; | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class RightControls extends Combine { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |     constructor(state: MapState & { featurePipeline: FeaturePipeline }) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |         const geolocatioHandler = new GeoLocationHandler( | 
					
						
							| 
									
										
										
										
											2021-11-04 02:16:07 +01:00
										 |  |  |             state | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |         const geolocationButton = new Toggle( | 
					
						
							|  |  |  |             new MapControlButton( | 
					
						
							| 
									
										
										
										
											2021-11-03 00:44:53 +01:00
										 |  |  |                 geolocatioHandler | 
					
						
							|  |  |  |                 , { | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |                     dontStyle: true | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             undefined, | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |             state.featureSwitchGeolocation | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const plus = new MapControlButton( | 
					
						
							| 
									
										
										
										
											2021-08-20 00:06:44 +02:00
										 |  |  |             Svg.plus_svg() | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |         ).onClick(() => { | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |             state.locationControl.data.zoom++; | 
					
						
							|  |  |  |             state.locationControl.ping(); | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const min = new MapControlButton( | 
					
						
							| 
									
										
										
										
											2021-08-20 00:06:44 +02:00
										 |  |  |             Svg.min_svg() | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |         ).onClick(() => { | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |             state.locationControl.data.zoom--; | 
					
						
							|  |  |  |             state.locationControl.ping(); | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |         const levelsInView = state.currentBounds.map(bbox => { | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |             if (bbox === undefined) { | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |                 return [] | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |             const allElementsUnfiltered: OsmFeature[] = [].concat(... state.featurePipeline.GetAllFeaturesAndMetaWithin(bbox).map(ff => ff.features)) | 
					
						
							|  |  |  |             const allElements = allElementsUnfiltered.filter(f => BBox.get(f).overlapsWith(bbox)) | 
					
						
							|  |  |  |             const allLevelsRaw: string[] = allElements.map(f => f.properties["level"]) | 
					
						
							|  |  |  |             const allLevels = [].concat(...allLevelsRaw.map(l => TagUtils.LevelsParser(l)))  | 
					
						
							|  |  |  |             if (allLevels.indexOf("0") < 0) { | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |                 allLevels.push("0") | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             allLevels.sort((a, b) => a < b ? -1 : 1) | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |             return Utils.Dedup(allLevels) | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |         state.globalFilters.data.push({ | 
					
						
							|  |  |  |             filter: { | 
					
						
							|  |  |  |                 currentFilter: undefined, | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                 state: undefined, | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             id: "level", | 
					
						
							|  |  |  |             onNewPoint: undefined | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |         const levelSelect = new LevelSelector(levelsInView) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |         const isShown = levelsInView.map(levelsInView => { | 
					
						
							|  |  |  |                 if (levelsInView.length == 0) { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (state.locationControl.data.zoom <= 16) { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (levelsInView.length == 1 && levelsInView[0] == "0") { | 
					
						
							|  |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |             [state.locationControl]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function setLevelFilter() { | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |             console.log("Updating levels filter") | 
					
						
							|  |  |  |             const filter: GlobalFilter = state.globalFilters.data.find(gf => gf.id === "level") | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |             if (!isShown.data) { | 
					
						
							|  |  |  |                 filter.filter = { | 
					
						
							|  |  |  |                     state: "*", | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                     currentFilter: undefined, | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                 filter.onNewPoint = undefined | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const l = levelSelect.GetValue().data | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                 let neededLevel: TagsFilter = new RegexTag("level", new RegExp("(^|;)" + l + "(;|$)")); | 
					
						
							|  |  |  |                 if (l === "0") { | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |                     neededLevel = new Or([neededLevel, new Tag("level", "")]) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 filter.filter = { | 
					
						
							|  |  |  |                     state: l, | 
					
						
							|  |  |  |                     currentFilter: neededLevel | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                 const t = Translations.t.general.levelSelection | 
					
						
							|  |  |  |                 filter.onNewPoint = { | 
					
						
							|  |  |  |                     confirmAddNew: t.confirmLevel.PartialSubs({level: l}), | 
					
						
							|  |  |  |                     safetyCheck: t.addNewOnLevel.Subs({level: l}), | 
					
						
							|  |  |  |                     tags: [new Tag("level", l)] | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |             state.globalFilters.ping(); | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         isShown.addCallbackAndRun(shown => { | 
					
						
							|  |  |  |             console.log("Is level selector shown?", shown) | 
					
						
							|  |  |  |             setLevelFilter() | 
					
						
							|  |  |  |             if (shown) { | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |                 levelSelect.RemoveClass("invisible") | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 levelSelect.SetClass("invisible") | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |         levelSelect.GetValue().addCallback(_ => setLevelFilter()) | 
					
						
							| 
									
										
										
										
											2022-07-21 15:54:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:33:11 +02:00
										 |  |  |         super([new Combine([levelSelect]).SetClass(""), plus, min, geolocationButton].map(el => el.SetClass("m-0.5 md:m-1"))) | 
					
						
							| 
									
										
										
										
											2021-08-19 23:41:48 +02:00
										 |  |  |         this.SetClass("flex flex-col items-center") | 
					
						
							| 
									
										
										
										
											2021-07-28 16:48:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |