]>
Commit | Line | Data |
---|---|---|
42b40636 | 1 | import debug from 'debug' |
f77eb73b | 2 | import { LazyLoadEvent, SortMeta } from 'primeng/api' |
1fd61899 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
42b40636 | 4 | import { logger } from '@root-helpers/logger' |
5ed46c1b C |
5 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' |
6 | import { RestPagination } from './rest-pagination' | |
cfde28ba | 7 | |
42b40636 | 8 | const debugLogger = debug('peertube:tables:RestTable') |
d592e0a9 | 9 | |
2e46eb97 | 10 | export abstract class RestTable { |
ab998f7b | 11 | |
d592e0a9 | 12 | abstract totalRecords: number |
d592e0a9 C |
13 | abstract sort: SortMeta |
14 | abstract pagination: RestPagination | |
15 | ||
0251197e RK |
16 | rowsPerPageOptions = [ 10, 20, 50, 100 ] |
17 | rowsPerPage = this.rowsPerPageOptions[0] | |
d4051183 | 18 | expandedRows = {} |
0251197e | 19 | |
2e46eb97 | 20 | search: string |
8e11a1b3 | 21 | |
5ed46c1b C |
22 | protected route: ActivatedRoute |
23 | protected router: Router | |
24 | ||
8e11a1b3 | 25 | abstract getIdentifier (): string |
ab998f7b | 26 | |
24b9417c C |
27 | initialize () { |
28 | this.loadSort() | |
24b9417c C |
29 | } |
30 | ||
ab998f7b | 31 | loadSort () { |
8e11a1b3 | 32 | const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey()) |
ab998f7b C |
33 | |
34 | if (result) { | |
35 | try { | |
36 | this.sort = JSON.parse(result) | |
37 | } catch (err) { | |
42b40636 | 38 | logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err) |
ab998f7b C |
39 | } |
40 | } | |
41 | } | |
42 | ||
87a0cac6 C |
43 | saveSort () { |
44 | peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort)) | |
45 | } | |
46 | ||
d592e0a9 | 47 | loadLazy (event: LazyLoadEvent) { |
42b40636 | 48 | debugLogger('Load lazy %o.', event) |
cfde28ba | 49 | |
d592e0a9 C |
50 | this.sort = { |
51 | order: event.sortOrder, | |
52 | field: event.sortField | |
53 | } | |
54 | ||
e6492b2d C |
55 | this.rowsPerPage = event.rows |
56 | ||
d592e0a9 C |
57 | this.pagination = { |
58 | start: event.first, | |
59 | count: this.rowsPerPage | |
60 | } | |
61 | ||
e6492b2d C |
62 | this.expandedRows = {} |
63 | ||
2e46eb97 | 64 | this.reloadData() |
ab998f7b C |
65 | this.saveSort() |
66 | } | |
67 | ||
2e46eb97 | 68 | onSearch (search: string) { |
0667dbaf C |
69 | this.pagination = { |
70 | start: 0, | |
71 | count: this.rowsPerPage | |
72 | } | |
73 | ||
2e46eb97 C |
74 | this.search = search |
75 | this.reloadData() | |
76 | } | |
77 | ||
78 | protected abstract reloadData (): void | |
8e11a1b3 C |
79 | |
80 | private getSortLocalStorageKey () { | |
81 | return 'rest-table-sort-' + this.getIdentifier() | |
82 | } | |
d592e0a9 | 83 | } |