feat(sorting): add multi-field sorting support (#2628)
This commit is contained in:
@@ -12,9 +12,15 @@ export interface EntityViewPreferences {
|
||||
overrides: EntityViewPreferenceOverride[];
|
||||
}
|
||||
|
||||
export interface SortCriterion {
|
||||
field: string;
|
||||
direction: 'ASC' | 'DESC';
|
||||
}
|
||||
|
||||
export interface EntityViewPreference {
|
||||
sortKey: string;
|
||||
sortDir: 'ASC' | 'DESC';
|
||||
sortCriteria?: SortCriterion[];
|
||||
view: 'GRID' | 'TABLE';
|
||||
coverSize: number;
|
||||
seriesCollapsed: boolean;
|
||||
|
||||
+52
-5
@@ -14,17 +14,64 @@
|
||||
<div class="setting-header-standalone">
|
||||
<label class="setting-label">Default Preferences</label>
|
||||
</div>
|
||||
<p class="setting-description">Configure the default sorting field, direction, and view mode for all libraries and shelves.</p>
|
||||
<p class="setting-description">Configure the default view mode and multi-field sorting for all libraries and shelves. Drag to reorder sort priority.</p>
|
||||
<div class="setting-options">
|
||||
<div class="select-group">
|
||||
<p-select size="small" [options]="sortOptions" optionLabel="label" optionValue="field"
|
||||
[(ngModel)]="selectedSort" placeholder="Sort By" appendTo="body"></p-select>
|
||||
<p-select size="small" [options]="sortDirectionOptions" [(ngModel)]="selectedSortDir"
|
||||
placeholder="Direction" appendTo="body"></p-select>
|
||||
<p-select size="small" [options]="viewModeOptions" [(ngModel)]="selectedView"
|
||||
placeholder="View Mode" appendTo="body"></p-select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="multi-sort-editor">
|
||||
<label class="multi-sort-label">Sort Order (drag to reorder)</label>
|
||||
<div
|
||||
class="sort-criteria-list"
|
||||
cdkDropList
|
||||
[cdkDropListData]="sortCriteria"
|
||||
(cdkDropListDropped)="onSortCriteriaDrop($event)">
|
||||
@for (criterion of sortCriteria; track criterion.field; let i = $index) {
|
||||
<div class="sort-criterion-row" cdkDrag>
|
||||
<div class="criterion-drag-handle" cdkDragHandle>
|
||||
<i class="pi pi-bars"></i>
|
||||
</div>
|
||||
<span class="criterion-rank">{{ i + 1 }}.</span>
|
||||
<span class="criterion-label">{{ getSortLabel(criterion.field) }}</span>
|
||||
<button
|
||||
type="button"
|
||||
class="direction-toggle-btn"
|
||||
(click)="toggleSortDirection(i)"
|
||||
[pTooltip]="criterion.direction === 'ASC' ? 'Ascending' : 'Descending'"
|
||||
tooltipPosition="top">
|
||||
<i [class]="criterion.direction === 'ASC' ? 'pi pi-arrow-up' : 'pi pi-arrow-down'"></i>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="remove-criterion-btn"
|
||||
(click)="removeSortCriterion(i)"
|
||||
[disabled]="sortCriteria.length === 1"
|
||||
pTooltip="Remove"
|
||||
tooltipPosition="top">
|
||||
<i class="pi pi-times"></i>
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
@if (availableSortFields.length > 0) {
|
||||
<div class="add-sort-row">
|
||||
<p-select
|
||||
size="small"
|
||||
[options]="availableSortFields"
|
||||
[(ngModel)]="selectedAddField"
|
||||
optionLabel="label"
|
||||
optionValue="field"
|
||||
placeholder="Add sort field..."
|
||||
[style]="{ width: '200px' }"
|
||||
appendTo="body"
|
||||
(onChange)="addSortCriterion()">
|
||||
</p-select>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-item">
|
||||
|
||||
+132
-1
@@ -56,5 +56,136 @@
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding-top: 0.5rem;
|
||||
border-top: 1px solid var(--p-content-border-color);
|
||||
border-top: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
// Multi-sort editor styles
|
||||
.multi-sort-editor {
|
||||
margin-top: 1rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px dashed var(--border-color);
|
||||
}
|
||||
|
||||
.multi-sort-label {
|
||||
display: block;
|
||||
font-weight: 500;
|
||||
font-size: 0.875rem;
|
||||
margin-bottom: 0.5rem;
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.sort-criteria-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
min-height: 40px;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.sort-criterion-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.5rem 0.75rem;
|
||||
background-color: var(--overlay-background);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 6px;
|
||||
cursor: move;
|
||||
transition: background-color 0.15s ease, box-shadow 0.15s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: var(--ground-background);
|
||||
}
|
||||
}
|
||||
|
||||
.cdk-drag-preview {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.5rem 0.75rem;
|
||||
background-color: var(--card-background);
|
||||
border: 1px solid var(--primary-color);
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.25);
|
||||
}
|
||||
|
||||
.cdk-drag-placeholder {
|
||||
opacity: 0.3;
|
||||
}
|
||||
|
||||
.cdk-drag-animating {
|
||||
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
.sort-criteria-list.cdk-drop-list-dragging .sort-criterion-row:not(.cdk-drag-placeholder) {
|
||||
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
.criterion-drag-handle {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--text-secondary-color);
|
||||
cursor: grab;
|
||||
|
||||
&:active {
|
||||
cursor: grabbing;
|
||||
}
|
||||
|
||||
i {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
}
|
||||
|
||||
.criterion-rank {
|
||||
font-weight: 600;
|
||||
color: var(--primary-color);
|
||||
min-width: 1.5rem;
|
||||
}
|
||||
|
||||
.criterion-label {
|
||||
flex: 1;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.direction-toggle-btn,
|
||||
.remove-criterion-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 1.75rem;
|
||||
height: 1.75rem;
|
||||
padding: 0;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
background-color: transparent;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.15s ease;
|
||||
|
||||
i {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
background-color: var(--ground-background);
|
||||
}
|
||||
|
||||
&:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
}
|
||||
|
||||
.direction-toggle-btn {
|
||||
color: var(--primary-color);
|
||||
}
|
||||
|
||||
.remove-criterion-btn {
|
||||
color: var(--p-red-500);
|
||||
}
|
||||
|
||||
.add-sort-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
+60
-2
@@ -4,7 +4,7 @@ import {Button} from 'primeng/button';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Select} from 'primeng/select';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {User, UserService} from '../../user-management/user.service';
|
||||
import {SortCriterion, User, UserService} from '../../user-management/user.service';
|
||||
import {LibraryService} from '../../../book/service/library.service';
|
||||
import {ShelfService} from '../../../book/service/shelf.service';
|
||||
import {MagicShelfService} from '../../../magic-shelf/service/magic-shelf.service';
|
||||
@@ -14,6 +14,7 @@ import {ToastModule} from 'primeng/toast';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {filter, take, takeUntil} from 'rxjs/operators';
|
||||
import {ToggleSwitch} from 'primeng/toggleswitch';
|
||||
import {CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray} from '@angular/cdk/drag-drop';
|
||||
|
||||
@Component({
|
||||
selector: 'app-view-preferences',
|
||||
@@ -25,7 +26,10 @@ import {ToggleSwitch} from 'primeng/toggleswitch';
|
||||
TableModule,
|
||||
ToastModule,
|
||||
Tooltip,
|
||||
ToggleSwitch
|
||||
ToggleSwitch,
|
||||
CdkDropList,
|
||||
CdkDrag,
|
||||
CdkDragHandle
|
||||
],
|
||||
templateUrl: './view-preferences.component.html',
|
||||
styleUrl: './view-preferences.component.scss'
|
||||
@@ -81,6 +85,8 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
|
||||
selectedSortDir: 'ASC' | 'DESC' = 'ASC';
|
||||
selectedView: 'GRID' | 'TABLE' = 'GRID';
|
||||
autoSaveMetadata: boolean = false;
|
||||
sortCriteria: SortCriterion[] = [];
|
||||
selectedAddField: string | null = null;
|
||||
|
||||
overrides: {
|
||||
entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF';
|
||||
@@ -117,6 +123,13 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
|
||||
this.selectedView = global?.view ?? 'GRID';
|
||||
this.autoSaveMetadata = userState.user?.userSettings?.autoSaveMetadata ?? false;
|
||||
|
||||
// Load multi-sort criteria, falling back to legacy single sort
|
||||
if (global?.sortCriteria && global.sortCriteria.length > 0) {
|
||||
this.sortCriteria = [...global.sortCriteria];
|
||||
} else {
|
||||
this.sortCriteria = [{field: this.selectedSort, direction: this.selectedSortDir}];
|
||||
}
|
||||
|
||||
this.overrides = (prefs?.overrides ?? []).map(o => ({
|
||||
entityType: o.entityType,
|
||||
library: o.entityId,
|
||||
@@ -195,6 +208,50 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
|
||||
this.overrides.splice(index, 1);
|
||||
}
|
||||
|
||||
// Multi-sort criteria methods
|
||||
get availableSortFields(): {label: string; field: string}[] {
|
||||
const usedFields = new Set(this.sortCriteria.map(c => c.field));
|
||||
return this.sortOptions.filter(opt => !usedFields.has(opt.field));
|
||||
}
|
||||
|
||||
getSortLabel(field: string): string {
|
||||
return this.sortOptions.find(opt => opt.field === field)?.label ?? field;
|
||||
}
|
||||
|
||||
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';
|
||||
this.syncLegacySort();
|
||||
}
|
||||
|
||||
onSortCriteriaDrop(event: CdkDragDrop<SortCriterion[]>): void {
|
||||
moveItemInArray(this.sortCriteria, event.previousIndex, event.currentIndex);
|
||||
this.syncLegacySort();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
saveSettings(): void {
|
||||
if (!this.user) return;
|
||||
|
||||
@@ -204,6 +261,7 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
|
||||
...prefs.global,
|
||||
sortKey: this.selectedSort,
|
||||
sortDir: this.selectedSortDir,
|
||||
sortCriteria: [...this.sortCriteria],
|
||||
view: this.selectedView
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user