Implemented load next on scroll for grid view
This commit is contained in:
1 parent
9342fb631b
commit
8e05c669db
2 files changed
+60
-14
No files matched your search
@@ -108,12 +108,12 @@
|
||||
<div class="dataTables_info" role="status" aria-live="polite">
|
||||
Total entries {{entriesCount}}
|
||||
</div>
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1 && entries.length != 0">
|
||||
<div nclass="pagination-container d-flex justify-content-end"
|
||||
*ngIf="viewMode == 'table' && maxPage > 1 && entries.length != 0">
|
||||
<ul class="pagination pagination-sm">
|
||||
<li class="page-item" [class.disabled]="currentPage === 1">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
|
||||
</li>
|
||||
<!-- Dynamically generate page numbers -->
|
||||
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
|
||||
</li>
|
||||
@@ -127,20 +127,20 @@
|
||||
|
||||
|
||||
|
||||
<app-library-table *ngIf="viewMode == 'table'" [entries]="entriesToShow"
|
||||
<app-library-table *ngIf="viewMode == 'table'" [entries]="entriesToDisplay"
|
||||
(onEntryRemove)="entryRemoved($event)"></app-library-table>
|
||||
|
||||
<app-dynamic-entry-list *ngIf="viewMode == 'grid'" [entries]="entriesToShow"
|
||||
<app-dynamic-entry-list *ngIf="viewMode == 'grid'" [entries]="entriesToDisplay"
|
||||
(onEntryRemove)="entryRemoved($event)"></app-dynamic-entry-list>
|
||||
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="pagination-container d-flex justify-content-end" *ngIf="maxPage > 1 && entries.length != 0">
|
||||
<div class="pagination-container d-flex justify-content-end"
|
||||
*ngIf="viewMode == 'table' && maxPage > 1 && entries.length != 0">
|
||||
<ul class="pagination pagination-sm">
|
||||
<li class="page-item" [class.disabled]="currentPage === 1">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
|
||||
</li>
|
||||
<!-- Dynamically generate page numbers -->
|
||||
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
|
||||
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
|
||||
</li>
|
||||
|
||||
@@ -5,6 +5,8 @@ import { ActivatedRoute, Event, Router } from '@angular/router';
|
||||
import { LibraryQueryParams } from '../../../shared/models/library-query-params';
|
||||
import { SettingsService } from '../../../core/services/settings.service';
|
||||
import { Settings } from '../../../shared/models/settings.model';
|
||||
import { ScrollService } from '../../../core/services/scroll.service';
|
||||
import { Subscription } from 'rxjs';
|
||||
|
||||
declare var $: any;
|
||||
@Component({
|
||||
@@ -14,7 +16,8 @@ declare var $: any;
|
||||
})
|
||||
export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
entries: Entry[] = [];
|
||||
entriesToShow: Entry[] = [];
|
||||
entriesList: Entry[] = [];
|
||||
entriesToDisplay: Entry[] = [];
|
||||
|
||||
// ? pagination
|
||||
entriesCount: number = 0;
|
||||
@@ -76,17 +79,20 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
matchAll: true,
|
||||
};
|
||||
|
||||
private scrollSubscription!: Subscription;
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private route: ActivatedRoute,
|
||||
private libraryService: LibraryService,
|
||||
private settingsService: SettingsService
|
||||
private settingsService: SettingsService,
|
||||
private scrollService: ScrollService
|
||||
) {}
|
||||
|
||||
async ngOnInit() {
|
||||
this.getDefaultViewMode();
|
||||
|
||||
this.pageSize = await this.getDefaultPageSize();
|
||||
this.pageSize = await this.getDefaultTableSize();
|
||||
await this.getEntries(true);
|
||||
// this.router.events.subscribe((event) => {
|
||||
// if (event instanceof NavigationEnd && event.url === '/library') {
|
||||
@@ -94,6 +100,12 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
// this.getEntries(true);
|
||||
// }
|
||||
// });
|
||||
|
||||
this.scrollSubscription = this.scrollService.scrollEnd$.subscribe(() => {
|
||||
if (this.viewMode == 'grid') {
|
||||
this.loadNext();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
@@ -119,6 +131,13 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
// $('#genre-select').val(this.filterCriteria.genres)
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
// Unsubscribe to avoid memory leaks
|
||||
if (this.scrollSubscription) {
|
||||
this.scrollSubscription.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
// ? uses libraryService to get library all entries
|
||||
getEntries(forceFetch: boolean = false) {
|
||||
this.libraryService.newGetAllEntries().subscribe({
|
||||
@@ -325,18 +344,40 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
|
||||
// console.log(temp);
|
||||
// ? sort filter
|
||||
temp = await this.sortEntries(temp);
|
||||
this.entriesList = await this.sortEntries(temp);
|
||||
|
||||
this.entriesCount = temp.length;
|
||||
this.entriesCount = this.entriesList.length;
|
||||
this.maxPage = Math.ceil(this.entriesCount / this.pageSize);
|
||||
if (this.currentPage > this.maxPage) this.currentPage = this.maxPage; // fix for when 1 entry in the last page is removed
|
||||
|
||||
this.entriesToDisplay = [];
|
||||
|
||||
if (this.viewMode == 'table') {
|
||||
this.getPageContent();
|
||||
} else if (this.viewMode == 'grid') {
|
||||
this.loadNext();
|
||||
}
|
||||
}
|
||||
// + FILTER END
|
||||
|
||||
private loadNext(): void {
|
||||
const nextItems = this.entriesList.slice(
|
||||
this.entriesToDisplay.length,
|
||||
Math.min(this.entriesToDisplay.length + 30, this.entriesList.length)
|
||||
);
|
||||
if (nextItems.length > 0) {
|
||||
this.entriesToDisplay = [...this.entriesToDisplay, ...nextItems];
|
||||
}
|
||||
}
|
||||
|
||||
private getPageContent(): void {
|
||||
const startIndex = (this.currentPage - 1) * this.pageSize;
|
||||
const endIndex = startIndex + this.pageSize;
|
||||
|
||||
this.entriesToShow = temp.slice(startIndex, endIndex);
|
||||
console.log(this.pageSize, startIndex, endIndex);
|
||||
|
||||
this.entriesToDisplay = this.entriesList.slice(startIndex, endIndex);
|
||||
}
|
||||
// + FILTER END
|
||||
|
||||
// ! sorting
|
||||
sortEntries(entries: Entry[]) {
|
||||
@@ -438,9 +479,14 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
return 54;
|
||||
}
|
||||
|
||||
getDefaultTableSize() {
|
||||
// TODO get default page size from settings service
|
||||
return 10;
|
||||
}
|
||||
|
||||
goToPage(page: number) {
|
||||
this.currentPage = page;
|
||||
this.filterEntries();
|
||||
this.getPageContent();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user