]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-list-installed/plugin-list-installed.component.ts
Fix infinite scroll on big screens
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-list-installed / plugin-list-installed.component.ts
CommitLineData
d00dc28d
C
1import { Component, OnInit } from '@angular/core'
2import { PluginType } from '@shared/models/plugins/plugin.type'
3import { I18n } from '@ngx-translate/i18n-polyfill'
4import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
dba85a1e 6import { ConfirmService, Notifier } from '@app/core'
d00dc28d 7import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
dba85a1e 8import { ActivatedRoute, Router } from '@angular/router'
6702a1b2 9import { compareSemVer } from '@shared/core-utils/miscs/miscs'
23bdacf8 10import { PluginService } from '@app/core/plugins/plugin.service'
ad453580 11import { Subject } from 'rxjs'
d00dc28d
C
12
13@Component({
14 selector: 'my-plugin-list-installed',
15 templateUrl: './plugin-list-installed.component.html',
dba85a1e
C
16 styleUrls: [
17 '../shared/toggle-plugin-type.scss',
6702a1b2 18 '../shared/plugin-list.component.scss',
dba85a1e
C
19 './plugin-list-installed.component.scss'
20 ]
d00dc28d
C
21})
22export class PluginListInstalledComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
24 pluginType: PluginType = PluginType.PLUGIN
25
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10
29 }
30 sort = 'name'
31
32 plugins: PeerTubePlugin[] = []
b5f919ac
C
33 updating: { [name: string]: boolean } = {}
34
35 PluginType = PluginType
d00dc28d 36
ad453580
C
37 onDataSubject = new Subject<any[]>()
38
d00dc28d
C
39 constructor (
40 private i18n: I18n,
23bdacf8
C
41 private pluginService: PluginService,
42 private pluginApiService: PluginApiService,
dba85a1e
C
43 private notifier: Notifier,
44 private confirmService: ConfirmService,
45 private router: Router,
46 private route: ActivatedRoute
d00dc28d 47 ) {
23bdacf8 48 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
d00dc28d
C
49 }
50
51 ngOnInit () {
dba85a1e
C
52 const query = this.route.snapshot.queryParams
53 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
54
d00dc28d
C
55 this.reloadPlugins()
56 }
57
58 reloadPlugins () {
59 this.pagination.currentPage = 1
60 this.plugins = []
61
b5f919ac 62 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
dba85a1e 63
d00dc28d
C
64 this.loadMorePlugins()
65 }
66
67 loadMorePlugins () {
23bdacf8 68 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
d00dc28d
C
69 .subscribe(
70 res => {
71 this.plugins = this.plugins.concat(res.data)
72 this.pagination.totalItems = res.total
ad453580
C
73
74 this.onDataSubject.next(res.data)
d00dc28d
C
75 },
76
77 err => this.notifier.error(err.message)
78 )
79 }
80
81 onNearOfBottom () {
82 if (!hasMoreItems(this.pagination)) return
83
84 this.pagination.currentPage += 1
85
86 this.loadMorePlugins()
87 }
88
89 getNoResultMessage () {
90 if (this.pluginType === PluginType.PLUGIN) {
91 return this.i18n('You don\'t have plugins installed yet.')
92 }
93
94 return this.i18n('You don\'t have themes installed yet.')
95 }
dba85a1e 96
b5f919ac
C
97 isUpdateAvailable (plugin: PeerTubePlugin) {
98 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
99 }
100
101 getUpdateLabel (plugin: PeerTubePlugin) {
102 return this.i18n('Update to {{version}}', { version: plugin.latestVersion })
103 }
104
105 isUpdating (plugin: PeerTubePlugin) {
106 return !!this.updating[this.getUpdatingKey(plugin)]
107 }
108
dba85a1e
C
109 async uninstall (plugin: PeerTubePlugin) {
110 const res = await this.confirmService.confirm(
111 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
112 this.i18n('Uninstall')
113 )
114 if (res === false) return
115
23bdacf8 116 this.pluginApiService.uninstall(plugin.name, plugin.type)
dba85a1e
C
117 .subscribe(
118 () => {
119 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
120
121 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
122 this.pagination.totalItems--
123 },
124
125 err => this.notifier.error(err.message)
126 )
127 }
128
b5f919ac
C
129 async update (plugin: PeerTubePlugin) {
130 const updatingKey = this.getUpdatingKey(plugin)
131 if (this.updating[updatingKey]) return
132
133 this.updating[updatingKey] = true
134
23bdacf8 135 this.pluginApiService.update(plugin.name, plugin.type)
b5f919ac
C
136 .pipe()
137 .subscribe(
138 res => {
139 this.updating[updatingKey] = false
140
141 this.notifier.success(this.i18n('{{pluginName}} updated.', { pluginName: plugin.name }))
142
143 Object.assign(plugin, res)
144 },
145
146 err => this.notifier.error(err.message)
147 )
148 }
149
dba85a1e
C
150 getShowRouterLink (plugin: PeerTubePlugin) {
151 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
152 }
b5f919ac
C
153
154 private getUpdatingKey (plugin: PeerTubePlugin) {
155 return plugin.name + plugin.type
156 }
d00dc28d 157}