]>
Commit | Line | Data |
---|---|---|
88a7f93f | 1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
3523b64a C |
2 | import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent' |
3 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
d592e0a9 | 4 | import { RestPagination } from './rest-pagination' |
24b9417c C |
5 | import { Subject } from 'rxjs' |
6 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' | |
d592e0a9 C |
7 | |
8 | export 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 | ||
24b9417c C |
19 | initialize () { |
20 | this.loadSort() | |
21 | this.initSearch() | |
22 | } | |
23 | ||
ab998f7b | 24 | loadSort () { |
0bd78bf3 | 25 | const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey) |
ab998f7b C |
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.sortLocalStorageKey, err) | |
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 () { | |
0bd78bf3 | 52 | peertubeLocalStorage.setItem(this.sortLocalStorageKey, 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 | ||
69 | onSearch (search: string) { | |
70 | this.searchStream.next(search) | |
71 | } | |
dffd5d12 B |
72 | |
73 | protected abstract loadData (): void | |
d592e0a9 | 74 | } |