1 import debug from 'debug'
2 import { LazyLoadEvent, SortMeta } from 'primeng/api'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { logger } from '@root-helpers/logger'
5 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
6 import { RestPagination } from './rest-pagination'
8 const debugLogger = debug('peertube:tables:RestTable')
10 export abstract class RestTable <T = unknown> {
12 abstract totalRecords: number
13 abstract sort: SortMeta
14 abstract pagination: RestPagination
16 rowsPerPageOptions = [ 10, 20, 50, 100 ]
17 rowsPerPage = this.rowsPerPageOptions[0]
20 selectedRows: T[] = []
24 protected route: ActivatedRoute
25 protected router: Router
27 abstract getIdentifier (): string
34 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
38 this.sort = JSON.parse(result)
40 logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
46 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
49 loadLazy (event: LazyLoadEvent) {
50 debugLogger('Load lazy %o.', event)
53 order: event.sortOrder,
54 field: event.sortField
57 this.rowsPerPage = event.rows
61 count: this.rowsPerPage
64 this.expandedRows = {}
70 onSearch (search: string) {
73 count: this.rowsPerPage
80 isInSelectionMode () {
81 return this.selectedRows.length !== 0
84 protected abstract reloadDataInternal (): void
86 protected reloadData () {
87 this.selectedRows = []
89 this.reloadDataInternal()
92 private getSortLocalStorageKey () {
93 return 'rest-table-sort-' + this.getIdentifier()