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