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