]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/rest/rest-table.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / rest / rest-table.ts
CommitLineData
88a7f93f 1import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
f77eb73b 2import { LazyLoadEvent, SortMeta } from 'primeng/api'
d592e0a9 3import { RestPagination } from './rest-pagination'
24b9417c
C
4import { Subject } from 'rxjs'
5import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
d592e0a9
C
6
7export abstract class RestTable {
ab998f7b 8
d592e0a9 9 abstract totalRecords: number
d592e0a9
C
10 abstract sort: SortMeta
11 abstract pagination: RestPagination
12
042daa70 13 search: string
0251197e
RK
14 rowsPerPageOptions = [ 10, 20, 50, 100 ]
15 rowsPerPage = this.rowsPerPageOptions[0]
d4051183 16 expandedRows = {}
0251197e 17
24b9417c 18 private searchStream: Subject<string>
8e11a1b3
C
19
20 abstract getIdentifier (): string
ab998f7b 21
24b9417c
C
22 initialize () {
23 this.loadSort()
24 this.initSearch()
25 }
26
ab998f7b 27 loadSort () {
8e11a1b3 28 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
ab998f7b
C
29
30 if (result) {
31 try {
32 this.sort = JSON.parse(result)
33 } catch (err) {
8e11a1b3 34 console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
ab998f7b
C
35 }
36 }
37 }
38
d592e0a9
C
39 loadLazy (event: LazyLoadEvent) {
40 this.sort = {
41 order: event.sortOrder,
42 field: event.sortField
43 }
44
45 this.pagination = {
46 start: event.first,
47 count: this.rowsPerPage
48 }
49
50 this.loadData()
ab998f7b
C
51 this.saveSort()
52 }
53
54 saveSort () {
8e11a1b3 55 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
d592e0a9
C
56 }
57
24b9417c
C
58 initSearch () {
59 this.searchStream = new Subject()
60
61 this.searchStream
62 .pipe(
63 debounceTime(400),
64 distinctUntilChanged()
65 )
66 .subscribe(search => {
67 this.search = search
68 this.loadData()
69 })
70 }
71
be27ef3b
C
72 onSearch (event: Event) {
73 const target = event.target as HTMLInputElement
74 this.searchStream.next(target.value)
24b9417c 75 }
dffd5d12 76
25a42e29
RK
77 onPage (event: { first: number, rows: number }) {
78 if (this.rowsPerPage !== event.rows) {
79 this.rowsPerPage = event.rows
80 this.pagination = {
81 start: event.first,
82 count: this.rowsPerPage
83 }
84 this.loadData()
85 }
d4051183
RK
86 this.expandedRows = {}
87 }
88
25a42e29
RK
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
93 }
94
95 resetSearch () {
96 this.searchStream.next('')
97 this.setTableFilter('')
98 }
99
dffd5d12 100 protected abstract loadData (): void
8e11a1b3
C
101
102 private getSortLocalStorageKey () {
103 return 'rest-table-sort-' + this.getIdentifier()
104 }
d592e0a9 105}