mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-11-04 21:41:07 +00:00
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
|
|
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 {
|
||
|
|
let statistics = new GPXStatistics();
|
||
|
|
let id = item.getIdAtLevel(this.level);
|
||
|
|
if (id === undefined || id === 'waypoints') {
|
||
|
|
Object.keys(this.statistics).forEach((key) => {
|
||
|
|
if (this.statistics[key] instanceof GPXStatistics) {
|
||
|
|
statistics.mergeWith(this.statistics[key]);
|
||
|
|
} else {
|
||
|
|
statistics.mergeWith(this.statistics[key].getStatisticsFor(item));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
let child = this.statistics[id];
|
||
|
|
if (child instanceof GPXStatistics) {
|
||
|
|
statistics.mergeWith(child);
|
||
|
|
} else if (child !== undefined) {
|
||
|
|
statistics.mergeWith(child.getStatisticsFor(item));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return statistics;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
export type GPXFileWithStatistics = { file: GPXFile; statistics: GPXStatisticsTree };
|