Files
gpx.studio/website/src/lib/components/file-list/Selection.ts

95 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-05-22 16:05:31 +02:00
import { get, writable } from "svelte/store";
2024-05-23 16:35:20 +02:00
import { ListFileItem, ListItem, ListRootItem, ListTrackItem, ListTrackSegmentItem, ListWaypointItem, SelectionTreeType, type ListLevel } from "./FileList";
import { fileObservers, settings } from "$lib/db";
2024-05-22 16:05:31 +02:00
export const selection = writable<SelectionTreeType>(new SelectionTreeType(new ListRootItem()));
2024-05-23 14:44:07 +02:00
export function selectItem(item: ListItem) {
2024-05-23 11:21:57 +02:00
selection.update(($selection) => {
$selection.clear();
2024-05-23 14:44:07 +02:00
$selection.set(item, true);
2024-05-23 11:21:57 +02:00
return $selection;
});
}
2024-05-23 14:44:07 +02:00
export function selectFile(fileId: string) {
selectItem(new ListFileItem(fileId));
}
2024-05-24 13:16:41 +02:00
export function addSelectItem(item: ListItem) {
2024-05-23 11:21:57 +02:00
selection.update(($selection) => {
2024-05-24 13:16:41 +02:00
$selection.toggle(item);
2024-05-23 11:21:57 +02:00
return $selection;
});
}
2024-05-24 13:16:41 +02:00
export function addSelectFile(fileId: string) {
addSelectItem(new ListFileItem(fileId));
}
2024-05-22 16:05:31 +02:00
export function selectAll() {
selection.update(($selection) => {
2024-05-23 15:08:34 +02:00
let item: ListItem = new ListRootItem();
$selection.forEach((i) => {
item = i;
2024-05-22 16:05:31 +02:00
});
2024-05-23 15:08:34 +02:00
if (item instanceof ListRootItem || item instanceof ListFileItem) {
$selection.clear();
get(fileObservers).forEach((_file, fileId) => {
$selection.set(new ListFileItem(fileId), true);
});
} else if (item instanceof ListTrackItem) {
let fileStore = get(fileObservers).get(item.getFileId());
if (fileStore) {
get(fileStore)?.file.trk.forEach((_track, trackId) => {
$selection.set(new ListTrackItem(item.getFileId(), trackId), true);
});
}
} else if (item instanceof ListTrackSegmentItem) {
let fileStore = get(fileObservers).get(item.getFileId());
if (fileStore) {
get(fileStore)?.file.trk[item.getTrackIndex()].trkseg.forEach((_segment, segmentId) => {
$selection.set(new ListTrackSegmentItem(item.getFileId(), item.getTrackIndex(), segmentId), true);
});
}
} else if (item instanceof ListWaypointItem) {
let fileStore = get(fileObservers).get(item.getFileId());
if (fileStore) {
get(fileStore)?.file.wpt.forEach((_waypoint, waypointId) => {
$selection.set(new ListWaypointItem(item.getFileId(), waypointId), true);
});
}
}
2024-05-22 16:05:31 +02:00
return $selection;
});
2024-05-23 16:35:20 +02:00
}
export function applyToOrderedSelectedItemsFromFile(callback: (fileId: string, level: ListLevel | undefined, items: ListItem[]) => void) {
get(settings.fileOrder).forEach((fileId) => {
let level: ListLevel | undefined = undefined;
let items: ListItem[] = [];
get(selection).forEach((item) => {
if (item.getFileId() === fileId) {
level = item.level;
if (item instanceof ListTrackItem || item instanceof ListTrackSegmentItem || item instanceof ListWaypointItem) {
items.push(item);
}
}
});
items.sort((a, b) => { // Process the items in reverse order to avoid index conflicts
if (a instanceof ListTrackItem && b instanceof ListTrackItem) {
return b.getTrackIndex() - a.getTrackIndex();
} else if (a instanceof ListTrackSegmentItem && b instanceof ListTrackSegmentItem) {
return b.getSegmentIndex() - a.getSegmentIndex();
} else if (a instanceof ListWaypointItem && b instanceof ListWaypointItem) {
return b.getWaypointIndex() - a.getWaypointIndex();
}
2024-05-24 20:23:49 +02:00
return b.level - a.level;
2024-05-23 16:35:20 +02:00
});
callback(fileId, level, items);
});
2024-05-22 16:05:31 +02:00
}