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

366 lines
9.0 KiB
TypeScript
Raw Normal View History

2024-05-22 16:05:31 +02:00
export class SelectionTreeType {
item: ListItem;
selected: boolean;
children: {
[key: string | number]: SelectionTreeType
};
2024-05-24 16:37:26 +02:00
size: number = 0;
2024-05-22 16:05:31 +02:00
constructor(item: ListItem) {
this.item = item;
this.selected = false;
this.children = {};
}
clear() {
this.selected = false;
for (let key in this.children) {
this.children[key].clear();
}
2024-05-24 16:37:26 +02:00
this.size = 0;
2024-05-22 16:05:31 +02:00
}
_setOrToggle(item: ListItem, value?: boolean) {
if (item.level === this.item.level) {
2024-05-24 16:37:26 +02:00
let newSelected = value === undefined ? !this.selected : value;
if (this.selected !== newSelected) {
this.selected = newSelected;
this.size += this.selected ? 1 : -1;
}
2024-05-22 16:05:31 +02:00
} else {
let id = item.getIdAtLevel(this.item.level);
if (id !== undefined) {
if (!this.children.hasOwnProperty(id)) {
this.children[id] = new SelectionTreeType(this.item.extend(id));
}
2024-05-24 16:37:26 +02:00
this.size -= this.children[id].size;
2024-05-22 16:05:31 +02:00
this.children[id]._setOrToggle(item, value);
2024-05-24 16:37:26 +02:00
this.size += this.children[id].size;
2024-05-22 16:05:31 +02:00
}
}
}
set(item: ListItem, value: boolean) {
this._setOrToggle(item, value);
}
toggle(item: ListItem) {
this._setOrToggle(item);
}
has(item: ListItem): boolean {
if (item.level === this.item.level) {
return this.selected;
} else {
let id = item.getIdAtLevel(this.item.level);
if (id !== undefined) {
if (this.children.hasOwnProperty(id)) {
return this.children[id].has(item);
}
}
}
return false;
}
2024-05-24 13:16:41 +02:00
hasAnyParent(item: ListItem, self: boolean = true): boolean {
if (this.selected && this.item.level <= item.level && (self || this.item.level < item.level)) {
return this.selected;
}
let id = item.getIdAtLevel(this.item.level);
if (id !== undefined) {
if (this.children.hasOwnProperty(id)) {
return this.children[id].hasAnyParent(item, self);
}
}
return false;
}
2024-05-24 16:37:26 +02:00
hasAnyChildren(item: ListItem, self: boolean = true, ignoreIds?: (string | number)[]): boolean {
2024-05-24 13:16:41 +02:00
if (this.selected && this.item.level >= item.level && (self || this.item.level > item.level)) {
return this.selected;
}
let id = item.getIdAtLevel(this.item.level);
if (id !== undefined) {
2024-05-24 16:37:26 +02:00
if (ignoreIds === undefined || ignoreIds.indexOf(id) === -1) {
if (this.children.hasOwnProperty(id)) {
return this.children[id].hasAnyChildren(item, self, ignoreIds);
}
2024-05-24 13:16:41 +02:00
}
} else {
for (let key in this.children) {
2024-05-24 16:37:26 +02:00
if (ignoreIds === undefined || ignoreIds.indexOf(key) === -1) {
if (this.children[key].hasAnyChildren(item, self, ignoreIds)) {
return true;
}
2024-05-24 13:16:41 +02:00
}
}
}
return false;
}
2024-05-22 16:05:31 +02:00
getSelected(selection?: ListItem[]): ListItem[] {
if (selection === undefined) {
selection = [];
}
if (this.selected) {
selection.push(this.item);
}
for (let key in this.children) {
this.children[key].getSelected(selection);
}
return selection;
}
forEach(callback: (item: ListItem) => void) {
if (this.selected) {
callback(this.item);
}
for (let key in this.children) {
this.children[key].forEach(callback);
}
}
getChild(id: string | number): SelectionTreeType | undefined {
return this.children[id];
}
2024-05-23 16:35:20 +02:00
deleteChild(id: string | number) {
2024-05-24 16:37:26 +02:00
this.size -= this.children[id].size;
2024-05-23 16:35:20 +02:00
delete this.children[id];
}
2024-05-22 16:05:31 +02:00
};
2024-05-24 13:16:41 +02:00
export enum ListLevel {
ROOT,
FILE,
TRACK,
SEGMENT,
WAYPOINTS,
WAYPOINT
}
2024-05-22 16:05:31 +02:00
export abstract class ListItem {
level: ListLevel;
constructor(level: ListLevel) {
this.level = level;
}
abstract getId(): string | number;
abstract getIdAtLevel(level: ListLevel): string | number | undefined;
abstract getFileId(): string;
abstract extend(id: string | number): ListItem;
}
export class ListRootItem extends ListItem {
constructor() {
2024-05-24 13:16:41 +02:00
super(ListLevel.ROOT);
2024-05-22 16:05:31 +02:00
}
getId(): string {
return 'root';
}
getIdAtLevel(level: ListLevel): string | number | undefined {
return undefined;
}
getFileId(): string {
return '';
}
extend(id: string): ListFileItem {
return new ListFileItem(id);
}
}
export class ListFileItem extends ListItem {
fileId: string;
constructor(fileId: string) {
2024-05-24 13:16:41 +02:00
super(ListLevel.FILE);
2024-05-22 16:05:31 +02:00
this.fileId = fileId;
}
getId(): string {
return this.fileId;
}
getIdAtLevel(level: ListLevel): string | number | undefined {
switch (level) {
2024-05-24 13:16:41 +02:00
case ListLevel.ROOT:
2024-05-22 16:05:31 +02:00
return this.fileId;
default:
return undefined;
}
}
getFileId(): string {
return this.fileId;
}
extend(id: number | 'waypoints'): ListTrackItem | ListWaypointsItem {
if (id === 'waypoints') {
return new ListWaypointsItem(this.fileId);
} else {
return new ListTrackItem(this.fileId, id);
}
}
}
export class ListTrackItem extends ListItem {
fileId: string;
trackIndex: number;
constructor(fileId: string, trackIndex: number) {
2024-05-24 13:16:41 +02:00
super(ListLevel.TRACK);
2024-05-22 16:05:31 +02:00
this.fileId = fileId;
this.trackIndex = trackIndex;
}
getId(): number {
return this.trackIndex;
}
getIdAtLevel(level: ListLevel): string | number | undefined {
switch (level) {
2024-05-24 13:16:41 +02:00
case ListLevel.ROOT:
2024-05-22 16:05:31 +02:00
return this.fileId;
2024-05-24 13:16:41 +02:00
case ListLevel.FILE:
2024-05-22 16:05:31 +02:00
return this.trackIndex;
default:
return undefined;
}
}
getFileId(): string {
return this.fileId;
}
2024-05-23 14:44:07 +02:00
getTrackIndex(): number {
return this.trackIndex;
}
extend(id: number): ListTrackSegmentItem {
return new ListTrackSegmentItem(this.fileId, this.trackIndex, id);
2024-05-22 16:05:31 +02:00
}
}
2024-05-23 14:44:07 +02:00
export class ListTrackSegmentItem extends ListItem {
2024-05-22 16:05:31 +02:00
fileId: string;
trackIndex: number;
segmentIndex: number;
constructor(fileId: string, trackIndex: number, segmentIndex: number) {
2024-05-24 13:16:41 +02:00
super(ListLevel.SEGMENT);
2024-05-22 16:05:31 +02:00
this.fileId = fileId;
this.trackIndex = trackIndex;
this.segmentIndex = segmentIndex;
}
getId(): number {
return this.segmentIndex;
}
getIdAtLevel(level: ListLevel): string | number | undefined {
switch (level) {
2024-05-24 13:16:41 +02:00
case ListLevel.ROOT:
2024-05-22 16:05:31 +02:00
return this.fileId;
2024-05-24 13:16:41 +02:00
case ListLevel.FILE:
2024-05-22 16:05:31 +02:00
return this.trackIndex;
2024-05-24 13:16:41 +02:00
case ListLevel.TRACK:
2024-05-22 16:05:31 +02:00
return this.segmentIndex;
default:
return undefined;
}
}
getFileId(): string {
return this.fileId;
}
2024-05-23 14:44:07 +02:00
getTrackIndex(): number {
return this.trackIndex;
}
getSegmentIndex(): number {
return this.segmentIndex;
}
extend(): ListTrackSegmentItem {
2024-05-22 16:05:31 +02:00
return this;
}
}
export class ListWaypointsItem extends ListItem {
fileId: string;
constructor(fileId: string) {
2024-05-24 13:16:41 +02:00
super(ListLevel.WAYPOINTS);
2024-05-22 16:05:31 +02:00
this.fileId = fileId;
}
getId(): string {
return 'waypoints';
}
getIdAtLevel(level: ListLevel): string | number | undefined {
switch (level) {
2024-05-24 13:16:41 +02:00
case ListLevel.ROOT:
2024-05-22 16:05:31 +02:00
return this.fileId;
2024-05-24 13:16:41 +02:00
case ListLevel.FILE:
2024-05-22 16:05:31 +02:00
return 'waypoints';
default:
return undefined;
}
}
getFileId(): string {
return this.fileId;
}
extend(id: number): ListWaypointItem {
return new ListWaypointItem(this.fileId, id);
}
}
export class ListWaypointItem extends ListItem {
fileId: string;
waypointIndex: number;
constructor(fileId: string, waypointIndex: number) {
2024-05-24 13:16:41 +02:00
super(ListLevel.WAYPOINT);
2024-05-22 16:05:31 +02:00
this.fileId = fileId;
this.waypointIndex = waypointIndex;
}
getId(): number {
return this.waypointIndex;
}
getIdAtLevel(level: ListLevel): string | number | undefined {
switch (level) {
2024-05-24 13:16:41 +02:00
case ListLevel.ROOT:
2024-05-22 16:05:31 +02:00
return this.fileId;
2024-05-24 13:16:41 +02:00
case ListLevel.FILE:
2024-05-22 16:05:31 +02:00
return 'waypoints';
2024-05-24 13:16:41 +02:00
case ListLevel.WAYPOINTS:
2024-05-22 16:05:31 +02:00
return this.waypointIndex;
default:
return undefined;
}
}
getFileId(): string {
return this.fileId;
}
2024-05-23 14:44:07 +02:00
getWaypointIndex(): number {
return this.waypointIndex;
}
2024-05-22 16:05:31 +02:00
extend(): ListWaypointItem {
return this;
}
}