fix(table-view): table-view to also actually select all when users select press all (#645)

This commit is contained in:
brios
2026-04-22 17:27:40 -04:00
committed by Zach Yale
parent 52ed009098
commit 791a4eab6e
6 changed files with 65 additions and 45 deletions
@@ -313,6 +313,7 @@
[books]="books" [books]="books"
[preselectedBookIds]="selectedBooks()" [preselectedBookIds]="selectedBooks()"
(selectedBooksChange)="onSelectedBooksChange($event)" (selectedBooksChange)="onSelectedBooksChange($event)"
(selectAllRequested)="selectAllBooks()"
[sortOption]="this.bookSorter.selectedSort!" [sortOption]="this.bookSorter.selectedSort!"
[visibleColumns]="visibleColumns()" [visibleColumns]="visibleColumns()"
></app-book-table> ></app-book-table>
@@ -334,7 +335,7 @@
[onBookSelect]="onBookCardSelect" [onBookSelect]="onBookCardSelect"
[isSeriesCollapsed]="seriesCollapseFilter.seriesCollapsed()" [isSeriesCollapsed]="seriesCollapseFilter.seriesCollapsed()"
(checkboxClick)="onCheckboxClicked($event)" (checkboxClick)="onCheckboxClicked($event)"
[isSelected]="selectedBooks().has(book.id)" [isSelected]="bookSelectionService.isBookSelected(book)"
[useSquareCovers]="isAudiobookOnlyLibrary()" [useSquareCovers]="isAudiobookOnlyLibrary()"
[overlayPreferenceService]="bookCardOverlayPreferenceService"> [overlayPreferenceService]="bookCardOverlayPreferenceService">
</app-book-card> </app-book-card>
@@ -851,15 +851,9 @@ export class BookBrowserComponent implements AfterViewInit {
this.appBooksApi.fetchAllBookIds().pipe(take(1)).subscribe({ this.appBooksApi.fetchAllBookIds().pipe(take(1)).subscribe({
next: (allIds) => { next: (allIds) => {
this.bookSelectionService.selectAll(allIds); this.bookSelectionService.selectAll(allIds);
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
}, },
error: () => { error: () => {
this.bookSelectionService.selectAll(); this.bookSelectionService.selectAll();
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
} }
}); });
} }
@@ -21,14 +21,22 @@ export class BookSelectionService {
this.currentBooks = books; this.currentBooks = books;
} }
getSelectableBookIds(book: Book): number[] {
return book.seriesBooks?.length
? book.seriesBooks.map(seriesBook => seriesBook.id)
: [book.id];
}
isBookSelected(book: Book): boolean {
const selectedIds = this._selectedBooks();
const ids = this.getSelectableBookIds(book);
return ids.every(id => selectedIds.has(id));
}
selectBook(book: Book): void { selectBook(book: Book): void {
this._selectedBooks.update(current => { this._selectedBooks.update(current => {
const next = new Set(current); const next = new Set(current);
if (book.seriesBooks) { this.getSelectableBookIds(book).forEach(bookId => next.add(bookId));
book.seriesBooks.forEach(seriesBook => next.add(seriesBook.id));
} else {
next.add(book.id);
}
return next; return next;
}); });
} }
@@ -36,11 +44,7 @@ export class BookSelectionService {
deselectBook(book: Book): void { deselectBook(book: Book): void {
this._selectedBooks.update(current => { this._selectedBooks.update(current => {
const next = new Set(current); const next = new Set(current);
if (book.seriesBooks) { this.getSelectableBookIds(book).forEach(bookId => next.delete(bookId));
book.seriesBooks.forEach(seriesBook => next.delete(seriesBook.id));
} else {
next.delete(book.id);
}
return next; return next;
}); });
} }
@@ -83,7 +87,7 @@ export class BookSelectionService {
this._selectedBooks.update(current => { this._selectedBooks.update(current => {
const next = new Set(current); const next = new Set(current);
for (const book of this.currentBooks) { for (const book of this.currentBooks) {
next.add(book.id); this.getSelectableBookIds(book).forEach(bookId => next.add(bookId));
} }
return next; return next;
}); });
@@ -40,6 +40,7 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
selectedBookIds = new Set<number>(); selectedBookIds = new Set<number>();
@Output() selectedBooksChange = new EventEmitter<Set<number>>(); @Output() selectedBooksChange = new EventEmitter<Set<number>>();
@Output() selectAllRequested = new EventEmitter<void>();
@Input() books: Book[] = []; @Input() books: Book[] = [];
@Input() sortOption: SortOption | null = null; @Input() sortOption: SortOption | null = null;
@Input() visibleColumns: { field: string; header: string }[] = []; @Input() visibleColumns: { field: string; header: string }[] = [];
@@ -91,8 +92,8 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
this.metadataCenterViewMode = user.userSettings.metadataCenterViewMode ?? 'route'; this.metadataCenterViewMode = user.userSettings.metadataCenterViewMode ?? 'route';
} }
this.selectedBookIds = this.preselectedBookIds; this.selectedBookIds = new Set(this.preselectedBookIds);
this.selectedBooks = this.bookService.getBooksByIds([...this.selectedBookIds]); this.syncSelectedBooks();
this.setScrollHeight(); this.setScrollHeight();
window.addEventListener('resize', this.resizeListener); window.addEventListener('resize', this.resizeListener);
} }
@@ -104,7 +105,12 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
: 'calc(100dvh - 150px)'; : 'calc(100dvh - 150px)';
} }
ngOnChanges() { ngOnChanges(changes: import('@angular/core').SimpleChanges) {
if (changes['preselectedBookIds'] || changes['books']) {
this.selectedBookIds = new Set(this.preselectedBookIds);
this.syncSelectedBooks();
}
const wrapperElements = this.elementRef.nativeElement.querySelectorAll('.p-virtualscroller'); const wrapperElements = this.elementRef.nativeElement.querySelectorAll('.p-virtualscroller');
wrapperElements.forEach((wrapperElement: Element) => { wrapperElements.forEach((wrapperElement: Element) => {
if (wrapperElement instanceof HTMLElement) { if (wrapperElement instanceof HTMLElement) {
@@ -113,6 +119,22 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
}); });
} }
private syncSelectedBooks(): void {
// Only include in 'selectedBooks' the objects that are actually in 'this.books'
// so that p-table correctly shows them as selected in the UI.
this.selectedBooks = this.books.filter(book => this.isBookSelected(book));
}
private getSelectableBookIds(book: Book): number[] {
return book.seriesBooks?.length
? book.seriesBooks.map(seriesBook => seriesBook.id)
: [book.id];
}
private isBookSelected(book: Book): boolean {
return this.getSelectableBookIds(book).every(bookId => this.selectedBookIds.has(bookId));
}
scrollToTop(): void { scrollToTop(): void {
const tableElement = this.elementRef.nativeElement.querySelector('.p-datatable-wrapper'); const tableElement = this.elementRef.nativeElement.querySelector('.p-datatable-wrapper');
if (tableElement instanceof HTMLElement) { if (tableElement instanceof HTMLElement) {
@@ -121,9 +143,9 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
} }
selectAllBooks(): void { selectAllBooks(): void {
this.selectedBookIds = new Set(this.books.map(book => book.id)); // We delegate the "Select All" logic to the parent component
this.selectedBooks = [...this.books]; // which can fetch all book IDs from the API.
this.selectedBooksChange.emit(this.selectedBookIds); this.selectAllRequested.emit();
} }
clearSelectedBooks(): void { clearSelectedBooks(): void {
@@ -134,26 +156,24 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
onRowSelect(event: { data?: Book | Book[] }): void { onRowSelect(event: { data?: Book | Book[] }): void {
if (event.data && !Array.isArray(event.data)) { if (event.data && !Array.isArray(event.data)) {
this.selectedBookIds.add(event.data.id); this.getSelectableBookIds(event.data).forEach(bookId => this.selectedBookIds.add(bookId));
this.selectedBooksChange.emit(this.selectedBookIds); this.selectedBooksChange.emit(new Set(this.selectedBookIds));
} }
} }
onRowUnselect(event: { data?: Book | Book[] }): void { onRowUnselect(event: { data?: Book | Book[] }): void {
if (event.data && !Array.isArray(event.data)) { if (event.data && !Array.isArray(event.data)) {
this.selectedBookIds.delete(event.data.id); this.getSelectableBookIds(event.data).forEach(bookId => this.selectedBookIds.delete(bookId));
this.selectedBooksChange.emit(this.selectedBookIds); this.selectedBooksChange.emit(new Set(this.selectedBookIds));
} }
} }
onHeaderCheckboxToggle(event: { checked: boolean }): void { onHeaderCheckboxToggle(event: { checked: boolean }): void {
if (event.checked) { if (event.checked) {
this.selectedBooks = [...this.books]; this.selectAllRequested.emit();
this.selectedBookIds = new Set(this.books.map(book => book.id));
} else { } else {
this.clearSelectedBooks(); this.clearSelectedBooks();
} }
this.selectedBooksChange.emit(this.selectedBookIds);
} }
getStarColor(rating: number): string { getStarColor(rating: number): string {
@@ -288,7 +288,7 @@
[index]="i" [index]="i"
[book]="book" [book]="book"
[isCheckboxEnabled]="true" [isCheckboxEnabled]="true"
[isSelected]="selectedBooks.has(book.id)" [isSelected]="isBookSelected(book)"
(checkboxClick)="onCheckboxClicked($event)" (checkboxClick)="onCheckboxClicked($event)"
[onBookSelect]="handleBookSelect.bind(this)" [onBookSelect]="handleBookSelect.bind(this)"
[isSeriesCollapsed]="false" [isSeriesCollapsed]="false"
@@ -519,21 +519,22 @@ export class SeriesPageComponent implements AfterViewChecked {
return Math.round((rating / 5) * 100); return Math.round((rating / 5) * 100);
} }
private getSelectableBookIds(book: Book): number[] {
return book.seriesBooks?.length
? book.seriesBooks.map(seriesBook => seriesBook.id)
: [book.id];
}
isBookSelected(book: Book): boolean {
return this.getSelectableBookIds(book).every(id => this.selectedBooks.has(id));
}
handleBookSelection(book: Book, selected: boolean) { handleBookSelection(book: Book, selected: boolean) {
const ids = this.getSelectableBookIds(book);
if (selected) { if (selected) {
if (book.seriesBooks) { ids.forEach(id => this.selectedBooks.add(id));
this.selectedBooks = new Set([...this.selectedBooks, ...book.seriesBooks.map(book => book.id)]);
} else {
this.selectedBooks.add(book.id);
}
} else { } else {
if (book.seriesBooks) { ids.forEach(id => this.selectedBooks.delete(id));
book.seriesBooks.forEach(book => {
this.selectedBooks.delete(book.id);
});
} else {
this.selectedBooks.delete(book.id);
}
} }
} }
@@ -563,7 +564,7 @@ export class SeriesPageComponent implements AfterViewChecked {
selectAllBooks(): void { selectAllBooks(): void {
for (const book of this.filteredBooks()) { for (const book of this.filteredBooks()) {
this.selectedBooks.add(book.id); this.getSelectableBookIds(book).forEach(id => this.selectedBooks.add(id));
} }
this.moreActionsMenuItems = this.bookMenuService.getMoreActionsMenu(this.selectedBooks, this.user()); this.moreActionsMenuItems = this.bookMenuService.getMoreActionsMenu(this.selectedBooks, this.user());
} }