1 import * as debug from 'debug'
2 import { LazyLoadEvent, SortMeta } from 'primeng/api'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
5 import { RestPagination } from './rest-pagination'
7 const logger = debug('peertube:tables:RestTable')
9 export abstract class RestTable {
11 abstract totalRecords: number
12 abstract sort: SortMeta
13 abstract pagination: RestPagination
15 rowsPerPageOptions = [ 10, 20, 50, 100 ]
16 rowsPerPage = this.rowsPerPageOptions[0]
21 protected route: ActivatedRoute
22 protected router: Router
24 abstract getIdentifier (): string
31 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
35 this.sort = JSON.parse(result)
37 console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
42 loadLazy (event: LazyLoadEvent) {
43 logger('Load lazy %o.', event)
46 order: event.sortOrder,
47 field: event.sortField
52 count: this.rowsPerPage
60 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
63 onPage (event: { first: number, rows: number }) {
64 logger('On page %o.', event)
66 if (this.rowsPerPage !== event.rows) {
67 this.rowsPerPage = event.rows
70 count: this.rowsPerPage
76 this.expandedRows = {}
79 onSearch (search: string) {
84 protected abstract reloadData (): void
86 private getSortLocalStorageKey () {
87 return 'rest-table-sort-' + this.getIdentifier()