]>
Commit | Line | Data |
---|---|---|
1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' | |
2 | import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent' | |
3 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
4 | import { RestPagination } from './rest-pagination' | |
5 | import { Subject } from 'rxjs' | |
6 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' | |
7 | ||
8 | export abstract class RestTable { | |
9 | ||
10 | abstract totalRecords: number | |
11 | abstract rowsPerPage: number | |
12 | abstract sort: SortMeta | |
13 | abstract pagination: RestPagination | |
14 | ||
15 | protected search: string | |
16 | private searchStream: Subject<string> | |
17 | private sortLocalStorageKey = 'rest-table-sort-' + this.constructor.name | |
18 | ||
19 | protected abstract loadData (): void | |
20 | ||
21 | initialize () { | |
22 | this.loadSort() | |
23 | this.initSearch() | |
24 | } | |
25 | ||
26 | loadSort () { | |
27 | const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey) | |
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 | ||
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() | |
50 | this.saveSort() | |
51 | } | |
52 | ||
53 | saveSort () { | |
54 | peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort)) | |
55 | } | |
56 | ||
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 | } | |
74 | } |