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