132 lines
3.4 KiB
TypeScript
132 lines
3.4 KiB
TypeScript
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { PageEvent } from '@angular/material/paginator';
|
|
|
|
import { SettingsService } from '../../services/settings.service';
|
|
import { Subscription } from 'rxjs';
|
|
|
|
@Component({
|
|
selector: 'page-entries',
|
|
templateUrl: './entries.page.html'
|
|
})
|
|
export class PageEntries implements OnInit, OnDestroy {
|
|
|
|
@Input() fetch: Function;
|
|
@Input() filter: boolean = true;
|
|
@Input() text: boolean = false;
|
|
@Input() gravityFilter: boolean = false;
|
|
entries: any;
|
|
asc: boolean = false;
|
|
boundRefresh: Function;
|
|
boundUpdate: Function;
|
|
init: boolean = true;
|
|
filterOpen: boolean = false;
|
|
|
|
settings: any;
|
|
settingsSubscription: Subscription;
|
|
|
|
constructor(
|
|
private settingsService: SettingsService, private router: Router, private route: ActivatedRoute) { }
|
|
|
|
ngOnInit(): void {
|
|
this.boundRefresh = this.refresh.bind(this);
|
|
this.boundUpdate = this.update.bind(this);
|
|
this.route.queryParams.subscribe({
|
|
next: (params) => {
|
|
if (this.init) {
|
|
this.entries = { filter: {} };
|
|
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'];
|
|
}
|
|
|
|
if (params['asc']) {
|
|
this.asc = true;
|
|
}
|
|
|
|
for (const param in params) {
|
|
if (param != 's' && param != 'p' && param != 'asc') {
|
|
this.entries.filter[param] = params[param];
|
|
}
|
|
}
|
|
|
|
this.settingsSubscription = this.settingsService.settings.subscribe({
|
|
next: (settings) => {
|
|
this.settings = settings;
|
|
this.refresh();
|
|
this.init = false;
|
|
}
|
|
})
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.settingsSubscription.unsubscribe();
|
|
}
|
|
|
|
refresh(): void {
|
|
if (!this.entries) {
|
|
this.entries = {};
|
|
}
|
|
|
|
const filter = JSON.parse(JSON.stringify(this.entries.filter || {}))
|
|
this.entries.content = null;
|
|
this.fetch(this.entries.number || 0, this.entries.size || this.settings.pageSize, this.asc, this.entries.filter).subscribe({
|
|
next: (data: any) => {
|
|
this.entries = data;
|
|
this.entries.filter = filter;
|
|
},
|
|
error: (error) => {
|
|
this.entries = { error: 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 != this.settings.pageSize) {
|
|
params.s = event.pageSize;
|
|
}
|
|
|
|
if (this.entries.filter) {
|
|
for (const param in this.entries.filter) {
|
|
params[param] = this.entries.filter[param];
|
|
}
|
|
}
|
|
|
|
this.router.navigate(
|
|
[],
|
|
{
|
|
relativeTo: this.route,
|
|
queryParams: params,
|
|
queryParamsHandling: 'merge'
|
|
});
|
|
|
|
const filter = JSON.parse(JSON.stringify(this.entries.filter || {}))
|
|
this.fetch(event.pageIndex, event.pageSize, this.asc, this.entries.filter).subscribe({
|
|
next: (data: any) => {
|
|
this.entries = data;
|
|
this.entries.filter = filter;
|
|
}, error: (error) => {
|
|
this.entries = { error: error };
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|