]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/rest/rest-table.ts
Merge remote-tracking branch 'weblate/develop' into develop
[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
50 this.pagination = {
51 start: event.first,
52 count: this.rowsPerPage
53 }
54
2e46eb97 55 this.reloadData()
ab998f7b
C
56 this.saveSort()
57 }
58
59 saveSort () {
8e11a1b3 60 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
d592e0a9
C
61 }
62
25a42e29 63 onPage (event: { first: number, rows: number }) {
cfde28ba
C
64 logger('On page %o.', event)
65
25a42e29
RK
66 if (this.rowsPerPage !== event.rows) {
67 this.rowsPerPage = event.rows
68 this.pagination = {
69 start: event.first,
70 count: this.rowsPerPage
71 }
cfde28ba 72
2e46eb97 73 this.reloadData()
25a42e29 74 }
cfde28ba 75
d4051183
RK
76 this.expandedRows = {}
77 }
78
2e46eb97
C
79 onSearch (search: string) {
80 this.search = search
81 this.reloadData()
82 }
83
84 protected abstract reloadData (): void
8e11a1b3
C
85
86 private getSortLocalStorageKey () {
87 return 'rest-table-sort-' + this.getIdentifier()
88 }
d592e0a9 89}