Implemented load next on scroll for grid view

This commit is contained in:
X4Lo committed 2025-01-27 07:04:48 +01:00
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"> <div class="dataTables_info" role="status" aria-live="polite">
Total entries {{entriesCount}} Total entries {{entriesCount}}
</div> </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"> <ul class="pagination pagination-sm">
<li class="page-item" [class.disabled]="currentPage === 1"> <li class="page-item" [class.disabled]="currentPage === 1">
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a> <a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
</li> </li>
<!-- Dynamically generate page numbers -->
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page"> <li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a> <a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
</li> </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> (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> (onEntryRemove)="entryRemoved($event)"></app-dynamic-entry-list>
<!-- Pagination --> <!-- 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"> <ul class="pagination pagination-sm">
<li class="page-item" [class.disabled]="currentPage === 1"> <li class="page-item" [class.disabled]="currentPage === 1">
<a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a> <a class="page-link" href="javascript:;" (click)="goToPage(currentPage - 1)">Previous</a>
</li> </li>
<!-- Dynamically generate page numbers -->
<li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page"> <li class="page-item" *ngFor="let page of pageNumbers" [class.active]="currentPage === page">
<a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a> <a class="page-link" href="javascript:;" (click)="goToPage(page)">{{ page }}</a>
</li> </li>
@@ -5,6 +5,8 @@ import { ActivatedRoute, Event, Router } from '@angular/router';
import { LibraryQueryParams } from '../../../shared/models/library-query-params'; import { LibraryQueryParams } from '../../../shared/models/library-query-params';
import { SettingsService } from '../../../core/services/settings.service'; import { SettingsService } from '../../../core/services/settings.service';
import { Settings } from '../../../shared/models/settings.model'; import { Settings } from '../../../shared/models/settings.model';
import { ScrollService } from '../../../core/services/scroll.service';
import { Subscription } from 'rxjs';
declare var $: any; declare var $: any;
@Component({ @Component({
@@ -14,7 +16,8 @@ declare var $: any;
}) })
export class LibraryPageComponent implements OnInit, AfterViewInit { export class LibraryPageComponent implements OnInit, AfterViewInit {
entries: Entry[] = []; entries: Entry[] = [];
entriesToShow: Entry[] = []; entriesList: Entry[] = [];
entriesToDisplay: Entry[] = [];
// ? pagination // ? pagination
entriesCount: number = 0; entriesCount: number = 0;
@@ -76,17 +79,20 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
matchAll: true, matchAll: true,
}; };
private scrollSubscription!: Subscription;
constructor( constructor(
private router: Router, private router: Router,
private route: ActivatedRoute, private route: ActivatedRoute,
private libraryService: LibraryService, private libraryService: LibraryService,
private settingsService: SettingsService private settingsService: SettingsService,
private scrollService: ScrollService
) {} ) {}
async ngOnInit() { async ngOnInit() {
this.getDefaultViewMode(); this.getDefaultViewMode();
this.pageSize = await this.getDefaultPageSize(); this.pageSize = await this.getDefaultTableSize();
await this.getEntries(true); await this.getEntries(true);
// this.router.events.subscribe((event) => { // this.router.events.subscribe((event) => {
// if (event instanceof NavigationEnd && event.url === '/library') { // if (event instanceof NavigationEnd && event.url === '/library') {
@@ -94,6 +100,12 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
// this.getEntries(true); // this.getEntries(true);
// } // }
// }); // });
this.scrollSubscription = this.scrollService.scrollEnd$.subscribe(() => {
if (this.viewMode == 'grid') {
this.loadNext();
}
});
} }
ngAfterViewInit() { ngAfterViewInit() {
@@ -119,6 +131,13 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
// $('#genre-select').val(this.filterCriteria.genres) // $('#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 // ? uses libraryService to get library all entries
getEntries(forceFetch: boolean = false) { getEntries(forceFetch: boolean = false) {
this.libraryService.newGetAllEntries().subscribe({ this.libraryService.newGetAllEntries().subscribe({
@@ -325,18 +344,40 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
// console.log(temp); // console.log(temp);
// ? sort filter // ? 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); 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 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 startIndex = (this.currentPage - 1) * this.pageSize;
const endIndex = startIndex + 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 // ! sorting
sortEntries(entries: Entry[]) { sortEntries(entries: Entry[]) {
@@ -438,9 +479,14 @@ export class LibraryPageComponent implements OnInit, AfterViewInit {
return 54; return 54;
} }
getDefaultTableSize() {
// TODO get default page size from settings service
return 10;
}
goToPage(page: number) { goToPage(page: number) {
this.currentPage = page; this.currentPage = page;
this.filterEntries(); this.getPageContent();
this.setQueryParameters(); // * update url query this.setQueryParameters(); // * update url query
} }