import {sortByNumberAscending, sortByStringAscending, sortByDateAscending} from '../../services/sorting.service'; import {Priority, ScrumTask, ScrumUserstory, ScrumSprint} from '../../services/backend.service'; import { ActivatedRoute, ParamMap } from '@angular/router'; export abstract class TableComponentBase { public sortBy: string; public sortDescending = false; public items: T[] = []; public highlightId: number; protected route: ActivatedRoute; /** * Constructor that handels ParamMap extraction from given route and calls the applyFilterParameters method with the extracted ParamMap. * @param route ActivatedRoute object to get ParamMap from. */ public constructor(route: ActivatedRoute) { this.route = route; this.applyFilterParameters(this.route.snapshot.paramMap); this.route.paramMap.subscribe((map) => this.applyFilterParameters(map)); } //#region getters /** * Returns a string array containing all possible values of the enum Priority. */ public getAllPriorities(): string[] { return Object.values(Priority); } //#endregion getters //#region tableFunctions /** * Parses ParamMap from url (route) and sets parsed params accordingly. * Only used to extract the id from the ParamMap to highlight a certain item. * @param params ParamMap read from url */ private applyFilterParameters(params: ParamMap) { if (params.has('id')) { this.highlightId = parseInt(params.get('id')); } } //#endregion tableFunctions //#region sorters /** * Sorts items by number of a provided kay * @param by TODO: * @param key property/key to sort the items by */ protected doNumericSort(by: string, key: (item: T) => number) { if (this.sortBy === by) { this.sortDescending = !this.sortDescending; } else { this.sortBy = by; } this.items = sortByNumberAscending(this.items, key); if (this.sortDescending) { this.items = this.items.reverse(); } } /** * Sorts items by string of a provided kay * @param by TODO: * @param key property/key to sort the items by */ protected doStringSort(by: string, key: (item: T) => string) { if (this.sortBy === by) { this.sortDescending = !this.sortDescending; } else { this.sortBy = by; } this.items = sortByStringAscending(this.items, key); if (this.sortDescending) { this.items = this.items.reverse(); } } /** * Sorts items by date of a provided kay * @param by TODO: * @param key property/key to sort the items by */ protected doDateSort(by: string, key: (item: T) => Date) { if (this.sortBy === by) { this.sortDescending = !this.sortDescending; } else { this.sortBy = by; } this.items = sortByDateAscending(this.items, key); if (this.sortDescending) { this.items = this.items.reverse(); } } /** * Sorts this tabel's items by id */ public sortById() { this.doNumericSort('id', (obj) => obj.id); } /** * Sorts this tabel's items by title */ public sortByTitle() { this.doStringSort('title', (obj) => obj.title); } //#endregion sorters }