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