2025-10-18 00:31:14 +02:00
|
|
|
import { ListItem, ListLevel } from '$lib/components/file-list/file-list';
|
|
|
|
|
import { GPXFile, GPXStatistics, type Track } from 'gpx';
|
|
|
|
|
|
|
|
|
|
export class GPXStatisticsTree {
|
|
|
|
|
level: ListLevel;
|
|
|
|
|
statistics: {
|
|
|
|
|
[key: string]: GPXStatisticsTree | GPXStatistics;
|
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
|
|
constructor(element: GPXFile | Track) {
|
|
|
|
|
if (element instanceof GPXFile) {
|
|
|
|
|
this.level = ListLevel.FILE;
|
|
|
|
|
element.children.forEach((child, index) => {
|
|
|
|
|
this.statistics[index] = new GPXStatisticsTree(child);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.level = ListLevel.TRACK;
|
|
|
|
|
element.children.forEach((child, index) => {
|
|
|
|
|
this.statistics[index] = child.getStatistics();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStatisticsFor(item: ListItem): GPXStatistics {
|
2025-12-24 12:21:27 +01:00
|
|
|
let statistics = [];
|
2025-10-18 00:31:14 +02:00
|
|
|
let id = item.getIdAtLevel(this.level);
|
|
|
|
|
if (id === undefined || id === 'waypoints') {
|
|
|
|
|
Object.keys(this.statistics).forEach((key) => {
|
|
|
|
|
if (this.statistics[key] instanceof GPXStatistics) {
|
2025-12-24 12:21:27 +01:00
|
|
|
statistics.push(this.statistics[key]);
|
2025-10-18 00:31:14 +02:00
|
|
|
} else {
|
2025-12-24 12:21:27 +01:00
|
|
|
statistics.push(this.statistics[key].getStatisticsFor(item));
|
2025-10-18 00:31:14 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
let child = this.statistics[id];
|
|
|
|
|
if (child instanceof GPXStatistics) {
|
2025-12-24 12:21:27 +01:00
|
|
|
statistics.push(child);
|
2025-10-18 00:31:14 +02:00
|
|
|
} else if (child !== undefined) {
|
2025-12-24 12:21:27 +01:00
|
|
|
statistics.push(child.getStatisticsFor(item));
|
2025-10-18 00:31:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-24 12:21:27 +01:00
|
|
|
if (statistics.length === 0) {
|
|
|
|
|
return new GPXStatistics();
|
|
|
|
|
} else if (statistics.length === 1) {
|
|
|
|
|
return statistics[0];
|
|
|
|
|
} else {
|
|
|
|
|
return statistics.reduce((acc, curr) => {
|
|
|
|
|
acc.mergeWith(curr);
|
|
|
|
|
return acc;
|
|
|
|
|
}, new GPXStatistics());
|
|
|
|
|
}
|
2025-10-18 00:31:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export type GPXFileWithStatistics = { file: GPXFile; statistics: GPXStatisticsTree };
|