]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/rest/rest-table.ts
Merge branch 'release/v1.0.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / rest / rest-table.ts
CommitLineData
0bd78bf3 1import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
3523b64a
C
2import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'
3import { SortMeta } from 'primeng/components/common/sortmeta'
d592e0a9 4import { RestPagination } from './rest-pagination'
24b9417c
C
5import { Subject } from 'rxjs'
6import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
d592e0a9
C
7
8export abstract class RestTable {
ab998f7b 9
d592e0a9
C
10 abstract totalRecords: number
11 abstract rowsPerPage: number
12 abstract sort: SortMeta
13 abstract pagination: RestPagination
14
24b9417c
C
15 protected search: string
16 private searchStream: Subject<string>
ab998f7b
C
17 private sortLocalStorageKey = 'rest-table-sort-' + this.constructor.name
18
d592e0a9
C
19 protected abstract loadData (): void
20
24b9417c
C
21 initialize () {
22 this.loadSort()
23 this.initSearch()
24 }
25
ab998f7b 26 loadSort () {
0bd78bf3 27 const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey)
ab998f7b
C
28
29 if (result) {
30 try {
31 this.sort = JSON.parse(result)
32 } catch (err) {
33 console.error('Cannot load sort of local storage key ' + this.sortLocalStorageKey, err)
34 }
35 }
36 }
37
d592e0a9
C
38 loadLazy (event: LazyLoadEvent) {
39 this.sort = {
40 order: event.sortOrder,
41 field: event.sortField
42 }
43
44 this.pagination = {
45 start: event.first,
46 count: this.rowsPerPage
47 }
48
49 this.loadData()
ab998f7b
C
50 this.saveSort()
51 }
52
53 saveSort () {
0bd78bf3 54 peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort))
d592e0a9
C
55 }
56
24b9417c
C
57 initSearch () {
58 this.searchStream = new Subject()
59
60 this.searchStream
61 .pipe(
62 debounceTime(400),
63 distinctUntilChanged()
64 )
65 .subscribe(search => {
66 this.search = search
67 this.loadData()
68 })
69 }
70
71 onSearch (search: string) {
72 this.searchStream.next(search)
73 }
d592e0a9 74}