Linked data loader: prettier UI, some bugfixes

This commit is contained in:
Pieter Vander Vennet 2024-03-01 00:50:19 +01:00
parent 13506a0e59
commit da1eca797c
4 changed files with 208 additions and 139 deletions

View file

@ -6,7 +6,10 @@
import LayerConfig from "../../Models/ThemeConfig/LayerConfig" import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction" import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction"
import { Tag } from "../../Logic/Tags/Tag" import { Tag } from "../../Logic/Tags/Tag"
import TagRenderingAnswer from "../Popup/TagRendering/TagRenderingAnswer.svelte"
import Loading from "../Base/Loading.svelte" import Loading from "../Base/Loading.svelte"
import Tr from "../Base/Tr.svelte"
import Translations from "../i18n/Translations"
export let key: string export let key: string
export let externalProperties: Record<string, string> export let externalProperties: Record<string, string>
@ -38,34 +41,77 @@
await state.changes.applyChanges(await change.CreateChangeDescriptions()) await state.changes.applyChanges(await change.CreateChangeDescriptions())
currentStep = "done" currentStep = "done"
} }
let _country = $tags["_country"]
let mockPropertiesOsm = { id: feature.properties.id, [key]: $tags[key], _country }
let mockPropertiesExternal = { id: feature.properties.id, [key]: externalProperties[key], _country }
let trsWithKeys = layer.tagRenderings.filter(tr => {
const keys: string[] = [].concat(...tr.usedTags().map(t => t.usedKeys()))
return keys.indexOf(key) >= 0
})
let renderingBoth = undefined // trsWithKeys.find(tr => tr.IsKnown(mockPropertiesOsm) && tr.IsKnown(mockPropertiesExternal))
let renderingExternal = undefined // renderingBoth ?? trsWithKeys.find(tr => tr.IsKnown(mockPropertiesExternal))
let onOverwrite = false
const t = Translations.t.external
</script> </script>
<tr> <div>
<td><b>{key}</b></td>
<div class="py-1 px-2 interactive flex w-full justify-between">
{#if renderingExternal}
<TagRenderingAnswer tags={new UIEventSource(mockPropertiesExternal)} selectedElement={feature}
config={renderingExternal}
{layer} {state} />
{:else}
<div class="flex gap-x-1 items-center">
<b>{key}</b>{externalProperties[key]}
</div>
{#if $tags[key]}
{$tags[key]}
{/if} {/if}
<td>
{#if externalProperties[key].startsWith("http")}
<a href={externalProperties[key]} target="_blank">
{externalProperties[key]}
</a>
{:else}
{externalProperties[key]}
{/if}
</td>
{#if !readonly} {#if !readonly}
<td> {#if currentStep === "init"}
{#if currentStep === "init"} <button class="small" on:click={() => apply(key)}
<button class="small" on:click={() => apply(key)}>Apply</button>
{:else if currentStep === "applying"} on:mouseover={() => onOverwrite = true}
<Loading /> on:focus={() => onOverwrite = true}
{:else if currentStep === "done"} on:blur={() => onOverwrite = false}
<div class="thanks">Done</div> on:mouseout={() => onOverwrite = false }
{:else} >
<div class="alert">Error</div> {#if $tags[key]}
{/if} <Tr t={t.overwrite}/>
</td> {:else}
<Tr t={t.apply}/>
{/if}
</button>
{:else if currentStep === "applying"}
<Loading />
{:else if currentStep === "done"}
<div class="thanks">
<Tr t={t.done}/>
</div>
{:else}
<div class="alert">
<Tr t={t.error}/>
</div>
{/if}
{/if} {/if}
</tr>
</div>
{#if $tags[key]}
<div class:glowing-shadow={onOverwrite}>
<span class="subtle">
<Tr t={t.currentInOsmIs}/>
</span>
{#if renderingBoth}
<TagRenderingAnswer tags={new UIEventSource(mockPropertiesOsm)} selectedElement={feature} config={renderingBoth}
{layer} {state} />
{:else}
<div class="flex gap-x-2 items-center">
<b>{key}</b> {$tags[key]}
</div>
{/if}
</div>
{/if}
</div>

View file

@ -1,124 +1,136 @@
<script lang="ts"> <script lang="ts">
import LinkableImage from "../Image/LinkableImage.svelte" import LinkableImage from "../Image/LinkableImage.svelte"
import { UIEventSource } from "../../Logic/UIEventSource" import { UIEventSource } from "../../Logic/UIEventSource"
import type { OsmTags } from "../../Models/OsmFeature" import type { OsmTags } from "../../Models/OsmFeature"
import type { SpecialVisualizationState } from "../SpecialVisualization" import type { SpecialVisualizationState } from "../SpecialVisualization"
import type { Feature } from "geojson" import type { Feature } from "geojson"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig" import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import ComparisonAction from "./ComparisonAction.svelte" import ComparisonAction from "./ComparisonAction.svelte"
import Party from "../../assets/svg/Party.svelte" import Party from "../../assets/svg/Party.svelte"
import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction" import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction"
import { Tag } from "../../Logic/Tags/Tag" import { Tag } from "../../Logic/Tags/Tag"
import { And } from "../../Logic/Tags/And" import { And } from "../../Logic/Tags/And"
import Loading from "../Base/Loading.svelte" import Loading from "../Base/Loading.svelte"
import AttributedImage from "../Image/AttributedImage.svelte" import AttributedImage from "../Image/AttributedImage.svelte"
import Translations from "../i18n/Translations"
import Tr from "../Base/Tr.svelte"
export let osmProperties: Record<string, string> export let osmProperties: Record<string, string>
export let externalProperties: Record<string, string> export let externalProperties: Record<string, string>
export let sourceUrl: string
export let tags: UIEventSource<OsmTags> export let tags: UIEventSource<OsmTags>
export let state: SpecialVisualizationState export let state: SpecialVisualizationState
export let feature: Feature export let feature: Feature
export let layer: LayerConfig export let layer: LayerConfig
export let readonly = false export let readonly = false
let externalKeys: string[] = Object.keys(externalProperties).sort() const t = Translations.t.external
const imageKeyRegex = /image|image:[0-9]+/ let externalKeys: string[] = Object.keys(externalProperties).sort()
let knownImages = new Set(
Object.keys(osmProperties)
.filter((k) => k.match(imageKeyRegex))
.map((k) => osmProperties[k])
)
let unknownImages = externalKeys
.filter((k) => k.match(imageKeyRegex))
.map((k) => externalProperties[k])
.filter((i) => !knownImages.has(i))
let propertyKeysExternal = externalKeys.filter((k) => k.match(imageKeyRegex) === null) const imageKeyRegex = /image|image:[0-9]+/
let missing = propertyKeysExternal.filter((k) => osmProperties[k] === undefined && typeof externalProperties[k] === "string") let knownImages = new Set(
let same = propertyKeysExternal.filter((key) => osmProperties[key] === externalProperties[key]) Object.keys(osmProperties)
let different = propertyKeysExternal.filter( .filter((k) => k.match(imageKeyRegex))
(key) => osmProperties[key] !== undefined && osmProperties[key] !== externalProperties[key] && typeof externalProperties[key] === "string" .map((k) => osmProperties[k]),
) )
let unknownImages = externalKeys
.filter((k) => k.match(imageKeyRegex))
.map((k) => externalProperties[k])
.filter((i) => !knownImages.has(i))
let currentStep: "init" | "applying_all" | "all_applied" = "init" let propertyKeysExternal = externalKeys.filter((k) => k.match(imageKeyRegex) === null)
let missing = propertyKeysExternal.filter((k) => osmProperties[k] === undefined && typeof externalProperties[k] === "string")
let same = propertyKeysExternal.filter((key) => osmProperties[key] === externalProperties[key])
let different = propertyKeysExternal.filter(
(key) => osmProperties[key] !== undefined && osmProperties[key] !== externalProperties[key] && typeof externalProperties[key] === "string",
)
async function applyAllMissing() { let currentStep: "init" | "applying_all" | "all_applied" = "init"
currentStep = "applying_all" let applyAllHovered = false
const tagsToApply = missing.map((k) => new Tag(k, externalProperties[k]))
const change = new ChangeTagAction(tags.data.id, new And(tagsToApply), tags.data, { async function applyAllMissing() {
theme: state.layout.id, currentStep = "applying_all"
changeType: "import", const tagsToApply = missing.map((k) => new Tag(k, externalProperties[k]))
}) const change = new ChangeTagAction(tags.data.id, new And(tagsToApply), tags.data, {
await state.changes.applyChanges(await change.CreateChangeDescriptions()) theme: state.layout.id,
currentStep = "all_applied" changeType: "import",
} })
await state.changes.applyChanges(await change.CreateChangeDescriptions())
currentStep = "all_applied"
}
</script> </script>
{#if different.length > 0}
<h3>Conflicting items</h3>
<table>
<tr>
<th>Key</th>
<th>OSM</th>
<th>External</th>
</tr>
{#each different as key}
<ComparisonAction {key} {state} {tags} {externalProperties} {layer} {feature} {readonly} />
{/each}
</table>
{/if}
{#if missing.length > 0}
{#if currentStep === "init"}
<table class="w-full">
<tr>
<th>Key</th>
<th>External</th>
</tr>
{#each missing as key}
<ComparisonAction {key} {state} {tags} {externalProperties} {layer} {feature} {readonly} />
{/each}
</table>
{#if !readonly}
<button on:click={() => applyAllMissing()}>Apply all missing values</button>
{/if}
{:else if currentStep === "applying_all"}
<Loading>Applying all missing values</Loading>
{:else if currentStep === "all_applied"}
<div class="thanks">All values are applied</div>
{/if}
{/if}
{#if unknownImages.length === 0 && missing.length === 0 && different.length === 0} {#if unknownImages.length === 0 && missing.length === 0 && different.length === 0}
<div class="thanks m-0 flex items-center gap-x-2 px-2"> <div class="thanks m-0 flex items-center gap-x-2 px-2">
<Party class="h-8 w-8" /> <Party class="h-8 w-8" />
All data from Velopark is also included into OpenStreetMap <Tr t={t.allIncluded} />
</div> </div>
{/if} {:else}
<div class="low-interaction p-1 border-interactive">
{#if unknownImages.length > 0} <Tr t={t.loadedFrom.Subs({url: sourceUrl, source: sourceUrl})} />
{#if readonly} <h3>
<div class="w-full overflow-x-auto"> <Tr t={t.conflicting.title} />
<div class="flex h-32 w-max gap-x-2"> </h3>
{#each unknownImages as image} <div class="flex flex-col gap-y-8">
<AttributedImage <Tr t={t.conflicting.intro} />
imgClass="h-32 w-max shrink-0" {#if different.length > 0}
image={{ url: image }} {#each different as key}
previewedImage={state.previewedImage} <div class="mx-2 rounded-2xl">
/> <ComparisonAction {key} {state} {tags} {externalProperties} {layer} {feature} {readonly} />
</div>
{/each} {/each}
</div> {/if}
{#if missing.length > 0}
{#if currentStep === "init"}
{#each missing as key}
<div class:glowing-shadow={applyAllHovered} class="mx-2 rounded-2xl">
<ComparisonAction {key} {state} {tags} {externalProperties} {layer} {feature} {readonly} />
</div>
{/each}
{#if !readonly && missing.length > 1}
<button on:click={() => applyAllMissing()}
on:mouseover={() => applyAllHovered = true}
on:focus={() => applyAllHovered = true}
on:blur={() => applyAllHovered = false}
on:mouseout={() => applyAllHovered = false }
>
<Tr t={t.applyAll} />
</button>
{/if}
{:else if currentStep === "applying_all"}
<Loading/>
{:else if currentStep === "all_applied"}
<div class="thanks">
<Tr t={t.allAreApplied} />
</div>
{/if}
{/if}
</div> </div>
{:else}
{#each unknownImages as image} {#if unknownImages.length > 0}
<LinkableImage {#if readonly}
{tags} <div class="w-full overflow-x-auto">
{state} <div class="flex h-32 w-max gap-x-2">
image={{ {#each unknownImages as image}
<AttributedImage
imgClass="h-32 w-max shrink-0"
image={{ url: image }}
previewedImage={state.previewedImage}
/>
{/each}
</div>
</div>
{:else}
{#each unknownImages as image}
<LinkableImage
{tags}
{state}
image={{
pictureUrl: image, pictureUrl: image,
provider: "Velopark", provider: "Velopark",
thumbUrl: image, thumbUrl: image,
@ -126,9 +138,12 @@
coordinates: undefined, coordinates: undefined,
osmTags: { image }, osmTags: { image },
}} }}
{feature} {feature}
{layer} {layer}
/> />
{/each} {/each}
{/if} {/if}
{/if}
</div>
{/if} {/if}

View file

@ -10,6 +10,8 @@
import LayerConfig from "../../Models/ThemeConfig/LayerConfig" import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import type { Feature } from "geojson" import type { Feature } from "geojson"
import type { OsmTags } from "../../Models/OsmFeature" import type { OsmTags } from "../../Models/OsmFeature"
import Translations from "../i18n/Translations"
import Tr from "../Base/Tr.svelte"
export let externalData: Store<{ success: {content: Record<string, string> } } | { error: string } | undefined | null /* null if no URL is found, undefined if loading*/> export let externalData: Store<{ success: {content: Record<string, string> } } | { error: string } | undefined | null /* null if no URL is found, undefined if loading*/>
export let state: SpecialVisualizationState export let state: SpecialVisualizationState
@ -17,14 +19,17 @@
export let layer: LayerConfig export let layer: LayerConfig
export let feature: Feature export let feature: Feature
export let readonly = false export let readonly = false
export let sourceUrl: Store<string>
</script> </script>
{#if $externalData === null} {#if !$sourceUrl}
<!-- empty block --> <!-- empty block -->
{:else if $externalData === undefined} {:else if $externalData === undefined}
<Loading>{$externalData}</Loading> <Loading/>
{:else if $externalData["error"] !== undefined} {:else if $externalData["error"] !== undefined}
<div class="alert"> <div class="alert flex">
Something went wrong: {$externalData["error"]} <Tr t={Translations.t.general.error}/>
{$externalData["error"]}
</div> </div>
{:else if $externalData["success"] !== undefined} {:else if $externalData["success"] !== undefined}
<ComparisonTable <ComparisonTable
@ -35,5 +40,6 @@
{layer} {layer}
{tags} {tags}
{readonly} {readonly}
sourceUrl={$sourceUrl}
/> />
{/if} {/if}

View file

@ -1743,6 +1743,7 @@ export default class SpecialVisualizations {
} }
return ({ url: tags[key], country: tags._country }) return ({ url: tags[key], country: tags._country })
}) })
const sourceUrl: Store<string | undefined> = url.mapD(url => url.url)
const externalData: Store<{ success: { content: any } } | { const externalData: Store<{ success: { content: any } } | {
error: string error: string
} | undefined | null> = url.bindD(({ } | undefined | null> = url.bindD(({
@ -1759,6 +1760,7 @@ export default class SpecialVisualizations {
tags, tags,
layer, layer,
externalData, externalData,
sourceUrl
}), undefined, url.map(url => !!url), }), undefined, url.map(url => !!url),
) )
}, },