]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/rest/rest-table.ts
ec4c570d496ffa4f98997b79e395bbf28b3f79bd
[github/Chocobozzz/PeerTube.git] / client / src / app / core / rest / rest-table.ts
1 import debug from 'debug'
2 import { SortMeta } from 'primeng/api'
3 import { TableLazyLoadEvent } from 'primeng/table'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { logger } from '@root-helpers/logger'
6 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
7 import { RestPagination } from './rest-pagination'
8
9 const debugLogger = debug('peertube:tables:RestTable')
10
11 export abstract class RestTable <T = unknown> {
12
13 abstract totalRecords: number
14 abstract sort: SortMeta
15 abstract pagination: RestPagination
16
17 rowsPerPageOptions = [ 10, 20, 50, 100 ]
18 rowsPerPage = this.rowsPerPageOptions[0]
19 expandedRows = {}
20
21 selectedRows: T[] = []
22
23 search: string
24
25 protected route: ActivatedRoute
26 protected router: Router
27
28 abstract getIdentifier (): string
29
30 initialize () {
31 this.loadSort()
32 }
33
34 loadSort () {
35 const result = peertubeLocalStorage.getItem(this.getSortLocalStorageKey())
36
37 if (result) {
38 try {
39 this.sort = JSON.parse(result)
40 } catch (err) {
41 logger.error('Cannot load sort of local storage key ' + this.getSortLocalStorageKey(), err)
42 }
43 }
44 }
45
46 saveSort () {
47 peertubeLocalStorage.setItem(this.getSortLocalStorageKey(), JSON.stringify(this.sort))
48 }
49
50 loadLazy (event: TableLazyLoadEvent) {
51 debugLogger('Load lazy %o.', event)
52
53 this.sort = {
54 order: event.sortOrder,
55 field: event.sortField as string
56 }
57
58 this.rowsPerPage = event.rows
59
60 this.pagination = {
61 start: event.first,
62 count: this.rowsPerPage
63 }
64
65 this.expandedRows = {}
66
67 this.reloadData()
68 this.saveSort()
69 }
70
71 onSearch (search: string) {
72 this.pagination = {
73 start: 0,
74 count: this.rowsPerPage
75 }
76
77 this.search = search
78 this.reloadData()
79 }
80
81 isInSelectionMode () {
82 return this.selectedRows.length !== 0
83 }
84
85 protected abstract reloadDataInternal (): void
86
87 protected reloadData () {
88 this.selectedRows = []
89
90 this.reloadDataInternal()
91 }
92
93 private getSortLocalStorageKey () {
94 return 'rest-table-sort-' + this.getIdentifier()
95 }
96 }