1 import debug from 'debug'
2 import { SortMeta } from 'primeng/api'
3 import { TableLazyLoadEvent } from 'primeng/table'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { logger } from '@root-helpers/logger'
6 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
7 import { RestPagination } from './rest-pagination'
9 const debugLogger = debug('peertube:tables:RestTable')
11 export abstract class RestTable <T = unknown> {
13 abstract totalRecords: number
14 abstract sort: SortMeta
15 abstract pagination: RestPagination
17 rowsPerPageOptions = [ 10, 20, 50, 100 ]
18 rowsPerPage = this.rowsPerPageOptions[0]
21 selectedRows: T[] = []
25 protected route: ActivatedRoute
26 protected router: Router
28 abstract getIdentifier (): string
35 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
39 this.sort = JSON.parse(result)
41 logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
47 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
50 loadLazy (event: TableLazyLoadEvent) {
51 debugLogger('Load lazy %o.', event)
54 order: event.sortOrder,
55 field: event.sortField as string
58 this.rowsPerPage = event.rows
62 count: this.rowsPerPage
65 this.expandedRows = {}
71 onSearch (search: string) {
74 count: this.rowsPerPage
81 isInSelectionMode () {
82 return this.selectedRows.length !== 0
85 protected abstract reloadDataInternal (): void
87 protected reloadData () {
88 this.selectedRows = []
90 this.reloadDataInternal()
93 private getSortLocalStorageKey () {
94 return 'rest-table-sort-' + this.getIdentifier()