]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/rest/rest-table.ts
Strict templates enabled
[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
C
9 abstract totalRecords: number
10 abstract rowsPerPage: number
11 abstract sort: SortMeta
12 abstract pagination: RestPagination
13
24b9417c
C
14 protected search: string
15 private searchStream: Subject<string>
ab998f7b
C
16 private sortLocalStorageKey = 'rest-table-sort-' + this.constructor.name
17
24b9417c
C
18 initialize () {
19 this.loadSort()
20 this.initSearch()
21 }
22
ab998f7b 23 loadSort () {
0bd78bf3 24 const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey)
ab998f7b
C
25
26 if (result) {
27 try {
28 this.sort = JSON.parse(result)
29 } catch (err) {
30 console.error('Cannot load sort of local storage key ' + this.sortLocalStorageKey, err)
31 }
32 }
33 }
34
d592e0a9
C
35 loadLazy (event: LazyLoadEvent) {
36 this.sort = {
37 order: event.sortOrder,
38 field: event.sortField
39 }
40
41 this.pagination = {
42 start: event.first,
43 count: this.rowsPerPage
44 }
45
46 this.loadData()
ab998f7b
C
47 this.saveSort()
48 }
49
50 saveSort () {
0bd78bf3 51 peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort))
d592e0a9
C
52 }
53
24b9417c
C
54 initSearch () {
55 this.searchStream = new Subject()
56
57 this.searchStream
58 .pipe(
59 debounceTime(400),
60 distinctUntilChanged()
61 )
62 .subscribe(search => {
63 this.search = search
64 this.loadData()
65 })
66 }
67
be27ef3b
C
68 onSearch (event: Event) {
69 const target = event.target as HTMLInputElement
70 this.searchStream.next(target.value)
24b9417c 71 }
dffd5d12
B
72
73 protected abstract loadData (): void
d592e0a9 74}