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