1 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
2 import { LazyLoadEvent, SortMeta } from 'primeng/api'
3 import { RestPagination } from './rest-pagination'
4 import { Subject } from 'rxjs'
5 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
6 import * as debug from 'debug'
8 const logger = debug('peertube:tables:RestTable')
10 export abstract class RestTable {
12 abstract totalRecords: number
13 abstract sort: SortMeta
14 abstract pagination: RestPagination
17 rowsPerPageOptions = [ 10, 20, 50, 100 ]
18 rowsPerPage = this.rowsPerPageOptions[0]
21 protected searchStream: Subject<string>
23 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))
64 this.searchStream = new Subject()
69 distinctUntilChanged()
71 .subscribe(search => {
74 logger('On search %s.', this.search)
80 onSearch (event: Event) {
81 const target = event.target as HTMLInputElement
82 this.searchStream.next(target.value)
85 onPage (event: { first: number, rows: number }) {
86 logger('On page %o.', event)
88 if (this.rowsPerPage !== event.rows) {
89 this.rowsPerPage = event.rows
92 count: this.rowsPerPage
98 this.expandedRows = {}
101 setTableFilter (filter: string) {
102 // FIXME: cannot use ViewChild, so create a component for the filter input
103 const filterInput = document.getElementById('table-filter') as HTMLInputElement
104 if (filterInput) filterInput.value = filter
108 this.searchStream.next('')
109 this.setTableFilter('')
112 protected abstract loadData (): void
114 private getSortLocalStorageKey () {
115 return 'rest-table-sort-' + this.getIdentifier()