]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/rest/rest-table.ts
Translated using Weblate (German)
[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
d592e0a9 42 loadLazy (event: LazyLoadEvent) {
cfde28ba
C
43 logger('Load lazy %o.', event)
44
d592e0a9
C
45 this.sort = {
46 order: event.sortOrder,
47 field: event.sortField
48 }
49
e6492b2d
C
50 this.rowsPerPage = event.rows
51
d592e0a9
C
52 this.pagination = {
53 start: event.first,
54 count: this.rowsPerPage
55 }
56
e6492b2d
C
57 this.expandedRows = {}
58
2e46eb97 59 this.reloadData()
ab998f7b
C
60 this.saveSort()
61 }
62
63 saveSort () {
8e11a1b3 64 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
d592e0a9
C
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}