]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/rest/rest-table.ts
Merge branch 'release/2.1.0' into develop
[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 private sortLocalStorageKey = 'rest-table-sort-' + this.constructor.name
17
18 initialize () {
19 this.loadSort()
20 this.initSearch()
21 }
22
23 loadSort () {
24 const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey)
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
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()
47 this.saveSort()
48 }
49
50 saveSort () {
51 peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort))
52 }
53
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
68 onSearch (event: Event) {
69 const target = event.target as HTMLInputElement
70 this.searchStream.next(target.value)
71 }
72
73 protected abstract loadData (): void
74 }