2025-10-05 19:34:05 +02:00
|
|
|
import { ListWaypointItem } from '$lib/components/file-list/file-list';
|
2025-10-17 23:54:45 +02:00
|
|
|
import { fileStateCollection } from '$lib/logic/file-state';
|
|
|
|
|
import { selection } from '$lib/logic/selection';
|
|
|
|
|
import { settings } from '$lib/logic/settings';
|
2025-10-05 19:34:05 +02:00
|
|
|
import type { Waypoint } from 'gpx';
|
2025-10-17 23:54:45 +02:00
|
|
|
import { get, writable, type Writable } from 'svelte/store';
|
2025-10-05 19:34:05 +02:00
|
|
|
|
|
|
|
|
export class WaypointSelection {
|
2025-10-17 23:54:45 +02:00
|
|
|
private _selection: Writable<[Waypoint, string] | undefined>;
|
2025-10-05 19:34:05 +02:00
|
|
|
|
|
|
|
|
constructor() {
|
2025-10-17 23:54:45 +02:00
|
|
|
this._selection = writable(undefined);
|
|
|
|
|
settings.treeFileView.subscribe(() => {
|
|
|
|
|
this.update();
|
|
|
|
|
});
|
|
|
|
|
selection.subscribe(() => {
|
|
|
|
|
this.update();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update() {
|
|
|
|
|
this._selection.update(() => {
|
|
|
|
|
if (get(settings.treeFileView) && get(selection).size === 1) {
|
|
|
|
|
let item = get(selection).getSelected()[0];
|
2025-10-05 19:34:05 +02:00
|
|
|
if (item instanceof ListWaypointItem) {
|
|
|
|
|
let file = fileStateCollection.getFile(item.getFileId());
|
|
|
|
|
let waypoint = file?.wpt[item.getWaypointIndex()];
|
|
|
|
|
if (waypoint) {
|
|
|
|
|
return [waypoint, item.getFileId()];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reset() {
|
2025-10-17 23:54:45 +02:00
|
|
|
this._selection.set(undefined);
|
2025-10-05 19:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get wpt(): Waypoint | undefined {
|
2025-10-17 23:54:45 +02:00
|
|
|
const selection = get(this._selection);
|
|
|
|
|
return selection ? selection[0] : undefined;
|
2025-10-05 19:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get fileId(): string | undefined {
|
2025-10-17 23:54:45 +02:00
|
|
|
const selection = get(this._selection);
|
|
|
|
|
return selection ? selection[1] : undefined;
|
2025-10-05 19:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO update the waypoint data if the file changes
|
|
|
|
|
// function updateWaypointData(fileStore: GPXFileWithStatistics | undefined) {
|
|
|
|
|
// if (selectedWaypoint.wpt) {
|
|
|
|
|
// if (fileStore) {
|
|
|
|
|
// if ($selectedWaypoint[0]._data.index < fileStore.file.wpt.length) {
|
|
|
|
|
// $selectedWaypoint[0] = fileStore.file.wpt[$selectedWaypoint[0]._data.index];
|
|
|
|
|
// name = $selectedWaypoint[0].name ?? '';
|
|
|
|
|
// description = $selectedWaypoint[0].desc ?? '';
|
|
|
|
|
// if (
|
|
|
|
|
// $selectedWaypoint[0].cmt !== undefined &&
|
|
|
|
|
// $selectedWaypoint[0].cmt !== $selectedWaypoint[0].desc
|
|
|
|
|
// ) {
|
|
|
|
|
// description += '\n\n' + $selectedWaypoint[0].cmt;
|
|
|
|
|
// }
|
|
|
|
|
// link = $selectedWaypoint[0].link?.attributes?.href ?? '';
|
|
|
|
|
// let symbol = $selectedWaypoint[0].sym ?? '';
|
|
|
|
|
// symbolKey = getSymbolKey(symbol) ?? symbol ?? '';
|
|
|
|
|
// longitude = parseFloat($selectedWaypoint[0].getLongitude().toFixed(6));
|
|
|
|
|
// latitude = parseFloat($selectedWaypoint[0].getLatitude().toFixed(6));
|
|
|
|
|
// } else {
|
|
|
|
|
// selectedWaypoint.reset();
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// selectedWaypoint.reset();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const selectedWaypoint = new WaypointSelection();
|