diff --git a/booklore-api/src/main/java/org/booklore/mapper/custom/BookLoreUserTransformer.java b/booklore-api/src/main/java/org/booklore/mapper/custom/BookLoreUserTransformer.java index 3c8319fc1..cd52141b4 100644 --- a/booklore-api/src/main/java/org/booklore/mapper/custom/BookLoreUserTransformer.java +++ b/booklore-api/src/main/java/org/booklore/mapper/custom/BookLoreUserTransformer.java @@ -61,6 +61,8 @@ public class BookLoreUserTransformer { case DASHBOARD_CONFIG -> userSettings.setDashboardConfig(objectMapper.readValue(value, BookLoreUser.UserSettings.DashboardConfig.class)); case VISIBLE_FILTERS -> userSettings.setVisibleFilters(objectMapper.readValue(value, new TypeReference<>() { })); + case VISIBLE_SORT_FIELDS -> userSettings.setVisibleSortFields(objectMapper.readValue(value, new TypeReference<>() { + })); } } else { switch (settingKey) { diff --git a/booklore-api/src/main/java/org/booklore/model/dto/BookLoreUser.java b/booklore-api/src/main/java/org/booklore/model/dto/BookLoreUser.java index 534804d7c..990f04a48 100644 --- a/booklore-api/src/main/java/org/booklore/model/dto/BookLoreUser.java +++ b/booklore-api/src/main/java/org/booklore/model/dto/BookLoreUser.java @@ -78,6 +78,7 @@ public class BookLoreUser { public boolean enableSeriesView; public boolean autoSaveMetadata; public List visibleFilters; + public List visibleSortFields; public DashboardConfig dashboardConfig; @Data @@ -107,6 +108,7 @@ public class BookLoreUser { public static class GlobalPreferences { private String sortKey; private String sortDir; + private List sortCriteria; private String view; private Float coverSize; @JsonAlias("seriesCollapse") @@ -131,10 +133,21 @@ public class BookLoreUser { public static class OverrideDetails { private String sortKey; private String sortDir; + private List sortCriteria; private String view; @JsonAlias("seriesCollapse") private Boolean seriesCollapsed; private Boolean overlayBookType; + private Float coverSize; + } + + @Data + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class SortCriterion { + private String field; + private String direction; } @Data diff --git a/booklore-api/src/main/java/org/booklore/model/dto/settings/UserSettingKey.java b/booklore-api/src/main/java/org/booklore/model/dto/settings/UserSettingKey.java index 1cbab8247..6c5db1712 100644 --- a/booklore-api/src/main/java/org/booklore/model/dto/settings/UserSettingKey.java +++ b/booklore-api/src/main/java/org/booklore/model/dto/settings/UserSettingKey.java @@ -23,7 +23,8 @@ public enum UserSettingKey { HARDCOVER_API_KEY("hardcoverApiKey", false), HARDCOVER_SYNC_ENABLED("hardcoverSyncEnabled", false), AUTO_SAVE_METADATA("autoSaveMetadata", false), - VISIBLE_FILTERS("visibleFilters", true); + VISIBLE_FILTERS("visibleFilters", true), + VISIBLE_SORT_FIELDS("visibleSortFields", true); private final String dbKey; diff --git a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html index 9dd283179..3a470fcf5 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html @@ -177,11 +177,10 @@ + [availableSortOptions]="visibleSortOptions" + [showSaveButton]="canSaveSort" + (criteriaChange)="onSortCriteriaChange($event)" + (saveSortConfig)="onSaveSortConfig($event)"> diff --git a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts index 8dd33487e..a6c79fc29 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts @@ -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): 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 { diff --git a/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts b/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts index 57ed43164..c7bb75c2c 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts +++ b/booklore-ui/src/app/features/book/components/book-browser/sorting/BookSorter.ts @@ -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}, diff --git a/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.html b/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.html index ed27916b9..0d45c30d4 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.html +++ b/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.html @@ -53,4 +53,17 @@ } + + @if (showSaveButton) { +
+ + +
+ } diff --git a/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.scss b/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.scss index f91b2dea1..683d9f0b4 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.scss +++ b/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.scss @@ -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); +} diff --git a/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.ts b/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.ts index 3a1fbeecf..c0ab9e917 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.ts +++ b/booklore-ui/src/app/features/book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component.ts @@ -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(); - @Output() addCriterion = new EventEmitter(); - @Output() removeCriterion = new EventEmitter(); - @Output() toggleDirection = new EventEmitter(); - @Output() reorder = new EventEmitter>(); + @Output() saveSortConfig = new EventEmitter(); selectedField: string | null = null; @@ -37,22 +37,41 @@ export class MultiSortPopoverComponent { } onDrop(event: CdkDragDrop): 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 { diff --git a/booklore-ui/src/app/features/book/service/sort.service.ts b/booklore-ui/src/app/features/book/service/sort.service.ts index 7cc3fb6e5..a2f97d0b0 100644 --- a/booklore-ui/src/app/features/book/service/sort.service.ts +++ b/booklore-ui/src/app/features/book/service/sort.service.ts @@ -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 = { + [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 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[] { diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts index 091e0109e..80a8fd4b3 100644 --- a/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts @@ -93,17 +93,21 @@ export class DashboardSettingsComponent implements OnInit { this.sortFieldOptions = [ {label: t('sortFields.title'), value: 'title'}, - {label: t('sortFields.titleSeries'), value: 'titleSeries'}, {label: t('sortFields.fileName'), value: 'fileName'}, {label: t('sortFields.filePath'), value: 'filePath'}, {label: t('sortFields.addedOn'), value: 'addedOn'}, {label: t('sortFields.author'), value: 'author'}, {label: t('sortFields.authorSurnameVorname'), value: 'authorSurnameVorname'}, - {label: t('sortFields.authorSeries'), value: 'authorSeries'}, + {label: t('sortFields.seriesName'), value: 'seriesName'}, + {label: t('sortFields.seriesNumber'), value: 'seriesNumber'}, {label: t('sortFields.personalRating'), value: 'personalRating'}, {label: t('sortFields.publisher'), value: 'publisher'}, {label: t('sortFields.publishedDate'), value: 'publishedDate'}, {label: t('sortFields.lastReadTime'), value: 'lastReadTime'}, + {label: t('sortFields.readStatus'), value: 'readStatus'}, + {label: t('sortFields.dateFinished'), value: 'dateFinished'}, + {label: t('sortFields.readingProgress'), value: 'readingProgress'}, + {label: t('sortFields.bookType'), value: 'bookType'}, {label: t('sortFields.pageCount'), value: 'pageCount'} ]; diff --git a/booklore-ui/src/app/features/settings/user-management/user.service.ts b/booklore-ui/src/app/features/settings/user-management/user.service.ts index d9e2a688c..e92d919da 100644 --- a/booklore-ui/src/app/features/settings/user-management/user.service.ts +++ b/booklore-ui/src/app/features/settings/user-management/user.service.ts @@ -231,6 +231,13 @@ export const ALL_FILTER_OPTIONS: { label: string; value: VisibleFilterType }[] = {label: 'Comic Creator', value: 'comicCreator'} ]; +export const DEFAULT_VISIBLE_SORT_FIELDS: string[] = [ + 'title', 'seriesName', 'fileName', 'filePath', + 'author', 'authorSurnameVorname', 'seriesNumber', + 'lastReadTime', 'personalRating', 'addedOn', + 'fileSizeKb', 'locked', 'publisher', 'publishedDate', 'pageCount', 'random' +]; + export interface UserSettings { perBookSetting: PerBookSetting; pdfReaderSetting: PdfReaderSetting; @@ -243,6 +250,7 @@ export interface UserSettings { sidebarMagicShelfSorting: SidebarMagicShelfSorting; filterMode: BookFilterMode; visibleFilters?: VisibleFilterType[]; + visibleSortFields?: string[]; metadataCenterViewMode: 'route' | 'dialog'; enableSeriesView: boolean; entityViewPreferences: EntityViewPreferences; diff --git a/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.html b/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.html index 7c58837d8..962602138 100644 --- a/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.html +++ b/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.html @@ -26,31 +26,41 @@
+ + +
+ + +
+
+ +
+

+ {{ t('visibleSortFieldsDesc') }} + ({{ sortFieldSelectionCountText }}) +

+
- @for (criterion of sortCriteria; track criterion.field; let i = $index) { -
-
+ [cdkDropListData]="visibleSortFields" + (cdkDropListDropped)="onSortFieldDrop($event)"> + @for (f of visibleSortFields; track f; let i = $index) { +
+
- {{ i + 1 }}. - {{ getSortLabel(criterion.field) }} + {{ i + 1 }}. + {{ getSortFieldLabel(f) }} -
}
- @if (availableSortFields.length > 0) { -
+
+ @if (availableSortFieldsToAdd.length > 0 && visibleSortFields.length < maxSortFields) { + (onChange)="addSortField()"> -
- } + } + +
@@ -101,8 +119,7 @@ {{ t('tableType') }} {{ t('tableName') }} - {{ t('tableSortBy') }} - {{ t('tableDirection') }} + {{ t('tableSortConfig') }} {{ t('tableViewMode') }} {{ t('tableActions') }} @@ -124,13 +141,20 @@ } - - - - + + + + + + >('sortFieldList'); overrides: { entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF'; library: number; sort: string; sortDir: 'ASC' | 'DESC'; + sortCriteria: SortCriterion[]; + sortCriteriaAsOptions: SortOption[]; view: 'GRID' | 'TABLE'; }[] = []; @@ -134,13 +151,32 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy { this.sortCriteria = [{field: this.selectedSort, direction: this.selectedSortDir}]; } - this.overrides = (prefs?.overrides ?? []).map(o => ({ - entityType: o.entityType, - library: o.entityId, - sort: o.preferences.sortKey, - sortDir: o.preferences.sortDir ?? 'ASC', - view: o.preferences.view ?? 'GRID' + // Build SortOption[] versions for the popover + this.allSortAsOptions = this.sortOptions.map(o => ({ + label: this.t.translate('settingsView.librarySort.' + o.translationKey), + field: o.field, + direction: SortDirection.ASCENDING })); + this.globalSortAsOptions = this.toSortOptions(this.sortCriteria); + + this.visibleSortFields = userState.user?.userSettings?.visibleSortFields + ? [...userState.user.userSettings.visibleSortFields] + : [...DEFAULT_VISIBLE_SORT_FIELDS]; + + this.overrides = (prefs?.overrides ?? []).map(o => { + const sc = o.preferences.sortCriteria?.length + ? [...o.preferences.sortCriteria] + : [{field: o.preferences.sortKey, direction: o.preferences.sortDir ?? 'ASC'} as SortCriterion]; + return { + entityType: o.entityType, + library: o.entityId, + sort: o.preferences.sortKey, + sortDir: o.preferences.sortDir ?? 'ASC', + sortCriteria: sc, + sortCriteriaAsOptions: this.toSortOptions(sc), + view: o.preferences.view ?? 'GRID' + }; + }); this.libraryOptions = (librariesState.libraries ?? []).filter(lib => lib.id !== undefined).map(lib => ({ label: lib.name, @@ -198,11 +234,14 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy { addOverride(): void { const next = this.availableLibraries[0]; if (next) { + const defaultCriteria: SortCriterion[] = [{field: 'title', direction: 'ASC'}]; this.overrides.push({ entityType: next.entityType, library: next.value, sort: 'title', sortDir: 'ASC', + sortCriteria: defaultCriteria, + sortCriteriaAsOptions: this.toSortOptions(defaultCriteria), view: 'GRID' }); } @@ -212,51 +251,87 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy { this.overrides.splice(index, 1); } - // Multi-sort criteria methods - get availableSortFields(): {label: string; field: string; translationKey: string}[] { - const usedFields = new Set(this.sortCriteria.map(c => c.field)); - return this.sortOptions.filter(opt => !usedFields.has(opt.field)); + // Conversion helpers + private toSortOptions(criteria: SortCriterion[]): SortOption[] { + return criteria.map(c => ({ + label: this.t.translate('settingsView.librarySort.' + (this.sortOptions.find(o => o.field === c.field)?.translationKey ?? c.field)), + field: c.field, + direction: c.direction === 'ASC' ? SortDirection.ASCENDING : SortDirection.DESCENDING + })); } - getSortLabel(field: string): string { - const key = this.sortOptions.find(opt => opt.field === field)?.translationKey; - return key ? this.t.translate('settingsView.librarySort.' + key) : field; + private toSortCriteria(options: SortOption[]): SortCriterion[] { + return options.map(o => ({ + field: o.field, + direction: o.direction === SortDirection.ASCENDING ? 'ASC' as const : 'DESC' as const + })); } - addSortCriterion(): void { - if (this.selectedAddField) { - this.sortCriteria.push({field: this.selectedAddField, direction: 'ASC'}); - this.selectedAddField = null; - this.syncLegacySort(); - } - } - - removeSortCriterion(index: number): void { - if (this.sortCriteria.length > 1) { - this.sortCriteria.splice(index, 1); - this.syncLegacySort(); - } - } - - toggleSortDirection(index: number): void { - const criterion = this.sortCriteria[index]; - criterion.direction = criterion.direction === 'ASC' ? 'DESC' : 'ASC'; + onGlobalSortCriteriaChange(criteria: SortOption[]): void { + this.globalSortAsOptions = criteria; + this.sortCriteria = this.toSortCriteria(criteria); this.syncLegacySort(); } - onSortCriteriaDrop(event: CdkDragDrop): void { - moveItemInArray(this.sortCriteria, event.previousIndex, event.currentIndex); - this.syncLegacySort(); + onOverrideSortCriteriaChange(index: number, criteria: SortOption[]): void { + this.overrides[index].sortCriteriaAsOptions = criteria; + this.overrides[index].sortCriteria = this.toSortCriteria(criteria); + this.overrides[index].sort = criteria[0]?.field ?? 'title'; + this.overrides[index].sortDir = criteria[0]?.direction === SortDirection.ASCENDING ? 'ASC' : 'DESC'; } private syncLegacySort(): void { - // Keep legacy fields in sync with first criterion if (this.sortCriteria.length > 0) { this.selectedSort = this.sortCriteria[0].field; this.selectedSortDir = this.sortCriteria[0].direction; } } + // Visible sort fields management + getSortFieldLabel(field: string): string { + const key = this.sortOptions.find(opt => opt.field === field)?.translationKey; + return key ? this.t.translate('settingsView.librarySort.' + key) : field; + } + + get availableSortFieldsToAdd(): {label: string; value: string}[] { + const used = new Set(this.visibleSortFields); + return this.sortOptions + .filter(opt => !used.has(opt.field)) + .map(opt => ({label: this.t.translate('settingsView.librarySort.' + opt.translationKey), value: opt.field})); + } + + get sortFieldSelectionCountText(): string { + return this.t.translate('settingsView.librarySort.sortFieldCount', { + count: this.visibleSortFields.length, + total: this.sortOptions.length + }); + } + + onSortFieldDrop(event: CdkDragDrop): void { + moveItemInArray(this.visibleSortFields, event.previousIndex, event.currentIndex); + } + + addSortField(): void { + if (this.selectedAddSortField) { + this.visibleSortFields.push(this.selectedAddSortField); + this.selectedAddSortField = null; + requestAnimationFrame(() => { + const el = this.sortFieldList()?.nativeElement; + if (el) el.scrollTop = el.scrollHeight; + }); + } + } + + removeSortField(index: number): void { + if (this.visibleSortFields.length > this.minSortFields) { + this.visibleSortFields.splice(index, 1); + } + } + + resetSortFieldsToDefaults(): void { + this.visibleSortFields = [...DEFAULT_VISIBLE_SORT_FIELDS]; + } + saveSettings(): void { if (!this.user) return; @@ -279,8 +354,9 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy { entityType: o.entityType, entityId: o.library, preferences: { - sortKey: o.sort, - sortDir: o.sortDir, + sortKey: o.sortCriteria[0]?.field ?? o.sort, + sortDir: o.sortCriteria[0]?.direction ?? o.sortDir, + sortCriteria: [...o.sortCriteria], view: o.view, coverSize: existing?.coverSize ?? 1.0, seriesCollapsed: existing?.seriesCollapsed ?? false, @@ -291,6 +367,7 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy { this.userService.updateUserSetting(this.user.id, 'entityViewPreferences', prefs); this.userService.updateUserSetting(this.user.id, 'autoSaveMetadata', this.autoSaveMetadata); + this.userService.updateUserSetting(this.user.id, 'visibleSortFields', this.visibleSortFields); this.messageService.add({ severity: 'success', diff --git a/booklore-ui/src/app/shared/layout/component/layout-menu/app.menu.component.html b/booklore-ui/src/app/shared/layout/component/layout-menu/app.menu.component.html index 0ae1df612..d189901c2 100644 --- a/booklore-ui/src/app/shared/layout/component/layout-menu/app.menu.component.html +++ b/booklore-ui/src/app/shared/layout/component/layout-menu/app.menu.component.html @@ -3,7 +3,7 @@
    @if (homeMenu$ | async; as homeMenu) { - @for (item of homeMenu; track item; let i = $index) { + @for (item of homeMenu; track item.label; let i = $index) { @if (!item.separator) {
  • } @@ -13,7 +13,7 @@
      @if (libraryMenu$ | async; as libraryMenu) { - @for (item of libraryMenu; track item; let i = $index) { + @for (item of libraryMenu; track item.label; let i = $index) { @if (!item.separator) {
    • } @@ -23,7 +23,7 @@
        @if (shelfMenu$ | async; as shelfMenu) { - @for (item of shelfMenu; track item; let i = $index) { + @for (item of shelfMenu; track item.label; let i = $index) { @if (!item.separator) {
      • } @@ -33,7 +33,7 @@
          @if (magicShelfMenu$ | async; as shelfMenu) { - @for (item of shelfMenu; track item; let i = $index) { + @for (item of shelfMenu; track item.label; let i = $index) { @if (!item.separator) {
        • } diff --git a/booklore-ui/src/app/shared/layout/component/layout-menu/app.menuitem.component.html b/booklore-ui/src/app/shared/layout/component/layout-menu/app.menuitem.component.html index e51759553..3014be4fb 100644 --- a/booklore-ui/src/app/shared/layout/component/layout-menu/app.menuitem.component.html +++ b/booklore-ui/src/app/shared/layout/component/layout-menu/app.menuitem.component.html @@ -31,6 +31,7 @@