]>
Commit | Line | Data |
---|---|---|
d00dc28d C |
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' | |
dba85a1e | 6 | import { ConfirmService, Notifier } from '@app/core' |
d00dc28d | 7 | import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model' |
dba85a1e | 8 | import { ActivatedRoute, Router } from '@angular/router' |
6702a1b2 | 9 | import { compareSemVer } from '@shared/core-utils/miscs/miscs' |
23bdacf8 | 10 | import { PluginService } from '@app/core/plugins/plugin.service' |
d00dc28d C |
11 | |
12 | @Component({ | |
13 | selector: 'my-plugin-list-installed', | |
14 | templateUrl: './plugin-list-installed.component.html', | |
dba85a1e C |
15 | styleUrls: [ |
16 | '../shared/toggle-plugin-type.scss', | |
6702a1b2 | 17 | '../shared/plugin-list.component.scss', |
dba85a1e C |
18 | './plugin-list-installed.component.scss' |
19 | ] | |
d00dc28d C |
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[] = [] | |
b5f919ac C |
32 | updating: { [name: string]: boolean } = {} |
33 | ||
34 | PluginType = PluginType | |
d00dc28d C |
35 | |
36 | constructor ( | |
37 | private i18n: I18n, | |
23bdacf8 C |
38 | private pluginService: PluginService, |
39 | private pluginApiService: PluginApiService, | |
dba85a1e C |
40 | private notifier: Notifier, |
41 | private confirmService: ConfirmService, | |
42 | private router: Router, | |
43 | private route: ActivatedRoute | |
d00dc28d | 44 | ) { |
23bdacf8 | 45 | this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions() |
d00dc28d C |
46 | } |
47 | ||
48 | ngOnInit () { | |
dba85a1e C |
49 | const query = this.route.snapshot.queryParams |
50 | if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10) | |
51 | ||
d00dc28d C |
52 | this.reloadPlugins() |
53 | } | |
54 | ||
55 | reloadPlugins () { | |
56 | this.pagination.currentPage = 1 | |
57 | this.plugins = [] | |
58 | ||
b5f919ac | 59 | this.router.navigate([], { queryParams: { pluginType: this.pluginType } }) |
dba85a1e | 60 | |
d00dc28d C |
61 | this.loadMorePlugins() |
62 | } | |
63 | ||
64 | loadMorePlugins () { | |
23bdacf8 | 65 | this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort) |
d00dc28d C |
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 | } | |
dba85a1e | 91 | |
b5f919ac C |
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 | ||
dba85a1e C |
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 | ||
23bdacf8 | 111 | this.pluginApiService.uninstall(plugin.name, plugin.type) |
dba85a1e C |
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 | ||
b5f919ac C |
124 | async update (plugin: PeerTubePlugin) { |
125 | const updatingKey = this.getUpdatingKey(plugin) | |
126 | if (this.updating[updatingKey]) return | |
127 | ||
128 | this.updating[updatingKey] = true | |
129 | ||
23bdacf8 | 130 | this.pluginApiService.update(plugin.name, plugin.type) |
b5f919ac C |
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 | ||
dba85a1e C |
145 | getShowRouterLink (plugin: PeerTubePlugin) { |
146 | return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ] | |
147 | } | |
b5f919ac C |
148 | |
149 | private getUpdatingKey (plugin: PeerTubePlugin) { | |
150 | return plugin.name + plugin.type | |
151 | } | |
d00dc28d | 152 | } |