feat(sort): replace compound sort options with atomic fields and multi-sort support (#2686)
* fix(sidebar): replace routerLinkActive with getter-based route matching to prevent stale highlights * feat(sort): replace compound sort options with atomic fields and add 6 new sort criteria
This commit is contained in:
+4
-5
@@ -177,11 +177,10 @@
|
||||
<p-popover #sortPopover [dismissable]="true" appendTo="body">
|
||||
<app-multi-sort-popover
|
||||
[sortCriteria]="this.bookSorter.selectedSortCriteria"
|
||||
[availableSortOptions]="this.bookSorter.sortOptions"
|
||||
(addCriterion)="onAddSortCriterion($event)"
|
||||
(removeCriterion)="onRemoveSortCriterion($event)"
|
||||
(toggleDirection)="onToggleSortDirection($event)"
|
||||
(reorder)="onReorderSortCriteria($event)">
|
||||
[availableSortOptions]="visibleSortOptions"
|
||||
[showSaveButton]="canSaveSort"
|
||||
(criteriaChange)="onSortCriteriaChange($event)"
|
||||
(saveSortConfig)="onSaveSortConfig($event)">
|
||||
</app-multi-sort-popover>
|
||||
</p-popover>
|
||||
</div>
|
||||
|
||||
+88
-13
@@ -24,7 +24,7 @@ import {InputText} from 'primeng/inputtext';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {BookFilterComponent} from './book-filter/book-filter.component';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {BookFilterMode, EntityViewPreferences, UserService} from '../../../settings/user-management/user.service';
|
||||
import {BookFilterMode, DEFAULT_VISIBLE_SORT_FIELDS, EntityViewPreferences, SortCriterion, UserService} from '../../../settings/user-management/user.service';
|
||||
import {SeriesCollapseFilter} from './filters/SeriesCollapseFilter';
|
||||
import {SideBarFilter} from './filters/sidebar-filter';
|
||||
import {HeaderFilter} from './filters/HeaderFilter';
|
||||
@@ -56,7 +56,6 @@ import {BookFilterOrchestrationService} from './book-filter-orchestration.servic
|
||||
import {BookBrowserScrollService} from './book-browser-scroll.service';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {MultiSortPopoverComponent} from './sorting/multi-sort-popover/multi-sort-popover.component';
|
||||
import {CdkDragDrop} from '@angular/cdk/drag-drop';
|
||||
import {SortService} from '../../service/sort.service';
|
||||
|
||||
export enum EntityType {
|
||||
@@ -144,6 +143,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
entityViewPreferences: EntityViewPreferences | undefined;
|
||||
currentViewMode: string | undefined;
|
||||
lastAppliedSortCriteria: SortOption[] = [];
|
||||
visibleSortOptions: SortOption[] = [];
|
||||
showFilter = false;
|
||||
screenWidth = typeof window !== 'undefined' ? window.innerWidth : 1024;
|
||||
mobileColumnCount = 3;
|
||||
@@ -446,8 +446,15 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.columnPreferenceService.initPreferences(user.user?.userSettings?.tableColumnPreference);
|
||||
this.visibleColumns = this.columnPreferenceService.visibleColumns;
|
||||
|
||||
const visibleFields = user.user?.userSettings?.visibleSortFields ?? DEFAULT_VISIBLE_SORT_FIELDS;
|
||||
const sortOptionsByField = new Map(this.bookSorter.sortOptions.map(o => [o.field, o]));
|
||||
this.visibleSortOptions = visibleFields.map(f => sortOptionsByField.get(f)).filter((o): o is SortOption => !!o);
|
||||
|
||||
this.bookSorter.setSortCriteria(parseResult.sortCriteria);
|
||||
|
||||
// Only update sort criteria if they actually changed to avoid resetting popover/CDK state
|
||||
if (!this.areSortCriteriaEqual(this.bookSorter.selectedSortCriteria, parseResult.sortCriteria)) {
|
||||
this.bookSorter.setSortCriteria(parseResult.sortCriteria);
|
||||
}
|
||||
this.currentViewMode = parseResult.viewMode;
|
||||
|
||||
if (!this.areSortCriteriaEqual(this.lastAppliedSortCriteria, this.bookSorter.selectedSortCriteria)) {
|
||||
@@ -650,21 +657,89 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
);
|
||||
}
|
||||
|
||||
// Multi-sort popover handlers
|
||||
onAddSortCriterion(field: string): void {
|
||||
this.bookSorter.addSortCriterion(field);
|
||||
onSortCriteriaChange(criteria: SortOption[]): void {
|
||||
this.bookSorter.setSortCriteria(criteria);
|
||||
this.onMultiSortChange(criteria);
|
||||
}
|
||||
|
||||
onRemoveSortCriterion(index: number): void {
|
||||
this.bookSorter.removeSortCriterion(index);
|
||||
get canSaveSort(): boolean {
|
||||
return this.entityType === EntityType.LIBRARY ||
|
||||
this.entityType === EntityType.SHELF ||
|
||||
this.entityType === EntityType.MAGIC_SHELF ||
|
||||
this.entityType === EntityType.ALL_BOOKS ||
|
||||
this.entityType === EntityType.UNSHELVED;
|
||||
}
|
||||
|
||||
onToggleSortDirection(index: number): void {
|
||||
this.bookSorter.toggleCriterionDirection(index);
|
||||
}
|
||||
onSaveSortConfig(criteria: SortOption[]): void {
|
||||
if (!this.entityType) return;
|
||||
|
||||
onReorderSortCriteria(event: CdkDragDrop<SortOption[]>): void {
|
||||
this.bookSorter.reorderCriteria(event);
|
||||
const user = this.userService.getCurrentUser();
|
||||
if (!user) return;
|
||||
|
||||
const sortCriteria: SortCriterion[] = criteria.map(c => ({
|
||||
field: c.field,
|
||||
direction: c.direction === SortDirection.ASCENDING ? 'ASC' as const : 'DESC' as const
|
||||
}));
|
||||
|
||||
const prefs: EntityViewPreferences = structuredClone(
|
||||
user.userSettings.entityViewPreferences ?? {global: {sortKey: 'title', sortDir: 'ASC', view: 'GRID', coverSize: 1.0, seriesCollapsed: false, overlayBookType: true}, overrides: []}
|
||||
);
|
||||
|
||||
if (this.entityType === EntityType.ALL_BOOKS || this.entityType === EntityType.UNSHELVED) {
|
||||
prefs.global = {
|
||||
...prefs.global,
|
||||
sortKey: sortCriteria[0]?.field ?? 'title',
|
||||
sortDir: sortCriteria[0]?.direction ?? 'ASC',
|
||||
sortCriteria
|
||||
};
|
||||
} else {
|
||||
if (!this.entity) return;
|
||||
if (!prefs.overrides) prefs.overrides = [];
|
||||
|
||||
let overrideEntityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF';
|
||||
switch (this.entityType) {
|
||||
case EntityType.LIBRARY: overrideEntityType = 'LIBRARY'; break;
|
||||
case EntityType.SHELF: overrideEntityType = 'SHELF'; break;
|
||||
case EntityType.MAGIC_SHELF: overrideEntityType = 'MAGIC_SHELF'; break;
|
||||
default: return;
|
||||
}
|
||||
|
||||
const existingIndex = prefs.overrides.findIndex(
|
||||
o => o.entityType === overrideEntityType && o.entityId === this.entity!.id
|
||||
);
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
prefs.overrides[existingIndex].preferences = {
|
||||
...prefs.overrides[existingIndex].preferences,
|
||||
sortKey: sortCriteria[0]?.field ?? 'title',
|
||||
sortDir: sortCriteria[0]?.direction ?? 'ASC',
|
||||
sortCriteria
|
||||
};
|
||||
} else {
|
||||
prefs.overrides.push({
|
||||
entityType: overrideEntityType,
|
||||
entityId: this.entity!.id!,
|
||||
preferences: {
|
||||
sortKey: sortCriteria[0]?.field ?? 'title',
|
||||
sortDir: sortCriteria[0]?.direction ?? 'ASC',
|
||||
sortCriteria,
|
||||
view: 'GRID',
|
||||
coverSize: 1.0,
|
||||
seriesCollapsed: false,
|
||||
overlayBookType: true
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
this.userService.updateUserSetting(user.id, 'entityViewPreferences', prefs);
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Sort Saved',
|
||||
detail: this.entityType === EntityType.ALL_BOOKS || this.entityType === EntityType.UNSHELVED
|
||||
? 'Default sort configuration saved.'
|
||||
: `Sort configuration saved for this ${this.entityType.toLowerCase()}.`
|
||||
});
|
||||
}
|
||||
|
||||
get sortCriteriaCount(): number {
|
||||
|
||||
@@ -6,12 +6,12 @@ export class BookSorter {
|
||||
|
||||
sortOptions: SortOption[] = [
|
||||
{label: 'Title', field: 'title', direction: SortDirection.ASCENDING},
|
||||
{label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING},
|
||||
{label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING},
|
||||
{label: 'File Path', field: 'filePath', direction: SortDirection.ASCENDING},
|
||||
{label: 'Author', field: 'author', direction: SortDirection.ASCENDING},
|
||||
{label: 'Author (Surname)', field: 'authorSurnameVorname', direction: SortDirection.ASCENDING},
|
||||
{label: 'Author + Series', field: 'authorSeries', direction: SortDirection.ASCENDING},
|
||||
{label: 'Series Name', field: 'seriesName', direction: SortDirection.ASCENDING},
|
||||
{label: 'Series Number', field: 'seriesNumber', direction: SortDirection.ASCENDING},
|
||||
{label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING},
|
||||
{label: 'Personal Rating', field: 'personalRating', direction: SortDirection.ASCENDING},
|
||||
{label: 'Added On', field: 'addedOn', direction: SortDirection.ASCENDING},
|
||||
@@ -19,6 +19,10 @@ export class BookSorter {
|
||||
{label: 'Locked', field: 'locked', direction: SortDirection.ASCENDING},
|
||||
{label: 'Publisher', field: 'publisher', direction: SortDirection.ASCENDING},
|
||||
{label: 'Published Date', field: 'publishedDate', direction: SortDirection.ASCENDING},
|
||||
{label: 'Read Status', field: 'readStatus', direction: SortDirection.ASCENDING},
|
||||
{label: 'Date Finished', field: 'dateFinished', direction: SortDirection.ASCENDING},
|
||||
{label: 'Reading Progress', field: 'readingProgress', direction: SortDirection.ASCENDING},
|
||||
{label: 'Book Type', field: 'bookType', direction: SortDirection.ASCENDING},
|
||||
{label: 'Amazon Rating', field: 'amazonRating', direction: SortDirection.ASCENDING},
|
||||
{label: 'Amazon #', field: 'amazonReviewCount', direction: SortDirection.ASCENDING},
|
||||
{label: 'Goodreads Rating', field: 'goodreadsRating', direction: SortDirection.ASCENDING},
|
||||
|
||||
+13
@@ -53,4 +53,17 @@
|
||||
</p-select>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (showSaveButton) {
|
||||
<div class="save-sort-section">
|
||||
<p-button
|
||||
icon="pi pi-save"
|
||||
label="Save as Default"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[style]="{ width: '100%' }"
|
||||
(onClick)="onSave()">
|
||||
</p-button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
+6
@@ -142,3 +142,9 @@
|
||||
padding-top: 0.75rem;
|
||||
border-top: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.save-sort-section {
|
||||
margin-top: 0.75rem;
|
||||
padding-top: 0.75rem;
|
||||
border-top: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
+32
-13
@@ -1,9 +1,10 @@
|
||||
import {Component, EventEmitter, Input, Output} from '@angular/core';
|
||||
import {SortDirection, SortOption} from '../../../../model/sort.model';
|
||||
import {CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList} from '@angular/cdk/drag-drop';
|
||||
import {CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray} from '@angular/cdk/drag-drop';
|
||||
import {Select} from 'primeng/select';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {Button} from 'primeng/button';
|
||||
|
||||
@Component({
|
||||
selector: 'app-multi-sort-popover',
|
||||
@@ -14,7 +15,8 @@ import {Tooltip} from 'primeng/tooltip';
|
||||
CdkDragHandle,
|
||||
Select,
|
||||
FormsModule,
|
||||
Tooltip
|
||||
Tooltip,
|
||||
Button
|
||||
],
|
||||
templateUrl: './multi-sort-popover.component.html',
|
||||
styleUrl: './multi-sort-popover.component.scss'
|
||||
@@ -22,12 +24,10 @@ import {Tooltip} from 'primeng/tooltip';
|
||||
export class MultiSortPopoverComponent {
|
||||
@Input() sortCriteria: SortOption[] = [];
|
||||
@Input() availableSortOptions: SortOption[] = [];
|
||||
@Input() showSaveButton = false;
|
||||
|
||||
@Output() criteriaChange = new EventEmitter<SortOption[]>();
|
||||
@Output() addCriterion = new EventEmitter<string>();
|
||||
@Output() removeCriterion = new EventEmitter<number>();
|
||||
@Output() toggleDirection = new EventEmitter<number>();
|
||||
@Output() reorder = new EventEmitter<CdkDragDrop<SortOption[]>>();
|
||||
@Output() saveSortConfig = new EventEmitter<SortOption[]>();
|
||||
|
||||
selectedField: string | null = null;
|
||||
|
||||
@@ -37,22 +37,41 @@ export class MultiSortPopoverComponent {
|
||||
}
|
||||
|
||||
onDrop(event: CdkDragDrop<SortOption[]>): void {
|
||||
this.reorder.emit(event);
|
||||
const criteria = [...this.sortCriteria];
|
||||
moveItemInArray(criteria, event.previousIndex, event.currentIndex);
|
||||
this.sortCriteria = criteria;
|
||||
this.criteriaChange.emit(this.sortCriteria);
|
||||
}
|
||||
|
||||
onToggleDirection(index: number): void {
|
||||
this.toggleDirection.emit(index);
|
||||
this.sortCriteria = this.sortCriteria.map((c, i) =>
|
||||
i === index
|
||||
? {...c, direction: c.direction === SortDirection.ASCENDING ? SortDirection.DESCENDING : SortDirection.ASCENDING}
|
||||
: c
|
||||
);
|
||||
this.criteriaChange.emit(this.sortCriteria);
|
||||
}
|
||||
|
||||
onRemove(index: number): void {
|
||||
this.removeCriterion.emit(index);
|
||||
this.sortCriteria = this.sortCriteria.filter((_, i) => i !== index);
|
||||
this.criteriaChange.emit(this.sortCriteria);
|
||||
}
|
||||
|
||||
onAddField(): void {
|
||||
if (this.selectedField) {
|
||||
this.addCriterion.emit(this.selectedField);
|
||||
this.selectedField = null;
|
||||
}
|
||||
if (!this.selectedField) return;
|
||||
const option = this.availableSortOptions.find(o => o.field === this.selectedField);
|
||||
if (!option) return;
|
||||
this.sortCriteria = [...this.sortCriteria, {
|
||||
label: option.label,
|
||||
field: option.field,
|
||||
direction: SortDirection.ASCENDING
|
||||
}];
|
||||
this.selectedField = null;
|
||||
this.criteriaChange.emit(this.sortCriteria);
|
||||
}
|
||||
|
||||
onSave(): void {
|
||||
this.saveSortConfig.emit(this.sortCriteria);
|
||||
}
|
||||
|
||||
getDirectionIcon(direction: SortDirection): string {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Book} from '../model/book.model';
|
||||
import {Book, ReadStatus} from '../model/book.model';
|
||||
import {SortDirection, SortOption} from "../model/sort.model";
|
||||
|
||||
@Injectable({
|
||||
@@ -48,18 +48,22 @@ export class SortService {
|
||||
return aChunks.length - bChunks.length;
|
||||
}
|
||||
|
||||
private static readonly READ_STATUS_RANK: Record<string, number> = {
|
||||
[ReadStatus.UNSET]: 0,
|
||||
[ReadStatus.UNREAD]: 1,
|
||||
[ReadStatus.READING]: 2,
|
||||
[ReadStatus.RE_READING]: 3,
|
||||
[ReadStatus.PARTIALLY_READ]: 4,
|
||||
[ReadStatus.PAUSED]: 5,
|
||||
[ReadStatus.READ]: 6,
|
||||
[ReadStatus.ABANDONED]: 7,
|
||||
[ReadStatus.WONT_READ]: 8,
|
||||
};
|
||||
|
||||
private readonly fieldExtractors: Record<string, (book: Book) => unknown> = {
|
||||
title: (book) => (book.seriesCount ? (book.metadata?.seriesName?.toLowerCase() || null) : null)
|
||||
?? (book.metadata?.title?.toLowerCase() || null),
|
||||
titleSeries: (book) => {
|
||||
const title = book.metadata?.title?.toLowerCase() || '';
|
||||
const series = book.metadata?.seriesName?.toLowerCase();
|
||||
const seriesNumber = book.metadata?.seriesNumber ?? Number.MAX_SAFE_INTEGER;
|
||||
if (series) {
|
||||
return [series, seriesNumber];
|
||||
}
|
||||
return [title, Number.MAX_SAFE_INTEGER];
|
||||
},
|
||||
title: (book) => book.metadata?.seriesName?.toLowerCase()
|
||||
|| book.metadata?.title?.toLowerCase()
|
||||
|| null,
|
||||
author: (book) => book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null,
|
||||
authorSurnameVorname: (book) => book.metadata?.authors?.map(a => {
|
||||
const parts = a.trim().split(/\s+/);
|
||||
@@ -68,17 +72,6 @@ export class SortService {
|
||||
const firstname = parts.join(" ");
|
||||
return `${surname}, ${firstname}`.toLowerCase();
|
||||
}).join(", ") || null,
|
||||
authorSeries: (book) => {
|
||||
const author = book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null;
|
||||
const series = book.metadata?.seriesName?.toLowerCase() || null;
|
||||
const seriesNumber = book.metadata?.seriesNumber ?? Number.MAX_SAFE_INTEGER;
|
||||
const title = book.metadata?.title?.toLowerCase() || '';
|
||||
if (series) {
|
||||
return [author, series, seriesNumber, title];
|
||||
}
|
||||
// For books without a series, use a very large string for series name to sort them last within an author.
|
||||
return [author, '~~~~~~~~~~~~~~~~~', Number.MAX_SAFE_INTEGER, title];
|
||||
},
|
||||
publishedDate: (book) => {
|
||||
const date = book.metadata?.publishedDate;
|
||||
return date === null || date === undefined ? null : new Date(date).getTime();
|
||||
@@ -105,6 +98,19 @@ export class SortService {
|
||||
fileName: (book) => book.fileName,
|
||||
filePath: (book) => book.filePath,
|
||||
random: (book) => Math.random(),
|
||||
seriesName: (book) => book.metadata?.seriesName?.toLowerCase() || null,
|
||||
seriesNumber: (book) => book.metadata?.seriesNumber ?? null,
|
||||
readStatus: (book) => book.readStatus ? (SortService.READ_STATUS_RANK[book.readStatus] ?? null) : null,
|
||||
dateFinished: (book) => book.dateFinished ? new Date(book.dateFinished).getTime() : null,
|
||||
readingProgress: (book) =>
|
||||
book.epubProgress?.percentage
|
||||
?? book.pdfProgress?.percentage
|
||||
?? book.cbxProgress?.percentage
|
||||
?? book.audiobookProgress?.percentage
|
||||
?? book.koreaderProgress?.percentage
|
||||
?? book.koboProgress?.percentage
|
||||
?? null,
|
||||
bookType: (book) => book.primaryFile?.bookType || null,
|
||||
};
|
||||
|
||||
applySort(books: Book[], selectedSort: SortOption | null): Book[] {
|
||||
|
||||
Reference in New Issue
Block a user