1 import { peertubeLocalStorage } from '@app/shared/misc/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'
7 export abstract class RestTable {
9 abstract totalRecords: number
10 abstract sort: SortMeta
11 abstract pagination: RestPagination
14 rowsPerPageOptions = [ 10, 20, 50, 100 ]
15 rowsPerPage = this.rowsPerPageOptions[0]
18 private searchStream: Subject<string>
20 abstract getIdentifier (): string
28 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
32 this.sort = JSON.parse(result)
34 console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
39 loadLazy (event: LazyLoadEvent) {
41 order: event.sortOrder,
42 field: event.sortField
47 count: this.rowsPerPage
55 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
59 this.searchStream = new Subject()
64 distinctUntilChanged()
66 .subscribe(search => {
72 onSearch (event: Event) {
73 const target = event.target as HTMLInputElement
74 this.searchStream.next(target.value)
77 onPage (event: { first: number, rows: number }) {
78 if (this.rowsPerPage !== event.rows) {
79 this.rowsPerPage = event.rows
82 count: this.rowsPerPage
86 this.expandedRows = {}
89 setTableFilter (filter: string) {
90 // FIXME: cannot use ViewChild, so create a component for the filter input
91 const filterInput = document.getElementById('table-filter') as HTMLInputElement
92 if (filterInput) filterInput.value = filter
96 this.searchStream.next('')
97 this.setTableFilter('')
100 protected abstract loadData (): void
102 private getSortLocalStorageKey () {
103 return 'rest-table-sort-' + this.getIdentifier()