WIP: Rearrange code
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<p-table
|
||||
[value]="books"
|
||||
[resizableColumns]="true"
|
||||
showGridlines
|
||||
stripedRows
|
||||
[scrollable]="true"
|
||||
[scrollHeight]="'calc(100vh - 160px)'"
|
||||
[virtualScroll]="true"
|
||||
[virtualScrollItemSize]="46"
|
||||
[(selection)]="selectedBooks"
|
||||
(onHeaderCheckboxToggle)="onHeaderCheckboxToggle($event)"
|
||||
(onRowSelect)="onRowSelect($event)"
|
||||
(onRowUnselect)="onRowUnselect($event)">
|
||||
<ng-template #header>
|
||||
<tr>
|
||||
<th class="max-w-16">Cover</th>
|
||||
<th pResizableColumn>Title</th>
|
||||
<th pResizableColumn>Publisher</th>
|
||||
<th>Published</th>
|
||||
<th>ISBN</th>
|
||||
<th>Language</th>
|
||||
<th>Rating</th>
|
||||
<th>Pages</th>
|
||||
<th>Reviews</th>
|
||||
<th>
|
||||
<p-tableHeaderCheckbox/>
|
||||
</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
<ng-template #body let-book>
|
||||
<tr style="height:46px">
|
||||
<td (click)="openMetadataCenter(book.id)"><img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="size-7"/></td>
|
||||
<td (click)="openMetadataCenter(book.id)" class="w-1/2 max-w-96 overflow-hidden truncate" [title]="book.metadata?.id">{{ book.metadata?.title }}</td>
|
||||
<td class="w-1/6 max-w-40 overflow-hidden truncate">{{ book.metadata?.publisher }}</td>
|
||||
<td class="w-1/12 max-w-30 overflow-hidden truncate">{{ book.metadata?.publishedDate }}</td>
|
||||
<td class="w-1/12 max-w-32 overflow-hidden truncate">{{ book.metadata?.isbn10 }}</td>
|
||||
<td class="w-1/12 max-w-28 overflow-hidden truncate">{{ book.metadata?.language }}</td>
|
||||
<td class="w-1/12 max-w-30 overflow-hidden truncate">
|
||||
<ng-container *ngIf="book.metadata">
|
||||
<p-rating
|
||||
[(ngModel)]="book.metadata.rating"
|
||||
readonly
|
||||
stars="5"
|
||||
[style.--p-rating-icon-active-color]="getStarColor(book.metadata.rating)">
|
||||
</p-rating>
|
||||
</ng-container>
|
||||
</td>
|
||||
<td class="w-1/12 max-w-32 overflow-hidden truncate">{{ book.metadata?.pageCount }}</td>
|
||||
<td class="w-1/12 max-w-32 overflow-hidden truncate">{{ book.metadata?.reviewCount }}</td>
|
||||
<td class="max-w-16">
|
||||
<p-tableCheckbox [value]="book"></p-tableCheckbox>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
@@ -0,0 +1,6 @@
|
||||
/*
|
||||
.p-datatable-flex-scrollable .p-datatable-wrapper {
|
||||
flex-basis: auto !important;
|
||||
height: 0 !important;
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,114 @@
|
||||
import {Component, EventEmitter, Input, OnChanges, Output} from '@angular/core';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {NgIf} from '@angular/common';
|
||||
import {BookService} from '../../../service/book.service';
|
||||
import {Rating} from 'primeng/rating';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {BookMetadataCenterComponent} from '../../../../metadata/book-metadata-center/book-metadata-center.component';
|
||||
import {DialogService} from 'primeng/dynamicdialog';
|
||||
import {Book} from '../../../model/book.model';
|
||||
import {SortOption} from '../../../model/sort.model';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-table',
|
||||
standalone: true,
|
||||
templateUrl: './book-table.component.html',
|
||||
imports: [
|
||||
TableModule,
|
||||
NgIf,
|
||||
Rating,
|
||||
FormsModule
|
||||
],
|
||||
styleUrl: './book-table.component.scss'
|
||||
})
|
||||
export class BookTableComponent implements OnChanges {
|
||||
selectedBooks: Book[] = [];
|
||||
selectedBookIds: Set<number> = new Set();
|
||||
|
||||
@Output() selectedBooksChange = new EventEmitter<Set<number>>();
|
||||
@Input() books: Book[] = [];
|
||||
@Input() sortOption: SortOption | null = null;
|
||||
|
||||
constructor(private bookService: BookService, private dialogService: DialogService) {
|
||||
}
|
||||
|
||||
// Hack to set virtual-scroller height
|
||||
ngOnChanges() {
|
||||
let wrapperElements: HTMLCollection = document.getElementsByClassName('p-virtualscroller');
|
||||
Array.prototype.forEach.call(wrapperElements, function (wrapperElement) {
|
||||
wrapperElement.style["height"] = 'calc(100vh - 160px)';
|
||||
});
|
||||
}
|
||||
|
||||
clearSelectedBooks(): void {
|
||||
this.selectedBookIds.clear();
|
||||
this.selectedBooks = [];
|
||||
this.selectedBooksChange.emit(this.selectedBookIds);
|
||||
}
|
||||
|
||||
onRowSelect(event: any): void {
|
||||
this.selectedBookIds.add(event.data.id);
|
||||
this.selectedBooksChange.emit(this.selectedBookIds);
|
||||
}
|
||||
|
||||
onRowUnselect(event: any): void {
|
||||
this.selectedBookIds.delete(event.data.id);
|
||||
this.selectedBooksChange.emit(this.selectedBookIds);
|
||||
}
|
||||
|
||||
onHeaderCheckboxToggle(event: any): void {
|
||||
if (event.checked) {
|
||||
this.selectedBooks = [...this.books];
|
||||
this.selectedBookIds = new Set(this.books.map(book => book.id));
|
||||
} else {
|
||||
this.clearSelectedBooks();
|
||||
}
|
||||
this.selectedBooksChange.emit(this.selectedBookIds);
|
||||
}
|
||||
|
||||
getBookCoverUrl(bookId: number): string {
|
||||
return this.bookService.getBookCoverUrl(bookId);
|
||||
}
|
||||
|
||||
openMetadataCenter(id: number): void {
|
||||
this.openBookDetailsDialog(id);
|
||||
}
|
||||
|
||||
openBookDetailsDialog(bookId: number): void {
|
||||
this.bookService.getBookByIdFromAPI(bookId, true).subscribe({
|
||||
next: (book) => {
|
||||
this.dialogService.open(BookMetadataCenterComponent, {
|
||||
header: 'Open book details',
|
||||
modal: true,
|
||||
closable: true,
|
||||
width: '1200px',
|
||||
height: '835px',
|
||||
showHeader: false,
|
||||
closeOnEscape: true,
|
||||
dismissableMask: true,
|
||||
data: {
|
||||
book: book
|
||||
}
|
||||
});
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Error fetching book:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
getStarColor(rating: number): string {
|
||||
if (rating >= 4.5) {
|
||||
return 'rgb(34, 197, 94)';
|
||||
} else if (rating >= 4) {
|
||||
return 'rgb(52, 211, 153)';
|
||||
} else if (rating >= 3.5) {
|
||||
return 'rgb(234, 179, 8)';
|
||||
} else if (rating >= 2.5) {
|
||||
return 'rgb(249, 115, 22)';
|
||||
} else {
|
||||
return 'rgb(239, 68, 68)';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user