Merge pull request #432 from adityachandelgit/develop
Merge Develop into Master for Release
This commit is contained in:
@@ -59,6 +59,7 @@ public class BookLoreUser {
|
||||
private String sortKey;
|
||||
private String sortDir;
|
||||
private String view;
|
||||
private Float coverSize;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
+1
@@ -95,6 +95,7 @@ public class DefaultUserSettingsProvider {
|
||||
.sortKey("title")
|
||||
.sortDir("ASC")
|
||||
.view("GRID")
|
||||
.coverSize(1.0F)
|
||||
.build())
|
||||
.overrides(null)
|
||||
.build();
|
||||
|
||||
@@ -32,6 +32,33 @@
|
||||
</i>
|
||||
</a>
|
||||
|
||||
<div>
|
||||
<a
|
||||
class="topbar-items topbar-item"
|
||||
(click)="sizeOverlay.toggle($event)"
|
||||
pTooltip="Adjust grid item scale"
|
||||
tooltipPosition="top"
|
||||
>
|
||||
<i class="pi pi-th-large"></i>
|
||||
</a>
|
||||
|
||||
<p-popover #sizeOverlay [dismissable]="true" appendTo="body">
|
||||
<div style="width: 220px; padding: 8px;">
|
||||
<p-slider
|
||||
[(ngModel)]="scaleFactor"
|
||||
[min]="0.5"
|
||||
[max]="1.5"
|
||||
[step]="0.01"
|
||||
[style]="{ width: '100%' }"
|
||||
(onChange)="updateScale()"
|
||||
></p-slider>
|
||||
<div style="text-align: center; margin-top: 8px;">
|
||||
{{ scaleFactor.toFixed(2) }}x
|
||||
</div>
|
||||
</div>
|
||||
</p-popover>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<a class="topbar-items topbar-item" (click)="sortMenu.toggle($event)">
|
||||
<i
|
||||
@@ -112,11 +139,18 @@
|
||||
</div>
|
||||
|
||||
<div class="book-card-container" *ngIf="bookState?.books as books">
|
||||
<app-book-table *ngIf="currentViewMode === 'table' && books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)" [sortOption]="selectedSort!"></app-book-table>
|
||||
<app-book-table *ngIf="currentViewMode === 'table' && books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)"
|
||||
[sortOption]="selectedSort!"></app-book-table>
|
||||
|
||||
<virtual-scroller *ngIf="currentViewMode === 'grid'" class="virtual-scroller" #scroll [items]="books">
|
||||
<div class="grid grid-cols-12 gap-4" #container>
|
||||
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
||||
<div
|
||||
class="grid"
|
||||
#container
|
||||
[ngStyle]="{'grid-template-columns': 'repeat(auto-fit, minmax(' + gridColumnMinWidth + ', 1fr))'}">
|
||||
<div
|
||||
class="virtual-scroller-item"
|
||||
*ngFor="let book of scroll.viewPortItems"
|
||||
[ngStyle]="{width: currentCardSize.width + 'px', height: currentCardSize.height + 'px'}">
|
||||
<app-book-card
|
||||
[book]="book"
|
||||
[isCheckboxEnabled]="true"
|
||||
|
||||
@@ -18,19 +18,18 @@
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.virtual-scroller-item {
|
||||
height: 220px;
|
||||
width: 135px;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
|
||||
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
|
||||
gap: 1.3rem;
|
||||
overflow: hidden;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.virtual-scroller-item {
|
||||
transition: width 0.2s ease, height 0.2s ease;
|
||||
}
|
||||
|
||||
.book-browser-footer {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
|
||||
@@ -3,9 +3,8 @@ import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
|
||||
import {LibraryService} from '../../service/library.service';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {filter, take} from 'rxjs/operators';
|
||||
import {BehaviorSubject, Observable, of, Subject} from 'rxjs';
|
||||
import {debounceTime, filter, map, switchMap, take} from 'rxjs/operators';
|
||||
import {BehaviorSubject, combineLatest, Observable, of, Subject} from 'rxjs';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
@@ -21,18 +20,21 @@ import {animate, state, style, transition, trigger} from '@angular/animations';
|
||||
import {MetadataFetchOptionsComponent} from '../../metadata/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component';
|
||||
import {MetadataRefreshType} from '../../metadata/model/request/metadata-refresh-type.enum';
|
||||
import {Button} from 'primeng/button';
|
||||
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
|
||||
import {AsyncPipe, NgClass, NgForOf, NgIf, NgStyle} from '@angular/common';
|
||||
import {VirtualScrollerModule} from '@iharbeck/ngx-virtual-scroller';
|
||||
import {BookCardComponent} from './book-card/book-card.component';
|
||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
||||
import {Menu} from 'primeng/menu';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {BookFilterComponent} from './book-filter/book-filter.component';
|
||||
import {BookFilterComponent, isFileSizeInRange, isPageCountInRange, isRatingInRange} from './book-filter/book-filter.component';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {Fluid} from 'primeng/fluid';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {EntityViewPreferences, UserService} from '../../../settings/user-management/user.service';
|
||||
import {LockUnlockMetadataDialogComponent} from './lock-unlock-metadata-dialog/lock-unlock-metadata-dialog.component';
|
||||
import {OverlayPanelModule} from 'primeng/overlaypanel';
|
||||
import {Slider} from 'primeng/slider';
|
||||
import {Popover} from 'primeng/popover';
|
||||
|
||||
export enum EntityType {
|
||||
LIBRARY = 'Library',
|
||||
@@ -63,7 +65,7 @@ const SORT_DIRECTION = {
|
||||
standalone: true,
|
||||
templateUrl: './book-browser.component.html',
|
||||
styleUrls: ['./book-browser.component.scss'],
|
||||
imports: [Button, NgIf, VirtualScrollerModule, BookCardComponent, AsyncPipe, ProgressSpinner, Menu, NgForOf, InputText, FormsModule, BookTableComponent, BookFilterComponent, Tooltip, NgClass, Fluid, PrimeTemplate],
|
||||
imports: [Button, NgIf, VirtualScrollerModule, BookCardComponent, AsyncPipe, ProgressSpinner, Menu, NgForOf, InputText, FormsModule, BookTableComponent, BookFilterComponent, Tooltip, NgClass, Fluid, PrimeTemplate, NgStyle, OverlayPanelModule, Slider, Popover],
|
||||
animations: [
|
||||
trigger('slideInOut', [
|
||||
state('void', style({
|
||||
@@ -100,6 +102,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
|
||||
|
||||
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
||||
selectedFilterMode = new BehaviorSubject<'and' | 'or'>('and');
|
||||
protected resetFilterSubject = new Subject<void>();
|
||||
|
||||
protected userService = inject(UserService);
|
||||
@@ -138,8 +141,44 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
lastAppliedSort: SortOption | null = null;
|
||||
filterVisibility = true;
|
||||
|
||||
prefs!: EntityViewPreferences;
|
||||
|
||||
baseWidth = 135;
|
||||
baseHeight = 220;
|
||||
scaleFactor = 1.0;
|
||||
|
||||
get currentCardSize() {
|
||||
return {
|
||||
width: Math.round(this.baseWidth * this.scaleFactor),
|
||||
height: Math.round(this.baseHeight * this.scaleFactor)
|
||||
};
|
||||
}
|
||||
|
||||
get gridColumnMinWidth(): string {
|
||||
return `${this.currentCardSize.width}px`;
|
||||
}
|
||||
|
||||
updateScale(): void {
|
||||
this.scaleChange$.next(this.scaleFactor);
|
||||
}
|
||||
|
||||
private scaleChange$ = new Subject<number>();
|
||||
|
||||
ngOnInit(): void {
|
||||
|
||||
this.scaleChange$.pipe(debounceTime(1000)).subscribe(scale => {
|
||||
const user = this.userService.getCurrentUser();
|
||||
if (!user || !this.prefs) return;
|
||||
this.prefs.global = this.prefs.global ?? {};
|
||||
this.prefs.global.coverSize = scale;
|
||||
this.userService.updateUserSetting(user.id, 'entityViewPreferences', this.prefs);
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Cover Size Saved',
|
||||
detail: `Cover size set to ${scale.toFixed(2)}x.`
|
||||
});
|
||||
});
|
||||
|
||||
this.bookService.loadBooks();
|
||||
|
||||
const currentPath = this.activatedRoute.snapshot.routeConfig?.path;
|
||||
@@ -278,31 +317,54 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
private sideBarFilter(bookState: BookState): Observable<BookState> {
|
||||
return this.selectedFilter.pipe(
|
||||
map((activeFilters: Record<string, any[]> | null) => {
|
||||
if (!activeFilters) {
|
||||
return bookState;
|
||||
}
|
||||
|
||||
return combineLatest([this.selectedFilter, this.selectedFilterMode]).pipe(
|
||||
map(([activeFilters, mode]) => {
|
||||
if (!activeFilters) return bookState;
|
||||
const filteredBooks = (bookState.books || []).filter(book => {
|
||||
return Object.entries(activeFilters).every(([filterType, filterValues]) => {
|
||||
if (!Array.isArray(filterValues) || filterValues.length === 0) return true;
|
||||
|
||||
const matches = Object.entries(activeFilters).map(([filterType, filterValues]) => {
|
||||
if (!Array.isArray(filterValues) || filterValues.length === 0) {
|
||||
return mode === 'or';
|
||||
}
|
||||
switch (filterType) {
|
||||
case 'author':
|
||||
return filterValues.every(val => book.metadata?.authors?.includes(val));
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.authors?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.authors?.includes(val));
|
||||
case 'category':
|
||||
return filterValues.every(val => book.metadata?.categories?.includes(val));
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.categories?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.categories?.includes(val));
|
||||
case 'publisher':
|
||||
return filterValues.every(val => book.metadata?.publisher === val);
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.publisher === val)
|
||||
: filterValues.some(val => book.metadata?.publisher === val);
|
||||
case 'series':
|
||||
return filterValues.every(val => book.metadata?.seriesName === val);
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.seriesName === val)
|
||||
: filterValues.some(val => book.metadata?.seriesName === val);
|
||||
case 'amazonRating':
|
||||
return filterValues.some(range => isRatingInRange(book.metadata?.amazonRating, range));
|
||||
case 'goodreadsRating':
|
||||
return filterValues.some(range => isRatingInRange(book.metadata?.goodreadsRating, range));
|
||||
case 'hardcoverRating':
|
||||
return filterValues.some(range => isRatingInRange(book.metadata?.hardcoverRating, range));
|
||||
case 'publishedDate':
|
||||
return filterValues.includes(new Date(book.metadata?.publishedDate || '').getFullYear());
|
||||
case 'fileSize':
|
||||
return filterValues.some(range => isFileSizeInRange(book.fileSizeKb, range));
|
||||
case 'shelfStatus':
|
||||
const shelved = book.shelves && book.shelves.length > 0 ? 'shelved' : 'unshelved';
|
||||
return filterValues.includes(shelved);
|
||||
case 'pageCount':
|
||||
return filterValues.some(range => isPageCountInRange(book.metadata?.pageCount!, range));
|
||||
case 'language':
|
||||
return filterValues.includes(book.metadata?.language);
|
||||
default:
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return mode === 'and' ? matches.every(m => m) : matches.some(m => m);
|
||||
});
|
||||
|
||||
return {...bookState, books: filteredBooks};
|
||||
})
|
||||
);
|
||||
@@ -538,6 +600,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
this.selectedFilter.next(filters);
|
||||
});
|
||||
|
||||
this.bookFilterComponent.filterModeChanged.subscribe((mode: 'and' | 'or') => {
|
||||
this.selectedFilterMode.next(mode);
|
||||
});
|
||||
|
||||
this.activatedRoute.queryParamMap.subscribe(paramMap => {
|
||||
const viewParam = paramMap.get(QUERY_PARAMS.VIEW);
|
||||
const sortParam = paramMap.get(QUERY_PARAMS.SORT);
|
||||
@@ -550,11 +616,12 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
take(1)
|
||||
)
|
||||
.subscribe(user => {
|
||||
const prefs = user.userSettings?.entityViewPreferences;
|
||||
const globalPrefs = prefs?.global;
|
||||
this.prefs = user.userSettings?.entityViewPreferences;
|
||||
const globalPrefs = this.prefs?.global;
|
||||
const currentEntityTypeStr = this.entityType ? this.entityType.toString().toUpperCase() : undefined;
|
||||
this.scaleFactor = user.userSettings.entityViewPreferences?.global?.coverSize ?? 1.0;
|
||||
|
||||
const override = prefs?.overrides?.find(o =>
|
||||
const override = this.prefs?.overrides?.find(o =>
|
||||
o.entityType?.toUpperCase() === currentEntityTypeStr &&
|
||||
o.entityId === this.entity?.id
|
||||
);
|
||||
|
||||
+25
-6
@@ -1,27 +1,46 @@
|
||||
<div *ngIf="showFilters" class="w-[225px] ml-4 h-[calc(100vh-6.5rem)] rounded-xl bg-[var(--card-background)] overflow-hidden">
|
||||
<div class="flex items-center justify-between px-4 py-4">
|
||||
<span class="text font-semibold">Filter Mode</span>
|
||||
<p-selectButton
|
||||
[options]="filterModeOptions"
|
||||
[(ngModel)]="selectedFilterMode"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
class="text-sm"
|
||||
allowEmpty="false"
|
||||
styleClass="h-8"></p-selectButton>
|
||||
</div>
|
||||
<p-accordion [value]="0">
|
||||
<p-accordion-panel *ngFor="let filterType of filterTypes; let i = index" [value]="i">
|
||||
<p-accordion-header>
|
||||
<span class="inline-flex items-center gap-1">
|
||||
{{ filterType | titlecase }}
|
||||
{{ filterLabels[filterType] || (filterType | titlecase) }}
|
||||
<span *ngIf="activeFilters[filterType]?.length" class="text-sm text-[var(--primary-color)]">
|
||||
({{ activeFilters[filterType].length }})
|
||||
</span>
|
||||
</span>
|
||||
</p-accordion-header>
|
||||
<p-accordion-content class="h-[40rem] !overflow-y-auto">
|
||||
<p-accordion-content class="h-[31rem] !overflow-y-auto">
|
||||
<div *ngIf="filterStreams[filterType] | async as filters">
|
||||
<div *ngFor="let filter of filters">
|
||||
<p
|
||||
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1"
|
||||
<div
|
||||
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1 flex items-center gap-2"
|
||||
[ngClass]="{
|
||||
'text-[var(--primary-color)]':
|
||||
activeFilters[filterType]?.includes(filter.value?.id || filter.value)
|
||||
}"
|
||||
(click)="handleFilterClick(filterType, filter.value?.id || filter.value)">
|
||||
|
||||
<p-badge [value]="filter.bookCount"></p-badge>
|
||||
{{ filter.value.name || filter.value }}
|
||||
</p>
|
||||
<ng-container *ngIf="filterType === 'amazonRating'; else defaultLabel">
|
||||
<ng-container *ngIf="filterType === 'amazonRating'; else defaultLabel">
|
||||
<span>{{ filter.value.name }}</span>
|
||||
</ng-container>
|
||||
</ng-container>
|
||||
<ng-template #defaultLabel>
|
||||
{{ filter.value.name || filter.value }}
|
||||
</ng-template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</p-accordion-content>
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
:host ::ng-deep p-accordion-header {
|
||||
--p-accordion-header-padding: 0.6rem 1rem;
|
||||
}
|
||||
|
||||
+158
-22
@@ -9,10 +9,97 @@ import {Book} from '../../../model/book.model';
|
||||
import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion';
|
||||
import {AsyncPipe, NgClass, NgForOf, NgIf, TitleCasePipe} from '@angular/common';
|
||||
import {Badge} from 'primeng/badge';
|
||||
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {SelectButton} from 'primeng/selectbutton';
|
||||
|
||||
type Filter<T> = { value: T; bookCount: number };
|
||||
|
||||
export const ratingRanges = [
|
||||
{id: '0to1', label: '0 to 1', min: 0, max: 1},
|
||||
{id: '1to2', label: '1 to 2', min: 1, max: 2},
|
||||
{id: '2to3', label: '2 to 3', min: 2, max: 3},
|
||||
{id: '3to4', label: '3 to 4', min: 3, max: 4},
|
||||
{id: '4to4.5', label: '4 to 4.5', min: 4, max: 4.5},
|
||||
{id: '4.5plus', label: '4.5+', min: 4.5, max: Infinity}
|
||||
];
|
||||
|
||||
export const fileSizeRanges = [
|
||||
{id: '<1mb', label: '< 1 MB', min: 0, max: 1024},
|
||||
{id: '1to10mb', label: '1–10 MB', min: 1024, max: 10240},
|
||||
{id: '10to50mb', label: '10–50 MB', min: 10240, max: 51200},
|
||||
{id: '50to100mb', label: '50–100 MB', min: 51200, max: 102400},
|
||||
{id: '250to500mb', label: '250–500 MB', min: 256000, max: 512000},
|
||||
{id: '500mbto1gb', label: '500 MB – 1 GB', min: 512000, max: 1048576},
|
||||
{id: '1to2gb', label: '1–2 GB', min: 1048576, max: 2097152},
|
||||
{id: '5plusgb', label: '5+ GB', min: 5242880, max: Infinity}
|
||||
];
|
||||
|
||||
export const pageCountRanges = [
|
||||
{id: '<50', label: '< 50 pages', min: 0, max: 50},
|
||||
{id: '50to100', label: '50–100 pages', min: 50, max: 100},
|
||||
{id: '100to200', label: '100–200 pages', min: 100, max: 200},
|
||||
{id: '200to400', label: '200–400 pages', min: 200, max: 400},
|
||||
{id: '400to600', label: '400–600 pages', min: 400, max: 600},
|
||||
{id: '600to1000', label: '600–1000 pages', min: 600, max: 1000},
|
||||
{id: '1000plus', label: '1000+ pages', min: 1000, max: Infinity}
|
||||
];
|
||||
|
||||
export function isRatingInRange(rating: number | undefined | null, rangeId: string): boolean {
|
||||
if (rating == null) return false;
|
||||
const range = ratingRanges.find(r => r.id === rangeId);
|
||||
if (!range) return false;
|
||||
return rating >= range.min && rating < range.max;
|
||||
}
|
||||
|
||||
export function isFileSizeInRange(fileSizeKb: number | undefined, rangeId: string): boolean {
|
||||
if (fileSizeKb == null) return false;
|
||||
const range = fileSizeRanges.find(r => r.id === rangeId);
|
||||
if (!range) return false;
|
||||
return fileSizeKb >= range.min && fileSizeKb < range.max;
|
||||
}
|
||||
|
||||
export function isPageCountInRange(pageCount: number | undefined, rangeId: string): boolean {
|
||||
if (pageCount == null) return false;
|
||||
const range = pageCountRanges.find(r => r.id === rangeId);
|
||||
if (!range) return false;
|
||||
return pageCount >= range.min && pageCount < range.max;
|
||||
}
|
||||
|
||||
const getLanguageFilter = (book: Book) => {
|
||||
const lang = book.metadata?.language;
|
||||
return lang ? [{id: lang, name: lang}] : [];
|
||||
};
|
||||
|
||||
function getFileSizeRangeFilters(sizeKb?: number) {
|
||||
if (sizeKb == null) return [];
|
||||
const match = fileSizeRanges.find(r => sizeKb >= r.min && sizeKb < r.max);
|
||||
return match ? [{id: match.id, name: match.label}] : [];
|
||||
}
|
||||
|
||||
function getRatingRangeFilters(rating?: number) {
|
||||
if (rating == null) return [];
|
||||
const match = ratingRanges.find(r => rating >= r.min && rating < r.max);
|
||||
return match ? [{id: match.id, name: match.label}] : [];
|
||||
}
|
||||
|
||||
function extractPublishedYearFilter(book: Book): { id: number; name: string }[] {
|
||||
const date = book.metadata?.publishedDate;
|
||||
if (!date) return [];
|
||||
const year = new Date(date).getFullYear();
|
||||
return [{id: year, name: year.toString()}];
|
||||
}
|
||||
|
||||
function getShelfStatusFilter(book: Book): { id: string; name: string }[] {
|
||||
const isShelved = book.shelves && book.shelves.length > 0;
|
||||
return [{id: isShelved ? 'shelved' : 'unshelved', name: isShelved ? 'Shelved' : 'Unshelved'}];
|
||||
}
|
||||
|
||||
function getPageCountRangeFilters(pageCount?: number) {
|
||||
if (pageCount == null) return [];
|
||||
const match = pageCountRanges.find(r => pageCount >= r.min && pageCount < r.max);
|
||||
return match ? [{id: match.id, name: match.label}] : [];
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-filter',
|
||||
templateUrl: './book-filter.component.html',
|
||||
@@ -28,11 +115,14 @@ type Filter<T> = { value: T; bookCount: number };
|
||||
NgClass,
|
||||
Badge,
|
||||
AsyncPipe,
|
||||
TitleCasePipe
|
||||
TitleCasePipe,
|
||||
FormsModule,
|
||||
SelectButton
|
||||
]
|
||||
})
|
||||
export class BookFilterComponent implements OnInit {
|
||||
@Output() filterSelected = new EventEmitter<Record<string, any> | null>();
|
||||
@Output() filterModeChanged = new EventEmitter<'and' | 'or'>();
|
||||
|
||||
@Input() showFilters: boolean = true;
|
||||
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
||||
@@ -42,42 +132,87 @@ export class BookFilterComponent implements OnInit {
|
||||
activeFilters: Record<string, any> = {};
|
||||
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
|
||||
filterTypes: string[] = [];
|
||||
filterModeOptions = [
|
||||
{ label: 'AND', value: 'and' },
|
||||
{ label: 'OR', value: 'or' }
|
||||
];
|
||||
private _selectedFilterMode: 'and' | 'or' = 'and';
|
||||
|
||||
readonly filterLabels: Record<string, string> = {
|
||||
author: 'Author',
|
||||
category: 'Category',
|
||||
series: 'Series',
|
||||
publisher: 'Publisher',
|
||||
amazonRating: 'Amazon Rating',
|
||||
goodreadsRating: 'Goodreads Rating',
|
||||
hardcoverRating: 'Hardcover Rating',
|
||||
publishedDate: 'Published Year',
|
||||
fileSize: 'File Size',
|
||||
shelfStatus: 'Shelf Status',
|
||||
pageCount: 'Page Count',
|
||||
language: 'Language',
|
||||
};
|
||||
|
||||
bookService = inject(BookService);
|
||||
|
||||
ngOnInit(): void {
|
||||
if (this.entity$ && this.entityType$) {
|
||||
this.filterStreams = {
|
||||
author: this.getFilterStream((book: Book) => book.metadata?.authors.map((name) => ({id: name, name})) || [], 'id', 'name'),
|
||||
category: this.getFilterStream((book: Book) => book.metadata?.categories.map((name) => ({id: name, name})) || [], 'id', 'name'),
|
||||
author: this.getFilterStream((book: Book) => book.metadata?.authors.map(name => ({id: name, name})) || [], 'id', 'name'),
|
||||
category: this.getFilterStream((book: Book) => book.metadata?.categories.map(name => ({id: name, name})) || [], 'id', 'name'),
|
||||
series: this.getFilterStream((book) => (book.metadata?.seriesName ? [{id: book.metadata.seriesName, name: book.metadata.seriesName}] : []), 'id', 'name'),
|
||||
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name'),
|
||||
amazonRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.amazonRating!), 'id', 'name'),
|
||||
goodreadsRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.goodreadsRating!), 'id', 'name'),
|
||||
hardcoverRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.hardcoverRating!), 'id', 'name'),
|
||||
publishedDate: this.getFilterStream(extractPublishedYearFilter, 'id', 'name'),
|
||||
fileSize: this.getFilterStream((book: Book) => getFileSizeRangeFilters(book.fileSizeKb), 'id', 'name'),
|
||||
pageCount: this.getFilterStream((book: Book) => getPageCountRangeFilters(book.metadata?.pageCount!), 'id', 'name'),
|
||||
language: this.getFilterStream(getLanguageFilter, 'id', 'name'),
|
||||
shelfStatus: this.getFilterStream(getShelfStatusFilter, 'id', 'name')
|
||||
};
|
||||
this.filterTypes = Object.keys(this.filterStreams);
|
||||
}
|
||||
|
||||
if (this.resetFilter$) {
|
||||
this.resetFilter$.subscribe(() => this.clearActiveFilter());
|
||||
}
|
||||
}
|
||||
|
||||
private getFilterStream<T>(extractor: (book: Book) => T[] | undefined, idKey: keyof T, nameKey: keyof T): Observable<Filter<T[keyof T]>[]> {
|
||||
return combineLatest([this.bookService.bookState$, this.entity$ ?? of(null), this.entityType$ ?? of(EntityType.ALL_BOOKS)])
|
||||
.pipe(
|
||||
map(([state, entity, entityType]) => {
|
||||
const filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||
const filterMap = new Map<any, Filter<any>>();
|
||||
filteredBooks.forEach((book) => {
|
||||
(extractor(book) || []).forEach((item) => {
|
||||
const id = item[idKey];
|
||||
if (!filterMap.has(id)) {
|
||||
filterMap.set(id, {value: item, bookCount: 0});
|
||||
}
|
||||
filterMap.get(id)!.bookCount += 1;
|
||||
});
|
||||
private getFilterStream<T>(
|
||||
extractor: (book: Book) => T[] | undefined,
|
||||
idKey: keyof T,
|
||||
nameKey: keyof T
|
||||
): Observable<Filter<T[keyof T]>[]> {
|
||||
return combineLatest([this.bookService.bookState$, this.entity$ ?? of(null), this.entityType$ ?? of(EntityType.ALL_BOOKS)]).pipe(
|
||||
map(([state, entity, entityType]) => {
|
||||
const filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||
const filterMap = new Map<any, Filter<any>>();
|
||||
filteredBooks.forEach((book) => {
|
||||
(extractor(book) || []).forEach((item) => {
|
||||
const id = item[idKey];
|
||||
if (!filterMap.has(id)) {
|
||||
filterMap.set(id, {value: item, bookCount: 0});
|
||||
}
|
||||
filterMap.get(id)!.bookCount += 1;
|
||||
});
|
||||
return Array.from(filterMap.values()).sort((a, b) => b.bookCount - a.bookCount || a.value[nameKey].localeCompare(b.value[nameKey]));
|
||||
})
|
||||
);
|
||||
});
|
||||
return Array.from(filterMap.values()).sort(
|
||||
(a, b) =>
|
||||
b.bookCount - a.bookCount ||
|
||||
a.value[nameKey].toString().localeCompare(b.value[nameKey].toString())
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
get selectedFilterMode(): 'and' | 'or' {
|
||||
return this._selectedFilterMode;
|
||||
}
|
||||
|
||||
set selectedFilterMode(mode: 'and' | 'or') {
|
||||
this._selectedFilterMode = mode;
|
||||
this.filterModeChanged.emit(mode);
|
||||
}
|
||||
|
||||
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||
@@ -104,7 +239,8 @@ export class BookFilterComponent implements OnInit {
|
||||
} else {
|
||||
this.activeFilters[filterType].push(value);
|
||||
}
|
||||
this.filterSelected.emit({ ...this.activeFilters });
|
||||
|
||||
this.filterSelected.emit({...this.activeFilters});
|
||||
}
|
||||
|
||||
clearActiveFilter() {
|
||||
|
||||
@@ -16,6 +16,7 @@ export interface EntityViewPreference {
|
||||
sortKey: string;
|
||||
sortDir: 'ASC' | 'DESC';
|
||||
view: 'GRID' | 'TABLE';
|
||||
coverSize: number
|
||||
}
|
||||
|
||||
export interface EntityViewPreferenceOverride {
|
||||
@@ -116,6 +117,10 @@ export class UserService {
|
||||
});
|
||||
}
|
||||
|
||||
getCurrentUser(): User | null {
|
||||
return this.userStateSubject.getValue();
|
||||
}
|
||||
|
||||
getMyself(): Observable<User> {
|
||||
return this.http.get<User>(`${this.userUrl}/me`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user