Add filter mode to querystring (#1728)
* Add filter mode to query string * Fix state of clear filters button * Ensures filter divider lines meet container border
This commit is contained in:
+3
-3
@@ -8,15 +8,15 @@
|
||||
@if (entityType === EntityType.ALL_BOOKS) {
|
||||
@if (currentFilterLabel && rawFilterParamFromUrl) {
|
||||
{{ currentFilterLabel }}
|
||||
} @else if (hasSidebarFilters || hasSearchTerm) {
|
||||
} @else if (isFilterActive || hasSearchTerm) {
|
||||
{{ computedFilterLabel }}
|
||||
} @else {
|
||||
All Books
|
||||
}
|
||||
} @else if (entityType === EntityType.UNSHELVED) {
|
||||
{{ (hasSidebarFilters || hasSearchTerm) ? 'Unshelved Books (Filtered)' : 'Unshelved Books' }}
|
||||
{{ (isFilterActive || hasSearchTerm) ? 'Unshelved Books (Filtered)' : 'Unshelved Books' }}
|
||||
} @else {
|
||||
{{ entityType }}: {{ (entity$ | async)?.name }}{{ (hasSidebarFilters || hasSearchTerm) ? ' (Filtered)' : '' }}
|
||||
{{ entityType }}: {{ (entity$ | async)?.name }}{{ (isFilterActive || hasSearchTerm) ? ' (Filtered)' : '' }}
|
||||
}
|
||||
</p>
|
||||
|
||||
|
||||
+34
-9
@@ -25,7 +25,7 @@ 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, BookFilterMode} from './book-filter/book-filter.component';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {EntityViewPreferences, UserService} from '../../../settings/user-management/user.service';
|
||||
import {SeriesCollapseFilter} from './filters/SeriesCollapseFilter';
|
||||
@@ -63,6 +63,7 @@ const QUERY_PARAMS = {
|
||||
SORT: 'sort',
|
||||
DIRECTION: 'direction',
|
||||
FILTER: 'filter',
|
||||
FMODE: 'fmode',
|
||||
SIDEBAR: 'sidebar',
|
||||
FROM: 'from',
|
||||
};
|
||||
@@ -126,7 +127,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
searchTerm$ = new BehaviorSubject<string>('');
|
||||
parsedFilters: Record<string, string[]> = {};
|
||||
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
||||
selectedFilterMode = new BehaviorSubject<'and' | 'or' | 'single'>('and');
|
||||
selectedFilterMode = new BehaviorSubject<BookFilterMode>('and');
|
||||
protected resetFilterSubject = new Subject<void>();
|
||||
entity: Library | Shelf | MagicShelf | null = null;
|
||||
entityType: EntityType | undefined;
|
||||
@@ -173,12 +174,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
return this.currentViewMode === VIEW_MODES.GRID ? 'pi pi-objects-column' : 'pi pi-table';
|
||||
}
|
||||
|
||||
get hasSidebarFilters(): boolean {
|
||||
return !!this.selectedFilter.value && Object.keys(this.selectedFilter.value).length > 0;
|
||||
}
|
||||
|
||||
get isFilterActive(): boolean {
|
||||
return this.selectedFilter.value !== null;
|
||||
return !!this.selectedFilter.value && Object.keys(this.selectedFilter.value).length > 0;
|
||||
}
|
||||
|
||||
get computedFilterLabel(): string {
|
||||
@@ -270,6 +267,14 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
const sortParam = queryParamMap.get(QUERY_PARAMS.SORT);
|
||||
const directionParam = queryParamMap.get(QUERY_PARAMS.DIRECTION);
|
||||
const filterParams = queryParamMap.get(QUERY_PARAMS.FILTER);
|
||||
const filterMode = <BookFilterMode>queryParamMap.get(QUERY_PARAMS.FMODE);
|
||||
|
||||
if (filterMode && filterMode !== this.selectedFilterMode.getValue()) {
|
||||
this.selectedFilterMode.next(filterMode);
|
||||
if (this.bookFilterComponent) {
|
||||
this.bookFilterComponent.selectedFilterMode = filterMode;
|
||||
}
|
||||
}
|
||||
|
||||
this.sidebarFilterTogglePrefService.showFilter$.subscribe(value => {
|
||||
this.showFilter = value;
|
||||
@@ -366,7 +371,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
const queryParams: any = {
|
||||
[QUERY_PARAMS.VIEW]: this.currentViewMode,
|
||||
[QUERY_PARAMS.SORT]: this.bookSorter.selectedSort.field,
|
||||
[QUERY_PARAMS.DIRECTION]: this.bookSorter.selectedSort.direction === SortDirection.ASCENDING ? SORT_DIRECTION.ASCENDING : SORT_DIRECTION.DESCENDING
|
||||
[QUERY_PARAMS.DIRECTION]: this.bookSorter.selectedSort.direction === SortDirection.ASCENDING ? SORT_DIRECTION.ASCENDING : SORT_DIRECTION.DESCENDING,
|
||||
[QUERY_PARAMS.FMODE]: this.selectedFilterMode.getValue(),
|
||||
};
|
||||
|
||||
if (Object.keys(parsedFilters).length > 0) {
|
||||
@@ -393,6 +399,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
if (this.bookFilterComponent) {
|
||||
this.bookFilterComponent.setFilters?.(this.parsedFilters);
|
||||
this.bookFilterComponent.onFiltersChanged?.();
|
||||
this.bookFilterComponent.selectedFilterMode = this.selectedFilterMode.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -416,8 +423,26 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
}
|
||||
|
||||
onFilterModeChanged(mode: 'and' | 'or' | 'single'): void {
|
||||
onFilterModeChanged(mode: BookFilterMode): void {
|
||||
if (this.settingFiltersFromUrl || mode === this.selectedFilterMode.getValue()) return;
|
||||
|
||||
this.selectedFilterMode.next(mode);
|
||||
|
||||
// Clear filters if switching from multiple selected to single mode
|
||||
const params: any = {[QUERY_PARAMS.FMODE]: mode};
|
||||
if (mode === 'single') {
|
||||
const categories = Object.keys(this.parsedFilters);
|
||||
if (categories.length > 1 || (categories.length == 1 && this.parsedFilters[categories[0]].length > 1)) {
|
||||
params[QUERY_PARAMS.FILTER] = null;
|
||||
}
|
||||
}
|
||||
|
||||
this.router.navigate([], {
|
||||
relativeTo: this.activatedRoute,
|
||||
queryParams: params,
|
||||
queryParamsHandling: 'merge',
|
||||
replaceUrl: false
|
||||
});
|
||||
}
|
||||
|
||||
toggleSidebar(): void {
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
</div>
|
||||
|
||||
<div class="filter-content">
|
||||
<p-accordion [value]="expandedPanels">
|
||||
<p-accordion [value]="expandedPanels" [multiple]="true">
|
||||
@for (filterType of filterTypes; track trackByFilterType(i, filterType); let i = $index) {
|
||||
<p-accordion-panel [value]="i">
|
||||
<p-accordion-header>
|
||||
|
||||
-1
@@ -36,7 +36,6 @@
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding-bottom: 1rem;
|
||||
padding-right: 0.25rem;
|
||||
overscroll-behavior: contain;
|
||||
}
|
||||
|
||||
|
||||
+22
-14
@@ -19,6 +19,8 @@ import {Tooltip} from 'primeng/tooltip';
|
||||
|
||||
type Filter<T> = { value: T; bookCount: number };
|
||||
|
||||
export type BookFilterMode = 'and' | 'or' | 'single';
|
||||
|
||||
export const ratingRanges = [
|
||||
{id: '0to1', label: '0 to 1', min: 0, max: 1, sortIndex: 0},
|
||||
{id: '1to2', label: '1 to 2', min: 1, max: 2, sortIndex: 1},
|
||||
@@ -158,7 +160,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
private filterChangeSubject = new Subject<Record<string, any> | null>();
|
||||
|
||||
@Output() filterSelected = new EventEmitter<Record<string, any> | null>();
|
||||
@Output() filterModeChanged = new EventEmitter<'and' | 'or' | 'single'>();
|
||||
@Output() filterModeChanged = new EventEmitter<BookFilterMode>();
|
||||
|
||||
@Input() entity$!: Observable<Library | Shelf | MagicShelf | null> | undefined;
|
||||
@Input() entityType$!: Observable<EntityType> | undefined;
|
||||
@@ -174,8 +176,8 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
{label: 'OR', value: 'or'},
|
||||
{label: '1', value: 'single'},
|
||||
];
|
||||
private _selectedFilterMode: 'and' | 'or' | 'single' = 'and';
|
||||
expandedPanels: number = 0;
|
||||
private _selectedFilterMode: BookFilterMode = 'and';
|
||||
expandedPanels: number[] = [0];
|
||||
readonly filterLabels: Record<string, string> = {
|
||||
author: 'Author',
|
||||
category: 'Genre',
|
||||
@@ -323,16 +325,18 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
);
|
||||
}
|
||||
|
||||
get selectedFilterMode(): 'and' | 'or' | 'single' {
|
||||
get selectedFilterMode(): BookFilterMode {
|
||||
return this._selectedFilterMode;
|
||||
}
|
||||
|
||||
set selectedFilterMode(mode: 'and' | 'or' | 'single') {
|
||||
this._selectedFilterMode = mode;
|
||||
this.filterModeChanged.emit(mode);
|
||||
this.filterChangeSubject.next(
|
||||
Object.keys(this.activeFilters).length ? {...this.activeFilters} : null
|
||||
);
|
||||
set selectedFilterMode(mode: BookFilterMode) {
|
||||
if (mode !== this._selectedFilterMode) {
|
||||
this._selectedFilterMode = mode;
|
||||
this.filterModeChanged.emit(mode);
|
||||
this.filterChangeSubject.next(
|
||||
Object.keys(this.activeFilters).length ? {...this.activeFilters} : null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||
@@ -396,14 +400,18 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
|
||||
clearActiveFilter() {
|
||||
this.activeFilters = {};
|
||||
this.setExpandedPanels();
|
||||
this.filterChangeSubject.next(null);
|
||||
}
|
||||
|
||||
setExpandedPanels(): void {
|
||||
const firstActiveIndex = this.filterTypes.findIndex(
|
||||
type => this.activeFilters[type]?.length
|
||||
);
|
||||
this.expandedPanels = firstActiveIndex !== -1 ? firstActiveIndex : 0;
|
||||
const indexes = [];
|
||||
for (let i = 0; i < this.filterTypes.length; i++) {
|
||||
if (this.activeFilters[this.filterTypes[i]]?.length) {
|
||||
indexes.push(i);
|
||||
}
|
||||
}
|
||||
this.expandedPanels = indexes.length > 0 ? indexes : [0];
|
||||
}
|
||||
|
||||
onFiltersChanged(): void {
|
||||
|
||||
+21
-21
@@ -2,7 +2,7 @@ import {Observable, combineLatest, of} from 'rxjs';
|
||||
import {map} from 'rxjs/operators';
|
||||
import {BookFilter} from './BookFilter';
|
||||
import {BookState} from '../../../model/state/book-state.model';
|
||||
import {fileSizeRanges, matchScoreRanges, pageCountRanges, ratingRanges} from '../book-filter/book-filter.component';
|
||||
import {fileSizeRanges, matchScoreRanges, pageCountRanges, ratingRanges, BookFilterMode} from '../book-filter/book-filter.component';
|
||||
import {Book, ReadStatus} from '../../../model/book.model';
|
||||
|
||||
export function isRatingInRange(rating: number | undefined | null, rangeId: string): boolean {
|
||||
@@ -46,7 +46,7 @@ export function doesBookMatchReadStatus(book: Book, selected: string[]): boolean
|
||||
|
||||
export class SideBarFilter implements BookFilter {
|
||||
|
||||
constructor(private selectedFilter$: Observable<any>, private selectedFilterMode$: Observable<'and' | 'or' | 'single'>) {
|
||||
constructor(private selectedFilter$: Observable<any>, private selectedFilterMode$: Observable<BookFilterMode>) {
|
||||
}
|
||||
|
||||
filter(bookState: BookState): Observable<BookState> {
|
||||
@@ -60,29 +60,29 @@ export class SideBarFilter implements BookFilter {
|
||||
}
|
||||
switch (filterType) {
|
||||
case 'author':
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.authors?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.authors?.includes(val));
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.metadata?.authors?.includes(val))
|
||||
: filterValues.every(val => book.metadata?.authors?.includes(val));
|
||||
case 'category':
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.categories?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.categories?.includes(val));
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.metadata?.categories?.includes(val))
|
||||
: filterValues.every(val => book.metadata?.categories?.includes(val));
|
||||
case 'mood':
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.moods?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.moods?.includes(val));
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.metadata?.moods?.includes(val))
|
||||
: filterValues.every(val => book.metadata?.moods?.includes(val));
|
||||
case 'tag':
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.tags?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.tags?.includes(val));
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.metadata?.tags?.includes(val))
|
||||
: filterValues.every(val => book.metadata?.tags?.includes(val));
|
||||
case 'publisher':
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.publisher === val)
|
||||
: filterValues.some(val => book.metadata?.publisher === val);
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.metadata?.publisher === val)
|
||||
: filterValues.every(val => book.metadata?.publisher === val);
|
||||
case 'series':
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.seriesName === val)
|
||||
: filterValues.some(val => book.metadata?.seriesName === val);
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.metadata?.seriesName === val)
|
||||
: filterValues.every(val => book.metadata?.seriesName === val);
|
||||
case 'readStatus':
|
||||
return doesBookMatchReadStatus(book, filterValues);
|
||||
case 'amazonRating':
|
||||
@@ -115,7 +115,7 @@ export class SideBarFilter implements BookFilter {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return mode === 'and' ? matches.every(m => m) : matches.some(m => m);
|
||||
return mode === 'or' ? matches.some(m => m) : matches.every(m => m);
|
||||
});
|
||||
return {...bookState, books: filteredBooks};
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user