feat(book-browser): enable shift-click multi-selection and "Select All" support

- Implemented shift-click selection in grid view to allow selecting a range of books
- Tracked last selected index to determine selection range when shift key is pressed
- Maintained selected book IDs in a Set for fast lookup and deduplication
- Added a "Select All" button in the book browser footer to select all filtered books
- Synced selection state with PrimeNG checkbox behavior
- Ensured selection works correctly with filtered, sorted, or paginated results
This commit is contained in:
aditya.chandel
2025-06-21 16:11:38 -06:00
committed by Aditya Chandel
parent 0e8d1e6124
commit 6f032153b8
5 changed files with 83 additions and 12 deletions
@@ -145,6 +145,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
filterVisibility = true;
private settingFiltersFromUrl = false;
protected metadataMenuItems: MenuItem[] | undefined;
currentBooks: Book[] = [];
lastSelectedIndex: number | null = null;
get currentCardSize() {
return this.coverScalePreferenceService.currentCardSize;
@@ -478,6 +480,32 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
);
}
onCheckboxClicked(event: { index: number; bookId: number; selected: boolean; shiftKey: boolean }) {
const {index, bookId, selected, shiftKey} = event;
if (!shiftKey || this.lastSelectedIndex === null) {
if (selected) {
this.selectedBooks.add(bookId);
} else {
this.selectedBooks.delete(bookId);
}
this.lastSelectedIndex = index;
} else {
const start = Math.min(this.lastSelectedIndex, index);
const end = Math.max(this.lastSelectedIndex, index);
const isUnselectingRange = !selected;
for (let i = start; i <= end; i++) {
const book = this.currentBooks[i];
if (!book) continue;
if (isUnselectingRange) {
this.selectedBooks.delete(book.id);
} else {
this.selectedBooks.add(book.id);
}
}
}
}
handleBookSelect(bookId: number, selected: boolean): void {
if (selected) {
this.selectedBooks.add(bookId);
@@ -492,6 +520,16 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.isDrawerVisible = this.selectedBooks.size > 0;
}
selectAllBooks(): void {
if (!this.currentBooks) return;
for (const book of this.currentBooks) {
this.selectedBooks.add(book.id);
}
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
}
deselectAllBooks(): void {
this.selectedBooks.clear();
this.isDrawerVisible = false;
@@ -515,6 +553,14 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
switchMap(({entityId, entityType}) => this.fetchBooksByEntity(entityId, entityType))
);
}
this.bookState$
.pipe(
filter(state => state.loaded && !state.error),
map(state => state.books || [])
)
.subscribe(books => {
this.currentBooks = books;
});
}
onSearchTermChange(term: string): void {