]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/rest/rest-table.ts
Remove unnecessary onPage event on admin tables
[github/Chocobozzz/PeerTube.git] / client / src / app / core / rest / rest-table.ts
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 loadLazy (event: LazyLoadEvent) {
43 logger('Load lazy %o.', event)
44
45 this.sort = {
46 order: event.sortOrder,
47 field: event.sortField
48 }
49
50 this.rowsPerPage = event.rows
51
52 this.pagination = {
53 start: event.first,
54 count: this.rowsPerPage
55 }
56
57 this.expandedRows = {}
58
59 this.reloadData()
60 this.saveSort()
61 }
62
63 saveSort () {
64 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
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 }