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