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"
[preselectedBookIds]="selectedBooks()"
(selectedBooksChange)="onSelectedBooksChange($event)"
(selectAllRequested)="selectAllBooks()"
[sortOption]="this.bookSorter.selectedSort!"
[visibleColumns]="visibleColumns()"
></app-book-table>
@@ -334,7 +335,7 @@
[onBookSelect]="onBookCardSelect"
[isSeriesCollapsed]="seriesCollapseFilter.seriesCollapsed()"
(checkboxClick)="onCheckboxClicked($event)"
[isSelected]="selectedBooks().has(book.id)"
[isSelected]="bookSelectionService.isBookSelected(book)"
[useSquareCovers]="isAudiobookOnlyLibrary()"
[overlayPreferenceService]="bookCardOverlayPreferenceService">
</app-book-card>
@@ -851,15 +851,9 @@ export class BookBrowserComponent implements AfterViewInit {
this.appBooksApi.fetchAllBookIds().pipe(take(1)).subscribe({
next: (allIds) => {
this.bookSelectionService.selectAll(allIds);
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
},
error: () => {
this.bookSelectionService.selectAll();
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
}
});
}
@@ -21,14 +21,22 @@ export class BookSelectionService {
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 {
this._selectedBooks.update(current => {
const next = new Set(current);
if (book.seriesBooks) {
book.seriesBooks.forEach(seriesBook => next.add(seriesBook.id));
} else {
next.add(book.id);
}
this.getSelectableBookIds(book).forEach(bookId => next.add(bookId));
return next;
});
}
@@ -36,11 +44,7 @@ export class BookSelectionService {
deselectBook(book: Book): void {
this._selectedBooks.update(current => {
const next = new Set(current);
if (book.seriesBooks) {
book.seriesBooks.forEach(seriesBook => next.delete(seriesBook.id));
} else {
next.delete(book.id);
}
this.getSelectableBookIds(book).forEach(bookId => next.delete(bookId));
return next;
});
}
@@ -83,7 +87,7 @@ export class BookSelectionService {
this._selectedBooks.update(current => {
const next = new Set(current);
for (const book of this.currentBooks) {
next.add(book.id);
this.getSelectableBookIds(book).forEach(bookId => next.add(bookId));
}
return next;
});
@@ -40,6 +40,7 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
selectedBookIds = new Set<number>();
@Output() selectedBooksChange = new EventEmitter<Set<number>>();
@Output() selectAllRequested = new EventEmitter<void>();
@Input() books: Book[] = [];
@Input() sortOption: SortOption | null = null;
@Input() visibleColumns: { field: string; header: string }[] = [];
@@ -91,8 +92,8 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
this.metadataCenterViewMode = user.userSettings.metadataCenterViewMode ?? 'route';
}
this.selectedBookIds = this.preselectedBookIds;
this.selectedBooks = this.bookService.getBooksByIds([...this.selectedBookIds]);
this.selectedBookIds = new Set(this.preselectedBookIds);
this.syncSelectedBooks();
this.setScrollHeight();
window.addEventListener('resize', this.resizeListener);
}
@@ -104,7 +105,12 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
: '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');
wrapperElements.forEach((wrapperElement: Element) => {
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 {
const tableElement = this.elementRef.nativeElement.querySelector('.p-datatable-wrapper');
if (tableElement instanceof HTMLElement) {
@@ -121,9 +143,9 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
}
selectAllBooks(): void {
this.selectedBookIds = new Set(this.books.map(book => book.id));
this.selectedBooks = [...this.books];
this.selectedBooksChange.emit(this.selectedBookIds);
// We delegate the "Select All" logic to the parent component
// which can fetch all book IDs from the API.
this.selectAllRequested.emit();
}
clearSelectedBooks(): void {
@@ -134,26 +156,24 @@ export class BookTableComponent implements OnInit, OnDestroy, OnChanges {
onRowSelect(event: { data?: Book | Book[] }): void {
if (event.data && !Array.isArray(event.data)) {
this.selectedBookIds.add(event.data.id);
this.selectedBooksChange.emit(this.selectedBookIds);
this.getSelectableBookIds(event.data).forEach(bookId => this.selectedBookIds.add(bookId));
this.selectedBooksChange.emit(new Set(this.selectedBookIds));
}
}
onRowUnselect(event: { data?: Book | Book[] }): void {
if (event.data && !Array.isArray(event.data)) {
this.selectedBookIds.delete(event.data.id);
this.selectedBooksChange.emit(this.selectedBookIds);
this.getSelectableBookIds(event.data).forEach(bookId => this.selectedBookIds.delete(bookId));
this.selectedBooksChange.emit(new Set(this.selectedBookIds));
}
}
onHeaderCheckboxToggle(event: { checked: boolean }): void {
if (event.checked) {
this.selectedBooks = [...this.books];
this.selectedBookIds = new Set(this.books.map(book => book.id));
this.selectAllRequested.emit();
} else {
this.clearSelectedBooks();
}
this.selectedBooksChange.emit(this.selectedBookIds);
}
getStarColor(rating: number): string {
@@ -288,7 +288,7 @@
[index]="i"
[book]="book"
[isCheckboxEnabled]="true"
[isSelected]="selectedBooks.has(book.id)"
[isSelected]="isBookSelected(book)"
(checkboxClick)="onCheckboxClicked($event)"
[onBookSelect]="handleBookSelect.bind(this)"
[isSeriesCollapsed]="false"
@@ -519,21 +519,22 @@ export class SeriesPageComponent implements AfterViewChecked {
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) {
const ids = this.getSelectableBookIds(book);
if (selected) {
if (book.seriesBooks) {
this.selectedBooks = new Set([...this.selectedBooks, ...book.seriesBooks.map(book => book.id)]);
} else {
this.selectedBooks.add(book.id);
}
ids.forEach(id => this.selectedBooks.add(id));
} else {
if (book.seriesBooks) {
book.seriesBooks.forEach(book => {
this.selectedBooks.delete(book.id);
});
} else {
this.selectedBooks.delete(book.id);
}
ids.forEach(id => this.selectedBooks.delete(id));
}
}
@@ -563,7 +564,7 @@ export class SeriesPageComponent implements AfterViewChecked {
selectAllBooks(): void {
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());
}