From 583af07412822a8bd115b534edf32bf003cbc1eb Mon Sep 17 00:00:00 2001 From: vcoppe Date: Sun, 28 Apr 2024 18:59:31 +0200 Subject: [PATCH] refactoring for tools and start waypoint --- gpx/src/gpx.ts | 8 ++ .../src/lib/components/gpx-layer/GPXLayer.ts | 32 ++++++- .../lib/components/gpx-layer/GPXLayers.svelte | 53 ++++++++---- .../components/gpx-layer/WaypointPopup.svelte | 13 +++ .../src/lib/components/toolbar/Toolbar.svelte | 34 ++++---- .../lib/components/toolbar/ToolbarItem.svelte | 14 +++- .../components/toolbar/ToolbarItemMenu.svelte | 21 ++++- .../toolbar/tools/routing/Routing.svelte | 84 ++++++++----------- .../toolbar/tools/waypoint/Waypoint.svelte | 24 ++++++ website/src/lib/stores.ts | 14 +++- 10 files changed, 202 insertions(+), 95 deletions(-) create mode 100644 website/src/lib/components/gpx-layer/WaypointPopup.svelte create mode 100644 website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte diff --git a/gpx/src/gpx.ts b/gpx/src/gpx.ts index 2a9e8349..3920cbeb 100644 --- a/gpx/src/gpx.ts +++ b/gpx/src/gpx.ts @@ -567,6 +567,14 @@ export class Waypoint { this.sym = waypoint.sym; this.type = waypoint.type; } + + getCoordinates(): Coordinates { + return this.attributes; + } + + setCoordinates(coordinates: Coordinates): void { + this.attributes = coordinates; + } } export class GPXStatistics { diff --git a/website/src/lib/components/gpx-layer/GPXLayer.ts b/website/src/lib/components/gpx-layer/GPXLayer.ts index 94f0776e..ded70c94 100644 --- a/website/src/lib/components/gpx-layer/GPXLayer.ts +++ b/website/src/lib/components/gpx-layer/GPXLayer.ts @@ -1,7 +1,7 @@ -import type { GPXFile } from "gpx"; +import type { GPXFile, Waypoint } from "gpx"; import { map, selectFiles, currentTool, Tool } from "$lib/stores"; import { get, type Writable } from "svelte/store"; -import type mapboxgl from "mapbox-gl"; +import mapboxgl from "mapbox-gl"; let id = 0; function getLayerId() { @@ -46,16 +46,21 @@ export class GPXLayer { file: Writable; layerId: string; layerColor: string; + popup: mapboxgl.Popup; + popupElement: HTMLElement; + markers: mapboxgl.Marker[] = []; unsubscribe: () => void; addBinded: () => void = this.add.bind(this); selectOnClickBinded: (e: any) => void = this.selectOnClick.bind(this); - constructor(map: mapboxgl.Map, file: Writable) { + constructor(map: mapboxgl.Map, file: Writable, popup: mapboxgl.Popup, popupElement: HTMLElement) { this.map = map; this.file = file; this.layerId = getLayerId(); this.layerColor = getColor(); + this.popup = popup; + this.popupElement = popupElement; this.unsubscribe = file.subscribe(this.updateData.bind(this)); get(this.file)._data = { @@ -97,6 +102,23 @@ export class GPXLayer { this.map.on('mouseenter', this.layerId, toPointerCursor); this.map.on('mouseleave', this.layerId, toDefaultCursor); } + + if (this.markers.length == 0) { + get(this.file).wpt.forEach((waypoint) => { + let marker = new mapboxgl.Marker().setLngLat(waypoint.getCoordinates()); + marker.getElement().addEventListener('click', (e) => { + marker.setPopup(this.popup); + marker.togglePopup(); + e.stopPropagation(); + }); + + this.markers.push(marker); + }); + } + + this.markers.forEach((marker) => { + marker.addTo(this.map); + }); } updateData() { @@ -115,6 +137,10 @@ export class GPXLayer { this.map.removeLayer(this.layerId); this.map.removeSource(this.layerId); + this.markers.forEach((marker) => { + marker.remove(); + }); + this.unsubscribe(); decrementColor(this.layerColor); diff --git a/website/src/lib/components/gpx-layer/GPXLayers.svelte b/website/src/lib/components/gpx-layer/GPXLayers.svelte index 568d90d1..e627f531 100644 --- a/website/src/lib/components/gpx-layer/GPXLayers.svelte +++ b/website/src/lib/components/gpx-layer/GPXLayers.svelte @@ -1,31 +1,48 @@ + + diff --git a/website/src/lib/components/gpx-layer/WaypointPopup.svelte b/website/src/lib/components/gpx-layer/WaypointPopup.svelte new file mode 100644 index 00000000..6b0bdba2 --- /dev/null +++ b/website/src/lib/components/gpx-layer/WaypointPopup.svelte @@ -0,0 +1,13 @@ + + + diff --git a/website/src/lib/components/toolbar/Toolbar.svelte b/website/src/lib/components/toolbar/Toolbar.svelte index e176a0e0..53e51668 100644 --- a/website/src/lib/components/toolbar/Toolbar.svelte +++ b/website/src/lib/components/toolbar/Toolbar.svelte @@ -1,6 +1,7 @@
@@ -31,47 +24,48 @@
- + {$_('toolbar.routing.tooltip')} - + {$_('toolbar.time_tooltip')} - + {$_('toolbar.reverse_tooltip')} - + {$_('toolbar.merge_tooltip')} - + {$_('toolbar.extract_tooltip')} - + {$_('toolbar.waypoint_tooltip')} - + {$_('toolbar.reduce_tooltip')} - + {$_('toolbar.clean_tooltip')} - + {$_('toolbar.style_tooltip')} - + {$_('toolbar.structure_tooltip')}
+
diff --git a/website/src/lib/components/toolbar/ToolbarItem.svelte b/website/src/lib/components/toolbar/ToolbarItem.svelte index 4f270f11..b6be5685 100644 --- a/website/src/lib/components/toolbar/ToolbarItem.svelte +++ b/website/src/lib/components/toolbar/ToolbarItem.svelte @@ -1,11 +1,23 @@ - diff --git a/website/src/lib/components/toolbar/ToolbarItemMenu.svelte b/website/src/lib/components/toolbar/ToolbarItemMenu.svelte index ce3471a4..8c6df33c 100644 --- a/website/src/lib/components/toolbar/ToolbarItemMenu.svelte +++ b/website/src/lib/components/toolbar/ToolbarItemMenu.svelte @@ -1,9 +1,22 @@ -
-
- +{#if active} +
+
+ + + + + +
-
+{/if} diff --git a/website/src/lib/components/toolbar/tools/routing/Routing.svelte b/website/src/lib/components/toolbar/tools/routing/Routing.svelte index 0880ab1d..1c56090e 100644 --- a/website/src/lib/components/toolbar/tools/routing/Routing.svelte +++ b/website/src/lib/components/toolbar/tools/routing/Routing.svelte @@ -1,13 +1,12 @@ -{#if active} - - - -
- - - - - - - {#each Object.keys(brouterProfiles) as profile} - {$_(`toolbar.routing.activities.${profile}`)} - {/each} - - -
-
- - -
-
- - -
- - - - - {#if $selectedFiles.size > 1} -
{$_('toolbar.routing.help_multiple_files')}
- {:else if $selectedFiles.size == 0} -
{$_('toolbar.routing.help_no_file')}
- {:else} -
{$_('toolbar.routing.help')}
- {/if} -
-
-
-
-
-{/if} + +
+ + + + + + + {#each Object.keys(brouterProfiles) as profile} + {$_(`toolbar.routing.activities.${profile}`)} + {/each} + + +
+
+ + +
+
+ + +
+ + + + {#if $selectedFiles.size > 1} +
{$_('toolbar.routing.help_multiple_files')}
+ {:else if $selectedFiles.size == 0} +
{$_('toolbar.routing.help_no_file')}
+ {:else} +
{$_('toolbar.routing.help')}
+ {/if} +
+
+
diff --git a/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte b/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte new file mode 100644 index 00000000..e6f5c76f --- /dev/null +++ b/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte @@ -0,0 +1,24 @@ + + + +
todo
+ + + + +
{$_('toolbar.waypoint.help')}
+
+
+
diff --git a/website/src/lib/stores.ts b/website/src/lib/stores.ts index 462b6f3d..c9d82132 100644 --- a/website/src/lib/stores.ts +++ b/website/src/lib/stores.ts @@ -4,6 +4,7 @@ import mapboxgl from 'mapbox-gl'; import { GPXFile, buildGPX, parseGPX, type AnyGPXTreeElement } from 'gpx'; import { tick } from 'svelte'; import { _ } from 'svelte-i18n'; +import type { GPXLayer } from '$lib/components/gpx-layer/GPXLayer'; export const map = writable(null); export const files = writable[]>([]); @@ -16,8 +17,19 @@ export const settings = writable<{ [key: string]: any }>({ temperatureUnits: 'celsius', mode: 'system' }); +export const gpxLayers: Writable, GPXLayer>> = writable(new Map()); + export enum Tool { - ROUTING + ROUTING, + TIME, + REVERSE, + MERGE, + EXTRACT, + WAYPOINT, + REDUCE, + CLEAN, + STYLE, + STRUCTURE } export const currentTool = writable(null);