import { Component, OnInit, Input } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { PageEvent } from '@angular/material/paginator'; import { BookmarksService } from '../../services/bookmarks.service'; @Component({ selector: 'page-bookmarks', templateUrl: './bookmarks.page.html' }) export class PageBookmarks implements OnInit { @Input() entries: any; boundRefresh: Function; boundUpdate: Function; init: boolean = true; constructor(private bookmarksService: BookmarksService, private router: Router, private route: ActivatedRoute) { } ngOnInit(): void { this.boundRefresh = this.refresh.bind(this); this.boundUpdate = this.update.bind(this); this.route.queryParams.subscribe(params => { if (this.init) { this.entries = {}; if (params[ 'p' ]) { this.entries.number = +params[ 'p' ] - 1; if (this.entries.number < 0) { this.entries.number = 0; } } if (params[ 's' ]) { this.entries.size = +params[ 's' ]; } this.refresh(); this.init = false; } }); } refresh(): void { if (!this.entries) { this.entries = {}; } this.entries.content = null; this.bookmarksService.getEntriesPages(this.entries.number || 0, this.entries.size || 30).subscribe((data: any) => { this.entries = data; this.entries.bookmarks = true; }, (error) => { }) } update(event: PageEvent) { this.entries.content = null; const params: any = { p: null, s: null }; if (event.pageIndex != 0) { params.p = event.pageIndex + 1; } if (event.pageSize != 30) { params.s = event.pageSize; } this.router.navigate( [], { relativeTo: this.route, queryParams: params, queryParamsHandling: 'merge' }); this.bookmarksService.getEntriesPages(event.pageIndex, event.pageSize).subscribe((data: any) => { this.entries = data; }, (error) => { }) } }