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