]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/rest/rest-table.ts
Put admin actions on the left
[github/Chocobozzz/PeerTube.git] / client / src / app / core / rest / rest-table.ts
CommitLineData
4504f09f 1import { peertubeLocalStorage } from '@root-helpers/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'
cfde28ba
C
6import * as debug from 'debug'
7
8const logger = debug('peertube:tables:RestTable')
d592e0a9
C
9
10export abstract class RestTable {
ab998f7b 11
d592e0a9 12 abstract totalRecords: number
d592e0a9
C
13 abstract sort: SortMeta
14 abstract pagination: RestPagination
15
042daa70 16 search: string
0251197e
RK
17 rowsPerPageOptions = [ 10, 20, 50, 100 ]
18 rowsPerPage = this.rowsPerPageOptions[0]
d4051183 19 expandedRows = {}
0251197e 20
cfde28ba 21 protected searchStream: Subject<string>
8e11a1b3
C
22
23 abstract getIdentifier (): string
ab998f7b 24
24b9417c
C
25 initialize () {
26 this.loadSort()
27 this.initSearch()
28 }
29
ab998f7b 30 loadSort () {
8e11a1b3 31 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
ab998f7b
C
32
33 if (result) {
34 try {
35 this.sort = JSON.parse(result)
36 } catch (err) {
8e11a1b3 37 console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
ab998f7b
C
38 }
39 }
40 }
41
d592e0a9 42 loadLazy (event: LazyLoadEvent) {
cfde28ba
C
43 logger('Load lazy %o.', event)
44
d592e0a9
C
45 this.sort = {
46 order: event.sortOrder,
47 field: event.sortField
48 }
49
50 this.pagination = {
51 start: event.first,
52 count: this.rowsPerPage
53 }
54
55 this.loadData()
ab998f7b
C
56 this.saveSort()
57 }
58
59 saveSort () {
8e11a1b3 60 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
d592e0a9
C
61 }
62
24b9417c
C
63 initSearch () {
64 this.searchStream = new Subject()
65
66 this.searchStream
67 .pipe(
68 debounceTime(400),
69 distinctUntilChanged()
70 )
71 .subscribe(search => {
72 this.search = search
cfde28ba
C
73
74 logger('On search %s.', this.search)
75
24b9417c
C
76 this.loadData()
77 })
78 }
79
be27ef3b
C
80 onSearch (event: Event) {
81 const target = event.target as HTMLInputElement
82 this.searchStream.next(target.value)
24b9417c 83 }
dffd5d12 84
25a42e29 85 onPage (event: { first: number, rows: number }) {
cfde28ba
C
86 logger('On page %o.', event)
87
25a42e29
RK
88 if (this.rowsPerPage !== event.rows) {
89 this.rowsPerPage = event.rows
90 this.pagination = {
91 start: event.first,
92 count: this.rowsPerPage
93 }
cfde28ba 94
25a42e29
RK
95 this.loadData()
96 }
cfde28ba 97
d4051183
RK
98 this.expandedRows = {}
99 }
100
25a42e29
RK
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
105 }
106
107 resetSearch () {
108 this.searchStream.next('')
109 this.setTableFilter('')
110 }
111
dffd5d12 112 protected abstract loadData (): void
8e11a1b3
C
113
114 private getSortLocalStorageKey () {
115 return 'rest-table-sort-' + this.getIdentifier()
116 }
d592e0a9 117}