-import { peertubeLocalStorage } from '@app/helpers/peertube-web-storage'
+import debug from 'debug'
import { LazyLoadEvent, SortMeta } from 'primeng/api'
+import { ActivatedRoute, Router } from '@angular/router'
+import { logger } from '@root-helpers/logger'
+import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
import { RestPagination } from './rest-pagination'
-import { Subject } from 'rxjs'
-import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
-import * as debug from 'debug'
-const logger = debug('peertube:tables:RestTable')
+const debugLogger = debug('peertube:tables:RestTable')
-export abstract class RestTable {
+export abstract class RestTable <T = unknown> {
abstract totalRecords: number
abstract sort: SortMeta
abstract pagination: RestPagination
- search: string
rowsPerPageOptions = [ 10, 20, 50, 100 ]
rowsPerPage = this.rowsPerPageOptions[0]
expandedRows = {}
- protected searchStream: Subject<string>
+ selectedRows: T[] = []
+
+ search: string
+
+ protected route: ActivatedRoute
+ protected router: Router
abstract getIdentifier (): string
initialize () {
this.loadSort()
- this.initSearch()
}
loadSort () {
try {
this.sort = JSON.parse(result)
} catch (err) {
- console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
+ logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
}
}
}
+ saveSort () {
+ peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
+ }
+
loadLazy (event: LazyLoadEvent) {
- logger('Load lazy %o.', event)
+ debugLogger('Load lazy %o.', event)
this.sort = {
order: event.sortOrder,
field: event.sortField
}
+ this.rowsPerPage = event.rows
+
this.pagination = {
start: event.first,
count: this.rowsPerPage
}
- this.loadData()
- this.saveSort()
- }
+ this.expandedRows = {}
- saveSort () {
- peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
+ this.reloadData()
+ this.saveSort()
}
- initSearch () {
- this.searchStream = new Subject()
-
- this.searchStream
- .pipe(
- debounceTime(400),
- distinctUntilChanged()
- )
- .subscribe(search => {
- this.search = search
-
- logger('On search %s.', this.search)
+ onSearch (search: string) {
+ this.pagination = {
+ start: 0,
+ count: this.rowsPerPage
+ }
- this.loadData()
- })
+ this.search = search
+ this.reloadData()
}
- onSearch (event: Event) {
- const target = event.target as HTMLInputElement
- this.searchStream.next(target.value)
+ isInSelectionMode () {
+ return this.selectedRows.length !== 0
}
- onPage (event: { first: number, rows: number }) {
- logger('On page %o.', event)
-
- if (this.rowsPerPage !== event.rows) {
- this.rowsPerPage = event.rows
- this.pagination = {
- start: event.first,
- count: this.rowsPerPage
- }
-
- this.loadData()
- }
+ protected abstract reloadDataInternal (): void
- this.expandedRows = {}
- }
+ protected reloadData () {
+ this.selectedRows = []
- setTableFilter (filter: string) {
- // FIXME: cannot use ViewChild, so create a component for the filter input
- const filterInput = document.getElementById('table-filter') as HTMLInputElement
- if (filterInput) filterInput.value = filter
+ this.reloadDataInternal()
}
- resetSearch () {
- this.searchStream.next('')
- this.setTableFilter('')
- }
-
- protected abstract loadData (): void
-
private getSortLocalStorageKey () {
return 'rest-table-sort-' + this.getIdentifier()
}