MapComplete/src/UI/BigComponents/SearchResult.svelte

85 lines
3.3 KiB
Svelte
Raw Normal View History

2024-08-15 01:51:33 +02:00
<script lang="ts">
import type { GeoCodeResult } from "../../Logic/Geocoding/GeocodingProvider"
2024-08-21 14:06:42 +02:00
import { GeocodingUtils } from "../../Logic/Geocoding/GeocodingProvider"
2024-08-15 01:51:33 +02:00
import type { SpecialVisualizationState } from "../SpecialVisualization"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import ToSvelte from "../Base/ToSvelte.svelte"
import { GeoOperations } from "../../Logic/GeoOperations"
import { createEventDispatcher } from "svelte"
2024-08-21 14:06:42 +02:00
import Icon from "../Map/Icon.svelte"
import { BBox } from "../../Logic/BBox"
import TagRenderingAnswer from "../Popup/TagRendering/TagRenderingAnswer.svelte"
import { UIEventSource } from "../../Logic/UIEventSource"
import ArrowUp from "@babeard/svelte-heroicons/mini/ArrowUp"
2024-08-15 01:51:33 +02:00
export let entry: GeoCodeResult
export let state: SpecialVisualizationState
let layer: LayerConfig
2024-08-21 14:06:42 +02:00
let tags : UIEventSource<Record<string, string>>
if (entry.feature?.properties?.id) {
2024-08-15 01:51:33 +02:00
layer = state.layout.getMatchingLayer(entry.feature.properties)
2024-08-21 14:06:42 +02:00
tags = state.featureProperties.getStore(entry.feature.properties.id)
2024-08-15 01:51:33 +02:00
}
2024-08-21 14:06:42 +02:00
let dispatch = createEventDispatcher<{ select }>()
2024-08-15 01:51:33 +02:00
let distance = state.mapProperties.location.mapD(l => GeoOperations.distanceBetween([l.lon, l.lat], [entry.lon, entry.lat]))
2024-08-21 14:06:42 +02:00
let bearing = state.mapProperties.location.mapD(l => GeoOperations.bearing([l.lon, l.lat], [entry.lon, entry.lat]))
let mapRotation = state.mapProperties.rotation
let inView = state.mapProperties.bounds.mapD(bounds => bounds.contains([entry.lon, entry.lat]))
2024-08-15 01:51:33 +02:00
function select() {
2024-08-21 14:06:42 +02:00
console.log("Selected search entry", entry)
if (entry.boundingbox) {
const [lat0, lat1, lon0, lon1] = entry.boundingbox
state.mapProperties.bounds.set(
new BBox([
[lon0, lat0],
[lon1, lat1]
]).pad(0.01)
)
} else {
state.mapProperties.flyTo(entry.lon, entry.lat, GeocodingUtils.categoryToZoomLevel[entry.category] ?? 17)
}
2024-08-15 01:51:33 +02:00
if (entry.feature) {
state.selectedElement.set(entry.feature)
}
2024-08-21 14:06:42 +02:00
state.recentlySearched.addSelected(entry)
2024-08-15 01:51:33 +02:00
dispatch("select")
}
</script>
2024-08-21 14:06:42 +02:00
<button class="unstyled w-full link-no-underline" on:click={() => select() }>
<div class="p-2 flex items-center w-full gap-y-2 w-full">
2024-08-15 01:51:33 +02:00
{#if layer}
<ToSvelte construct={() => layer.defaultIcon(entry.feature.properties).SetClass("w-6 h-6")} />
2024-08-21 14:06:42 +02:00
{:else if entry.category}
<Icon icon={GeocodingUtils.categoryToIcon[entry.category]} clss="w-6 h-6 shrink-0" color="#aaa" />
2024-08-15 01:51:33 +02:00
{/if}
2024-08-21 14:06:42 +02:00
<div class="flex flex-col items-start pl-2 w-full">
<div class="flex flex-wrap gap-x-2 justify-between w-full">
<b class="nowrap">
{#if layer && $tags?.id}
<TagRenderingAnswer config={layer.title} selectedElement={entry.feature} {state} {tags} {layer} />
{:else}
{entry.display_name ?? entry.osm_id}
{/if}
</b>
<div class="flex gap-x-1 items-center">
{#if $bearing && !$inView}
<ArrowUp class="w-4 h-4 shrink-0" style={`transform: rotate(${$bearing - $mapRotation}deg)`} />
{/if}
{#if $distance}
{GeoOperations.distanceToHuman($distance)}
{/if}
</div>
2024-08-15 01:51:33 +02:00
</div>
2024-08-21 14:06:42 +02:00
{#if entry.description}
<div class="subtle flex justify-between w-full">
{entry.description}
</div>
{/if}
2024-08-15 01:51:33 +02:00
</div>
</div>
</button>