]>
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 | |
cd940f40 | 10 | export abstract class RestTable <T = unknown> { |
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 | |
cd940f40 C |
20 | selectedRows: T[] = [] |
21 | ||
2e46eb97 | 22 | search: string |
8e11a1b3 | 23 | |
5ed46c1b C |
24 | protected route: ActivatedRoute |
25 | protected router: Router | |
26 | ||
8e11a1b3 | 27 | abstract getIdentifier (): string |
ab998f7b | 28 | |
24b9417c C |
29 | initialize () { |
30 | this.loadSort() | |
24b9417c C |
31 | } |
32 | ||
ab998f7b | 33 | loadSort () { |
8e11a1b3 | 34 | const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey()) |
ab998f7b C |
35 | |
36 | if (result) { | |
37 | try { | |
38 | this.sort = JSON.parse(result) | |
39 | } catch (err) { | |
42b40636 | 40 | logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err) |
ab998f7b C |
41 | } |
42 | } | |
43 | } | |
44 | ||
87a0cac6 C |
45 | saveSort () { |
46 | peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort)) | |
47 | } | |
48 | ||
d592e0a9 | 49 | loadLazy (event: LazyLoadEvent) { |
42b40636 | 50 | debugLogger('Load lazy %o.', event) |
cfde28ba | 51 | |
d592e0a9 C |
52 | this.sort = { |
53 | order: event.sortOrder, | |
54 | field: event.sortField | |
55 | } | |
56 | ||
e6492b2d C |
57 | this.rowsPerPage = event.rows |
58 | ||
d592e0a9 C |
59 | this.pagination = { |
60 | start: event.first, | |
61 | count: this.rowsPerPage | |
62 | } | |
63 | ||
e6492b2d C |
64 | this.expandedRows = {} |
65 | ||
2e46eb97 | 66 | this.reloadData() |
ab998f7b C |
67 | this.saveSort() |
68 | } | |
69 | ||
2e46eb97 | 70 | onSearch (search: string) { |
0667dbaf C |
71 | this.pagination = { |
72 | start: 0, | |
73 | count: this.rowsPerPage | |
74 | } | |
75 | ||
2e46eb97 C |
76 | this.search = search |
77 | this.reloadData() | |
78 | } | |
79 | ||
cd940f40 C |
80 | isInSelectionMode () { |
81 | return this.selectedRows.length !== 0 | |
82 | } | |
83 | ||
e854d57b C |
84 | protected abstract reloadDataInternal (): void |
85 | ||
86 | protected reloadData () { | |
87 | this.selectedRows = [] | |
88 | ||
89 | this.reloadDataInternal() | |
90 | } | |
8e11a1b3 C |
91 | |
92 | private getSortLocalStorageKey () { | |
93 | return 'rest-table-sort-' + this.getIdentifier() | |
94 | } | |
d592e0a9 | 95 | } |