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