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