]>
Commit | Line | Data |
---|---|---|
0bd78bf3 | 1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-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 | ||
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 | } |