]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
2 import { LazyLoadEvent, SortMeta } from 'primeng/api'
3 import { RestPagination } from './rest-pagination'
4 import { Subject } from 'rxjs'
5 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
6
7 export abstract class RestTable {
8
9 abstract totalRecords: number
10 abstract rowsPerPage: number
11 abstract sort: SortMeta
12 abstract pagination: RestPagination
13
14 protected search: string
15 private searchStream: Subject<string>
16
17 abstract getIdentifier (): string
18
19 initialize () {
20 this.loadSort()
21 this.initSearch()
22 }
23
24 loadSort () {
25 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
26
27 if (result) {
28 try {
29 this.sort = JSON.parse(result)
30 } catch (err) {
31 console.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
32 }
33 }
34 }
35
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()
48 this.saveSort()
49 }
50
51 saveSort () {
52 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
53 }
54
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
69 onSearch (event: Event) {
70 const target = event.target as HTMLInputElement
71 this.searchStream.next(target.value)
72 }
73
74 protected abstract loadData (): void
75
76 private getSortLocalStorageKey () {
77 return 'rest-table-sort-' + this.getIdentifier()
78 }
79 }