]>
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 | initialize () { | |
20 | this.loadSort() | |
21 | this.initSearch() | |
22 | } | |
23 | ||
24 | loadSort () { | |
25 | const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey) | |
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 | ||
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() | |
48 | this.saveSort() | |
49 | } | |
50 | ||
51 | saveSort () { | |
52 | peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort)) | |
53 | } | |
54 | ||
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 | } | |
72 | ||
73 | protected abstract loadData (): void | |
74 | } |